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:io' as io; | 7 import 'dart:io' as io; |
8 import 'dart:json'; | 8 import 'dart:json'; |
9 import 'dart:uri'; | 9 import 'dart:uri'; |
10 | 10 |
11 // TODO(nweiz): Make this import better. | 11 // TODO(nweiz): Make this import better. |
12 import '../../pkg/http/lib/http.dart' as http; | 12 import '../../pkg/http/lib/http.dart' as http; |
13 import 'http.dart'; | 13 import 'http.dart'; |
14 import 'io.dart'; | 14 import 'io.dart'; |
15 import 'log.dart' as log; | 15 import 'log.dart' as log; |
16 import 'package.dart'; | 16 import 'package.dart'; |
17 import 'pubspec.dart'; | 17 import 'pubspec.dart'; |
18 import 'source.dart'; | 18 import 'source.dart'; |
19 import 'source_registry.dart'; | 19 import 'source_registry.dart'; |
20 import 'utils.dart'; | 20 import 'utils.dart'; |
21 import 'version.dart'; | 21 import 'version.dart'; |
22 | 22 |
23 /** | 23 /// A package source that installs packages from a package hosting site that |
24 * A package source that installs packages from a package hosting site that | 24 /// uses the same API as pub.dartlang.org. |
25 * uses the same API as pub.dartlang.org. | |
26 */ | |
27 class HostedSource extends Source { | 25 class HostedSource extends Source { |
28 final name = "hosted"; | 26 final name = "hosted"; |
29 final shouldCache = true; | 27 final shouldCache = true; |
30 | 28 |
31 /** | 29 /// The URL of the default package repository. |
32 * The URL of the default package repository. | |
33 */ | |
34 static final defaultUrl = "http://pub.dartlang.org"; | 30 static final defaultUrl = "http://pub.dartlang.org"; |
35 | 31 |
36 /** | 32 /// Downloads a list of all versions of a package that are available from the |
37 * Downloads a list of all versions of a package that are available from the | 33 /// site. |
38 * site. | |
39 */ | |
40 Future<List<Version>> getVersions(String name, description) { | 34 Future<List<Version>> getVersions(String name, description) { |
41 var parsed = _parseDescription(description); | 35 var parsed = _parseDescription(description); |
42 var fullUrl = "${parsed.last}/packages/${parsed.first}.json"; | 36 var fullUrl = "${parsed.last}/packages/${parsed.first}.json"; |
43 | 37 |
44 return httpClient.read(fullUrl).transform((body) { | 38 return httpClient.read(fullUrl).transform((body) { |
45 var doc = JSON.parse(body); | 39 var doc = JSON.parse(body); |
46 return doc['versions'].map((version) => new Version.parse(version)); | 40 return doc['versions'].map((version) => new Version.parse(version)); |
47 }).transformException((ex) { | 41 }).transformException((ex) { |
48 _throwFriendlyError(ex, parsed.first, parsed.last); | 42 _throwFriendlyError(ex, parsed.first, parsed.last); |
49 }); | 43 }); |
50 } | 44 } |
51 | 45 |
52 /** | 46 /// Downloads and parses the pubspec for a specific version of a package that |
53 * Downloads and parses the pubspec for a specific version of a package that | 47 /// is available from the site. |
54 * is available from the site. | |
55 */ | |
56 Future<Pubspec> describe(PackageId id) { | 48 Future<Pubspec> describe(PackageId id) { |
57 var parsed = _parseDescription(id.description); | 49 var parsed = _parseDescription(id.description); |
58 var fullUrl = "${parsed.last}/packages/${parsed.first}/versions/" | 50 var fullUrl = "${parsed.last}/packages/${parsed.first}/versions/" |
59 "${id.version}.yaml"; | 51 "${id.version}.yaml"; |
60 | 52 |
61 return httpClient.read(fullUrl).transform((yaml) { | 53 return httpClient.read(fullUrl).transform((yaml) { |
62 return new Pubspec.parse(yaml, systemCache.sources); | 54 return new Pubspec.parse(yaml, systemCache.sources); |
63 }).transformException((ex) { | 55 }).transformException((ex) { |
64 _throwFriendlyError(ex, id, parsed.last); | 56 _throwFriendlyError(ex, id, parsed.last); |
65 }); | 57 }); |
66 } | 58 } |
67 | 59 |
68 /** | 60 /// Downloads a package from the site and unpacks it. |
69 * Downloads a package from the site and unpacks it. | |
70 */ | |
71 Future<bool> install(PackageId id, String destPath) { | 61 Future<bool> install(PackageId id, String destPath) { |
72 var parsedDescription = _parseDescription(id.description); | 62 var parsedDescription = _parseDescription(id.description); |
73 var name = parsedDescription.first; | 63 var name = parsedDescription.first; |
74 var url = parsedDescription.last; | 64 var url = parsedDescription.last; |
75 | 65 |
76 var fullUrl = "$url/packages/$name/versions/${id.version}.tar.gz"; | 66 var fullUrl = "$url/packages/$name/versions/${id.version}.tar.gz"; |
77 | 67 |
78 log.message('Downloading $id...'); | 68 log.message('Downloading $id...'); |
79 | 69 |
80 // Download and extract the archive to a temp directory. | 70 // Download and extract the archive to a temp directory. |
81 var tempDir; | 71 var tempDir; |
82 return Futures.wait([ | 72 return Futures.wait([ |
83 httpClient.send(new http.Request("GET", new Uri.fromString(fullUrl))) | 73 httpClient.send(new http.Request("GET", new Uri.fromString(fullUrl))) |
84 .transform((response) => response.stream), | 74 .transform((response) => response.stream), |
85 systemCache.createTempDir() | 75 systemCache.createTempDir() |
86 ]).chain((args) { | 76 ]).chain((args) { |
87 tempDir = args[1]; | 77 tempDir = args[1]; |
88 return timeout(extractTarGz(args[0], tempDir), HTTP_TIMEOUT, | 78 return timeout(extractTarGz(args[0], tempDir), HTTP_TIMEOUT, |
89 'fetching URL "$fullUrl"'); | 79 'fetching URL "$fullUrl"'); |
90 }).chain((_) { | 80 }).chain((_) { |
91 // Now that the install has succeeded, move it to the real location in | 81 // Now that the install has succeeded, move it to the real location in |
92 // the cache. This ensures that we don't leave half-busted ghost | 82 // the cache. This ensures that we don't leave half-busted ghost |
93 // directories in the user's pub cache if an install fails. | 83 // directories in the user's pub cache if an install fails. |
94 return renameDir(tempDir, destPath); | 84 return renameDir(tempDir, destPath); |
95 }).transform((_) => true); | 85 }).transform((_) => true); |
96 } | 86 } |
97 | 87 |
98 /** | 88 /// The system cache directory for the hosted source contains subdirectories |
99 * The system cache directory for the hosted source contains subdirectories | 89 /// for each separate repository URL that's used on the system. Each of these |
100 * for each separate repository URL that's used on the system. Each of these | 90 /// subdirectories then contains a subdirectory for each package installed |
101 * subdirectories then contains a subdirectory for each package installed | 91 /// from that site. |
102 * from that site. | |
103 */ | |
104 String systemCacheDirectory(PackageId id) { | 92 String systemCacheDirectory(PackageId id) { |
105 var parsed = _parseDescription(id.description); | 93 var parsed = _parseDescription(id.description); |
106 var url = parsed.last.replaceAll(new RegExp(r"^https?://"), ""); | 94 var url = parsed.last.replaceAll(new RegExp(r"^https?://"), ""); |
107 var urlDir = replace(url, new RegExp(r'[<>:"\\/|?*%]'), (match) { | 95 var urlDir = replace(url, new RegExp(r'[<>:"\\/|?*%]'), (match) { |
108 return '%${match[0].charCodeAt(0)}'; | 96 return '%${match[0].charCodeAt(0)}'; |
109 }); | 97 }); |
110 return join(systemCacheRoot, urlDir, "${parsed.first}-${id.version}"); | 98 return join(systemCacheRoot, urlDir, "${parsed.first}-${id.version}"); |
111 } | 99 } |
112 | 100 |
113 String packageName(description) => _parseDescription(description).first; | 101 String packageName(description) => _parseDescription(description).first; |
114 | 102 |
115 bool descriptionsEqual(description1, description2) => | 103 bool descriptionsEqual(description1, description2) => |
116 _parseDescription(description1) == _parseDescription(description2); | 104 _parseDescription(description1) == _parseDescription(description2); |
117 | 105 |
118 /** | 106 /// Ensures that [description] is a valid hosted package description. |
119 * Ensures that [description] is a valid hosted package description. | 107 /// |
120 * | 108 /// There are two valid formats. A plain string refers to a package with the |
121 * There are two valid formats. A plain string refers to a package with the | 109 /// given name from the default host, while a map with keys "name" and "url" |
122 * given name from the default host, while a map with keys "name" and "url" | 110 /// refers to a package with the given name from the host at the given URL. |
123 * refers to a package with the given name from the host at the given URL. | |
124 */ | |
125 void validateDescription(description, {bool fromLockFile: false}) { | 111 void validateDescription(description, {bool fromLockFile: false}) { |
126 _parseDescription(description); | 112 _parseDescription(description); |
127 } | 113 } |
128 | 114 |
129 /// When an error occurs trying to read something about [package] from [url], | 115 /// When an error occurs trying to read something about [package] from [url], |
130 /// this tries to translate into a more user friendly error message. Always | 116 /// this tries to translate into a more user friendly error message. Always |
131 /// throws an error, either the original one or a better one. | 117 /// throws an error, either the original one or a better one. |
132 void _throwFriendlyError(ex, package, url) { | 118 void _throwFriendlyError(ex, package, url) { |
133 if (ex is PubHttpException && ex.response.statusCode == 404) { | 119 if (ex is PubHttpException && ex.response.statusCode == 404) { |
134 throw 'Could not find package "$package" at $url.'; | 120 throw 'Could not find package "$package" at $url.'; |
135 } | 121 } |
136 | 122 |
137 if (ex is TimeoutException) { | 123 if (ex is TimeoutException) { |
138 throw 'Timed out trying to find package "$package" at $url.'; | 124 throw 'Timed out trying to find package "$package" at $url.'; |
139 } | 125 } |
140 | 126 |
141 if (ex is io.SocketIOException) { | 127 if (ex is io.SocketIOException) { |
142 throw 'Got socket error trying to find package "$package" at $url.\n' | 128 throw 'Got socket error trying to find package "$package" at $url.\n' |
143 '${ex.osError}'; | 129 '${ex.osError}'; |
144 } | 130 } |
145 | 131 |
146 // Otherwise re-throw the original exception. | 132 // Otherwise re-throw the original exception. |
147 throw ex; | 133 throw ex; |
148 } | 134 } |
149 | 135 |
150 /** | 136 /// Parses the description for a package. |
151 * Parses the description for a package. | 137 /// |
152 * | 138 /// If the package parses correctly, this returns a (name, url) pair. If not, |
153 * If the package parses correctly, this returns a (name, url) pair. If not, | 139 /// this throws a descriptive FormatException. |
154 * this throws a descriptive FormatException. | |
155 */ | |
156 Pair<String, String> _parseDescription(description) { | 140 Pair<String, String> _parseDescription(description) { |
157 if (description is String) { | 141 if (description is String) { |
158 return new Pair<String, String>(description, defaultUrl); | 142 return new Pair<String, String>(description, defaultUrl); |
159 } | 143 } |
160 | 144 |
161 if (description is! Map) { | 145 if (description is! Map) { |
162 throw new FormatException( | 146 throw new FormatException( |
163 "The description must be a package name or map."); | 147 "The description must be a package name or map."); |
164 } | 148 } |
165 | 149 |
166 if (!description.containsKey("name")) { | 150 if (!description.containsKey("name")) { |
167 throw new FormatException( | 151 throw new FormatException( |
168 "The description map must contain a 'name' key."); | 152 "The description map must contain a 'name' key."); |
169 } | 153 } |
170 | 154 |
171 var name = description["name"]; | 155 var name = description["name"]; |
172 if (name is! String) { | 156 if (name is! String) { |
173 throw new FormatException("The 'name' key must have a string value."); | 157 throw new FormatException("The 'name' key must have a string value."); |
174 } | 158 } |
175 | 159 |
176 var url = description.containsKey("url") ? description["url"] : defaultUrl; | 160 var url = description.containsKey("url") ? description["url"] : defaultUrl; |
177 return new Pair<String, String>(name, url); | 161 return new Pair<String, String>(name, url); |
178 } | 162 } |
179 } | 163 } |
OLD | NEW |