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 command_lish; | 5 library command_lish; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'dart:json'; | 8 import 'dart:json'; |
9 import 'dart:uri'; | 9 import 'dart:uri'; |
10 | 10 |
11 import '../../pkg/args/lib/args.dart'; | 11 import '../../pkg/args/lib/args.dart'; |
12 import '../../pkg/http/lib/http.dart' as http; | 12 import '../../pkg/http/lib/http.dart' as http; |
13 import 'directory_tree.dart'; | 13 import 'directory_tree.dart'; |
14 import 'git.dart' as git; | 14 import 'git.dart' as git; |
| 15 import 'http.dart'; |
15 import 'io.dart'; | 16 import 'io.dart'; |
16 import 'log.dart' as log; | 17 import 'log.dart' as log; |
17 import 'oauth2.dart' as oauth2; | 18 import 'oauth2.dart' as oauth2; |
18 import 'path.dart' as path; | 19 import 'path.dart' as path; |
19 import 'pub.dart'; | 20 import 'pub.dart'; |
20 import 'validator.dart'; | 21 import 'validator.dart'; |
21 | 22 |
22 /// Handles the `lish` and `publish` pub commands. | 23 /// Handles the `lish` and `publish` pub commands. |
23 class LishCommand extends PubCommand { | 24 class LishCommand extends PubCommand { |
24 final description = "Publish the current package to pub.dartlang.org."; | 25 final description = "Publish the current package to pub.dartlang.org."; |
25 final usage = "pub publish [options]"; | 26 final usage = "pub publish [options]"; |
26 final aliases = const ["lish", "lush"]; | 27 final aliases = const ["lish", "lush"]; |
27 | 28 |
28 ArgParser get commandParser { | 29 ArgParser get commandParser { |
29 var parser = new ArgParser(); | 30 var parser = new ArgParser(); |
| 31 // TODO(nweiz): Use HostedSource.defaultUrl as the default value once we use |
| 32 // dart:io for HTTPS requests. |
30 parser.addOption('server', defaultsTo: 'https://pub.dartlang.org', | 33 parser.addOption('server', defaultsTo: 'https://pub.dartlang.org', |
31 help: 'The package server to which to upload this package'); | 34 help: 'The package server to which to upload this package'); |
32 return parser; | 35 return parser; |
33 } | 36 } |
34 | 37 |
35 /// The URL of the server to which to upload the package. | 38 /// The URL of the server to which to upload the package. |
36 Uri get server => new Uri.fromString(commandOptions['server']); | 39 Uri get server => new Uri.fromString(commandOptions['server']); |
37 | 40 |
38 Future _publish(packageBytes) { | 41 Future _publish(packageBytes) { |
39 var cloudStorageUrl; | 42 var cloudStorageUrl; |
40 return oauth2.withClient(cache, (client) { | 43 return oauth2.withClient(cache, (client) { |
41 // TODO(nweiz): Cloud Storage can provide an XML-formatted error. We | 44 // TODO(nweiz): Cloud Storage can provide an XML-formatted error. We |
42 // should report that error and exit. | 45 // should report that error and exit. |
43 var newUri = server.resolve("/packages/versions/new.json"); | 46 var newUri = server.resolve("/packages/versions/new.json"); |
44 return client.get(newUri).chain((response) { | 47 return client.get(newUri).chain((response) { |
45 var parameters = _parseJson(response); | 48 var parameters = parseJsonResponse(response); |
46 | 49 |
47 var url = _expectField(parameters, 'url', response); | 50 var url = _expectField(parameters, 'url', response); |
48 if (url is! String) _invalidServerResponse(response); | 51 if (url is! String) invalidServerResponse(response); |
49 cloudStorageUrl = new Uri.fromString(url); | 52 cloudStorageUrl = new Uri.fromString(url); |
50 var request = new http.MultipartRequest('POST', cloudStorageUrl); | 53 var request = new http.MultipartRequest('POST', cloudStorageUrl); |
51 | 54 |
52 var fields = _expectField(parameters, 'fields', response); | 55 var fields = _expectField(parameters, 'fields', response); |
53 if (fields is! Map) _invalidServerResponse(response); | 56 if (fields is! Map) invalidServerResponse(response); |
54 fields.forEach((key, value) { | 57 fields.forEach((key, value) { |
55 if (value is! String) _invalidServerResponse(response); | 58 if (value is! String) invalidServerResponse(response); |
56 request.fields[key] = value; | 59 request.fields[key] = value; |
57 }); | 60 }); |
58 | 61 |
59 request.followRedirects = false; | 62 request.followRedirects = false; |
60 request.files.add(new http.MultipartFile.fromBytes( | 63 request.files.add(new http.MultipartFile.fromBytes( |
61 'file', packageBytes, filename: 'package.tar.gz')); | 64 'file', packageBytes, filename: 'package.tar.gz')); |
62 return client.send(request); | 65 return client.send(request); |
63 }).chain(http.Response.fromStream).transform((response) { | 66 }).chain(http.Response.fromStream).transform((response) { |
64 var location = response.headers['location']; | 67 var location = response.headers['location']; |
65 if (location == null) throw new PubHttpException(response); | 68 if (location == null) throw new PubHttpException(response); |
66 return location; | 69 return location; |
67 }).chain((location) => client.get(location)).transform((response) { | 70 }).chain((location) => client.get(location)) |
68 var parsed = _parseJson(response); | 71 .transform(handleJsonSuccess); |
69 if (parsed['success'] is! Map || | |
70 !parsed['success'].containsKey('message') || | |
71 parsed['success']['message'] is! String) { | |
72 _invalidServerResponse(response); | |
73 } | |
74 log.message(parsed['success']['message']); | |
75 }); | |
76 }).transformException((e) { | 72 }).transformException((e) { |
77 if (e is PubHttpException) { | 73 if (e is! PubHttpException) throw e; |
78 var url = e.response.request.url; | 74 var url = e.response.request.url; |
79 if (url.toString() == cloudStorageUrl.toString()) { | 75 if (url.toString() == cloudStorageUrl.toString()) { |
80 // TODO(nweiz): the response may have XML-formatted information about | 76 // TODO(nweiz): the response may have XML-formatted information about |
81 // the error. Try to parse that out once we have an easily-accessible | 77 // the error. Try to parse that out once we have an easily-accessible |
82 // XML parser. | 78 // XML parser. |
83 throw 'Failed to upload the package.'; | 79 throw 'Failed to upload the package.'; |
84 } else if (url.origin == server.origin) { | 80 } else if (url.origin == server.origin) { |
85 var errorMap = _parseJson(e.response); | 81 handleJsonError(e.response); |
86 if (errorMap['error'] is! Map || | |
87 !errorMap['error'].containsKey('message') || | |
88 errorMap['error']['message'] is! String) { | |
89 _invalidServerResponse(e.response); | |
90 } | |
91 throw errorMap['error']['message']; | |
92 } | |
93 } else if (e is oauth2.ExpirationException) { | |
94 log.error("Pub's authorization to upload packages has expired and " | |
95 "can't be automatically refreshed."); | |
96 return _publish(packageBytes); | |
97 } else if (e is oauth2.AuthorizationException) { | |
98 var message = "OAuth2 authorization failed"; | |
99 if (e.description != null) message = "$message (${e.description})"; | |
100 log.error("$message."); | |
101 return oauth2.clearCredentials(cache).chain((_) => | |
102 _publish(packageBytes)); | |
103 } else { | |
104 throw e; | |
105 } | 82 } |
106 }); | 83 }); |
107 } | 84 } |
108 | 85 |
109 Future onRun() { | 86 Future onRun() { |
110 var files; | 87 var files; |
111 return _filesToPublish.transform((f) { | 88 return _filesToPublish.transform((f) { |
112 files = f; | 89 files = f; |
113 log.fine('Archiving and publishing ${entrypoint.root}.'); | 90 log.fine('Archiving and publishing ${entrypoint.root}.'); |
114 return createTarGz(files, baseDir: entrypoint.root.dir); | 91 return createTarGz(files, baseDir: entrypoint.root.dir); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 }); | 162 }); |
186 }).transform((files) => files.filter((file) { | 163 }).transform((files) => files.filter((file) { |
187 if (file == null || _BLACKLISTED_FILES.contains(basename(file))) { | 164 if (file == null || _BLACKLISTED_FILES.contains(basename(file))) { |
188 return false; | 165 return false; |
189 } | 166 } |
190 | 167 |
191 return !splitPath(file).some(_BLACKLISTED_DIRECTORIES.contains); | 168 return !splitPath(file).some(_BLACKLISTED_DIRECTORIES.contains); |
192 })); | 169 })); |
193 } | 170 } |
194 | 171 |
195 /// Parses a response body, assuming it's JSON-formatted. Throws a | |
196 /// user-friendly error if the response body is invalid JSON, or if it's not a | |
197 /// map. | |
198 Map _parseJson(http.Response response) { | |
199 var value; | |
200 try { | |
201 value = JSON.parse(response.body); | |
202 } catch (e) { | |
203 // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. | |
204 _invalidServerResponse(response); | |
205 } | |
206 if (value is! Map) _invalidServerResponse(response); | |
207 return value; | |
208 } | |
209 | |
210 /// Returns the value associated with [key] in [map]. Throws a user-friendly | 172 /// Returns the value associated with [key] in [map]. Throws a user-friendly |
211 /// error if [map] doens't contain [key]. | 173 /// error if [map] doens't contain [key]. |
212 _expectField(Map map, String key, http.Response response) { | 174 _expectField(Map map, String key, http.Response response) { |
213 if (map.containsKey(key)) return map[key]; | 175 if (map.containsKey(key)) return map[key]; |
214 _invalidServerResponse(response); | 176 invalidServerResponse(response); |
215 } | |
216 | |
217 /// Throws an error describing an invalid response from the server. | |
218 void _invalidServerResponse(http.Response response) { | |
219 throw 'Invalid server response:\n${response.body}'; | |
220 } | 177 } |
221 | 178 |
222 /// Validates the package. Throws an exception if it's invalid. | 179 /// Validates the package. Throws an exception if it's invalid. |
223 Future _validate() { | 180 Future _validate() { |
224 return Validator.runAll(entrypoint).chain((pair) { | 181 return Validator.runAll(entrypoint).chain((pair) { |
225 var errors = pair.first; | 182 var errors = pair.first; |
226 var warnings = pair.last; | 183 var warnings = pair.last; |
227 | 184 |
228 if (!errors.isEmpty) { | 185 if (!errors.isEmpty) { |
229 throw "Sorry, your package is missing " | 186 throw "Sorry, your package is missing " |
230 "${(errors.length > 1) ? 'some requirements' : 'a requirement'} " | 187 "${(errors.length > 1) ? 'some requirements' : 'a requirement'} " |
231 "and can't be published yet.\nFor more information, see: " | 188 "and can't be published yet.\nFor more information, see: " |
232 "http://pub.dartlang.org/doc/pub-lish.html.\n"; | 189 "http://pub.dartlang.org/doc/pub-lish.html.\n"; |
233 } | 190 } |
234 | 191 |
235 var message = 'Looks great! Are you ready to upload your package'; | 192 var message = 'Looks great! Are you ready to upload your package'; |
236 | 193 |
237 if (!warnings.isEmpty) { | 194 if (!warnings.isEmpty) { |
238 var s = warnings.length == 1 ? '' : 's'; | 195 var s = warnings.length == 1 ? '' : 's'; |
239 message = "Package has ${warnings.length} warning$s. Upload anyway"; | 196 message = "Package has ${warnings.length} warning$s. Upload anyway"; |
240 } | 197 } |
241 | 198 |
242 return confirm(message).transform((confirmed) { | 199 return confirm(message).transform((confirmed) { |
243 if (!confirmed) throw "Package upload canceled."; | 200 if (!confirmed) throw "Package upload canceled."; |
244 }); | 201 }); |
245 }); | 202 }); |
246 } | 203 } |
247 } | 204 } |
OLD | NEW |