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 | 10 |
(...skipping 16 matching lines...) Expand all Loading... |
27 /// The URL of the default package repository. | 27 /// The URL of the default package repository. |
28 static const DEFAULT_URL = "https://pub.dartlang.org"; | 28 static const DEFAULT_URL = "https://pub.dartlang.org"; |
29 | 29 |
30 final name = "hosted"; | 30 final name = "hosted"; |
31 final shouldCache = true; | 31 final shouldCache = true; |
32 | 32 |
33 /// Downloads a list of all versions of a package that are available from the | 33 /// Downloads a list of all versions of a package that are available from the |
34 /// site. | 34 /// site. |
35 Future<List<Version>> getVersions(String name, description) { | 35 Future<List<Version>> getVersions(String name, description) { |
36 var url = _makeUrl(description, | 36 var url = _makeUrl(description, |
37 (server, package) => "$server/packages/$package.json"); | 37 (server, package) => "$server/api/packages/$package"); |
38 | 38 |
39 log.io("Get versions from $url."); | 39 log.io("Get versions from $url."); |
40 return httpClient.read(url).then((body) { | 40 return httpClient.read(url, headers: PUB_API_HEADERS).then((body) { |
41 var doc = json.parse(body); | 41 var doc = json.parse(body); |
42 return doc['versions'] | 42 return doc['versions'] |
43 .map((version) => new Version.parse(version)) | 43 .map((version) => new Version.parse(version['version'])) |
44 .toList(); | 44 .toList(); |
45 }).catchError((ex) { | 45 }).catchError((ex) { |
46 var parsed = _parseDescription(description); | 46 var parsed = _parseDescription(description); |
47 _throwFriendlyError(ex, parsed.first, parsed.last); | 47 _throwFriendlyError(ex, parsed.first, parsed.last); |
48 }); | 48 }); |
49 } | 49 } |
50 | 50 |
51 /// Downloads and parses the pubspec for a specific version of a package that | 51 /// Downloads and parses the pubspec for a specific version of a package that |
52 /// is available from the site. | 52 /// is available from the site. |
53 Future<Pubspec> describeUncached(PackageId id) { | 53 Future<Pubspec> describeUncached(PackageId id) { |
54 // Request it from the server. | 54 // Request it from the server. |
55 var url = _makeVersionUrl(id, (server, package, version) => | 55 var url = _makeVersionUrl(id, (server, package, version) => |
56 "$server/packages/$package/versions/$version.yaml"); | 56 "$server/api/packages/$package/versions/$version"); |
57 | 57 |
58 log.io("Describe package at $url."); | 58 log.io("Describe package at $url."); |
59 return httpClient.read(url).then((yaml) { | 59 return httpClient.read(url, headers: PUB_API_HEADERS).then((version) { |
| 60 version = json.parse(version); |
| 61 |
60 // TODO(rnystrom): After this is pulled down, we could place it in | 62 // TODO(rnystrom): After this is pulled down, we could place it in |
61 // a secondary cache of just pubspecs. This would let us have a | 63 // a secondary cache of just pubspecs. This would let us have a |
62 // persistent cache for pubspecs for packages that haven't actually | 64 // persistent cache for pubspecs for packages that haven't actually |
63 // been installed. | 65 // been installed. |
64 return new Pubspec.parse(null, yaml, systemCache.sources); | 66 return new Pubspec.fromMap(version['pubspec'], systemCache.sources); |
65 }).catchError((ex) { | 67 }).catchError((ex) { |
66 var parsed = _parseDescription(id.description); | 68 var parsed = _parseDescription(id.description); |
67 _throwFriendlyError(ex, id, parsed.last); | 69 _throwFriendlyError(ex, id, parsed.last); |
68 }); | 70 }); |
69 } | 71 } |
70 | 72 |
71 /// Downloads a package from the site and unpacks it. | 73 /// Downloads a package from the site and unpacks it. |
72 Future<bool> install(PackageId id, String destPath) { | 74 Future<bool> install(PackageId id, String destPath) { |
73 return new Future.sync(() { | 75 return new Future.sync(() { |
74 var url = _makeVersionUrl(id, (server, package, version) => | 76 var url = _makeVersionUrl(id, (server, package, version) => |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 var name = description["name"]; | 253 var name = description["name"]; |
252 if (name is! String) { | 254 if (name is! String) { |
253 throw new FormatException("The 'name' key must have a string value."); | 255 throw new FormatException("The 'name' key must have a string value."); |
254 } | 256 } |
255 | 257 |
256 var url = description["url"]; | 258 var url = description["url"]; |
257 if (url == null) url = HostedSource.DEFAULT_URL; | 259 if (url == null) url = HostedSource.DEFAULT_URL; |
258 | 260 |
259 return new Pair<String, String>(name, url); | 261 return new Pair<String, String>(name, url); |
260 } | 262 } |
OLD | NEW |