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