| 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('../yaml/yaml.dart'); | 10 #import('../yaml/yaml.dart'); |
| 11 | 11 |
| 12 #import('io.dart'); | 12 #import('io.dart'); |
| 13 #import('utils.dart'); | 13 #import('utils.dart'); |
| 14 | 14 |
| 15 #source('cache.dart'); | 15 #source('cache.dart'); |
| 16 #source('command_list.dart'); | 16 #source('command_list.dart'); |
| 17 #source('command_update.dart'); | 17 #source('command_update.dart'); |
| 18 #source('command_version.dart'); |
| 18 #source('package.dart'); | 19 #source('package.dart'); |
| 19 | 20 |
| 20 Map<String, PubCommand> commands; | |
| 21 PackageCache cache; | |
| 22 | |
| 23 main() { | 21 main() { |
| 24 final args = new Options().arguments; | 22 final args = new Options().arguments; |
| 25 | 23 |
| 26 // TODO(rnystrom): In addition to explicit "help" and "version" commands, | 24 // TODO(rnystrom): In addition to explicit "help" and "version" commands, |
| 27 // should also add special-case support for --help and --version arguments to | 25 // should also add special-case support for --help and --version arguments to |
| 28 // be consistent with other Unix apps. | 26 // be consistent with other Unix apps. |
| 29 commands = { | 27 final commands = { |
| 30 'list': new PubCommand('print the contents of repositories', commandList), | 28 'list': new ListCommand(), |
| 31 'update': new PubCommand("update a package's dependencies", commandUpdate), | 29 'update': new UpdateCommand(), |
| 32 'version': new PubCommand('print Pub version', commandVersion) | 30 'version': new VersionCommand() |
| 33 }; | 31 }; |
| 34 | 32 |
| 35 if (args.length == 0) { | 33 if (args.length == 0) { |
| 36 showUsage(); | 34 printUsage(commands); |
| 37 return; | 35 return; |
| 38 } | 36 } |
| 39 | 37 |
| 38 // For consistency with expected unix idioms, support --help, -h, and |
| 39 // --version in addition to the regular commands. |
| 40 if (args.length == 1) { |
| 41 if (args[0] == '--help' || args[0] == '-h') { |
| 42 printUsage(commands); |
| 43 return; |
| 44 } |
| 45 |
| 46 if (args[0] == '--version') { |
| 47 printVersion(); |
| 48 return; |
| 49 } |
| 50 } |
| 51 |
| 40 // TODO(rnystrom): Hack. This is temporary code to allow the pub tests to | 52 // TODO(rnystrom): Hack. This is temporary code to allow the pub tests to |
| 41 // pass in relevant paths. Eventually these should be either environment | 53 // pass in relevant paths. Eventually these should be either environment |
| 42 // variables or at least a cleaner arg parser. | 54 // variables or at least a cleaner arg parser. |
| 43 var cacheDir; | 55 var cacheDir; |
| 44 for (var i = 0; i < args.length; i++) { | 56 for (var i = 0; i < args.length; i++) { |
| 45 if (args[i].startsWith('--cachedir=')) { | 57 if (args[i].startsWith('--cachedir=')) { |
| 46 cacheDir = args[i].substring('--cachedir='.length); | 58 cacheDir = args[i].substring('--cachedir='.length); |
| 47 args.removeRange(i, 1); | 59 args.removeRange(i, 1); |
| 48 break; | 60 break; |
| 49 } | 61 } |
| 50 } | 62 } |
| 51 | 63 |
| 52 // TODO(rnystrom): Do we want this to be global? | 64 // TODO(rnystrom): Do we want this to be global? |
| 53 cache = new PackageCache(cacheDir); | 65 final cache = new PackageCache(cacheDir); |
| 54 | |
| 55 var options = new PubOptions(cacheDir); | |
| 56 | 66 |
| 57 // Select the command. | 67 // Select the command. |
| 58 final command = commands[args[0]]; | 68 final command = commands[args[0]]; |
| 59 if (command == null) { | 69 if (command == null) { |
| 60 print('Unknown command "${args[0]}".'); | 70 print('Unknown command "${args[0]}".'); |
| 61 print('Run "pub help" to see available commands.'); | 71 print('Run "pub help" to see available commands.'); |
| 62 exit(64); // See http://www.freebsd.org/cgi/man.cgi?query=sysexits. | 72 exit(64); // See http://www.freebsd.org/cgi/man.cgi?query=sysexits. |
| 63 return; | 73 return; |
| 64 } | 74 } |
| 65 | 75 |
| 66 args.removeRange(0, 1); | 76 args.removeRange(0, 1); |
| 67 command.function(options, args); | 77 command.run(cache, args); |
| 68 } | 78 } |
| 69 | 79 |
| 70 /** Displays usage information for the app. */ | 80 /** Displays usage information for the app. */ |
| 71 void showUsage() { | 81 void printUsage(Map<String, PubCommand> commands) { |
| 72 print('Pub is a package manager for Dart.'); | 82 print('Pub is a package manager for Dart.'); |
| 73 print(''); | 83 print(''); |
| 74 print('Usage:'); | 84 print('Usage:'); |
| 75 print(''); | 85 print(''); |
| 76 print(' pub command [arguments]'); | 86 print(' pub command [arguments]'); |
| 77 print(''); | 87 print(''); |
| 78 print('The commands are:'); | 88 print('The commands are:'); |
| 79 print(''); | 89 print(''); |
| 80 | 90 |
| 81 // Show the commands sorted. | 91 // Show the commands sorted. |
| 82 // TODO(rnystrom): A sorted map would be nice. | 92 // TODO(rnystrom): A sorted map would be nice. |
| 83 int length = 0; | 93 int length = 0; |
| 84 final names = <String>[]; | 94 final names = <String>[]; |
| 85 for (final command in commands.getKeys()) { | 95 for (final command in commands.getKeys()) { |
| 86 length = Math.max(length, command.length); | 96 length = Math.max(length, command.length); |
| 87 names.add(command); | 97 names.add(command); |
| 88 } | 98 } |
| 89 | 99 |
| 90 names.sort((a, b) => a.compareTo(b)); | 100 names.sort((a, b) => a.compareTo(b)); |
| 91 | 101 |
| 92 for (final name in names) { | 102 for (final name in names) { |
| 93 print(' ${padRight(name, length)} ${commands[name].description}'); | 103 print(' ${padRight(name, length)} ${commands[name].description}'); |
| 94 } | 104 } |
| 95 | 105 |
| 96 print(''); | 106 print(''); |
| 97 print('Use "pub help [command]" for more information about a command.'); | 107 print('Use "pub help [command]" for more information about a command.'); |
| 98 } | 108 } |
| 99 | 109 |
| 100 /** Displays pub version information. */ | 110 void printVersion() { |
| 101 void commandVersion(PubOptions options, List<String> args) { | |
| 102 // TODO(rnystrom): Store some place central. | |
| 103 print('Pub 0.0.0'); | 111 print('Pub 0.0.0'); |
| 104 } | 112 } |
| 105 | 113 |
| 106 /** | 114 /** |
| 107 * Gets the package that contains the current working directory. In other words, | 115 * Gets the package that contains the current working directory. In other words, |
| 108 * finds the package that the user is currently "in". | 116 * finds the package that the user is currently "in". |
| 109 */ | 117 */ |
| 110 Future<Package> getWorkingPackage() { | 118 Future<Package> getWorkingPackage() { |
| 111 // TODO(rnystrom): Will eventually need better logic to walk up | 119 // TODO(rnystrom): Will eventually need better logic to walk up |
| 112 // subdirectories until we hit one that looks package-like. For now, just | 120 // subdirectories until we hit one that looks package-like. For now, just |
| 113 // assume the cwd is it. | 121 // assume the cwd is it. |
| 114 return Package.load(workingDir); | 122 return Package.load(workingDir); |
| 115 } | 123 } |
| 116 | 124 |
| 117 typedef void CommandFunction(PubOptions options, List<String> args); | 125 class PubCommand { |
| 126 PackageCache cache; |
| 118 | 127 |
| 119 class PubCommand { | 128 abstract String get description(); |
| 120 final String description; | |
| 121 final CommandFunction function; | |
| 122 | 129 |
| 123 PubCommand(this.description, this.function); | 130 void run(PackageCache cache_, List<String> args) { |
| 131 cache = cache_; |
| 132 |
| 133 // TODO(rnystrom): Each command should define the arguments it expects and |
| 134 // we can handle them generically here. |
| 135 |
| 136 onRun(); |
| 137 } |
| 138 |
| 139 abstract void onRun(); |
| 124 } | 140 } |
| 125 | |
| 126 class PubOptions { | |
| 127 final String cacheDir; | |
| 128 | |
| 129 PubOptions(this.cacheDir); | |
| 130 } | |
| OLD | NEW |