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/args.dart'); | 10 #import('../../pkg/args/args.dart'); |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 /** | 149 /** |
| 150 * A one-line description of this command. | 150 * A one-line description of this command. |
| 151 */ | 151 */ |
| 152 abstract String get description; | 152 abstract String get description; |
| 153 | 153 |
| 154 /** | 154 /** |
| 155 * How to invoke this command (e.g. `"pub install [package]"`). | 155 * How to invoke this command (e.g. `"pub install [package]"`). |
| 156 */ | 156 */ |
| 157 abstract String get usage; | 157 abstract String get usage; |
| 158 | 158 |
| 159 /// Whether or not this command uses [entrypoint]. If false, Pub won't look | |
| 160 /// for a pubspec and [entrypoint] will be null when the command runs. | |
| 161 bool get usesEntrypoint => true; | |
|
Bob Nystrom
2012/09/18 01:34:47
How about "requiresEntrypoint"?
nweiz
2012/09/18 19:55:30
Done.
| |
| 162 | |
| 159 /** | 163 /** |
| 160 * Override this to define command-specific options. The results will be made | 164 * Override this to define command-specific options. The results will be made |
| 161 * available in [commandOptions]. | 165 * available in [commandOptions]. |
| 162 */ | 166 */ |
| 163 ArgParser get commandParser => new ArgParser(); | 167 ArgParser get commandParser => new ArgParser(); |
| 164 | 168 |
| 165 void run(SystemCache cache_, ArgResults globalOptions_, | 169 void run(SystemCache cache_, ArgResults globalOptions_, |
| 166 List<String> commandArgs) { | 170 List<String> commandArgs) { |
| 167 cache = cache_; | 171 cache = cache_; |
| 168 globalOptions = globalOptions_; | 172 globalOptions = globalOptions_; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 188 printError(message); | 192 printError(message); |
| 189 if (globalOptions['trace'] && trace != null) { | 193 if (globalOptions['trace'] && trace != null) { |
| 190 printError(trace); | 194 printError(trace); |
| 191 } | 195 } |
| 192 | 196 |
| 193 // TODO(nweiz): Use the more semantic error codes in | 197 // TODO(nweiz): Use the more semantic error codes in |
| 194 // http://www.freebsd.org/cgi/man.cgi?query=sysexits | 198 // http://www.freebsd.org/cgi/man.cgi?query=sysexits |
| 195 exit(1); | 199 exit(1); |
| 196 } | 200 } |
| 197 | 201 |
| 198 // TODO(rnystrom): Will eventually need better logic to walk up | 202 var future = new Future.immediate(null); |
| 199 // subdirectories until we hit one that looks package-like. For now, just | 203 if (usesEntrypoint) { |
| 200 // assume the cwd is it. | 204 // TODO(rnystrom): Will eventually need better logic to walk up |
| 201 var future = Package.load(workingDir, cache.sources).chain((package) { | 205 // subdirectories until we hit one that looks package-like. For now, just |
| 202 entrypoint = new Entrypoint(package, cache); | 206 // assume the cwd is it. |
| 207 future = Package.load(null, workingDir, cache.sources) | |
| 208 .transform((package) => new Entrypoint(package, cache)); | |
|
Bob Nystrom
2012/09/18 01:34:47
Indent +2.
nweiz
2012/09/18 19:55:30
Done.
| |
| 209 } | |
| 203 | 210 |
| 211 future = future.chain((entrypoint) { | |
| 212 this.entrypoint = entrypoint; | |
| 204 try { | 213 try { |
| 205 var commandFuture = onRun(); | 214 var commandFuture = onRun(); |
| 206 if (commandFuture == null) return new Future.immediate(true); | 215 if (commandFuture == null) return new Future.immediate(true); |
| 207 | 216 |
| 208 return commandFuture; | 217 return commandFuture; |
| 209 } catch (error, trace) { | 218 } catch (error, trace) { |
| 210 handleError(error, trace); | 219 handleError(error, trace); |
| 211 return new Future.immediate(null); | 220 return new Future.immediate(null); |
| 212 } | 221 } |
| 213 }); | 222 }); |
| 214 future.handleException((e) => handleError(e, future.stackTrace)); | 223 |
| 224 future.handleException((e) { | |
| 225 if (e is PubspecNotFoundException && e.name == null) { | |
| 226 e = 'Could not find a file named "pubspec.yaml" in the directory ' | |
| 227 '$workingDir.'; | |
| 228 } else if (e is PubspecHasNoNameException && e.name == null) { | |
| 229 e = 'pubspec.yaml is missing the required "name" field (e.g. "name: ' | |
| 230 '${basename(workingDir)}").'; | |
| 231 } | |
| 232 | |
| 233 handleError(e, future.stackTrace); | |
| 234 }); | |
| 215 // Explicitly exit on success to ensure that any dangling dart:io handles | 235 // Explicitly exit on success to ensure that any dangling dart:io handles |
| 216 // don't cause the process to never terminate. | 236 // don't cause the process to never terminate. |
| 217 future.then((_) => exit(0)); | 237 future.then((_) => exit(0)); |
| 218 } | 238 } |
| 219 | 239 |
| 220 /** | 240 /** |
| 221 * Override this to perform the specific command. Return a future that | 241 * Override this to perform the specific command. Return a future that |
| 222 * completes when the command is done or fails if the command fails. If the | 242 * completes when the command is done or fails if the command fails. If the |
| 223 * command is synchronous, it may return `null`. | 243 * command is synchronous, it may return `null`. |
| 224 */ | 244 */ |
| 225 abstract Future onRun(); | 245 abstract Future onRun(); |
| 226 | 246 |
| 227 /** Displays usage information for this command. */ | 247 /** Displays usage information for this command. */ |
| 228 void printUsage([String description]) { | 248 void printUsage([String description]) { |
| 229 if (description == null) description = this.description; | 249 if (description == null) description = this.description; |
| 230 print(description); | 250 print(description); |
| 231 print(''); | 251 print(''); |
| 232 print('Usage: $usage'); | 252 print('Usage: $usage'); |
| 233 | 253 |
| 234 var commandUsage = commandParser.getUsage(); | 254 var commandUsage = commandParser.getUsage(); |
| 235 if (!commandUsage.isEmpty()) { | 255 if (!commandUsage.isEmpty()) { |
| 236 print(''); | 256 print(''); |
| 237 print(commandUsage); | 257 print(commandUsage); |
| 238 } | 258 } |
| 239 } | 259 } |
| 240 } | 260 } |
| OLD | NEW |