Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: utils/pub/pub.dart

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

Powered by Google App Engine
This is Rietveld 408576698