Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #library('dartlang_source'); | |
| 6 | |
| 7 #import('dart:io'); | |
| 8 #import('dart:uri'); | |
| 9 #import('io.dart'); | |
| 10 #import('package.dart'); | |
| 11 #import('source.dart'); | |
| 12 #import('utils.dart'); | |
| 13 | |
| 14 /** | |
| 15 * A package source that installs packages from a package repository that uses | |
| 16 * the same API as pub.dartlang.org. | |
| 17 */ | |
| 18 class RepoSource extends Source { | |
| 19 final String name = "repo"; | |
| 20 | |
| 21 final bool shouldCache = true; | |
| 22 | |
| 23 /** | |
| 24 * The URL of the default package repository. | |
| 25 * | |
| 26 * At time of writing, pub.dartlang.org is not yet online, but it should be | |
|
Bob Nystrom
2012/06/22 22:35:44
I'd leave a TODO here to update this comment when
nweiz
2012/06/25 22:25:17
Done.
| |
| 27 * soon. | |
| 28 */ | |
| 29 static final String defaultUrl = "http://pub.dartlang.org"; | |
| 30 | |
| 31 RepoSource(); | |
| 32 | |
| 33 /** | |
| 34 * Downloads a package from a package repository and unpacks it. | |
| 35 */ | |
| 36 Future<bool> install(PackageId id, String destPath) { | |
| 37 var parsedDescription = _parseDescription(id.description); | |
| 38 var name = parsedDescription.first; | |
| 39 var url = parsedDescription.last; | |
| 40 | |
| 41 return ensureDir(destPath).chain((_) { | |
| 42 var fullUrl = "$url/packages/$name/versions/${id.version}.tar.gz"; | |
| 43 var httpStream = httpGet(fullUrl); | |
| 44 var process = Process.start("tar", | |
| 45 ["--extract", "--gunzip", "--directory", destPath]); | |
|
Bob Nystrom
2012/06/22 22:35:44
How do you feel about wrapping this in a function
nweiz
2012/06/25 22:25:17
Done.
| |
| 46 var completer = new Completer<int>(); | |
| 47 | |
| 48 httpStream.pipe(process.stdin); | |
| 49 process.stdout.pipe(stdout, close: false); | |
| 50 process.stderr.pipe(stderr, close: false); | |
| 51 | |
| 52 process.onExit = completer.complete; | |
| 53 process.onError = completer.completeException; | |
| 54 return completer.future; | |
| 55 }).transform((exitCode) => exitCode == 0); | |
| 56 } | |
| 57 | |
| 58 /** | |
| 59 * The system cache directory for the repo source contains subdirectories for | |
| 60 * each separate repository URL that's used on the system. Each of these | |
| 61 * subdirectories then contains a subdirectory for each package installed from | |
| 62 * that repository. | |
| 63 */ | |
| 64 String systemCacheDirectory(PackageId id, String parent) { | |
| 65 var parsed = _parseDescription(id.description); | |
| 66 var url = parsed.last.replaceAll(new RegExp(@"^https?://"), ""); | |
| 67 var urlDir = replace(url, new RegExp(@'[<>:"\\/|?*%]'), (match) { | |
| 68 return '%${match[0].charCodeAt(0)}'; | |
| 69 }); | |
| 70 return join(parent, urlDir, "${parsed.first}-${id.version}"); | |
| 71 } | |
| 72 | |
| 73 String packageName(PackageId id) => _parseDescription(id.description).first; | |
| 74 | |
| 75 /** | |
| 76 * Ensures that [description] is a valid repo description. | |
| 77 * | |
| 78 * There are two valid formats. A plain string refers to a package with the | |
| 79 * given name from the default repository, while a map with keys "name" and | |
| 80 * "url" refers to a package with the given name from the repo at the given | |
| 81 * URL. | |
| 82 */ | |
| 83 void validateDescription(description) { | |
| 84 _parseDescription(description); | |
| 85 } | |
| 86 | |
| 87 /** | |
| 88 * Parses the description blob for a package. | |
| 89 * | |
| 90 * If the package parses correctly, this returns a (name, url) pair. If not, | |
| 91 * this throws a descriptive FormatException. | |
| 92 */ | |
| 93 Pair<String, String> _parseDescription(description) { | |
| 94 if (description is String) { | |
| 95 return new Pair<String, String>(description, defaultUrl); | |
| 96 } | |
| 97 | |
| 98 if (description is! Map) { | |
| 99 throw new FormatException( | |
| 100 "The description must be a package name or map."); | |
| 101 } | |
| 102 | |
| 103 if (!description.containsKey("name")) { | |
| 104 throw new FormatException( | |
| 105 "The description map must contain a 'name' key."); | |
| 106 } | |
| 107 | |
| 108 var name = description["name"]; | |
| 109 if (name is! String) { | |
| 110 throw new FormatException("The 'name' key must have a string value."); | |
| 111 } | |
| 112 | |
| 113 var url = description.containsKey("url") ? description["url"] : defaultUrl; | |
| 114 return new Pair<String, String>(name, url); | |
| 115 } | |
| 116 } | |
| OLD | NEW |