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 /// The main entrypoint for the pub command line application. | 5 /// The main entrypoint for the pub command line application. |
6 library pub; | 6 library pub; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:math'; | 10 import 'dart:math'; |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 exit(exit_codes.USAGE); | 153 exit(exit_codes.USAGE); |
154 } | 154 } |
155 }); | 155 }); |
156 }); | 156 }); |
157 } | 157 } |
158 | 158 |
159 /// Displays usage information for the app. | 159 /// Displays usage information for the app. |
160 void printUsage([String description = 'Pub is a package manager for Dart.']) { | 160 void printUsage([String description = 'Pub is a package manager for Dart.']) { |
161 // Build up a buffer so it shows up as a single log entry. | 161 // Build up a buffer so it shows up as a single log entry. |
162 var buffer = new StringBuffer(); | 162 var buffer = new StringBuffer(); |
163 buffer.add(description); | 163 buffer.write(description); |
164 buffer.add('\n\n'); | 164 buffer.write('\n\n'); |
165 buffer.add('Usage: pub command [arguments]\n\n'); | 165 buffer.write('Usage: pub command [arguments]\n\n'); |
166 buffer.add('Global options:\n'); | 166 buffer.write('Global options:\n'); |
167 buffer.add('${pubArgParser.getUsage()}\n\n'); | 167 buffer.write('${pubArgParser.getUsage()}\n\n'); |
168 | 168 |
169 // Show the commands sorted. | 169 // Show the commands sorted. |
170 buffer.add('Available commands:\n'); | 170 buffer.write('Available commands:\n'); |
171 | 171 |
172 // TODO(rnystrom): A sorted map would be nice. | 172 // TODO(rnystrom): A sorted map would be nice. |
173 int length = 0; | 173 int length = 0; |
174 var names = <String>[]; | 174 var names = <String>[]; |
175 for (var command in pubCommands.keys) { | 175 for (var command in pubCommands.keys) { |
176 // Hide aliases. | 176 // Hide aliases. |
177 if (pubCommands[command].aliases.indexOf(command) >= 0) continue; | 177 if (pubCommands[command].aliases.indexOf(command) >= 0) continue; |
178 length = max(length, command.length); | 178 length = max(length, command.length); |
179 names.add(command); | 179 names.add(command); |
180 } | 180 } |
181 | 181 |
182 names.sort((a, b) => a.compareTo(b)); | 182 names.sort((a, b) => a.compareTo(b)); |
183 | 183 |
184 for (var name in names) { | 184 for (var name in names) { |
185 buffer.add(' ${padRight(name, length)} ' | 185 buffer.write(' ${padRight(name, length)} ' |
186 '${pubCommands[name].description}\n'); | 186 '${pubCommands[name].description}\n'); |
187 } | 187 } |
188 | 188 |
189 buffer.add('\n'); | 189 buffer.write('\n'); |
190 buffer.add('Use "pub help [command]" for more information about a command.'); | 190 buffer.write( |
| 191 'Use "pub help [command]" for more information about a command.'); |
191 log.message(buffer.toString()); | 192 log.message(buffer.toString()); |
192 } | 193 } |
193 | 194 |
194 void printVersion() { | 195 void printVersion() { |
195 log.message('Pub ${sdk.version}'); | 196 log.message('Pub ${sdk.version}'); |
196 } | 197 } |
197 | 198 |
198 abstract class PubCommand { | 199 abstract class PubCommand { |
199 SystemCache cache; | 200 SystemCache cache; |
200 ArgResults globalOptions; | 201 ArgResults globalOptions; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 /// Override this to perform the specific command. Return a future that | 295 /// Override this to perform the specific command. Return a future that |
295 /// completes when the command is done or fails if the command fails. If the | 296 /// completes when the command is done or fails if the command fails. If the |
296 /// command is synchronous, it may return `null`. | 297 /// command is synchronous, it may return `null`. |
297 Future onRun(); | 298 Future onRun(); |
298 | 299 |
299 /// Displays usage information for this command. | 300 /// Displays usage information for this command. |
300 void printUsage([String description]) { | 301 void printUsage([String description]) { |
301 if (description == null) description = this.description; | 302 if (description == null) description = this.description; |
302 | 303 |
303 var buffer = new StringBuffer(); | 304 var buffer = new StringBuffer(); |
304 buffer.add('$description\n\nUsage: $usage'); | 305 buffer.write('$description\n\nUsage: $usage'); |
305 | 306 |
306 var commandUsage = commandParser.getUsage(); | 307 var commandUsage = commandParser.getUsage(); |
307 if (!commandUsage.isEmpty) { | 308 if (!commandUsage.isEmpty) { |
308 buffer.add('\n'); | 309 buffer.write('\n'); |
309 buffer.add(commandUsage); | 310 buffer.write(commandUsage); |
310 } | 311 } |
311 | 312 |
312 log.message(buffer.toString()); | 313 log.message(buffer.toString()); |
313 } | 314 } |
314 | 315 |
315 /// Returns the appropriate exit code for [exception], falling back on 1 if no | 316 /// Returns the appropriate exit code for [exception], falling back on 1 if no |
316 /// appropriate exit code could be found. | 317 /// appropriate exit code could be found. |
317 int _chooseExitCode(exception) { | 318 int _chooseExitCode(exception) { |
318 if (exception is HttpException || exception is HttpParserException || | 319 if (exception is HttpException || exception is HttpParserException || |
319 exception is SocketIOException || exception is PubHttpException) { | 320 exception is SocketIOException || exception is PubHttpException) { |
320 return exit_codes.UNAVAILABLE; | 321 return exit_codes.UNAVAILABLE; |
321 } else if (exception is FormatException) { | 322 } else if (exception is FormatException) { |
322 return exit_codes.DATA; | 323 return exit_codes.DATA; |
323 } else { | 324 } else { |
324 return 1; | 325 return 1; |
325 } | 326 } |
326 } | 327 } |
327 } | 328 } |
OLD | NEW |