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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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() { | |
Bob Nystrom
2013/03/13 21:10:43
Since this method returns a future but internally
keertip
2013/03/13 21:36:15
Done.
| |
122 var url = path.join(systemCacheRoot, _getSourceDirectory(_defaultUrl)); | |
Bob Nystrom
2013/03/13 21:10:43
This isn't a url. How about "url" -> "cacheDir".
keertip
2013/03/13 21:36:15
Done.
| |
123 if (!dirExists(url)) | |
124 return new Future.immediate([]); | |
Bob Nystrom
2013/03/13 21:10:43
Either put this on the same line as the if, or use
keertip
2013/03/13 21:36:15
Done.
| |
125 | |
126 return listDir(path.join(url)).then((entries) { | |
127 return entries.map((entry) => | |
128 new Package.load(null, entry, systemCache.sources)); | |
129 }); | |
130 } | |
131 | |
121 /// When an error occurs trying to read something about [package] from [url], | 132 /// 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 | 133 /// this tries to translate into a more user friendly error message. Always |
123 /// throws an error, either the original one or a better one. | 134 /// throws an error, either the original one or a better one. |
124 void _throwFriendlyError(AsyncError asyncError, package, url) { | 135 void _throwFriendlyError(AsyncError asyncError, package, url) { |
125 if (asyncError.error is PubHttpException && | 136 if (asyncError.error is PubHttpException && |
126 asyncError.error.response.statusCode == 404) { | 137 asyncError.error.response.statusCode == 404) { |
127 throw 'Could not find package "$package" at $url.'; | 138 throw 'Could not find package "$package" at $url.'; |
128 } | 139 } |
129 | 140 |
130 if (asyncError.error is TimeoutException) { | 141 if (asyncError.error is TimeoutException) { |
131 throw 'Timed out trying to find package "$package" at $url.'; | 142 throw 'Timed out trying to find package "$package" at $url.'; |
132 } | 143 } |
133 | 144 |
134 if (asyncError.error is io.SocketIOException) { | 145 if (asyncError.error is io.SocketIOException) { |
135 throw 'Got socket error trying to find package "$package" at $url.\n' | 146 throw 'Got socket error trying to find package "$package" at $url.\n' |
136 '${asyncError.error.osError}'; | 147 '${asyncError.error.osError}'; |
137 } | 148 } |
138 | 149 |
139 // Otherwise re-throw the original exception. | 150 // Otherwise re-throw the original exception. |
140 throw asyncError; | 151 throw asyncError; |
141 } | 152 } |
142 | 153 |
143 } | 154 } |
144 | 155 |
145 /// The URL of the default package repository. | 156 /// The URL of the default package repository. |
146 final _defaultUrl = "https://pub.dartlang.org"; | 157 final _defaultUrl = "https://pub.dartlang.org"; |
147 | 158 |
159 String _getSourceDirectory(String url) { | |
160 return url.replaceAll(new RegExp(r"^https?://"), ""); | |
161 } | |
162 | |
148 /// Parses [description] into its server and package name components, then | 163 /// Parses [description] into its server and package name components, then |
149 /// converts that to a Uri given [pattern]. Ensures the package name is | 164 /// converts that to a Uri given [pattern]. Ensures the package name is |
150 /// properly URL encoded. | 165 /// properly URL encoded. |
151 Uri _makeUrl(description, String pattern(String server, String package)) { | 166 Uri _makeUrl(description, String pattern(String server, String package)) { |
152 var parsed = _parseDescription(description); | 167 var parsed = _parseDescription(description); |
153 var server = parsed.last; | 168 var server = parsed.last; |
154 var package = encodeUriComponent(parsed.first); | 169 var package = encodeUriComponent(parsed.first); |
155 return new Uri(pattern(server, package)); | 170 return new Uri(pattern(server, package)); |
156 } | 171 } |
157 | 172 |
(...skipping 29 matching lines...) Expand all Loading... | |
187 } | 202 } |
188 | 203 |
189 var name = description["name"]; | 204 var name = description["name"]; |
190 if (name is! String) { | 205 if (name is! String) { |
191 throw new FormatException("The 'name' key must have a string value."); | 206 throw new FormatException("The 'name' key must have a string value."); |
192 } | 207 } |
193 | 208 |
194 var url = description.containsKey("url") ? description["url"] : _defaultUrl; | 209 var url = description.containsKey("url") ? description["url"] : _defaultUrl; |
195 return new Pair<String, String>(name, url); | 210 return new Pair<String, String>(name, url); |
196 } | 211 } |
OLD | NEW |