Chromium Code Reviews| 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 hosted_source; | 5 library hosted_source; |
| 6 | 6 |
| 7 import 'dart:io' as io; | 7 import 'dart:io' as io; |
| 8 import 'dart:json'; | 8 import 'dart:json'; |
| 9 import 'dart:uri'; | 9 import 'dart:uri'; |
| 10 | 10 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 * site. | 34 * site. |
| 35 */ | 35 */ |
| 36 Future<List<Version>> getVersions(String name, description) { | 36 Future<List<Version>> getVersions(String name, description) { |
| 37 var parsed = _parseDescription(description); | 37 var parsed = _parseDescription(description); |
| 38 var fullUrl = "${parsed.last}/packages/${parsed.first}.json"; | 38 var fullUrl = "${parsed.last}/packages/${parsed.first}.json"; |
| 39 | 39 |
| 40 return httpGetString(fullUrl).transform((body) { | 40 return httpGetString(fullUrl).transform((body) { |
| 41 var doc = JSON.parse(body); | 41 var doc = JSON.parse(body); |
| 42 return doc['versions'].map((version) => new Version.parse(version)); | 42 return doc['versions'].map((version) => new Version.parse(version)); |
| 43 }).transformException((ex) { | 43 }).transformException((ex) { |
| 44 if (ex is PubHttpException && ex.statusCode == 404) { | 44 _throwFriendlyError(ex, parsed.first, parsed.last); |
| 45 throw 'Could not find package "${parsed.first}" on ${parsed.last}.'; | |
| 46 } | |
| 47 | |
| 48 // Otherwise re-throw the original exception. | |
| 49 throw ex; | |
| 50 }); | 45 }); |
| 51 } | 46 } |
| 52 | 47 |
| 53 /** | 48 /** |
| 54 * Downloads and parses the pubspec for a specific version of a package that | 49 * Downloads and parses the pubspec for a specific version of a package that |
| 55 * is available from the site. | 50 * is available from the site. |
| 56 */ | 51 */ |
| 57 Future<Pubspec> describe(PackageId id) { | 52 Future<Pubspec> describe(PackageId id) { |
| 58 var parsed = _parseDescription(id.description); | 53 var parsed = _parseDescription(id.description); |
| 59 var fullUrl = "${parsed.last}/packages/${parsed.first}/versions/" | 54 var fullUrl = "${parsed.last}/packages/${parsed.first}/versions/" |
| 60 "${id.version}.yaml"; | 55 "${id.version}.yaml"; |
| 56 | |
| 61 return httpGetString(fullUrl).transform((yaml) { | 57 return httpGetString(fullUrl).transform((yaml) { |
| 62 return new Pubspec.parse(yaml, systemCache.sources); | 58 return new Pubspec.parse(yaml, systemCache.sources); |
| 59 }).transformException((ex) { | |
| 60 _throwFriendlyError(ex, id, parsed.last); | |
| 63 }); | 61 }); |
| 64 } | 62 } |
| 65 | 63 |
| 66 /** | 64 /** |
| 67 * Downloads a package from the site and unpacks it. | 65 * Downloads a package from the site and unpacks it. |
| 68 */ | 66 */ |
| 69 Future<bool> install(PackageId id, String destPath) { | 67 Future<bool> install(PackageId id, String destPath) { |
| 70 var parsedDescription = _parseDescription(id.description); | 68 var parsedDescription = _parseDescription(id.description); |
| 71 var name = parsedDescription.first; | 69 var name = parsedDescription.first; |
| 72 var url = parsedDescription.last; | 70 var url = parsedDescription.last; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 * Ensures that [description] is a valid hosted package description. | 112 * Ensures that [description] is a valid hosted package description. |
| 115 * | 113 * |
| 116 * There are two valid formats. A plain string refers to a package with the | 114 * There are two valid formats. A plain string refers to a package with the |
| 117 * given name from the default host, while a map with keys "name" and "url" | 115 * given name from the default host, while a map with keys "name" and "url" |
| 118 * refers to a package with the given name from the host at the given URL. | 116 * refers to a package with the given name from the host at the given URL. |
| 119 */ | 117 */ |
| 120 void validateDescription(description, {bool fromLockFile: false}) { | 118 void validateDescription(description, {bool fromLockFile: false}) { |
| 121 _parseDescription(description); | 119 _parseDescription(description); |
| 122 } | 120 } |
| 123 | 121 |
| 122 /// When an error occurs trying to read something about [package] from [url], | |
| 123 /// this tries to translate into a more user friendly error message. Always | |
| 124 /// throws an error, either the original one or a better one. | |
| 125 void _throwFriendlyError(ex, package, url) { | |
| 126 if (ex is PubHttpException && ex.statusCode == 404) { | |
| 127 throw 'Could not find package "${package}" at ${url}.'; | |
|
nweiz
2012/11/05 19:55:36
Why "${package}" and "${url}" over "$package" and
Bob Nystrom
2012/11/05 20:43:06
Done. There used to be more complex expressions in
| |
| 128 } | |
| 129 | |
| 130 if (ex is TimeoutException) { | |
| 131 throw 'Timed out trying to find package "${package}" at ${url}.'; | |
| 132 } | |
| 133 | |
| 134 if (ex is io.SocketIOException) { | |
| 135 throw 'Got socket error trying to find package "${package}" at ${url}.\n' | |
| 136 '${ex.osError}'; | |
| 137 } | |
| 138 | |
| 139 // Otherwise re-throw the original exception. | |
| 140 throw ex; | |
| 141 } | |
| 142 | |
| 124 /** | 143 /** |
| 125 * Parses the description for a package. | 144 * Parses the description for a package. |
| 126 * | 145 * |
| 127 * If the package parses correctly, this returns a (name, url) pair. If not, | 146 * If the package parses correctly, this returns a (name, url) pair. If not, |
| 128 * this throws a descriptive FormatException. | 147 * this throws a descriptive FormatException. |
| 129 */ | 148 */ |
| 130 Pair<String, String> _parseDescription(description) { | 149 Pair<String, String> _parseDescription(description) { |
| 131 if (description is String) { | 150 if (description is String) { |
| 132 return new Pair<String, String>(description, defaultUrl); | 151 return new Pair<String, String>(description, defaultUrl); |
| 133 } | 152 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 144 | 163 |
| 145 var name = description["name"]; | 164 var name = description["name"]; |
| 146 if (name is! String) { | 165 if (name is! String) { |
| 147 throw new FormatException("The 'name' key must have a string value."); | 166 throw new FormatException("The 'name' key must have a string value."); |
| 148 } | 167 } |
| 149 | 168 |
| 150 var url = description.containsKey("url") ? description["url"] : defaultUrl; | 169 var url = description.containsKey("url") ? description["url"] : defaultUrl; |
| 151 return new Pair<String, String>(name, url); | 170 return new Pair<String, String>(name, url); |
| 152 } | 171 } |
| 153 } | 172 } |
| OLD | NEW |