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

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

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

Powered by Google App Engine
This is Rietveld 408576698