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/lib/args.dart'; | 10 import '../../pkg/args/lib/args.dart'; |
11 import 'dart:io'; | 11 import 'dart:io'; |
12 import 'dart:math'; | 12 import 'dart:math'; |
13 import 'io.dart'; | 13 import 'io.dart'; |
14 import 'command_help.dart'; | 14 import 'command_help.dart'; |
15 import 'command_install.dart'; | 15 import 'command_install.dart'; |
16 import 'command_lish.dart'; | 16 import 'command_lish.dart'; |
17 import 'command_update.dart'; | 17 import 'command_update.dart'; |
18 import 'command_version.dart'; | 18 import 'command_version.dart'; |
19 import 'entrypoint.dart'; | 19 import 'entrypoint.dart'; |
20 import 'exit_codes.dart' as exit_codes; | 20 import 'exit_codes.dart' as exit_codes; |
21 import 'git_source.dart'; | |
22 import 'hosted_source.dart'; | |
23 import 'log.dart' as log; | |
24 import 'package.dart'; | 21 import 'package.dart'; |
25 import 'pubspec.dart'; | 22 import 'pubspec.dart'; |
26 import 'source.dart'; | 23 import 'source.dart'; |
27 import 'source_registry.dart'; | 24 import 'source_registry.dart'; |
28 import 'system_cache.dart'; | 25 import 'system_cache.dart'; |
29 import 'utils.dart'; | 26 import 'utils.dart'; |
30 import 'version.dart'; | 27 import 'version.dart'; |
31 | 28 |
32 Version get pubVersion => new Version(0, 0, 0); | 29 Version get pubVersion => new Version(0, 0, 0); |
33 | 30 |
(...skipping 16 matching lines...) Expand all Loading... |
50 return commands; | 47 return commands; |
51 } | 48 } |
52 | 49 |
53 /** | 50 /** |
54 * The parser for arguments that are global to Pub rather than specific to a | 51 * The parser for arguments that are global to Pub rather than specific to a |
55 * single command. | 52 * single command. |
56 */ | 53 */ |
57 ArgParser get pubArgParser { | 54 ArgParser get pubArgParser { |
58 var parser = new ArgParser(); | 55 var parser = new ArgParser(); |
59 parser.addFlag('help', abbr: 'h', negatable: false, | 56 parser.addFlag('help', abbr: 'h', negatable: false, |
60 help: 'print this usage information'); | 57 help: 'Prints this usage information'); |
61 parser.addFlag('version', negatable: false, | 58 parser.addFlag('version', negatable: false, |
62 help: 'print the version of pub'); | 59 help: 'Prints the version of Pub'); |
63 parser.addFlag('trace', | 60 parser.addFlag('trace', help: 'Prints a stack trace when an error occurs'); |
64 help: 'print debugging information when an error occurs'); | |
65 parser.addOption('verbosity', | |
66 help: 'control output verbosity', | |
67 allowed: ['normal', 'io', 'all'], | |
68 allowedHelp: { | |
69 'normal': 'errors, warnings, and user messages are shown', | |
70 'io': 'IO operations are also shown', | |
71 'all': 'all output including internal tracing messages are shown' | |
72 }); | |
73 parser.addFlag('verbose', abbr: 'v', negatable: false, | |
74 help: 'shortcut for "--verbosity=all"'); | |
75 return parser; | 61 return parser; |
76 } | 62 } |
77 | 63 |
78 main() { | 64 main() { |
79 var globalOptions; | 65 var globalOptions; |
80 try { | 66 try { |
81 globalOptions = pubArgParser.parse(new Options().arguments); | 67 globalOptions = pubArgParser.parse(new Options().arguments); |
82 } on FormatException catch (e) { | 68 } on FormatException catch (e) { |
83 log.error(e.message); | 69 printError(e.message); |
84 log.error('Run "pub help" to see available options.'); | 70 printError('Run "pub help" to see available options.'); |
85 exit(exit_codes.USAGE); | 71 exit(exit_codes.USAGE); |
86 } | 72 } |
87 | 73 |
88 if (globalOptions['version']) { | 74 if (globalOptions['version']) { |
89 printVersion(); | 75 printVersion(); |
90 return; | 76 return; |
91 } | 77 } |
92 | 78 |
93 if (globalOptions['help'] || globalOptions.rest.isEmpty) { | 79 if (globalOptions['help'] || globalOptions.rest.isEmpty) { |
94 printUsage(); | 80 printUsage(); |
95 return; | 81 return; |
96 } | 82 } |
97 | 83 |
98 if (globalOptions['trace']) { | |
99 log.recordTranscript(); | |
100 } | |
101 | |
102 switch (globalOptions['verbosity']) { | |
103 case 'normal': log.showNormal(); break; | |
104 case 'io': log.showIO(); break; | |
105 case 'all': log.showAll(); break; | |
106 default: | |
107 // No specific verbosity given, so check for the shortcut. | |
108 if (globalOptions['verbose']) { | |
109 log.showAll(); | |
110 } else { | |
111 log.showNormal(); | |
112 } | |
113 break; | |
114 } | |
115 | |
116 // TODO(nweiz): Have a fallback for this this out automatically once 1145 is | 84 // TODO(nweiz): Have a fallback for this this out automatically once 1145 is |
117 // fixed. | 85 // fixed. |
118 var sdkDir = Platform.environment['DART_SDK']; | 86 var sdkDir = Platform.environment['DART_SDK']; |
119 var cacheDir; | 87 var cacheDir; |
120 if (Platform.environment.containsKey('PUB_CACHE')) { | 88 if (Platform.environment.containsKey('PUB_CACHE')) { |
121 cacheDir = Platform.environment['PUB_CACHE']; | 89 cacheDir = Platform.environment['PUB_CACHE']; |
122 } else if (Platform.operatingSystem == 'windows') { | 90 } else if (Platform.operatingSystem == 'windows') { |
123 var appData = Platform.environment['APPDATA']; | 91 var appData = Platform.environment['APPDATA']; |
124 cacheDir = join(appData, 'Pub', 'Cache'); | 92 cacheDir = join(appData, 'Pub', 'Cache'); |
125 } else { | 93 } else { |
126 cacheDir = '${Platform.environment['HOME']}/.pub-cache'; | 94 cacheDir = '${Platform.environment['HOME']}/.pub-cache'; |
127 } | 95 } |
128 | 96 |
129 var cache = new SystemCache.withSources(cacheDir, sdkDir); | 97 var cache = new SystemCache.withSources(cacheDir, sdkDir); |
130 | 98 |
131 // Select the command. | 99 // Select the command. |
132 var command = pubCommands[globalOptions.rest[0]]; | 100 var command = pubCommands[globalOptions.rest[0]]; |
133 if (command == null) { | 101 if (command == null) { |
134 log.error('Could not find a command named "${globalOptions.rest[0]}".'); | 102 printError('Could not find a command named "${globalOptions.rest[0]}".'); |
135 log.error('Run "pub help" to see available commands.'); | 103 printError('Run "pub help" to see available commands.'); |
136 exit(exit_codes.USAGE); | 104 exit(exit_codes.USAGE); |
137 return; | 105 return; |
138 } | 106 } |
139 | 107 |
140 var commandArgs = | 108 var commandArgs = |
141 globalOptions.rest.getRange(1, globalOptions.rest.length - 1); | 109 globalOptions.rest.getRange(1, globalOptions.rest.length - 1); |
142 command.run(cache, globalOptions, commandArgs); | 110 command.run(cache, globalOptions, commandArgs); |
143 } | 111 } |
144 | 112 |
145 /** Displays usage information for the app. */ | 113 /** Displays usage information for the app. */ |
146 void printUsage([String description = 'Pub is a package manager for Dart.']) { | 114 void printUsage([String description = 'Pub is a package manager for Dart.']) { |
147 // Build up a buffer so it shows up as a single log entry. | 115 print(description); |
148 var buffer = new StringBuffer(); | 116 print(''); |
149 buffer.add(description); | 117 print('Usage: pub command [arguments]'); |
150 buffer.add('\n\n'); | 118 print(''); |
151 buffer.add('Usage: pub command [arguments]\n\n'); | 119 print('Global options:'); |
152 buffer.add('Global options:\n'); | 120 print(pubArgParser.getUsage()); |
153 buffer.add('${pubArgParser.getUsage()}\n\n'); | 121 print(''); |
154 | 122 |
155 // Show the commands sorted. | 123 // Show the commands sorted. |
156 buffer.add('Available commands:\n'); | 124 print('Available commands:'); |
157 | 125 |
158 // TODO(rnystrom): A sorted map would be nice. | 126 // TODO(rnystrom): A sorted map would be nice. |
159 int length = 0; | 127 int length = 0; |
160 var names = <String>[]; | 128 var names = <String>[]; |
161 for (var command in pubCommands.keys) { | 129 for (var command in pubCommands.keys) { |
162 // Hide aliases. | 130 // Hide aliases. |
163 if (pubCommands[command].aliases.indexOf(command) >= 0) continue; | 131 if (pubCommands[command].aliases.indexOf(command) >= 0) continue; |
164 length = max(length, command.length); | 132 length = max(length, command.length); |
165 names.add(command); | 133 names.add(command); |
166 } | 134 } |
167 | 135 |
168 names.sort((a, b) => a.compareTo(b)); | 136 names.sort((a, b) => a.compareTo(b)); |
169 | 137 |
170 for (var name in names) { | 138 for (var name in names) { |
171 buffer.add(' ${padRight(name, length)} ' | 139 print(' ${padRight(name, length)} ${pubCommands[name].description}'); |
172 '${pubCommands[name].description}\n'); | |
173 } | 140 } |
174 | 141 |
175 buffer.add('\n'); | 142 print(''); |
176 buffer.add('Use "pub help [command]" for more information about a command.'); | 143 print('Use "pub help [command]" for more information about a command.'); |
177 log.message(buffer.toString()); | |
178 } | 144 } |
179 | 145 |
180 void printVersion() { | 146 void printVersion() { |
181 log.message('Pub $pubVersion'); | 147 print('Pub $pubVersion'); |
182 } | 148 } |
183 | 149 |
184 abstract class PubCommand { | 150 abstract class PubCommand { |
185 SystemCache cache; | 151 SystemCache cache; |
186 ArgResults globalOptions; | 152 ArgResults globalOptions; |
187 ArgResults commandOptions; | 153 ArgResults commandOptions; |
188 | 154 |
189 Entrypoint entrypoint; | 155 Entrypoint entrypoint; |
190 | 156 |
191 /** | 157 /** |
(...skipping 22 matching lines...) Expand all Loading... |
214 ArgParser get commandParser => new ArgParser(); | 180 ArgParser get commandParser => new ArgParser(); |
215 | 181 |
216 void run(SystemCache cache_, ArgResults globalOptions_, | 182 void run(SystemCache cache_, ArgResults globalOptions_, |
217 List<String> commandArgs) { | 183 List<String> commandArgs) { |
218 cache = cache_; | 184 cache = cache_; |
219 globalOptions = globalOptions_; | 185 globalOptions = globalOptions_; |
220 | 186 |
221 try { | 187 try { |
222 commandOptions = commandParser.parse(commandArgs); | 188 commandOptions = commandParser.parse(commandArgs); |
223 } on FormatException catch (e) { | 189 } on FormatException catch (e) { |
224 log.error(e.message); | 190 printError(e.message); |
225 log.error('Use "pub help" for more information.'); | 191 printError('Use "pub help" for more information.'); |
226 exit(exit_codes.USAGE); | 192 exit(exit_codes.USAGE); |
227 } | 193 } |
228 | 194 |
229 handleError(error, trace) { | 195 handleError(error, trace) { |
230 // This is basically the top-level exception handler so that we don't | 196 // This is basically the top-level exception handler so that we don't |
231 // spew a stack trace on our users. | 197 // spew a stack trace on our users. |
232 var message = error.toString(); | 198 var message = error.toString(); |
233 | 199 |
234 // TODO(rnystrom): The default exception implementation class puts | 200 // TODO(rnystrom): The default exception implementation class puts |
235 // "Exception:" in the output, so strip that off. | 201 // "Exception:" in the output, so strip that off. |
236 if (message.startsWith("Exception: ")) { | 202 if (message.startsWith("Exception: ")) { |
237 message = message.substring("Exception: ".length); | 203 message = message.substring("Exception: ".length); |
238 } | 204 } |
239 | 205 |
240 log.error(message); | 206 printError(message); |
241 if (globalOptions['trace'] && trace != null) { | 207 if (globalOptions['trace'] && trace != null) { |
242 log.error(trace); | 208 printError(trace); |
243 log.dumpTranscript(); | |
244 } | 209 } |
245 | 210 |
246 exit(_chooseExitCode(error)); | 211 exit(_chooseExitCode(error)); |
247 } | 212 } |
248 | 213 |
249 var future = new Future.immediate(null); | 214 var future = new Future.immediate(null); |
250 if (requiresEntrypoint) { | 215 if (requiresEntrypoint) { |
251 // TODO(rnystrom): Will eventually need better logic to walk up | 216 // TODO(rnystrom): Will eventually need better logic to walk up |
252 // subdirectories until we hit one that looks package-like. For now, just | 217 // subdirectories until we hit one that looks package-like. For now, just |
253 // assume the cwd is it. | 218 // assume the cwd is it. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 /** | 254 /** |
290 * Override this to perform the specific command. Return a future that | 255 * Override this to perform the specific command. Return a future that |
291 * completes when the command is done or fails if the command fails. If the | 256 * completes when the command is done or fails if the command fails. If the |
292 * command is synchronous, it may return `null`. | 257 * command is synchronous, it may return `null`. |
293 */ | 258 */ |
294 Future onRun(); | 259 Future onRun(); |
295 | 260 |
296 /** Displays usage information for this command. */ | 261 /** Displays usage information for this command. */ |
297 void printUsage([String description]) { | 262 void printUsage([String description]) { |
298 if (description == null) description = this.description; | 263 if (description == null) description = this.description; |
299 log.message(description); | 264 print(description); |
300 log.message(''); | 265 print(''); |
301 log.message('Usage: $usage'); | 266 print('Usage: $usage'); |
302 | 267 |
303 var commandUsage = commandParser.getUsage(); | 268 var commandUsage = commandParser.getUsage(); |
304 if (!commandUsage.isEmpty) { | 269 if (!commandUsage.isEmpty) { |
305 log.message(''); | 270 print(''); |
306 log.message(commandUsage); | 271 print(commandUsage); |
307 } | 272 } |
308 } | 273 } |
309 | 274 |
310 /// Returns the appropriate exit code for [exception], falling back on 1 if no | 275 /// Returns the appropriate exit code for [exception], falling back on 1 if no |
311 /// appropriate exit code could be found. | 276 /// appropriate exit code could be found. |
312 int _chooseExitCode(exception) { | 277 int _chooseExitCode(exception) { |
313 if (exception is HttpException || exception is HttpParserException || | 278 if (exception is HttpException || exception is HttpParserException || |
314 exception is SocketIOException || exception is PubHttpException) { | 279 exception is SocketIOException || exception is PubHttpException) { |
315 return exit_codes.UNAVAILABLE; | 280 return exit_codes.UNAVAILABLE; |
316 } else if (exception is FormatException) { | 281 } else if (exception is FormatException) { |
317 return exit_codes.DATA; | 282 return exit_codes.DATA; |
318 } else { | 283 } else { |
319 return 1; | 284 return 1; |
320 } | 285 } |
321 } | 286 } |
322 } | 287 } |
OLD | NEW |