| 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:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:json'; | 9 import 'dart:json'; |
| 10 import 'dart:uri'; | 10 import 'dart:uri'; |
| 11 | 11 |
| 12 import '../../pkg/args/lib/args.dart'; | 12 import '../../pkg/args/lib/args.dart'; |
| 13 import '../../pkg/http/lib/http.dart' as http; | 13 import '../../pkg/http/lib/http.dart' as http; |
| 14 import '../../pkg/path/lib/path.dart' as path; | 14 import '../../pkg/path/lib/path.dart' as path; |
| 15 import 'directory_tree.dart'; | 15 import 'directory_tree.dart'; |
| 16 import 'exit_codes.dart' as exit_codes; |
| 16 import 'git.dart' as git; | 17 import 'git.dart' as git; |
| 17 import 'http.dart'; | 18 import 'http.dart'; |
| 18 import 'io.dart'; | 19 import 'io.dart'; |
| 19 import 'log.dart' as log; | 20 import 'log.dart' as log; |
| 20 import 'oauth2.dart' as oauth2; | 21 import 'oauth2.dart' as oauth2; |
| 21 import 'pub.dart'; | 22 import 'pub.dart'; |
| 22 import 'utils.dart'; | 23 import 'utils.dart'; |
| 23 import 'validator.dart'; | 24 import 'validator.dart'; |
| 24 | 25 |
| 25 /// Handles the `lish` and `publish` pub commands. | 26 /// Handles the `lish` and `publish` pub commands. |
| 26 class LishCommand extends PubCommand { | 27 class LishCommand extends PubCommand { |
| 27 final description = "Publish the current package to pub.dartlang.org."; | 28 final description = "Publish the current package to pub.dartlang.org."; |
| 28 final usage = "pub publish [options]"; | 29 final usage = "pub publish [options]"; |
| 29 final aliases = const ["lish", "lush"]; | 30 final aliases = const ["lish", "lush"]; |
| 30 | 31 |
| 31 ArgParser get commandParser { | 32 ArgParser get commandParser { |
| 32 var parser = new ArgParser(); | 33 var parser = new ArgParser(); |
| 33 // TODO(nweiz): Use HostedSource.defaultUrl as the default value once we use | 34 // TODO(nweiz): Use HostedSource.defaultUrl as the default value once we use |
| 34 // dart:io for HTTPS requests. | 35 // dart:io for HTTPS requests. |
| 35 parser.addFlag('dry-run', abbr: 'n', negatable: false, | 36 parser.addFlag('dry-run', abbr: 'n', negatable: false, |
| 36 help: 'Validate but do not publish the package'); | 37 help: 'Validate but do not publish the package'); |
| 38 parser.addFlag('force', abbr: 'f', negatable: false, |
| 39 help: 'Publish without confirmation if there are no errors'); |
| 37 parser.addOption('server', defaultsTo: 'https://pub.dartlang.org', | 40 parser.addOption('server', defaultsTo: 'https://pub.dartlang.org', |
| 38 help: 'The package server to which to upload this package'); | 41 help: 'The package server to which to upload this package'); |
| 39 return parser; | 42 return parser; |
| 40 } | 43 } |
| 41 | 44 |
| 42 /// The URL of the server to which to upload the package. | 45 /// The URL of the server to which to upload the package. |
| 43 Uri get server => Uri.parse(commandOptions['server']); | 46 Uri get server => Uri.parse(commandOptions['server']); |
| 44 | 47 |
| 48 /// Whether the publish is just a preview. |
| 49 bool get dryRun => commandOptions['dry-run']; |
| 50 |
| 51 /// Whether the publish requires confirmation. |
| 52 bool get force => commandOptions['force']; |
| 53 |
| 45 Future _publish(packageBytes) { | 54 Future _publish(packageBytes) { |
| 46 var cloudStorageUrl; | 55 var cloudStorageUrl; |
| 47 return oauth2.withClient(cache, (client) { | 56 return oauth2.withClient(cache, (client) { |
| 48 // TODO(nweiz): Cloud Storage can provide an XML-formatted error. We | 57 // TODO(nweiz): Cloud Storage can provide an XML-formatted error. We |
| 49 // should report that error and exit. | 58 // should report that error and exit. |
| 50 var newUri = server.resolve("/packages/versions/new.json"); | 59 var newUri = server.resolve("/packages/versions/new.json"); |
| 51 return client.get(newUri).then((response) { | 60 return client.get(newUri).then((response) { |
| 52 var parameters = parseJsonResponse(response); | 61 var parameters = parseJsonResponse(response); |
| 53 | 62 |
| 54 var url = _expectField(parameters, 'url', response); | 63 var url = _expectField(parameters, 'url', response); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 81 // the error. Try to parse that out once we have an easily-accessible | 90 // the error. Try to parse that out once we have an easily-accessible |
| 82 // XML parser. | 91 // XML parser. |
| 83 throw 'Failed to upload the package.'; | 92 throw 'Failed to upload the package.'; |
| 84 } else if (url.origin == server.origin) { | 93 } else if (url.origin == server.origin) { |
| 85 handleJsonError(asyncError.error.response); | 94 handleJsonError(asyncError.error.response); |
| 86 } | 95 } |
| 87 }); | 96 }); |
| 88 } | 97 } |
| 89 | 98 |
| 90 Future onRun() { | 99 Future onRun() { |
| 100 if (force && dryRun) { |
| 101 log.error('Cannot use both --force and --dry-run.'); |
| 102 this.printUsage(); |
| 103 exit(exit_codes.USAGE); |
| 104 } |
| 105 |
| 91 var packageBytesFuture = _filesToPublish.then((files) { | 106 var packageBytesFuture = _filesToPublish.then((files) { |
| 92 log.fine('Archiving and publishing ${entrypoint.root}.'); | 107 log.fine('Archiving and publishing ${entrypoint.root}.'); |
| 93 | 108 |
| 94 // Show the package contents so the user can verify they look OK. | 109 // Show the package contents so the user can verify they look OK. |
| 95 var package = entrypoint.root; | 110 var package = entrypoint.root; |
| 96 log.message( | 111 log.message( |
| 97 'Publishing "${package.name}" ${package.version}:\n' | 112 'Publishing "${package.name}" ${package.version}:\n' |
| 98 '${generateTree(files)}'); | 113 '${generateTree(files)}'); |
| 99 | 114 |
| 100 return createTarGz(files, baseDir: entrypoint.root.dir); | 115 return createTarGz(files, baseDir: entrypoint.root.dir); |
| 101 }).then((stream) => stream.toBytes()); | 116 }).then((stream) => stream.toBytes()); |
| 102 | 117 |
| 103 // Validate the package. | 118 // Validate the package. |
| 104 return _validate(packageBytesFuture.then((bytes) => bytes.length)) | 119 return _validate(packageBytesFuture.then((bytes) => bytes.length)) |
| 105 .then((isValid) { | 120 .then((isValid) { |
| 106 if (isValid) return packageBytesFuture.then(_publish); | 121 if (isValid) return packageBytesFuture.then(_publish); |
| 107 }); | 122 }); |
| 108 } | 123 } |
| 109 | 124 |
| 110 /// The basenames of files that are automatically excluded from archives. | 125 /// The basenames of files that are automatically excluded from archives. |
| 111 final _BLACKLISTED_FILES = const ['pubspec.lock']; | 126 final _BLACKLISTED_FILES = const ['pubspec.lock']; |
| 112 | 127 |
| 113 /// The basenames of directories that are automatically excluded from | 128 /// The basenames of directories that are automatically excluded from |
| 114 /// archives. | 129 /// archives. |
| 115 final _BLACKLISTED_DIRS = const ['packages']; | 130 final _BLACKLISTED_DIRS = const ['packages']; |
| 116 | 131 |
| 117 /// Returns a list of files that should be included in the published package. | 132 /// Returns a list of files that should be included in the published package. |
| 118 /// If this is a Git repository, this will respect .gitignore; otherwise, it | 133 /// If this is a Git repository, this will respect .gitignore; otherwise, it |
| 119 /// will return all non-hidden files. | 134 /// will return all non-hidden files. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 142 return !splitPath(file).any(_BLACKLISTED_DIRS.contains); | 157 return !splitPath(file).any(_BLACKLISTED_DIRS.contains); |
| 143 } | 158 } |
| 144 | 159 |
| 145 /// Returns the value associated with [key] in [map]. Throws a user-friendly | 160 /// Returns the value associated with [key] in [map]. Throws a user-friendly |
| 146 /// error if [map] doens't contain [key]. | 161 /// error if [map] doens't contain [key]. |
| 147 _expectField(Map map, String key, http.Response response) { | 162 _expectField(Map map, String key, http.Response response) { |
| 148 if (map.containsKey(key)) return map[key]; | 163 if (map.containsKey(key)) return map[key]; |
| 149 invalidServerResponse(response); | 164 invalidServerResponse(response); |
| 150 } | 165 } |
| 151 | 166 |
| 152 /// Validates the package. Throws an exception if it's invalid. | 167 /// Validates the package. Completes to false if the upload should not |
| 168 /// proceed. |
| 153 Future<bool> _validate(Future<int> packageSize) { | 169 Future<bool> _validate(Future<int> packageSize) { |
| 154 return Validator.runAll(entrypoint, packageSize).then((pair) { | 170 return Validator.runAll(entrypoint, packageSize).then((pair) { |
| 155 var errors = pair.first; | 171 var errors = pair.first; |
| 156 var warnings = pair.last; | 172 var warnings = pair.last; |
| 157 | 173 |
| 158 if (!errors.isEmpty) { | 174 if (!errors.isEmpty) { |
| 159 log.error("Sorry, your package is missing " | 175 log.error("Sorry, your package is missing " |
| 160 "${(errors.length > 1) ? 'some requirements' : 'a requirement'} " | 176 "${(errors.length > 1) ? 'some requirements' : 'a requirement'} " |
| 161 "and can't be published yet.\nFor more information, see: " | 177 "and can't be published yet.\nFor more information, see: " |
| 162 "http://pub.dartlang.org/doc/pub-lish.html.\n"); | 178 "http://pub.dartlang.org/doc/pub-lish.html.\n"); |
| 163 return false; | 179 return false; |
| 164 } | 180 } |
| 165 | 181 |
| 166 if (commandOptions['dry-run']){ | 182 if (force) return true; |
| 183 |
| 184 if (dryRun) { |
| 167 var s = warnings.length == 1 ? '' : 's'; | 185 var s = warnings.length == 1 ? '' : 's'; |
| 168 log.warning("Package has ${warnings.length} warning$s."); | 186 log.warning("Package has ${warnings.length} warning$s."); |
| 169 return false; | 187 return false; |
| 170 } | 188 } |
| 171 | 189 |
| 172 var message = 'Looks great! Are you ready to upload your package'; | 190 var message = 'Looks great! Are you ready to upload your package'; |
| 173 | 191 |
| 174 if (!warnings.isEmpty) { | 192 if (!warnings.isEmpty) { |
| 175 var s = warnings.length == 1 ? '' : 's'; | 193 var s = warnings.length == 1 ? '' : 's'; |
| 176 message = "Package has ${warnings.length} warning$s. Upload anyway"; | 194 message = "Package has ${warnings.length} warning$s. Upload anyway"; |
| 177 } | 195 } |
| 178 | 196 |
| 179 return confirm(message).then((confirmed) { | 197 return confirm(message).then((confirmed) { |
| 180 if (!confirmed) { | 198 if (!confirmed) { |
| 181 log.error("Package upload canceled."); | 199 log.error("Package upload canceled."); |
| 182 return false; | 200 return false; |
| 183 } | 201 } |
| 184 return true; | 202 return true; |
| 185 }); | 203 }); |
| 186 }); | 204 }); |
| 187 } | 205 } |
| 188 } | 206 } |
| OLD | NEW |