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