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 pub.source.hosted; | 5 library pub.source.hosted; |
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:convert"; | 9 import "dart:convert"; |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... |
30 /// Gets the default URL for the package server for hosted dependencies. | 30 /// Gets the default URL for the package server for hosted dependencies. |
31 static String get defaultUrl { | 31 static String get defaultUrl { |
32 var url = io.Platform.environment["PUB_HOSTED_URL"]; | 32 var url = io.Platform.environment["PUB_HOSTED_URL"]; |
33 if (url != null) return url; | 33 if (url != null) return url; |
34 | 34 |
35 return "https://pub.dartlang.org"; | 35 return "https://pub.dartlang.org"; |
36 } | 36 } |
37 | 37 |
38 /// Downloads a list of all versions of a package that are available from the | 38 /// Downloads a list of all versions of a package that are available from the |
39 /// site. | 39 /// site. |
40 Future<List<Pubspec>> getVersions(String name, description) async { | 40 Future<List<PackageId>> doGetVersions(PackageRef ref) async { |
41 var url = _makeUrl(description, | 41 var url = _makeUrl(ref.description, |
42 (server, package) => "$server/api/packages/$package"); | 42 (server, package) => "$server/api/packages/$package"); |
43 | 43 |
44 log.io("Get versions from $url."); | 44 log.io("Get versions from $url."); |
45 | 45 |
46 var body; | 46 var body; |
47 try { | 47 try { |
48 body = await httpClient.read(url, headers: PUB_API_HEADERS); | 48 body = await httpClient.read(url, headers: PUB_API_HEADERS); |
49 } catch (error, stackTrace) { | 49 } catch (error, stackTrace) { |
50 var parsed = _parseDescription(description); | 50 var parsed = _parseDescription(ref.description); |
51 _throwFriendlyError(error, stackTrace, parsed.first, parsed.last); | 51 _throwFriendlyError(error, stackTrace, parsed.first, parsed.last); |
52 } | 52 } |
53 | 53 |
54 var doc = JSON.decode(body); | 54 var doc = JSON.decode(body); |
55 return doc['versions'].map((map) { | 55 return doc['versions'].map((map) { |
56 return new Pubspec.fromMap( | 56 var pubspec = new Pubspec.fromMap( |
57 map['pubspec'], systemCache.sources, | 57 map['pubspec'], systemCache.sources, |
58 expectedName: name, location: url); | 58 expectedName: ref.name, location: url); |
| 59 var id = ref.atVersion(pubspec.version); |
| 60 memoizePubspec(id, pubspec); |
| 61 |
| 62 return id; |
59 }).toList(); | 63 }).toList(); |
60 } | 64 } |
61 | 65 |
62 /// Downloads and parses the pubspec for a specific version of a package that | 66 /// Downloads and parses the pubspec for a specific version of a package that |
63 /// is available from the site. | 67 /// is available from the site. |
64 Future<Pubspec> describeUncached(PackageId id) async { | 68 Future<Pubspec> describeUncached(PackageId id) async { |
65 // Request it from the server. | 69 // Request it from the server. |
66 var url = _makeVersionUrl(id, (server, package, version) => | 70 var url = _makeVersionUrl(id, (server, package, version) => |
67 "$server/api/packages/$package/versions/$version"); | 71 "$server/api/packages/$package/versions/$version"); |
68 | 72 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 throw error; | 220 throw error; |
217 } | 221 } |
218 } | 222 } |
219 | 223 |
220 /// This is the modified hosted source used when pub get or upgrade are run | 224 /// This is the modified hosted source used when pub get or upgrade are run |
221 /// with "--offline". | 225 /// with "--offline". |
222 /// | 226 /// |
223 /// This uses the system cache to get the list of available packages and does | 227 /// This uses the system cache to get the list of available packages and does |
224 /// no network access. | 228 /// no network access. |
225 class OfflineHostedSource extends HostedSource { | 229 class OfflineHostedSource extends HostedSource { |
226 /// Gets the list of all versions of [name] that are in the system cache. | 230 /// Gets the list of all versions of [ref] that are in the system cache. |
227 Future<List<Pubspec>> getVersions(String name, description) async { | 231 Future<List<PackageId>> doGetVersions(PackageRef ref) async { |
228 var parsed = _parseDescription(description); | 232 var parsed = _parseDescription(ref.description); |
229 var server = parsed.last; | 233 var server = parsed.last; |
230 log.io("Finding versions of $name in " | 234 log.io("Finding versions of ${ref.name} in " |
231 "$systemCacheRoot/${_urlToDirectory(server)}"); | 235 "$systemCacheRoot/${_urlToDirectory(server)}"); |
232 var versions = await _getCachedPackagesInDirectory(_urlToDirectory(server)) | 236 |
233 .where((package) => package.name == name) | 237 var dir = path.join(systemCacheRoot, _urlToDirectory(server)); |
234 .map((package) => package.pubspec) | 238 |
235 .toList(); | 239 var versions; |
| 240 if (dirExists(dir)) { |
| 241 versions = await listDir(dir).map((entry) { |
| 242 var components = path.basename(entry).split("-"); |
| 243 if (components.first != ref.name) return null; |
| 244 return ref.atVersion(new Version.parse(components.last)); |
| 245 }).where((id) => id != null).toList(); |
| 246 } else { |
| 247 versions = []; |
| 248 } |
236 | 249 |
237 // If there are no versions in the cache, report a clearer error. | 250 // If there are no versions in the cache, report a clearer error. |
238 if (versions.isEmpty) { | 251 if (versions.isEmpty) { |
239 throw new PackageNotFoundException( | 252 throw new PackageNotFoundException( |
240 "Could not find package $name in cache."); | 253 "Could not find package ${ref.name} in cache."); |
241 } | 254 } |
242 | 255 |
243 return versions; | 256 return versions; |
244 } | 257 } |
245 | 258 |
246 Future _download(String server, String package, Version version, | 259 Future _download(String server, String package, Version version, |
247 String destPath) { | 260 String destPath) { |
248 // Since HostedSource is cached, this will only be called for uncached | 261 // Since HostedSource is cached, this will only be called for uncached |
249 // packages. | 262 // packages. |
250 throw new UnsupportedError("Cannot download packages when offline."); | 263 throw new UnsupportedError("Cannot download packages when offline."); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 var name = description["name"]; | 359 var name = description["name"]; |
347 if (name is! String) { | 360 if (name is! String) { |
348 throw new FormatException("The 'name' key must have a string value."); | 361 throw new FormatException("The 'name' key must have a string value."); |
349 } | 362 } |
350 | 363 |
351 var url = description["url"]; | 364 var url = description["url"]; |
352 if (url == null) url = HostedSource.defaultUrl; | 365 if (url == null) url = HostedSource.defaultUrl; |
353 | 366 |
354 return new Pair<String, String>(name, url); | 367 return new Pair<String, String>(name, url); |
355 } | 368 } |
OLD | NEW |