Chromium Code Reviews| Index: utils/pub/pub.dart |
| diff --git a/utils/pub/pub.dart b/utils/pub/pub.dart |
| index 6ed5b269d50a13a135131cb64e7cf0f7c04cc8df..86557830c2e3d26fa5b6c33a1acce28812839814 100644 |
| --- a/utils/pub/pub.dart |
| +++ b/utils/pub/pub.dart |
| @@ -13,23 +13,35 @@ |
| #source('cache.dart'); |
| #source('command_list.dart'); |
| #source('command_update.dart'); |
| +#source('command_version.dart'); |
| #source('package.dart'); |
| -Map<String, PubCommand> commands; |
| - |
| main() { |
| final args = new Options().arguments; |
| // TODO(rnystrom): In addition to explicit "help" and "version" commands, |
| // should also add special-case support for --help and --version arguments to |
| // be consistent with other Unix apps. |
| - commands = { |
| - 'list': new PubCommand('print the contents of repositories', commandList), |
| - 'version': new PubCommand('print Pub version', commandVersion) |
| + final commands = { |
| + 'list': new ListCommand(), |
| + 'update': new UpdateCommand(), |
| + 'version': new VersionCommand() |
| }; |
| if (args.length == 0) { |
| - showUsage(); |
| + printUsage(commands); |
| + return; |
| + } |
| + |
| + // For consistency with expected unix idioms, support --help and --version |
| + // in addition to the regular commands. |
| + if (args.length == 1 && args[0] == '--help') { |
|
nweiz
2012/04/25 20:49:41
Also support -h
Bob Nystrom
2012/04/25 22:56:40
Done.
|
| + printUsage(commands); |
| + return; |
| + } |
| + |
| + if (args.length == 1 && args[0] == '--version') { |
| + printVersion(); |
| return; |
| } |
| @@ -45,7 +57,8 @@ main() { |
| } |
| } |
| - var options = new PubOptions(cacheDir); |
| + // TODO(rnystrom): Do we want this to be global? |
| + final cache = new PackageCache(cacheDir); |
| // Select the command. |
| final command = commands[args[0]]; |
| @@ -57,11 +70,11 @@ main() { |
| } |
| args.removeRange(0, 1); |
| - command.function(options, args); |
| + command.run(cache, args); |
| } |
| /** Displays usage information for the app. */ |
| -void showUsage() { |
| +void printUsage(Map<String, PubCommand> commands) { |
| print('Pub is a package manager for Dart.'); |
| print(''); |
| print('Usage:'); |
| @@ -90,23 +103,34 @@ void showUsage() { |
| print('Use "pub help [command]" for more information about a command.'); |
| } |
| -/** Displays pub version information. */ |
| -void commandVersion(PubOptions options, List<String> args) { |
| - // TODO(rnystrom): Store some place central. |
| +void printVersion() { |
| print('Pub 0.0.0'); |
| } |
| -typedef void CommandFunction(PubOptions options, List<String> args); |
| +/** |
| + * Gets the package that contains the current working directory. In other words, |
| + * finds the package that the user is currently "in". |
| + */ |
| +Future<Package> getWorkingPackage() { |
| + // TODO(rnystrom): Will eventually need better logic to walk up |
| + // subdirectories until we hit one that looks package-like. For now, just |
| + // assume the cwd is it. |
| + return Package.load(workingDir); |
| +} |
| class PubCommand { |
| - final String description; |
| - final CommandFunction function; |
| + PackageCache cache; |
| - PubCommand(this.description, this.function); |
| -} |
| + abstract String get description(); |
| -class PubOptions { |
| - final String cacheDir; |
| + void run(PackageCache cache_, List<String> args) { |
| + cache = cache_; |
| + |
| + // TODO(rnystrom): Each command should define the arguments it expects and |
| + // we can handle them generically here. |
| + |
| + onRun(); |
| + } |
| - PubOptions(this.cacheDir); |
| + abstract void onRun(); |
| } |