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

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
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 var url = _getSourceDirectory(_defaultUrl);
123 return listDir(path.join(systemCacheRoot, url)).then((entries) {
124 return entries.map((entry) {
125 return new Package.load(null, entry, systemCache.sources);
Bob Nystrom 2013/03/13 17:44:07 Only indent 2, or better, use => if it fits: retu
keertip 2013/03/13 18:09:48 Done.
126 });
127
128 });
129 // var list = new List();
130 // for (var entry in entries) {
131 // var name = path.basename(entry).replaceAll(new RegExp(r"-(.*)"),"");
132 // var p = new Package.load(null, entry, systemCache.sources);
133 // list.add(p);
134 // }
135 // return list;
136 // });
Bob Nystrom 2013/03/13 17:44:07 Remove commented out code. :)
keertip 2013/03/13 18:09:48 Oops! removed! On 2013/03/13 17:44:07, Bob Nystrom
137 }
138
121 /// When an error occurs trying to read something about [package] from [url], 139 /// 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 140 /// this tries to translate into a more user friendly error message. Always
123 /// throws an error, either the original one or a better one. 141 /// throws an error, either the original one or a better one.
124 void _throwFriendlyError(AsyncError asyncError, package, url) { 142 void _throwFriendlyError(AsyncError asyncError, package, url) {
125 if (asyncError.error is PubHttpException && 143 if (asyncError.error is PubHttpException &&
126 asyncError.error.response.statusCode == 404) { 144 asyncError.error.response.statusCode == 404) {
127 throw 'Could not find package "$package" at $url.'; 145 throw 'Could not find package "$package" at $url.';
128 } 146 }
129 147
130 if (asyncError.error is TimeoutException) { 148 if (asyncError.error is TimeoutException) {
131 throw 'Timed out trying to find package "$package" at $url.'; 149 throw 'Timed out trying to find package "$package" at $url.';
132 } 150 }
133 151
134 if (asyncError.error is io.SocketIOException) { 152 if (asyncError.error is io.SocketIOException) {
135 throw 'Got socket error trying to find package "$package" at $url.\n' 153 throw 'Got socket error trying to find package "$package" at $url.\n'
136 '${asyncError.error.osError}'; 154 '${asyncError.error.osError}';
137 } 155 }
138 156
139 // Otherwise re-throw the original exception. 157 // Otherwise re-throw the original exception.
140 throw asyncError; 158 throw asyncError;
141 } 159 }
142 160
143 } 161 }
144 162
145 /// The URL of the default package repository. 163 /// The URL of the default package repository.
146 final _defaultUrl = "https://pub.dartlang.org"; 164 final _defaultUrl = "https://pub.dartlang.org";
147 165
166 String _getSourceDirectory(String url){
Bob Nystrom 2013/03/13 17:44:07 Space after )
keertip 2013/03/13 18:09:48 Done.
167 return url.replaceAll(new RegExp(r"^https?://"), "");
168 }
169
148 /// Parses [description] into its server and package name components, then 170 /// Parses [description] into its server and package name components, then
149 /// converts that to a Uri given [pattern]. Ensures the package name is 171 /// converts that to a Uri given [pattern]. Ensures the package name is
150 /// properly URL encoded. 172 /// properly URL encoded.
151 Uri _makeUrl(description, String pattern(String server, String package)) { 173 Uri _makeUrl(description, String pattern(String server, String package)) {
152 var parsed = _parseDescription(description); 174 var parsed = _parseDescription(description);
153 var server = parsed.last; 175 var server = parsed.last;
154 var package = encodeUriComponent(parsed.first); 176 var package = encodeUriComponent(parsed.first);
155 return new Uri(pattern(server, package)); 177 return new Uri(pattern(server, package));
156 } 178 }
157 179
(...skipping 29 matching lines...) Expand all
187 } 209 }
188 210
189 var name = description["name"]; 211 var name = description["name"];
190 if (name is! String) { 212 if (name is! String) {
191 throw new FormatException("The 'name' key must have a string value."); 213 throw new FormatException("The 'name' key must have a string value.");
192 } 214 }
193 215
194 var url = description.containsKey("url") ? description["url"] : _defaultUrl; 216 var url = description.containsKey("url") ? description["url"] : _defaultUrl;
195 return new Pair<String, String>(name, url); 217 return new Pair<String, String>(name, url);
196 } 218 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698