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 var url = _makeVersionUrl(id, (server, package, version) => | 52 return systemCacheDirectory(id).then((cacheDir) { |
53 "$server/packages/$package/versions/$version.yaml"); | 53 // See if we already have it in the cache. If so, just load it locally. |
54 if (fileExists(path.join(cacheDir, "pubspec.yaml"))) { | |
55 return new Pubspec.load(id.name, cacheDir, systemCache.sources); | |
nweiz
2013/04/18 22:50:03
We should catch format/IO errors, print a warning,
Bob Nystrom
2013/04/18 23:07:10
Done.
| |
56 } | |
54 | 57 |
55 log.io("Describe package at $url."); | 58 // Request it from the server. |
56 return httpClient.read(url).then((yaml) { | 59 var url = _makeVersionUrl(id, (server, package, version) => |
57 return new Pubspec.parse(null, yaml, systemCache.sources); | 60 "$server/packages/$package/versions/$version.yaml"); |
58 }).catchError((ex) { | 61 |
59 var parsed = _parseDescription(id.description); | 62 log.io("Describe package at $url."); |
60 _throwFriendlyError(ex, id, parsed.last); | 63 return httpClient.read(url).then((yaml) { |
nweiz
2013/04/18 22:50:03
Add a TODO to cache these pubspecs as well. We pro
Bob Nystrom
2013/04/18 23:07:10
Done.
| |
64 return new Pubspec.parse(null, yaml, systemCache.sources); | |
65 }).catchError((ex) { | |
66 var parsed = _parseDescription(id.description); | |
67 _throwFriendlyError(ex, id, parsed.last); | |
68 }); | |
61 }); | 69 }); |
62 } | 70 } |
63 | 71 |
64 /// Downloads a package from the site and unpacks it. | 72 /// Downloads a package from the site and unpacks it. |
65 Future<bool> install(PackageId id, String destPath) { | 73 Future<bool> install(PackageId id, String destPath) { |
66 return new Future.sync(() { | 74 return new Future.sync(() { |
67 var url = _makeVersionUrl(id, (server, package, version) => | 75 var url = _makeVersionUrl(id, (server, package, version) => |
68 "$server/packages/$package/versions/$version.tar.gz"); | 76 "$server/packages/$package/versions/$version.tar.gz"); |
69 log.io("Install package from $url."); | 77 log.io("Install package from $url."); |
70 | 78 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 dynamic parseDescription(String containingPath, description, | 123 dynamic parseDescription(String containingPath, description, |
116 {bool fromLockFile: false}) { | 124 {bool fromLockFile: false}) { |
117 _parseDescription(description); | 125 _parseDescription(description); |
118 return description; | 126 return description; |
119 } | 127 } |
120 | 128 |
121 List<Package> getCachedPackages() { | 129 List<Package> getCachedPackages() { |
122 var cacheDir = path.join(systemCacheRoot, | 130 var cacheDir = path.join(systemCacheRoot, |
123 _getSourceDirectory(_defaultUrl)); | 131 _getSourceDirectory(_defaultUrl)); |
124 if (!dirExists(cacheDir)) return []; | 132 if (!dirExists(cacheDir)) return []; |
125 | 133 |
126 return listDir(path.join(cacheDir)).map((entry) => | 134 return listDir(path.join(cacheDir)).map((entry) => |
127 new Package.load(null, entry, systemCache.sources)).toList(); | 135 new Package.load(null, entry, systemCache.sources)).toList(); |
128 } | 136 } |
129 | 137 |
130 /// When an error occurs trying to read something about [package] from [url], | 138 /// 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 | 139 /// this tries to translate into a more user friendly error message. Always |
132 /// throws an error, either the original one or a better one. | 140 /// throws an error, either the original one or a better one. |
133 void _throwFriendlyError(error, package, url) { | 141 void _throwFriendlyError(error, package, url) { |
134 if (error is PubHttpException && | 142 if (error is PubHttpException && |
135 error.response.statusCode == 404) { | 143 error.response.statusCode == 404) { |
136 fail('Could not find package "$package" at $url.'); | 144 fail('Could not find package "$package" at $url.'); |
137 } | 145 } |
138 | 146 |
139 if (error is TimeoutException) { | 147 if (error is TimeoutException) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 } | 208 } |
201 | 209 |
202 var name = description["name"]; | 210 var name = description["name"]; |
203 if (name is! String) { | 211 if (name is! String) { |
204 throw new FormatException("The 'name' key must have a string value."); | 212 throw new FormatException("The 'name' key must have a string value."); |
205 } | 213 } |
206 | 214 |
207 var url = description.containsKey("url") ? description["url"] : _defaultUrl; | 215 var url = description.containsKey("url") ? description["url"] : _defaultUrl; |
208 return new Pair<String, String>(name, url); | 216 return new Pair<String, String>(name, url); |
209 } | 217 } |
OLD | NEW |