| 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 /** | 5 /// The main entrypoint for the pub command line application. |
| 6 * The main entrypoint for the pub command line application. | |
| 7 */ | |
| 8 library pub; | 6 library pub; |
| 9 | 7 |
| 10 import '../../pkg/args/lib/args.dart'; | 8 import '../../pkg/args/lib/args.dart'; |
| 11 import '../../pkg/path/lib/path.dart' as path; | 9 import '../../pkg/path/lib/path.dart' as path; |
| 12 import 'dart:io'; | 10 import 'dart:io'; |
| 13 import 'dart:math'; | 11 import 'dart:math'; |
| 14 import 'http.dart'; | 12 import 'http.dart'; |
| 15 import 'io.dart'; | 13 import 'io.dart'; |
| 16 import 'command_help.dart'; | 14 import 'command_help.dart'; |
| 17 import 'command_install.dart'; | 15 import 'command_install.dart'; |
| 18 import 'command_lish.dart'; | 16 import 'command_lish.dart'; |
| 19 import 'command_update.dart'; | 17 import 'command_update.dart'; |
| 20 import 'command_uploader.dart'; | 18 import 'command_uploader.dart'; |
| 21 import 'command_version.dart'; | 19 import 'command_version.dart'; |
| 22 import 'entrypoint.dart'; | 20 import 'entrypoint.dart'; |
| 23 import 'exit_codes.dart' as exit_codes; | 21 import 'exit_codes.dart' as exit_codes; |
| 24 import 'log.dart' as log; | 22 import 'log.dart' as log; |
| 25 import 'package.dart'; | 23 import 'package.dart'; |
| 26 import 'pubspec.dart'; | 24 import 'pubspec.dart'; |
| 27 import 'source.dart'; | 25 import 'source.dart'; |
| 28 import 'source_registry.dart'; | 26 import 'source_registry.dart'; |
| 29 import 'system_cache.dart'; | 27 import 'system_cache.dart'; |
| 30 import 'utils.dart'; | 28 import 'utils.dart'; |
| 31 import 'version.dart'; | 29 import 'version.dart'; |
| 32 | 30 |
| 33 Version get pubVersion => new Version(0, 0, 0); | 31 Version get pubVersion => new Version(0, 0, 0); |
| 34 | 32 |
| 35 /** | 33 /// The commands that Pub understands. |
| 36 * The commands that Pub understands. | |
| 37 */ | |
| 38 Map<String, PubCommand> get pubCommands { | 34 Map<String, PubCommand> get pubCommands { |
| 39 var commands = { | 35 var commands = { |
| 40 'help': new HelpCommand(), | 36 'help': new HelpCommand(), |
| 41 'install': new InstallCommand(), | 37 'install': new InstallCommand(), |
| 42 'publish': new LishCommand(), | 38 'publish': new LishCommand(), |
| 43 'update': new UpdateCommand(), | 39 'update': new UpdateCommand(), |
| 44 'uploader': new UploaderCommand(), | 40 'uploader': new UploaderCommand(), |
| 45 'version': new VersionCommand() | 41 'version': new VersionCommand() |
| 46 }; | 42 }; |
| 47 for (var command in commands.values) { | 43 for (var command in commands.values) { |
| 48 for (var alias in command.aliases) { | 44 for (var alias in command.aliases) { |
| 49 commands[alias] = command; | 45 commands[alias] = command; |
| 50 } | 46 } |
| 51 } | 47 } |
| 52 return commands; | 48 return commands; |
| 53 } | 49 } |
| 54 | 50 |
| 55 /** | 51 /// The parser for arguments that are global to Pub rather than specific to a |
| 56 * The parser for arguments that are global to Pub rather than specific to a | 52 /// single command. |
| 57 * single command. | |
| 58 */ | |
| 59 ArgParser get pubArgParser { | 53 ArgParser get pubArgParser { |
| 60 var parser = new ArgParser(); | 54 var parser = new ArgParser(); |
| 61 parser.addFlag('help', abbr: 'h', negatable: false, | 55 parser.addFlag('help', abbr: 'h', negatable: false, |
| 62 help: 'Print this usage information.'); | 56 help: 'Print this usage information.'); |
| 63 parser.addFlag('version', negatable: false, | 57 parser.addFlag('version', negatable: false, |
| 64 help: 'Print pub version.'); | 58 help: 'Print pub version.'); |
| 65 parser.addFlag('trace', | 59 parser.addFlag('trace', |
| 66 help: 'Print debugging information when an error occurs.'); | 60 help: 'Print debugging information when an error occurs.'); |
| 67 parser.addOption('verbosity', | 61 parser.addOption('verbosity', |
| 68 help: 'Control output verbosity.', | 62 help: 'Control output verbosity.', |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 log.error('Run "pub help" to see available commands.'); | 131 log.error('Run "pub help" to see available commands.'); |
| 138 exit(exit_codes.USAGE); | 132 exit(exit_codes.USAGE); |
| 139 return; | 133 return; |
| 140 } | 134 } |
| 141 | 135 |
| 142 var commandArgs = | 136 var commandArgs = |
| 143 globalOptions.rest.getRange(1, globalOptions.rest.length - 1); | 137 globalOptions.rest.getRange(1, globalOptions.rest.length - 1); |
| 144 command.run(cache, globalOptions, commandArgs); | 138 command.run(cache, globalOptions, commandArgs); |
| 145 } | 139 } |
| 146 | 140 |
| 147 /** Displays usage information for the app. */ | 141 /// Displays usage information for the app. |
| 148 void printUsage([String description = 'Pub is a package manager for Dart.']) { | 142 void printUsage([String description = 'Pub is a package manager for Dart.']) { |
| 149 // Build up a buffer so it shows up as a single log entry. | 143 // Build up a buffer so it shows up as a single log entry. |
| 150 var buffer = new StringBuffer(); | 144 var buffer = new StringBuffer(); |
| 151 buffer.add(description); | 145 buffer.add(description); |
| 152 buffer.add('\n\n'); | 146 buffer.add('\n\n'); |
| 153 buffer.add('Usage: pub command [arguments]\n\n'); | 147 buffer.add('Usage: pub command [arguments]\n\n'); |
| 154 buffer.add('Global options:\n'); | 148 buffer.add('Global options:\n'); |
| 155 buffer.add('${pubArgParser.getUsage()}\n\n'); | 149 buffer.add('${pubArgParser.getUsage()}\n\n'); |
| 156 | 150 |
| 157 // Show the commands sorted. | 151 // Show the commands sorted. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 183 log.message('Pub $pubVersion'); | 177 log.message('Pub $pubVersion'); |
| 184 } | 178 } |
| 185 | 179 |
| 186 abstract class PubCommand { | 180 abstract class PubCommand { |
| 187 SystemCache cache; | 181 SystemCache cache; |
| 188 ArgResults globalOptions; | 182 ArgResults globalOptions; |
| 189 ArgResults commandOptions; | 183 ArgResults commandOptions; |
| 190 | 184 |
| 191 Entrypoint entrypoint; | 185 Entrypoint entrypoint; |
| 192 | 186 |
| 193 /** | 187 /// A one-line description of this command. |
| 194 * A one-line description of this command. | |
| 195 */ | |
| 196 String get description; | 188 String get description; |
| 197 | 189 |
| 198 /** | 190 /// How to invoke this command (e.g. `"pub install [package]"`). |
| 199 * How to invoke this command (e.g. `"pub install [package]"`). | |
| 200 */ | |
| 201 String get usage; | 191 String get usage; |
| 202 | 192 |
| 203 /// Whether or not this command requires [entrypoint] to be defined. If false, | 193 /// Whether or not this command requires [entrypoint] to be defined. If false, |
| 204 /// Pub won't look for a pubspec and [entrypoint] will be null when the | 194 /// Pub won't look for a pubspec and [entrypoint] will be null when the |
| 205 /// command runs. | 195 /// command runs. |
| 206 final requiresEntrypoint = true; | 196 final requiresEntrypoint = true; |
| 207 | 197 |
| 208 /// Alternate names for this command. These names won't be used in the | 198 /// Alternate names for this command. These names won't be used in the |
| 209 /// documentation, but they will work when invoked on the command line. | 199 /// documentation, but they will work when invoked on the command line. |
| 210 final aliases = const <String>[]; | 200 final aliases = const <String>[]; |
| 211 | 201 |
| 212 /** | 202 /// Override this to define command-specific options. The results will be made |
| 213 * Override this to define command-specific options. The results will be made | 203 /// available in [commandOptions]. |
| 214 * available in [commandOptions]. | |
| 215 */ | |
| 216 ArgParser get commandParser => new ArgParser(); | 204 ArgParser get commandParser => new ArgParser(); |
| 217 | 205 |
| 218 void run(SystemCache cache_, ArgResults globalOptions_, | 206 void run(SystemCache cache_, ArgResults globalOptions_, |
| 219 List<String> commandArgs) { | 207 List<String> commandArgs) { |
| 220 cache = cache_; | 208 cache = cache_; |
| 221 globalOptions = globalOptions_; | 209 globalOptions = globalOptions_; |
| 222 | 210 |
| 223 try { | 211 try { |
| 224 commandOptions = commandParser.parse(commandArgs); | 212 commandOptions = commandParser.parse(commandArgs); |
| 225 } on FormatException catch (e) { | 213 } on FormatException catch (e) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 } | 269 } |
| 282 | 270 |
| 283 handleError(e, future.stackTrace); | 271 handleError(e, future.stackTrace); |
| 284 }); | 272 }); |
| 285 | 273 |
| 286 // Explicitly exit on success to ensure that any dangling dart:io handles | 274 // Explicitly exit on success to ensure that any dangling dart:io handles |
| 287 // don't cause the process to never terminate. | 275 // don't cause the process to never terminate. |
| 288 future.then((_) => exit(0)); | 276 future.then((_) => exit(0)); |
| 289 } | 277 } |
| 290 | 278 |
| 291 /** | 279 /// Override this to perform the specific command. Return a future that |
| 292 * Override this to perform the specific command. Return a future that | 280 /// completes when the command is done or fails if the command fails. If the |
| 293 * completes when the command is done or fails if the command fails. If the | 281 /// command is synchronous, it may return `null`. |
| 294 * command is synchronous, it may return `null`. | |
| 295 */ | |
| 296 Future onRun(); | 282 Future onRun(); |
| 297 | 283 |
| 298 /** Displays usage information for this command. */ | 284 /// Displays usage information for this command. |
| 299 void printUsage([String description]) { | 285 void printUsage([String description]) { |
| 300 if (description == null) description = this.description; | 286 if (description == null) description = this.description; |
| 301 | 287 |
| 302 var buffer = new StringBuffer(); | 288 var buffer = new StringBuffer(); |
| 303 buffer.add('$description\n\nUsage: $usage'); | 289 buffer.add('$description\n\nUsage: $usage'); |
| 304 | 290 |
| 305 var commandUsage = commandParser.getUsage(); | 291 var commandUsage = commandParser.getUsage(); |
| 306 if (!commandUsage.isEmpty) { | 292 if (!commandUsage.isEmpty) { |
| 307 buffer.add('\n'); | 293 buffer.add('\n'); |
| 308 buffer.add(commandUsage); | 294 buffer.add(commandUsage); |
| 309 } | 295 } |
| 310 | 296 |
| 311 log.message(buffer.toString()); | 297 log.message(buffer.toString()); |
| 312 } | 298 } |
| 313 | 299 |
| 314 /// Returns the appropriate exit code for [exception], falling back on 1 if no | 300 /// Returns the appropriate exit code for [exception], falling back on 1 if no |
| 315 /// appropriate exit code could be found. | 301 /// appropriate exit code could be found. |
| 316 int _chooseExitCode(exception) { | 302 int _chooseExitCode(exception) { |
| 317 if (exception is HttpException || exception is HttpParserException || | 303 if (exception is HttpException || exception is HttpParserException || |
| 318 exception is SocketIOException || exception is PubHttpException) { | 304 exception is SocketIOException || exception is PubHttpException) { |
| 319 return exit_codes.UNAVAILABLE; | 305 return exit_codes.UNAVAILABLE; |
| 320 } else if (exception is FormatException) { | 306 } else if (exception is FormatException) { |
| 321 return exit_codes.DATA; | 307 return exit_codes.DATA; |
| 322 } else { | 308 } else { |
| 323 return 1; | 309 return 1; |
| 324 } | 310 } |
| 325 } | 311 } |
| 326 } | 312 } |
| OLD | NEW |