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