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'; |
(...skipping 14 matching lines...) Expand all Loading... |
25 /// Handles the `lish` and `publish` pub commands. | 25 /// Handles the `lish` and `publish` pub commands. |
26 class LishCommand extends PubCommand { | 26 class LishCommand extends PubCommand { |
27 final description = "Publish the current package to pub.dartlang.org."; | 27 final description = "Publish the current package to pub.dartlang.org."; |
28 final usage = "pub publish [options]"; | 28 final usage = "pub publish [options]"; |
29 final aliases = const ["lish", "lush"]; | 29 final aliases = const ["lish", "lush"]; |
30 | 30 |
31 ArgParser get commandParser { | 31 ArgParser get commandParser { |
32 var parser = new ArgParser(); | 32 var parser = new ArgParser(); |
33 // TODO(nweiz): Use HostedSource.defaultUrl as the default value once we use | 33 // TODO(nweiz): Use HostedSource.defaultUrl as the default value once we use |
34 // dart:io for HTTPS requests. | 34 // dart:io for HTTPS requests. |
| 35 parser.addFlag('dry-run', abbr: 'n', negatable: false, |
| 36 help: 'Validate but do not publish the package'); |
35 parser.addOption('server', defaultsTo: 'https://pub.dartlang.org', | 37 parser.addOption('server', defaultsTo: 'https://pub.dartlang.org', |
36 help: 'The package server to which to upload this package'); | 38 help: 'The package server to which to upload this package'); |
37 return parser; | 39 return parser; |
38 } | 40 } |
39 | 41 |
40 /// The URL of the server to which to upload the package. | 42 /// The URL of the server to which to upload the package. |
41 Uri get server => Uri.parse(commandOptions['server']); | 43 Uri get server => Uri.parse(commandOptions['server']); |
42 | 44 |
43 Future _publish(packageBytes) { | 45 Future _publish(packageBytes) { |
44 var cloudStorageUrl; | 46 var cloudStorageUrl; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 var package = entrypoint.root; | 95 var package = entrypoint.root; |
94 log.message( | 96 log.message( |
95 'Publishing "${package.name}" ${package.version}:\n' | 97 'Publishing "${package.name}" ${package.version}:\n' |
96 '${generateTree(files)}'); | 98 '${generateTree(files)}'); |
97 | 99 |
98 return createTarGz(files, baseDir: entrypoint.root.dir); | 100 return createTarGz(files, baseDir: entrypoint.root.dir); |
99 }).then((stream) => stream.toBytes()); | 101 }).then((stream) => stream.toBytes()); |
100 | 102 |
101 // Validate the package. | 103 // Validate the package. |
102 return _validate(packageBytesFuture.then((bytes) => bytes.length)) | 104 return _validate(packageBytesFuture.then((bytes) => bytes.length)) |
103 .then((_) => packageBytesFuture).then(_publish); | 105 .then((isValid) { |
| 106 if (isValid) return packageBytesFuture.then(_publish); |
| 107 }); |
104 } | 108 } |
105 | 109 |
106 /// The basenames of files that are automatically excluded from archives. | 110 /// The basenames of files that are automatically excluded from archives. |
107 final _BLACKLISTED_FILES = const ['pubspec.lock']; | 111 final _BLACKLISTED_FILES = const ['pubspec.lock']; |
108 | 112 |
109 /// The basenames of directories that are automatically excluded from | 113 /// The basenames of directories that are automatically excluded from |
110 /// archives. | 114 /// archives. |
111 final _BLACKLISTED_DIRS = const ['packages']; | 115 final _BLACKLISTED_DIRS = const ['packages']; |
112 | 116 |
113 /// Returns a list of files that should be included in the published package. | 117 /// Returns a list of files that should be included in the published package. |
114 /// If this is a Git repository, this will respect .gitignore; otherwise, it | 118 /// If this is a Git repository, this will respect .gitignore; otherwise, it |
115 /// will return all non-hidden files. | 119 /// will return all non-hidden files. |
(...skipping 23 matching lines...) Expand all Loading... |
139 } | 143 } |
140 | 144 |
141 /// Returns the value associated with [key] in [map]. Throws a user-friendly | 145 /// Returns the value associated with [key] in [map]. Throws a user-friendly |
142 /// error if [map] doens't contain [key]. | 146 /// error if [map] doens't contain [key]. |
143 _expectField(Map map, String key, http.Response response) { | 147 _expectField(Map map, String key, http.Response response) { |
144 if (map.containsKey(key)) return map[key]; | 148 if (map.containsKey(key)) return map[key]; |
145 invalidServerResponse(response); | 149 invalidServerResponse(response); |
146 } | 150 } |
147 | 151 |
148 /// Validates the package. Throws an exception if it's invalid. | 152 /// Validates the package. Throws an exception if it's invalid. |
149 Future _validate(Future<int> packageSize) { | 153 Future<bool> _validate(Future<int> packageSize) { |
150 return Validator.runAll(entrypoint, packageSize).then((pair) { | 154 return Validator.runAll(entrypoint, packageSize).then((pair) { |
151 var errors = pair.first; | 155 var errors = pair.first; |
152 var warnings = pair.last; | 156 var warnings = pair.last; |
153 | 157 |
154 if (!errors.isEmpty) { | 158 if (!errors.isEmpty) { |
155 throw "Sorry, your package is missing " | 159 log.error("Sorry, your package is missing " |
156 "${(errors.length > 1) ? 'some requirements' : 'a requirement'} " | 160 "${(errors.length > 1) ? 'some requirements' : 'a requirement'} " |
157 "and can't be published yet.\nFor more information, see: " | 161 "and can't be published yet.\nFor more information, see: " |
158 "http://pub.dartlang.org/doc/pub-lish.html.\n"; | 162 "http://pub.dartlang.org/doc/pub-lish.html.\n"); |
| 163 return false; |
159 } | 164 } |
160 | 165 |
| 166 if (commandOptions['dry-run']){ |
| 167 var s = warnings.length == 1 ? '' : 's'; |
| 168 log.warning("Package has ${warnings.length} warning$s."); |
| 169 return false; |
| 170 } |
| 171 |
161 var message = 'Looks great! Are you ready to upload your package'; | 172 var message = 'Looks great! Are you ready to upload your package'; |
162 | 173 |
163 if (!warnings.isEmpty) { | 174 if (!warnings.isEmpty) { |
164 var s = warnings.length == 1 ? '' : 's'; | 175 var s = warnings.length == 1 ? '' : 's'; |
165 message = "Package has ${warnings.length} warning$s. Upload anyway"; | 176 message = "Package has ${warnings.length} warning$s. Upload anyway"; |
166 } | 177 } |
167 | 178 |
168 return confirm(message).then((confirmed) { | 179 return confirm(message).then((confirmed) { |
169 if (!confirmed) throw "Package upload canceled."; | 180 if (!confirmed) { |
| 181 log.error("Package upload canceled."); |
| 182 return false; |
| 183 } |
| 184 return true; |
170 }); | 185 }); |
171 }); | 186 }); |
172 } | 187 } |
173 } | 188 } |
OLD | NEW |