OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library hosted_source; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:io' as io; | |
9 import 'dart:json' as json; | |
10 import 'dart:uri'; | |
11 | |
12 import 'package:http/http.dart' as http; | |
13 import 'package:pathos/path.dart' as path; | |
14 | |
15 import 'http.dart'; | |
16 import 'io.dart'; | |
17 import 'log.dart' as log; | |
18 import 'package.dart'; | |
19 import 'pubspec.dart'; | |
20 import 'source.dart'; | |
21 import 'source_registry.dart'; | |
22 import 'utils.dart'; | |
23 import 'version.dart'; | |
24 | |
25 /// A package source that installs packages from a package hosting site that | |
26 /// uses the same API as pub.dartlang.org. | |
27 class HostedSource extends Source { | |
28 final name = "hosted"; | |
29 final shouldCache = true; | |
30 | |
31 /// Downloads a list of all versions of a package that are available from the | |
32 /// site. | |
33 Future<List<Version>> getVersions(String name, description) { | |
34 var url = _makeUrl(description, | |
35 (server, package) => "$server/packages/$package.json"); | |
36 | |
37 log.io("Get versions from $url."); | |
38 return httpClient.read(url).then((body) { | |
39 var doc = json.parse(body); | |
40 return doc['versions'] | |
41 .map((version) => new Version.parse(version)) | |
42 .toList(); | |
43 }).catchError((ex) { | |
44 var parsed = _parseDescription(description); | |
45 _throwFriendlyError(ex, parsed.first, parsed.last); | |
46 }); | |
47 } | |
48 | |
49 /// Downloads and parses the pubspec for a specific version of a package that | |
50 /// is available from the site. | |
51 Future<Pubspec> describe(PackageId id) { | |
52 // Request it from the server. | |
53 var url = _makeVersionUrl(id, (server, package, version) => | |
54 "$server/packages/$package/versions/$version.yaml"); | |
55 | |
56 log.io("Describe package at $url."); | |
57 return httpClient.read(url).then((yaml) { | |
58 // TODO(rnystrom): After this is pulled down, we could place it in | |
59 // a secondary cache of just pubspecs. This would let us have a | |
60 // persistent cache for pubspecs for packages that haven't actually | |
61 // been installed. | |
62 return new Pubspec.parse(null, yaml, systemCache.sources); | |
63 }).catchError((ex) { | |
64 var parsed = _parseDescription(id.description); | |
65 _throwFriendlyError(ex, id, parsed.last); | |
66 }); | |
67 } | |
68 | |
69 /// Downloads a package from the site and unpacks it. | |
70 Future<bool> install(PackageId id, String destPath) { | |
71 return new Future.sync(() { | |
72 var url = _makeVersionUrl(id, (server, package, version) => | |
73 "$server/packages/$package/versions/$version.tar.gz"); | |
74 log.io("Install package from $url."); | |
75 | |
76 log.message('Downloading $id...'); | |
77 | |
78 // Download and extract the archive to a temp directory. | |
79 var tempDir = systemCache.createTempDir(); | |
80 return httpClient.send(new http.Request("GET", url)) | |
81 .then((response) => response.stream) | |
82 .then((stream) { | |
83 return timeout(extractTarGz(stream, tempDir), HTTP_TIMEOUT, | |
84 'fetching URL "$url"'); | |
85 }).then((_) { | |
86 // Now that the install has succeeded, move it to the real location in | |
87 // the cache. This ensures that we don't leave half-busted ghost | |
88 // directories in the user's pub cache if an install fails. | |
89 renameDir(tempDir, destPath); | |
90 return true; | |
91 }); | |
92 }); | |
93 } | |
94 | |
95 /// The system cache directory for the hosted source contains subdirectories | |
96 /// for each separate repository URL that's used on the system. Each of these | |
97 /// subdirectories then contains a subdirectory for each package installed | |
98 /// from that site. | |
99 Future<String> systemCacheDirectory(PackageId id) { | |
100 var parsed = _parseDescription(id.description); | |
101 var url = _getSourceDirectory(parsed.last); | |
102 var urlDir = replace(url, new RegExp(r'[<>:"\\/|?*%]'), (match) { | |
103 return '%${match[0].codeUnitAt(0)}'; | |
104 }); | |
105 | |
106 return new Future.value( | |
107 path.join(systemCacheRoot, urlDir, "${parsed.first}-${id.version}")); | |
108 } | |
109 | |
110 String packageName(description) => _parseDescription(description).first; | |
111 | |
112 bool descriptionsEqual(description1, description2) => | |
113 _parseDescription(description1) == _parseDescription(description2); | |
114 | |
115 /// Ensures that [description] is a valid hosted package description. | |
116 /// | |
117 /// There are two valid formats. A plain string refers to a package with the | |
118 /// given name from the default host, while a map with keys "name" and "url" | |
119 /// refers to a package with the given name from the host at the given URL. | |
120 dynamic parseDescription(String containingPath, description, | |
121 {bool fromLockFile: false}) { | |
122 _parseDescription(description); | |
123 return description; | |
124 } | |
125 | |
126 List<Package> getCachedPackages() { | |
127 var cacheDir = path.join(systemCacheRoot, | |
128 _getSourceDirectory(_defaultUrl)); | |
129 if (!dirExists(cacheDir)) return []; | |
130 | |
131 return listDir(path.join(cacheDir)).map((entry) => | |
132 new Package.load(null, entry, systemCache.sources)).toList(); | |
133 } | |
134 | |
135 /// When an error occurs trying to read something about [package] from [url], | |
136 /// this tries to translate into a more user friendly error message. Always | |
137 /// throws an error, either the original one or a better one. | |
138 void _throwFriendlyError(error, package, url) { | |
139 if (error is PubHttpException && | |
140 error.response.statusCode == 404) { | |
141 fail('Could not find package "$package" at $url.'); | |
142 } | |
143 | |
144 if (error is TimeoutException) { | |
145 fail('Timed out trying to find package "$package" at $url.'); | |
146 } | |
147 | |
148 if (error is io.SocketIOException) { | |
149 fail('Got socket error trying to find package "$package" at $url.\n' | |
150 '${error.osError}'); | |
151 } | |
152 | |
153 // Otherwise re-throw the original exception. | |
154 throw error; | |
155 } | |
156 | |
157 } | |
158 | |
159 /// The URL of the default package repository. | |
160 final _defaultUrl = "https://pub.dartlang.org"; | |
161 | |
162 String _getSourceDirectory(String url) { | |
163 return url.replaceAll(new RegExp(r"^https?://"), ""); | |
164 } | |
165 | |
166 /// Parses [description] into its server and package name components, then | |
167 /// converts that to a Uri given [pattern]. Ensures the package name is | |
168 /// properly URL encoded. | |
169 Uri _makeUrl(description, String pattern(String server, String package)) { | |
170 var parsed = _parseDescription(description); | |
171 var server = parsed.last; | |
172 var package = encodeUriComponent(parsed.first); | |
173 return new Uri(pattern(server, package)); | |
174 } | |
175 | |
176 /// Parses [id] into its server, package name, and version components, then | |
177 /// converts that to a Uri given [pattern]. Ensures the package name is | |
178 /// properly URL encoded. | |
179 Uri _makeVersionUrl(PackageId id, | |
180 String pattern(String server, String package, String version)) { | |
181 var parsed = _parseDescription(id.description); | |
182 var server = parsed.last; | |
183 var package = encodeUriComponent(parsed.first); | |
184 var version = encodeUriComponent(id.version.toString()); | |
185 return new Uri(pattern(server, package, version)); | |
186 } | |
187 | |
188 /// Parses the description for a package. | |
189 /// | |
190 /// If the package parses correctly, this returns a (name, url) pair. If not, | |
191 /// this throws a descriptive FormatException. | |
192 Pair<String, String> _parseDescription(description) { | |
193 if (description is String) { | |
194 return new Pair<String, String>(description, _defaultUrl); | |
195 } | |
196 | |
197 if (description is! Map) { | |
198 throw new FormatException( | |
199 "The description must be a package name or map."); | |
200 } | |
201 | |
202 if (!description.containsKey("name")) { | |
203 throw new FormatException( | |
204 "The description map must contain a 'name' key."); | |
205 } | |
206 | |
207 var name = description["name"]; | |
208 if (name is! String) { | |
209 throw new FormatException("The 'name' key must have a string value."); | |
210 } | |
211 | |
212 var url = description.containsKey("url") ? description["url"] : _defaultUrl; | |
213 return new Pair<String, String>(name, url); | |
214 } | |
OLD | NEW |