Chromium Code Reviews| 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 /** | 
| 6 * The main entrypoint for the pub command line application. | 6 * The main entrypoint for the pub command line application. | 
| 7 */ | 7 */ | 
| 8 #library('pub'); | 8 #library('pub'); | 
| 9 | 9 | 
| 10 #import('../../pkg/args/lib/args.dart'); | 10 #import('../../pkg/args/lib/args.dart'); | 
| 11 #import('dart:io'); | 11 #import('dart:io'); | 
| 12 #import('dart:math'); | 12 #import('dart:math'); | 
| 13 #import('io.dart'); | 13 #import('io.dart'); | 
| 14 #import('command_help.dart'); | 14 #import('command_help.dart'); | 
| 15 #import('command_install.dart'); | 15 #import('command_install.dart'); | 
| 16 #import('command_list.dart'); | 16 #import('command_list.dart'); | 
| 17 #import('command_update.dart'); | 17 #import('command_update.dart'); | 
| 18 #import('command_version.dart'); | 18 #import('command_version.dart'); | 
| 19 #import('entrypoint.dart'); | 19 #import('entrypoint.dart'); | 
| 20 #import('exit_codes.dart', prefix: 'exit_codes'); | |
| 20 #import('git_source.dart'); | 21 #import('git_source.dart'); | 
| 21 #import('hosted_source.dart'); | 22 #import('hosted_source.dart'); | 
| 22 #import('package.dart'); | 23 #import('package.dart'); | 
| 23 #import('pubspec.dart'); | 24 #import('pubspec.dart'); | 
| 24 #import('sdk_source.dart'); | 25 #import('sdk_source.dart'); | 
| 25 #import('source.dart'); | 26 #import('source.dart'); | 
| 26 #import('source_registry.dart'); | 27 #import('source_registry.dart'); | 
| 27 #import('system_cache.dart'); | 28 #import('system_cache.dart'); | 
| 28 #import('utils.dart'); | 29 #import('utils.dart'); | 
| 29 #import('version.dart'); | 30 #import('version.dart'); | 
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 cache.register(new SdkSource(sdkDir)); | 90 cache.register(new SdkSource(sdkDir)); | 
| 90 cache.register(new GitSource()); | 91 cache.register(new GitSource()); | 
| 91 cache.register(new HostedSource()); | 92 cache.register(new HostedSource()); | 
| 92 cache.sources.setDefault('hosted'); | 93 cache.sources.setDefault('hosted'); | 
| 93 | 94 | 
| 94 // Select the command. | 95 // Select the command. | 
| 95 var command = pubCommands[globalOptions.rest[0]]; | 96 var command = pubCommands[globalOptions.rest[0]]; | 
| 96 if (command == null) { | 97 if (command == null) { | 
| 97 printError('Unknown command "${globalOptions.rest[0]}".'); | 98 printError('Unknown command "${globalOptions.rest[0]}".'); | 
| 98 printError('Run "pub help" to see available commands.'); | 99 printError('Run "pub help" to see available commands.'); | 
| 99 exit(64); // See http://www.freebsd.org/cgi/man.cgi?query=sysexits. | 100 exit(exit_codes.USAGE); | 
| 100 return; | 101 return; | 
| 101 } | 102 } | 
| 102 | 103 | 
| 103 var commandArgs = | 104 var commandArgs = | 
| 104 globalOptions.rest.getRange(1, globalOptions.rest.length - 1); | 105 globalOptions.rest.getRange(1, globalOptions.rest.length - 1); | 
| 105 command.run(cache, globalOptions, commandArgs); | 106 command.run(cache, globalOptions, commandArgs); | 
| 106 } | 107 } | 
| 107 | 108 | 
| 108 /** Displays usage information for the app. */ | 109 /** Displays usage information for the app. */ | 
| 109 void printUsage([String description = 'Pub is a package manager for Dart.']) { | 110 void printUsage([String description = 'Pub is a package manager for Dart.']) { | 
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 | 170 | 
| 170 void run(SystemCache cache_, ArgResults globalOptions_, | 171 void run(SystemCache cache_, ArgResults globalOptions_, | 
| 171 List<String> commandArgs) { | 172 List<String> commandArgs) { | 
| 172 cache = cache_; | 173 cache = cache_; | 
| 173 globalOptions = globalOptions_; | 174 globalOptions = globalOptions_; | 
| 174 | 175 | 
| 175 try { | 176 try { | 
| 176 commandOptions = commandParser.parse(commandArgs); | 177 commandOptions = commandParser.parse(commandArgs); | 
| 177 } on FormatException catch (e) { | 178 } on FormatException catch (e) { | 
| 178 this.printUsage(description: e.message); | 179 this.printUsage(description: e.message); | 
| 179 return; | 180 exit(exit_codes.USAGE); | 
| 180 } | 181 } | 
| 181 | 182 | 
| 182 handleError(error, trace) { | 183 handleError(error, trace) { | 
| 183 // This is basically the top-level exception handler so that we don't | 184 // This is basically the top-level exception handler so that we don't | 
| 184 // spew a stack trace on our users. | 185 // spew a stack trace on our users. | 
| 185 var message = error.toString(); | 186 var message = error.toString(); | 
| 186 | 187 | 
| 187 // TODO(rnystrom): The default exception implementation class puts | 188 // TODO(rnystrom): The default exception implementation class puts | 
| 188 // "Exception:" in the output, so strip that off. | 189 // "Exception:" in the output, so strip that off. | 
| 189 if (message.startsWith("Exception: ")) { | 190 if (message.startsWith("Exception: ")) { | 
| 190 message = message.substring("Exception: ".length); | 191 message = message.substring("Exception: ".length); | 
| 191 } | 192 } | 
| 192 | 193 | 
| 193 printError(message); | 194 printError(message); | 
| 194 if (globalOptions['trace'] && trace != null) { | 195 if (globalOptions['trace'] && trace != null) { | 
| 195 printError(trace); | 196 printError(trace); | 
| 196 } | 197 } | 
| 197 | 198 | 
| 198 // TODO(nweiz): Use the more semantic error codes in | 199 exit(_exitCode(error)); | 
| 199 // http://www.freebsd.org/cgi/man.cgi?query=sysexits | |
| 200 exit(1); | |
| 201 } | 200 } | 
| 202 | 201 | 
| 203 var future = new Future.immediate(null); | 202 var future = new Future.immediate(null); | 
| 204 if (requiresEntrypoint) { | 203 if (requiresEntrypoint) { | 
| 205 // TODO(rnystrom): Will eventually need better logic to walk up | 204 // TODO(rnystrom): Will eventually need better logic to walk up | 
| 206 // subdirectories until we hit one that looks package-like. For now, just | 205 // subdirectories until we hit one that looks package-like. For now, just | 
| 207 // assume the cwd is it. | 206 // assume the cwd is it. | 
| 208 future = Package.load(null, workingDir, cache.sources) | 207 future = Package.load(null, workingDir, cache.sources) | 
| 209 .transform((package) => new Entrypoint(package, cache)); | 208 .transform((package) => new Entrypoint(package, cache)); | 
| 210 } | 209 } | 
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 print(description); | 250 print(description); | 
| 252 print(''); | 251 print(''); | 
| 253 print('Usage: $usage'); | 252 print('Usage: $usage'); | 
| 254 | 253 | 
| 255 var commandUsage = commandParser.getUsage(); | 254 var commandUsage = commandParser.getUsage(); | 
| 256 if (!commandUsage.isEmpty()) { | 255 if (!commandUsage.isEmpty()) { | 
| 257 print(''); | 256 print(''); | 
| 258 print(commandUsage); | 257 print(commandUsage); | 
| 259 } | 258 } | 
| 260 } | 259 } | 
| 260 | |
| 261 /// Returns the appropriate exit code for [exception], falling back on 1 if no | |
| 262 /// appropriate exit code could be found. | |
| 263 int _exitCode(exception) { | |
| 
 
Bob Nystrom
2012/09/19 18:27:42
How about a nice verby name like _chooseExitCode?
 
nweiz
2012/09/19 18:33:56
Done.
 
 | |
| 264 if (exception is HttpException || exception is HttpParserException || | |
| 265 exception is SocketIOException || exception is PubHttpException) { | |
| 266 return exit_codes.UNAVAILABLE; | |
| 267 } else if (exception is FormatException) { | |
| 268 return exit_codes.DATA; | |
| 269 } else { | |
| 270 return 1; | |
| 271 } | |
| 272 } | |
| 261 } | 273 } | 
| OLD | NEW |