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:async'; | 7 import 'dart:async'; |
8 import 'dart:io' as io; | 8 import 'dart:io' as io; |
9 import 'dart:json' as json; | 9 import 'dart:json' as json; |
10 import 'dart:uri'; | 10 import 'dart:uri'; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 .toList(); | 42 .toList(); |
43 }).catchError((ex) { | 43 }).catchError((ex) { |
44 var parsed = _parseDescription(description); | 44 var parsed = _parseDescription(description); |
45 _throwFriendlyError(ex, parsed.first, parsed.last); | 45 _throwFriendlyError(ex, parsed.first, parsed.last); |
46 }); | 46 }); |
47 } | 47 } |
48 | 48 |
49 /// 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 |
50 /// is available from the site. | 50 /// is available from the site. |
51 Future<Pubspec> describe(PackageId id) { | 51 Future<Pubspec> describe(PackageId id) { |
| 52 // Request it from the server. |
52 var url = _makeVersionUrl(id, (server, package, version) => | 53 var url = _makeVersionUrl(id, (server, package, version) => |
53 "$server/packages/$package/versions/$version.yaml"); | 54 "$server/packages/$package/versions/$version.yaml"); |
54 | 55 |
55 log.io("Describe package at $url."); | 56 log.io("Describe package at $url."); |
56 return httpClient.read(url).then((yaml) { | 57 return httpClient.read(url).then((yaml) { |
| 58 // TODO(rnystrom): After this is pulled down, we could place it in |
| 59 // a secondary cache of just pubspecs. This would let us have a |
| 60 // persistent cache for pubspecs for packages that haven't actually |
| 61 // been installed. |
57 return new Pubspec.parse(null, yaml, systemCache.sources); | 62 return new Pubspec.parse(null, yaml, systemCache.sources); |
58 }).catchError((ex) { | 63 }).catchError((ex) { |
59 var parsed = _parseDescription(id.description); | 64 var parsed = _parseDescription(id.description); |
60 _throwFriendlyError(ex, id, parsed.last); | 65 _throwFriendlyError(ex, id, parsed.last); |
61 }); | 66 }); |
62 } | 67 } |
63 | 68 |
64 /// Downloads a package from the site and unpacks it. | 69 /// Downloads a package from the site and unpacks it. |
65 Future<bool> install(PackageId id, String destPath) { | 70 Future<bool> install(PackageId id, String destPath) { |
66 return new Future.sync(() { | 71 return new Future.sync(() { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 dynamic parseDescription(String containingPath, description, | 120 dynamic parseDescription(String containingPath, description, |
116 {bool fromLockFile: false}) { | 121 {bool fromLockFile: false}) { |
117 _parseDescription(description); | 122 _parseDescription(description); |
118 return description; | 123 return description; |
119 } | 124 } |
120 | 125 |
121 List<Package> getCachedPackages() { | 126 List<Package> getCachedPackages() { |
122 var cacheDir = path.join(systemCacheRoot, | 127 var cacheDir = path.join(systemCacheRoot, |
123 _getSourceDirectory(_defaultUrl)); | 128 _getSourceDirectory(_defaultUrl)); |
124 if (!dirExists(cacheDir)) return []; | 129 if (!dirExists(cacheDir)) return []; |
125 | 130 |
126 return listDir(path.join(cacheDir)).map((entry) => | 131 return listDir(path.join(cacheDir)).map((entry) => |
127 new Package.load(null, entry, systemCache.sources)).toList(); | 132 new Package.load(null, entry, systemCache.sources)).toList(); |
128 } | 133 } |
129 | 134 |
130 /// When an error occurs trying to read something about [package] from [url], | 135 /// When an error occurs trying to read something about [package] from [url], |
131 /// this tries to translate into a more user friendly error message. Always | 136 /// this tries to translate into a more user friendly error message. Always |
132 /// throws an error, either the original one or a better one. | 137 /// throws an error, either the original one or a better one. |
133 void _throwFriendlyError(error, package, url) { | 138 void _throwFriendlyError(error, package, url) { |
134 if (error is PubHttpException && | 139 if (error is PubHttpException && |
135 error.response.statusCode == 404) { | 140 error.response.statusCode == 404) { |
136 fail('Could not find package "$package" at $url.'); | 141 fail('Could not find package "$package" at $url.'); |
137 } | 142 } |
138 | 143 |
139 if (error is TimeoutException) { | 144 if (error is TimeoutException) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 } | 205 } |
201 | 206 |
202 var name = description["name"]; | 207 var name = description["name"]; |
203 if (name is! String) { | 208 if (name is! String) { |
204 throw new FormatException("The 'name' key must have a string value."); | 209 throw new FormatException("The 'name' key must have a string value."); |
205 } | 210 } |
206 | 211 |
207 var url = description.containsKey("url") ? description["url"] : _defaultUrl; | 212 var url = description.containsKey("url") ? description["url"] : _defaultUrl; |
208 return new Pair<String, String>(name, url); | 213 return new Pair<String, String>(name, url); |
209 } | 214 } |
OLD | NEW |