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