| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #library('dartlang_source'); | 5 #library('hosted_source'); |
| 6 | 6 |
| 7 #import('dart:io'); | 7 #import('dart:io'); |
| 8 #import('dart:json'); | 8 #import('dart:json'); |
| 9 #import('dart:uri'); | 9 #import('dart:uri'); |
| 10 #import('io.dart'); | 10 #import('io.dart'); |
| 11 #import('package.dart'); | 11 #import('package.dart'); |
| 12 #import('pubspec.dart'); | 12 #import('pubspec.dart'); |
| 13 #import('source.dart'); | 13 #import('source.dart'); |
| 14 #import('source_registry.dart'); | 14 #import('source_registry.dart'); |
| 15 #import('utils.dart'); | 15 #import('utils.dart'); |
| 16 #import('version.dart'); | 16 #import('version.dart'); |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * A package source that installs packages from a package repository that uses | 19 * A package source that installs packages from a package hosting site that |
| 20 * the same API as pub.dartlang.org. | 20 * uses the same API as pub.dartlang.org. |
| 21 */ | 21 */ |
| 22 class RepoSource extends Source { | 22 class HostedSource extends Source { |
| 23 final String name = "repo"; | 23 final name = "hosted"; |
| 24 final shouldCache = true; |
| 24 | 25 |
| 25 final bool shouldCache = true; | |
| 26 | |
| 27 // TODO(nweiz): update this comment once pub.dartlang.org is online | |
| 28 /** | 26 /** |
| 29 * The URL of the default package repository. | 27 * The URL of the default package repository. |
| 30 * | |
| 31 * At time of writing, pub.dartlang.org is not yet online, but it should be | |
| 32 * soon. | |
| 33 */ | 28 */ |
| 34 static final String defaultUrl = "http://pub.dartlang.org"; | 29 static final defaultUrl = "http://pub.dartlang.org"; |
| 35 | |
| 36 RepoSource(); | |
| 37 | 30 |
| 38 /** | 31 /** |
| 39 * Downloads a list of all versions of a package that have been uploaded to | 32 * Downloads a list of all versions of a package that are available from the |
| 40 * pub.dartlang.org. | 33 * site. |
| 41 */ | 34 */ |
| 42 Future<List<Version>> getVersions(description) { | 35 Future<List<Version>> getVersions(description) { |
| 43 var parsed = _parseDescription(description); | 36 var parsed = _parseDescription(description); |
| 44 var fullUrl = "${parsed.last}/packages/${parsed.first}.json"; | 37 var fullUrl = "${parsed.last}/packages/${parsed.first}.json"; |
| 45 return consumeInputStream(httpGet(fullUrl)).transform((data) { | 38 return consumeInputStream(httpGet(fullUrl)).transform((data) { |
| 46 var doc = JSON.parse(new String.fromCharCodes(data)); | 39 var doc = JSON.parse(new String.fromCharCodes(data)); |
| 47 return doc['versions'].map((version) => new Version.parse(version)); | 40 return doc['versions'].map((version) => new Version.parse(version)); |
| 48 }); | 41 }); |
| 49 } | 42 } |
| 50 | 43 |
| 51 /** | 44 /** |
| 52 * Downloads and parses the pubspec for a specific version of a package that | 45 * Downloads and parses the pubspec for a specific version of a package that |
| 53 * has been uploaded to pub.dartlang.org. | 46 * is available from the site. |
| 54 */ | 47 */ |
| 55 Future<Pubspec> describe(PackageId id) { | 48 Future<Pubspec> describe(PackageId id) { |
| 56 var parsed = _parseDescription(id.description); | 49 var parsed = _parseDescription(id.description); |
| 57 var fullUrl = "${parsed.last}/packages/${parsed.first}/versions/" | 50 var fullUrl = "${parsed.last}/packages/${parsed.first}/versions/" |
| 58 "${id.version}.yaml"; | 51 "${id.version}.yaml"; |
| 59 return consumeInputStream(httpGet(fullUrl)).transform((data) { | 52 return consumeInputStream(httpGet(fullUrl)).transform((data) { |
| 60 return new Pubspec.parse( | 53 return new Pubspec.parse( |
| 61 new String.fromCharCodes(data), systemCache.sources); | 54 new String.fromCharCodes(data), systemCache.sources); |
| 62 }); | 55 }); |
| 63 } | 56 } |
| 64 | 57 |
| 65 /** | 58 /** |
| 66 * Downloads a package from a package repository and unpacks it. | 59 * Downloads a package from the site and unpacks it. |
| 67 */ | 60 */ |
| 68 Future<bool> install(PackageId id, String destPath) { | 61 Future<bool> install(PackageId id, String destPath) { |
| 69 var parsedDescription = _parseDescription(id.description); | 62 var parsedDescription = _parseDescription(id.description); |
| 70 var name = parsedDescription.first; | 63 var name = parsedDescription.first; |
| 71 var url = parsedDescription.last; | 64 var url = parsedDescription.last; |
| 72 | 65 |
| 73 return ensureDir(destPath).chain((destDir) { | 66 return ensureDir(destPath).chain((destDir) { |
| 74 var fullUrl = "$url/packages/$name/versions/${id.version}.tar.gz"; | 67 var fullUrl = "$url/packages/$name/versions/${id.version}.tar.gz"; |
| 75 return extractTarGz(httpGet(fullUrl), destDir); | 68 return extractTarGz(httpGet(fullUrl), destDir); |
| 76 }); | 69 }); |
| 77 } | 70 } |
| 78 | 71 |
| 79 /** | 72 /** |
| 80 * The system cache directory for the repo source contains subdirectories for | 73 * The system cache directory for the hosted source contains subdirectories |
| 81 * each separate repository URL that's used on the system. Each of these | 74 * for each separate repository URL that's used on the system. Each of these |
| 82 * subdirectories then contains a subdirectory for each package installed from | 75 * subdirectories then contains a subdirectory for each package installed |
| 83 * that repository. | 76 * from that site. |
| 84 */ | 77 */ |
| 85 String systemCacheDirectory(PackageId id) { | 78 String systemCacheDirectory(PackageId id) { |
| 86 var parsed = _parseDescription(id.description); | 79 var parsed = _parseDescription(id.description); |
| 87 var url = parsed.last.replaceAll(new RegExp(@"^https?://"), ""); | 80 var url = parsed.last.replaceAll(new RegExp(@"^https?://"), ""); |
| 88 var urlDir = replace(url, new RegExp(@'[<>:"\\/|?*%]'), (match) { | 81 var urlDir = replace(url, new RegExp(@'[<>:"\\/|?*%]'), (match) { |
| 89 return '%${match[0].charCodeAt(0)}'; | 82 return '%${match[0].charCodeAt(0)}'; |
| 90 }); | 83 }); |
| 91 return join(systemCacheRoot, urlDir, "${parsed.first}-${id.version}"); | 84 return join(systemCacheRoot, urlDir, "${parsed.first}-${id.version}"); |
| 92 } | 85 } |
| 93 | 86 |
| 94 String packageName(description) => _parseDescription(description).first; | 87 String packageName(description) => _parseDescription(description).first; |
| 95 | 88 |
| 96 bool descriptionsEqual(description1, description2) => | 89 bool descriptionsEqual(description1, description2) => |
| 97 _parseDescription(description1) == _parseDescription(description2); | 90 _parseDescription(description1) == _parseDescription(description2); |
| 98 | 91 |
| 99 /** | 92 /** |
| 100 * Ensures that [description] is a valid repo description. | 93 * Ensures that [description] is a valid hosted package description. |
| 101 * | 94 * |
| 102 * There are two valid formats. A plain string refers to a package with the | 95 * There are two valid formats. A plain string refers to a package with the |
| 103 * given name from the default repository, while a map with keys "name" and | 96 * given name from the default host, while a map with keys "name" and "url" |
| 104 * "url" refers to a package with the given name from the repo at the given | 97 * refers to a package with the given name from the host at the given URL. |
| 105 * URL. | |
| 106 */ | 98 */ |
| 107 void validateDescription(description, [bool fromLockFile=false]) { | 99 void validateDescription(description, [bool fromLockFile=false]) { |
| 108 _parseDescription(description); | 100 _parseDescription(description); |
| 109 } | 101 } |
| 110 | 102 |
| 111 /** | 103 /** |
| 112 * Parses the description blob for a package. | 104 * Parses the description for a package. |
| 113 * | 105 * |
| 114 * If the package parses correctly, this returns a (name, url) pair. If not, | 106 * If the package parses correctly, this returns a (name, url) pair. If not, |
| 115 * this throws a descriptive FormatException. | 107 * this throws a descriptive FormatException. |
| 116 */ | 108 */ |
| 117 Pair<String, String> _parseDescription(description) { | 109 Pair<String, String> _parseDescription(description) { |
| 118 if (description is String) { | 110 if (description is String) { |
| 119 return new Pair<String, String>(description, defaultUrl); | 111 return new Pair<String, String>(description, defaultUrl); |
| 120 } | 112 } |
| 121 | 113 |
| 122 if (description is! Map) { | 114 if (description is! Map) { |
| 123 throw new FormatException( | 115 throw new FormatException( |
| 124 "The description must be a package name or map."); | 116 "The description must be a package name or map."); |
| 125 } | 117 } |
| 126 | 118 |
| 127 if (!description.containsKey("name")) { | 119 if (!description.containsKey("name")) { |
| 128 throw new FormatException( | 120 throw new FormatException( |
| 129 "The description map must contain a 'name' key."); | 121 "The description map must contain a 'name' key."); |
| 130 } | 122 } |
| 131 | 123 |
| 132 var name = description["name"]; | 124 var name = description["name"]; |
| 133 if (name is! String) { | 125 if (name is! String) { |
| 134 throw new FormatException("The 'name' key must have a string value."); | 126 throw new FormatException("The 'name' key must have a string value."); |
| 135 } | 127 } |
| 136 | 128 |
| 137 var url = description.containsKey("url") ? description["url"] : defaultUrl; | 129 var url = description.containsKey("url") ? description["url"] : defaultUrl; |
| 138 return new Pair<String, String>(name, url); | 130 return new Pair<String, String>(name, url); |
| 139 } | 131 } |
| 140 } | 132 } |
| OLD | NEW |