| 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('io.dart'); | 10 #import('io.dart'); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 } | 72 } |
| 73 | 73 |
| 74 var cache = new SystemCache(cacheDir); | 74 var cache = new SystemCache(cacheDir); |
| 75 cache.sources.register(new SdkSource(sdkDir)); | 75 cache.sources.register(new SdkSource(sdkDir)); |
| 76 cache.sources.register(new GitSource()); | 76 cache.sources.register(new GitSource()); |
| 77 cache.sources.setDefault('sdk'); | 77 cache.sources.setDefault('sdk'); |
| 78 | 78 |
| 79 // Select the command. | 79 // Select the command. |
| 80 var command = commands[args[0]]; | 80 var command = commands[args[0]]; |
| 81 if (command == null) { | 81 if (command == null) { |
| 82 print('Unknown command "${args[0]}".'); | 82 printError('Unknown command "${args[0]}".'); |
| 83 print('Run "pub help" to see available commands.'); | 83 printError('Run "pub help" to see available commands.'); |
| 84 exit(64); // See http://www.freebsd.org/cgi/man.cgi?query=sysexits. | 84 exit(64); // See http://www.freebsd.org/cgi/man.cgi?query=sysexits. |
| 85 return; | 85 return; |
| 86 } | 86 } |
| 87 | 87 |
| 88 args.removeRange(0, 1); | 88 args.removeRange(0, 1); |
| 89 command.run(cache, args); | 89 command.run(cache, args); |
| 90 } | 90 } |
| 91 | 91 |
| 92 /** Displays usage information for the app. */ | 92 /** Displays usage information for the app. */ |
| 93 void printUsage(Map<String, PubCommand> commands) { | 93 void printUsage(Map<String, PubCommand> commands) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 Entrypoint entrypoint; | 129 Entrypoint entrypoint; |
| 130 | 130 |
| 131 abstract String get description(); | 131 abstract String get description(); |
| 132 | 132 |
| 133 void run(SystemCache cache_, List<String> args) { | 133 void run(SystemCache cache_, List<String> args) { |
| 134 cache = cache_; | 134 cache = cache_; |
| 135 | 135 |
| 136 // TODO(rnystrom): Each command should define the arguments it expects and | 136 // TODO(rnystrom): Each command should define the arguments it expects and |
| 137 // we can handle them generically here. | 137 // we can handle them generically here. |
| 138 | 138 |
| 139 handleError(error) { |
| 140 // This is basically the top-level exception handler so that we don't |
| 141 // spew a stack trace on our users. |
| 142 // TODO(rnystrom): Add --trace flag so stack traces can be enabled for |
| 143 // debugging. |
| 144 var message = error.toString(); |
| 145 |
| 146 // TODO(rnystrom): The default exception implementation class puts |
| 147 // "Exception:" in the output, so strip that off. |
| 148 if (message.startsWith("Exception: ")) { |
| 149 message = message.substring("Exception: ".length); |
| 150 } |
| 151 |
| 152 printError(message); |
| 153 return true; |
| 154 } |
| 155 |
| 139 // TODO(rnystrom): Will eventually need better logic to walk up | 156 // TODO(rnystrom): Will eventually need better logic to walk up |
| 140 // subdirectories until we hit one that looks package-like. For now, just | 157 // subdirectories until we hit one that looks package-like. For now, just |
| 141 // assume the cwd is it. | 158 // assume the cwd is it. |
| 142 Package.load(workingDir, cache.sources).then((package) { | 159 Package.load(workingDir, cache.sources).chain((package) { |
| 143 entrypoint = new Entrypoint(package, cache); | 160 entrypoint = new Entrypoint(package, cache); |
| 144 onRun(); | 161 |
| 145 }); | 162 try { |
| 163 var commandFuture = onRun(); |
| 164 if (commandFuture == null) return new Future.immediate(true); |
| 165 |
| 166 return commandFuture; |
| 167 } catch (var error) { |
| 168 handleError(error); |
| 169 } |
| 170 }).handleException(handleError); |
| 146 } | 171 } |
| 147 | 172 |
| 148 abstract void onRun(); | 173 /** |
| 174 * Override this to perform the specific command. Return a future that |
| 175 * completes when the command is done or fails if the command fails. If the |
| 176 * command is synchronous, it may return `null`. |
| 177 */ |
| 178 abstract Future onRun(); |
| 149 } | 179 } |
| OLD | NEW |