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('../../lib/args/args.dart'); | 10 #import('../../lib/args/args.dart'); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 var parser = new ArgParser(); | 41 var parser = new ArgParser(); |
| 42 parser.addFlag('help', abbr: 'h', negatable: false, | 42 parser.addFlag('help', abbr: 'h', negatable: false, |
| 43 help: 'Prints this usage information'); | 43 help: 'Prints this usage information'); |
| 44 parser.addFlag('version', negatable: false, | 44 parser.addFlag('version', negatable: false, |
| 45 help: 'Prints the version of Pub'); | 45 help: 'Prints the version of Pub'); |
| 46 // TODO(rnystrom): Hack. These are temporary options to allow the pub tests to | 46 // TODO(rnystrom): Hack. These are temporary options to allow the pub tests to |
| 47 // pass in relevant paths. Eventually these should be environment variables. | 47 // pass in relevant paths. Eventually these should be environment variables. |
| 48 parser.addOption('cachedir', help: 'The directory containing the system-wide ' | 48 parser.addOption('cachedir', help: 'The directory containing the system-wide ' |
| 49 'Pub cache'); | 49 'Pub cache'); |
| 50 parser.addOption('sdkdir', help: 'The directory containing the Dart SDK'); | 50 parser.addOption('sdkdir', help: 'The directory containing the Dart SDK'); |
| 51 parser.addFlag('trace', help: 'Prints a stack trace when an error occurs'); | |
| 51 | 52 |
| 52 var results; | 53 var results; |
| 53 try { | 54 try { |
| 54 results = parser.parse(new Options().arguments); | 55 results = parser.parse(new Options().arguments); |
| 55 } catch (ArgFormatException e) { | 56 } catch (ArgFormatException e) { |
| 56 printUsage(parser, commands, description: e.message); | 57 printUsage(parser, commands, description: e.message); |
| 57 return; | 58 return; |
| 58 } | 59 } |
| 59 | 60 |
| 60 if (results['version']) { | 61 if (results['version']) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 76 | 77 |
| 77 // Select the command. | 78 // Select the command. |
| 78 var command = commands[results.rest[0]]; | 79 var command = commands[results.rest[0]]; |
| 79 if (command == null) { | 80 if (command == null) { |
| 80 printError('Unknown command "${results.rest[0]}".'); | 81 printError('Unknown command "${results.rest[0]}".'); |
| 81 printError('Run "pub help" to see available commands.'); | 82 printError('Run "pub help" to see available commands.'); |
| 82 exit(64); // See http://www.freebsd.org/cgi/man.cgi?query=sysexits. | 83 exit(64); // See http://www.freebsd.org/cgi/man.cgi?query=sysexits. |
| 83 return; | 84 return; |
| 84 } | 85 } |
| 85 | 86 |
| 86 command.run(cache, results.rest.getRange(1, results.rest.length - 1)); | 87 var args = results.rest.getRange(1, results.rest.length - 1); |
| 88 command.run(cache, results, args); | |
|
Bob Nystrom
2012/07/09 23:00:36
These names aren't very clear. How about:
results
nweiz
2012/07/09 23:38:09
Done.
| |
| 87 } | 89 } |
| 88 | 90 |
| 89 /** Displays usage information for the app. */ | 91 /** Displays usage information for the app. */ |
| 90 void printUsage(ArgParser parser, Map<String, PubCommand> commands, | 92 void printUsage(ArgParser parser, Map<String, PubCommand> commands, |
| 91 [String description = 'Pub is a package manager for Dart.']) { | 93 [String description = 'Pub is a package manager for Dart.']) { |
| 92 print(description); | 94 print(description); |
| 93 print(''); | 95 print(''); |
| 94 print('Usage: pub command [arguments]'); | 96 print('Usage: pub command [arguments]'); |
| 95 print(''); | 97 print(''); |
| 96 print('Global options:'); | 98 print('Global options:'); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 121 print('Pub $pubVersion'); | 123 print('Pub $pubVersion'); |
| 122 } | 124 } |
| 123 | 125 |
| 124 class PubCommand { | 126 class PubCommand { |
| 125 SystemCache cache; | 127 SystemCache cache; |
| 126 | 128 |
| 127 Entrypoint entrypoint; | 129 Entrypoint entrypoint; |
| 128 | 130 |
| 129 abstract String get description(); | 131 abstract String get description(); |
| 130 | 132 |
| 131 void run(SystemCache cache_, List<String> args) { | 133 void run(SystemCache cache_, ArgResults options, List<String> args) { |
| 132 cache = cache_; | 134 cache = cache_; |
| 133 | 135 |
| 134 // TODO(rnystrom): Each command should define the arguments it expects and | 136 // TODO(rnystrom): Each command should define the arguments it expects and |
| 135 // we can handle them generically here. | 137 // we can handle them generically here. |
| 136 | 138 |
| 137 handleError(error) { | 139 handleError(error, trace) { |
| 138 // This is basically the top-level exception handler so that we don't | 140 // This is basically the top-level exception handler so that we don't |
| 139 // spew a stack trace on our users. | 141 // spew a stack trace on our users. |
| 140 // TODO(rnystrom): Add --trace flag so stack traces can be enabled for | 142 // TODO(rnystrom): Add --trace flag so stack traces can be enabled for |
| 141 // debugging. | 143 // debugging. |
| 142 var message = error.toString(); | 144 var message = error.toString(); |
| 143 | 145 |
| 144 // TODO(rnystrom): The default exception implementation class puts | 146 // TODO(rnystrom): The default exception implementation class puts |
| 145 // "Exception:" in the output, so strip that off. | 147 // "Exception:" in the output, so strip that off. |
| 146 if (message.startsWith("Exception: ")) { | 148 if (message.startsWith("Exception: ")) { |
| 147 message = message.substring("Exception: ".length); | 149 message = message.substring("Exception: ".length); |
| 148 } | 150 } |
| 149 | 151 |
| 150 printError(message); | 152 printError(message); |
| 153 if (options['trace'] && trace != null) { | |
| 154 printError(trace); | |
| 155 } | |
| 151 return true; | 156 return true; |
| 152 } | 157 } |
| 153 | 158 |
| 154 // TODO(rnystrom): Will eventually need better logic to walk up | 159 // TODO(rnystrom): Will eventually need better logic to walk up |
| 155 // subdirectories until we hit one that looks package-like. For now, just | 160 // subdirectories until we hit one that looks package-like. For now, just |
| 156 // assume the cwd is it. | 161 // assume the cwd is it. |
| 157 Package.load(workingDir, cache.sources).chain((package) { | 162 var future = Package.load(workingDir, cache.sources).chain((package) { |
| 158 entrypoint = new Entrypoint(package, cache); | 163 entrypoint = new Entrypoint(package, cache); |
| 159 | 164 |
| 160 try { | 165 try { |
| 161 var commandFuture = onRun(); | 166 var commandFuture = onRun(); |
| 162 if (commandFuture == null) return new Future.immediate(true); | 167 if (commandFuture == null) return new Future.immediate(true); |
| 163 | 168 |
| 164 return commandFuture; | 169 return commandFuture; |
| 165 } catch (var error) { | 170 } catch (var error, var trace) { |
| 166 handleError(error); | 171 handleError(error, trace); |
| 167 return new Future.immediate(null); | 172 return new Future.immediate(null); |
| 168 } | 173 } |
| 169 }).handleException(handleError); | 174 }); |
| 175 future.handleException((e) => handleError(e, future.stackTrace)); | |
| 170 } | 176 } |
| 171 | 177 |
| 172 /** | 178 /** |
| 173 * Override this to perform the specific command. Return a future that | 179 * Override this to perform the specific command. Return a future that |
| 174 * completes when the command is done or fails if the command fails. If the | 180 * completes when the command is done or fails if the command fails. If the |
| 175 * command is synchronous, it may return `null`. | 181 * command is synchronous, it may return `null`. |
| 176 */ | 182 */ |
| 177 abstract Future onRun(); | 183 abstract Future onRun(); |
| 178 } | 184 } |
| OLD | NEW |