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 'pub.dart'; |
| 14 import 'io.dart'; |
13 import 'git.dart' as git; | 15 import 'git.dart' as git; |
14 import 'io.dart'; | |
15 import 'log.dart' as log; | |
16 import 'oauth2.dart' as oauth2; | 16 import 'oauth2.dart' as oauth2; |
17 import 'pub.dart'; | |
18 import 'validator.dart'; | 17 import 'validator.dart'; |
19 | 18 |
20 // TODO(nweiz): Make "publish" the primary name for this command. See issue | 19 // TODO(nweiz): Make "publish" the primary name for this command. See issue |
21 // 6949. | 20 // 6949. |
22 /// Handles the `lish` and `publish` pub commands. | 21 /// Handles the `lish` and `publish` pub commands. |
23 class LishCommand extends PubCommand { | 22 class LishCommand extends PubCommand { |
24 final description = "publish the current package to pub.dartlang.org"; | 23 final description = "publish the current package to pub.dartlang.org"; |
25 final usage = "pub publish [options]"; | 24 final usage = "pub publish [options]"; |
26 final aliases = const ["lish", "lush"]; | 25 final aliases = const ["lish", "lush"]; |
27 | 26 |
28 ArgParser get commandParser { | 27 ArgParser get commandParser { |
29 var parser = new ArgParser(); | 28 var parser = new ArgParser(); |
30 parser.addOption('server', defaultsTo: 'https://pub.dartlang.org', | 29 parser.addOption('server', defaultsTo: 'https://pub.dartlang.org', |
31 help: 'The package server to which to upload this package'); | 30 help: 'The package server to which to upload this package'); |
32 return parser; | 31 return parser; |
33 } | 32 } |
34 | 33 |
35 /// The URL of the server to which to upload the package. | 34 /// The URL of the server to which to upload the package. |
36 Uri get server => new Uri.fromString(commandOptions['server']); | 35 Uri get server => new Uri.fromString(commandOptions['server']); |
37 | 36 |
38 Future onRun() { | 37 Future onRun() { |
39 var cloudStorageUrl; | 38 var cloudStorageUrl; |
40 return oauth2.withClient(cache, (client) { | 39 return oauth2.withClient(cache, (client) { |
41 // TODO(nweiz): Cloud Storage can provide an XML-formatted error. We | 40 // TODO(nweiz): Cloud Storage can provide an XML-formatted error. We |
42 // should report that error and exit. | 41 // should report that error and exit. |
43 return Futures.wait([ | 42 return Futures.wait([ |
44 client.get(server.resolve("/packages/versions/new.json")), | 43 client.get(server.resolve("/packages/versions/new.json")), |
45 _filesToPublish.transform((files) { | 44 _filesToPublish.transform((files) { |
46 log.fine('Archiving and publishing ${entrypoint.root}.'); | |
47 return createTarGz(files, baseDir: entrypoint.root.dir); | 45 return createTarGz(files, baseDir: entrypoint.root.dir); |
48 }).chain(consumeInputStream), | 46 }).chain(consumeInputStream), |
49 _validate() | 47 _validate() |
50 ]).chain((results) { | 48 ]).chain((results) { |
51 var response = results[0]; | 49 var response = results[0]; |
52 var packageBytes = results[1]; | 50 var packageBytes = results[1]; |
53 var parameters = _parseJson(response); | 51 var parameters = _parseJson(response); |
54 | 52 |
55 var url = _expectField(parameters, 'url', response); | 53 var url = _expectField(parameters, 'url', response); |
56 if (url is! String) _invalidServerResponse(response); | 54 if (url is! String) _invalidServerResponse(response); |
(...skipping 15 matching lines...) Expand all Loading... |
72 var location = response.headers['location']; | 70 var location = response.headers['location']; |
73 if (location == null) throw new PubHttpException(response); | 71 if (location == null) throw new PubHttpException(response); |
74 return location; | 72 return location; |
75 }).chain((location) => client.get(location)).transform((response) { | 73 }).chain((location) => client.get(location)).transform((response) { |
76 var parsed = _parseJson(response); | 74 var parsed = _parseJson(response); |
77 if (parsed['success'] is! Map || | 75 if (parsed['success'] is! Map || |
78 !parsed['success'].containsKey('message') || | 76 !parsed['success'].containsKey('message') || |
79 parsed['success']['message'] is! String) { | 77 parsed['success']['message'] is! String) { |
80 _invalidServerResponse(response); | 78 _invalidServerResponse(response); |
81 } | 79 } |
82 log.message(parsed['success']['message']); | 80 print(parsed['success']['message']); |
83 }); | 81 }); |
84 }).transformException((e) { | 82 }).transformException((e) { |
85 if (e is PubHttpException) { | 83 if (e is PubHttpException) { |
86 var url = e.response.request.url; | 84 var url = e.response.request.url; |
87 if (url.toString() == cloudStorageUrl.toString()) { | 85 if (url.toString() == cloudStorageUrl.toString()) { |
88 // TODO(nweiz): the response may have XML-formatted information about | 86 // TODO(nweiz): the response may have XML-formatted information about |
89 // the error. Try to parse that out once we have an easily-accessible | 87 // the error. Try to parse that out once we have an easily-accessible |
90 // XML parser. | 88 // XML parser. |
91 throw 'Failed to upload the package.'; | 89 throw 'Failed to upload the package.'; |
92 } else if (url.origin == server.origin) { | 90 } else if (url.origin == server.origin) { |
93 var errorMap = _parseJson(e.response); | 91 var errorMap = _parseJson(e.response); |
94 if (errorMap['error'] is! Map || | 92 if (errorMap['error'] is! Map || |
95 !errorMap['error'].containsKey('message') || | 93 !errorMap['error'].containsKey('message') || |
96 errorMap['error']['message'] is! String) { | 94 errorMap['error']['message'] is! String) { |
97 _invalidServerResponse(e.response); | 95 _invalidServerResponse(e.response); |
98 } | 96 } |
99 throw errorMap['error']['message']; | 97 throw errorMap['error']['message']; |
100 } | 98 } |
101 } else if (e is oauth2.ExpirationException) { | 99 } else if (e is oauth2.ExpirationException) { |
102 log.error("Pub's authorization to upload packages has expired and " | 100 printError("Pub's authorization to upload packages has expired and " |
103 "can't be automatically refreshed."); | 101 "can't be automatically refreshed."); |
104 return onRun(); | 102 return onRun(); |
105 } else if (e is oauth2.AuthorizationException) { | 103 } else if (e is oauth2.AuthorizationException) { |
106 var message = "OAuth2 authorization failed"; | 104 var message = "OAuth2 authorization failed"; |
107 if (e.description != null) message = "$message (${e.description})"; | 105 if (e.description != null) message = "$message (${e.description})"; |
108 log.error("$message."); | 106 printError("$message."); |
109 return oauth2.clearCredentials(cache).chain((_) => onRun()); | 107 return oauth2.clearCredentials(cache).chain((_) => onRun()); |
110 } else { | 108 } else { |
111 throw e; | 109 throw e; |
112 } | 110 } |
113 | |
114 if (e is! oauth2.ExpirationException) throw e; | |
115 | |
116 log.error("Pub's authorization to upload packages has expired and can't " | |
117 "be automatically refreshed."); | |
118 return onRun(); | |
119 }); | 111 }); |
120 } | 112 } |
121 | 113 |
122 /// The basenames of files that are automatically excluded from archives. | 114 /// The basenames of files that are automatically excluded from archives. |
123 final _BLACKLISTED_FILES = const ['pubspec.lock']; | 115 final _BLACKLISTED_FILES = const ['pubspec.lock']; |
124 | 116 |
125 /// The basenames of directories that are automatically excluded from | 117 /// The basenames of directories that are automatically excluded from |
126 /// archives. | 118 /// archives. |
127 final _BLACKLISTED_DIRECTORIES = const ['packages']; | 119 final _BLACKLISTED_DIRECTORIES = const ['packages']; |
128 | 120 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 var s = warnings.length == 1 ? '' : 's'; | 185 var s = warnings.length == 1 ? '' : 's'; |
194 stdout.writeString("Package has ${warnings.length} warning$s. Upload " | 186 stdout.writeString("Package has ${warnings.length} warning$s. Upload " |
195 "anyway (y/n)? "); | 187 "anyway (y/n)? "); |
196 return readLine().transform((line) { | 188 return readLine().transform((line) { |
197 if (new RegExp(r"^[yY]").hasMatch(line)) return; | 189 if (new RegExp(r"^[yY]").hasMatch(line)) return; |
198 throw "Package upload canceled."; | 190 throw "Package upload canceled."; |
199 }); | 191 }); |
200 }); | 192 }); |
201 } | 193 } |
202 } | 194 } |
OLD | NEW |