Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: utils/pub/hosted_source.dart

Issue 12755024: add cache list command to pub (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « utils/pub/git_source.dart ('k') | utils/pub/pub.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 }).then((_) => true); 86 }).then((_) => true);
87 }); 87 });
88 } 88 }
89 89
90 /// The system cache directory for the hosted source contains subdirectories 90 /// The system cache directory for the hosted source contains subdirectories
91 /// for each separate repository URL that's used on the system. Each of these 91 /// for each separate repository URL that's used on the system. Each of these
92 /// subdirectories then contains a subdirectory for each package installed 92 /// subdirectories then contains a subdirectory for each package installed
93 /// from that site. 93 /// from that site.
94 Future<String> systemCacheDirectory(PackageId id) { 94 Future<String> systemCacheDirectory(PackageId id) {
95 var parsed = _parseDescription(id.description); 95 var parsed = _parseDescription(id.description);
96 var url = parsed.last.replaceAll(new RegExp(r"^https?://"), ""); 96 var url = _getSourceDirectory(parsed.last);
97 var urlDir = replace(url, new RegExp(r'[<>:"\\/|?*%]'), (match) { 97 var urlDir = replace(url, new RegExp(r'[<>:"\\/|?*%]'), (match) {
98 return '%${match[0].codeUnitAt(0)}'; 98 return '%${match[0].codeUnitAt(0)}';
99 }); 99 });
100 100
101 return new Future.immediate( 101 return new Future.immediate(
102 path.join(systemCacheRoot, urlDir, "${parsed.first}-${id.version}")); 102 path.join(systemCacheRoot, urlDir, "${parsed.first}-${id.version}"));
103 } 103 }
104 104
105 String packageName(description) => _parseDescription(description).first; 105 String packageName(description) => _parseDescription(description).first;
106 106
107 bool descriptionsEqual(description1, description2) => 107 bool descriptionsEqual(description1, description2) =>
108 _parseDescription(description1) == _parseDescription(description2); 108 _parseDescription(description1) == _parseDescription(description2);
109 109
110 /// Ensures that [description] is a valid hosted package description. 110 /// Ensures that [description] is a valid hosted package description.
111 /// 111 ///
112 /// There are two valid formats. A plain string refers to a package with the 112 /// There are two valid formats. A plain string refers to a package with the
113 /// given name from the default host, while a map with keys "name" and "url" 113 /// given name from the default host, while a map with keys "name" and "url"
114 /// refers to a package with the given name from the host at the given URL. 114 /// refers to a package with the given name from the host at the given URL.
115 dynamic parseDescription(String containingPath, description, 115 dynamic parseDescription(String containingPath, description,
116 {bool fromLockFile: false}) { 116 {bool fromLockFile: false}) {
117 _parseDescription(description); 117 _parseDescription(description);
118 return description; 118 return description;
119 } 119 }
120 120
121 Future<List<Package>> getCachedPackages() {
122 return defer(() {
123 var cacheDir = path.join(systemCacheRoot, _getSourceDirectory(_defaultUrl) );
Bob Nystrom 2013/03/13 21:44:41 Long line.
keertip 2013/03/13 21:46:55 fixed. On 2013/03/13 21:44:41, Bob Nystrom wrote:
124 if (!dirExists(cacheDir)) return [];
125
126 return listDir(path.join(cacheDir)).then((entries) {
127 return entries.map((entry) =>
128 new Package.load(null, entry, systemCache.sources));
129 });
130 });
131 }
132
121 /// When an error occurs trying to read something about [package] from [url], 133 /// When an error occurs trying to read something about [package] from [url],
122 /// this tries to translate into a more user friendly error message. Always 134 /// this tries to translate into a more user friendly error message. Always
123 /// throws an error, either the original one or a better one. 135 /// throws an error, either the original one or a better one.
124 void _throwFriendlyError(AsyncError asyncError, package, url) { 136 void _throwFriendlyError(AsyncError asyncError, package, url) {
125 if (asyncError.error is PubHttpException && 137 if (asyncError.error is PubHttpException &&
126 asyncError.error.response.statusCode == 404) { 138 asyncError.error.response.statusCode == 404) {
127 throw 'Could not find package "$package" at $url.'; 139 throw 'Could not find package "$package" at $url.';
128 } 140 }
129 141
130 if (asyncError.error is TimeoutException) { 142 if (asyncError.error is TimeoutException) {
131 throw 'Timed out trying to find package "$package" at $url.'; 143 throw 'Timed out trying to find package "$package" at $url.';
132 } 144 }
133 145
134 if (asyncError.error is io.SocketIOException) { 146 if (asyncError.error is io.SocketIOException) {
135 throw 'Got socket error trying to find package "$package" at $url.\n' 147 throw 'Got socket error trying to find package "$package" at $url.\n'
136 '${asyncError.error.osError}'; 148 '${asyncError.error.osError}';
137 } 149 }
138 150
139 // Otherwise re-throw the original exception. 151 // Otherwise re-throw the original exception.
140 throw asyncError; 152 throw asyncError;
141 } 153 }
142 154
143 } 155 }
144 156
145 /// The URL of the default package repository. 157 /// The URL of the default package repository.
146 final _defaultUrl = "https://pub.dartlang.org"; 158 final _defaultUrl = "https://pub.dartlang.org";
147 159
160 String _getSourceDirectory(String url) {
161 return url.replaceAll(new RegExp(r"^https?://"), "");
162 }
163
148 /// Parses [description] into its server and package name components, then 164 /// Parses [description] into its server and package name components, then
149 /// converts that to a Uri given [pattern]. Ensures the package name is 165 /// converts that to a Uri given [pattern]. Ensures the package name is
150 /// properly URL encoded. 166 /// properly URL encoded.
151 Uri _makeUrl(description, String pattern(String server, String package)) { 167 Uri _makeUrl(description, String pattern(String server, String package)) {
152 var parsed = _parseDescription(description); 168 var parsed = _parseDescription(description);
153 var server = parsed.last; 169 var server = parsed.last;
154 var package = encodeUriComponent(parsed.first); 170 var package = encodeUriComponent(parsed.first);
155 return new Uri(pattern(server, package)); 171 return new Uri(pattern(server, package));
156 } 172 }
157 173
(...skipping 29 matching lines...) Expand all
187 } 203 }
188 204
189 var name = description["name"]; 205 var name = description["name"];
190 if (name is! String) { 206 if (name is! String) {
191 throw new FormatException("The 'name' key must have a string value."); 207 throw new FormatException("The 'name' key must have a string value.");
192 } 208 }
193 209
194 var url = description.containsKey("url") ? description["url"] : _defaultUrl; 210 var url = description.containsKey("url") ? description["url"] : _defaultUrl;
195 return new Pair<String, String>(name, url); 211 return new Pair<String, String>(name, url);
196 } 212 }
OLDNEW
« no previous file with comments | « utils/pub/git_source.dart ('k') | utils/pub/pub.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698