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 try { | |
56 return new Pubspec.load(id.name, cacheDir, systemCache.sources); | |
57 } on FormatException catch(error) { | |
nweiz
2013/04/19 00:12:31
Nit: space between "catch" and "(error)".
We shou
| |
58 // If the cached pubspec is corrupted for some reason, don't use it | |
59 // and instead continue and request it from the server. | |
60 log.fine("Cached pubspec for $id had format error:\n$error"); | |
nweiz
2013/04/19 00:12:31
Print the stack trace as well.
| |
61 } | |
62 } | |
54 | 63 |
55 log.io("Describe package at $url."); | 64 // Request it from the server. |
56 return httpClient.read(url).then((yaml) { | 65 var url = _makeVersionUrl(id, (server, package, version) => |
57 return new Pubspec.parse(null, yaml, systemCache.sources); | 66 "$server/packages/$package/versions/$version.yaml"); |
58 }).catchError((ex) { | 67 |
59 var parsed = _parseDescription(id.description); | 68 log.io("Describe package at $url."); |
60 _throwFriendlyError(ex, id, parsed.last); | 69 return httpClient.read(url).then((yaml) { |
70 // TODO(rnystrom): After this is pulled down, we could place it in | |
71 // a secondary cache of just pubspecs. This would let us have a | |
72 // persistent cache for pubspecs for packages that haven't actually | |
73 // been installed. | |
74 return new Pubspec.parse(null, yaml, systemCache.sources); | |
75 }).catchError((ex) { | |
76 var parsed = _parseDescription(id.description); | |
77 _throwFriendlyError(ex, id, parsed.last); | |
78 }); | |
61 }); | 79 }); |
62 } | 80 } |
63 | 81 |
64 /// Downloads a package from the site and unpacks it. | 82 /// Downloads a package from the site and unpacks it. |
65 Future<bool> install(PackageId id, String destPath) { | 83 Future<bool> install(PackageId id, String destPath) { |
66 return new Future.sync(() { | 84 return new Future.sync(() { |
67 var url = _makeVersionUrl(id, (server, package, version) => | 85 var url = _makeVersionUrl(id, (server, package, version) => |
68 "$server/packages/$package/versions/$version.tar.gz"); | 86 "$server/packages/$package/versions/$version.tar.gz"); |
69 log.io("Install package from $url."); | 87 log.io("Install package from $url."); |
70 | 88 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 dynamic parseDescription(String containingPath, description, | 133 dynamic parseDescription(String containingPath, description, |
116 {bool fromLockFile: false}) { | 134 {bool fromLockFile: false}) { |
117 _parseDescription(description); | 135 _parseDescription(description); |
118 return description; | 136 return description; |
119 } | 137 } |
120 | 138 |
121 List<Package> getCachedPackages() { | 139 List<Package> getCachedPackages() { |
122 var cacheDir = path.join(systemCacheRoot, | 140 var cacheDir = path.join(systemCacheRoot, |
123 _getSourceDirectory(_defaultUrl)); | 141 _getSourceDirectory(_defaultUrl)); |
124 if (!dirExists(cacheDir)) return []; | 142 if (!dirExists(cacheDir)) return []; |
125 | 143 |
126 return listDir(path.join(cacheDir)).map((entry) => | 144 return listDir(path.join(cacheDir)).map((entry) => |
127 new Package.load(null, entry, systemCache.sources)).toList(); | 145 new Package.load(null, entry, systemCache.sources)).toList(); |
128 } | 146 } |
129 | 147 |
130 /// When an error occurs trying to read something about [package] from [url], | 148 /// 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 | 149 /// this tries to translate into a more user friendly error message. Always |
132 /// throws an error, either the original one or a better one. | 150 /// throws an error, either the original one or a better one. |
133 void _throwFriendlyError(error, package, url) { | 151 void _throwFriendlyError(error, package, url) { |
134 if (error is PubHttpException && | 152 if (error is PubHttpException && |
135 error.response.statusCode == 404) { | 153 error.response.statusCode == 404) { |
136 fail('Could not find package "$package" at $url.'); | 154 fail('Could not find package "$package" at $url.'); |
137 } | 155 } |
138 | 156 |
139 if (error is TimeoutException) { | 157 if (error is TimeoutException) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 } | 218 } |
201 | 219 |
202 var name = description["name"]; | 220 var name = description["name"]; |
203 if (name is! String) { | 221 if (name is! String) { |
204 throw new FormatException("The 'name' key must have a string value."); | 222 throw new FormatException("The 'name' key must have a string value."); |
205 } | 223 } |
206 | 224 |
207 var url = description.containsKey("url") ? description["url"] : _defaultUrl; | 225 var url = description.containsKey("url") ? description["url"] : _defaultUrl; |
208 return new Pair<String, String>(name, url); | 226 return new Pair<String, String>(name, url); |
209 } | 227 } |
OLD | NEW |