| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 import 'dart:math' as math; | |
| 8 | 7 |
| 9 import 'package:args/args.dart'; | 8 import 'package:args/args.dart'; |
| 10 import 'package:path/path.dart' as path; | 9 import 'package:path/path.dart' as path; |
| 11 | 10 |
| 12 import '../lib/src/command.dart'; | 11 import '../lib/src/command.dart'; |
| 13 import '../lib/src/exit_codes.dart' as exit_codes; | 12 import '../lib/src/exit_codes.dart' as exit_codes; |
| 14 import '../lib/src/io.dart'; | 13 import '../lib/src/io.dart'; |
| 15 import '../lib/src/log.dart' as log; | 14 import '../lib/src/log.dart' as log; |
| 16 import '../lib/src/sdk.dart' as sdk; | 15 import '../lib/src/sdk.dart' as sdk; |
| 17 import '../lib/src/utils.dart'; | 16 import '../lib/src/utils.dart'; |
| 18 | 17 |
| 19 final pubArgParser = initArgParser(); | |
| 20 | |
| 21 void main(List<String> arguments) { | 18 void main(List<String> arguments) { |
| 22 ArgResults options; | 19 ArgResults options; |
| 23 | 20 |
| 24 try { | 21 try { |
| 25 options = pubArgParser.parse(arguments); | 22 options = PubCommand.pubArgParser.parse(arguments); |
| 26 } on FormatException catch (e) { | 23 } on FormatException catch (e) { |
| 27 log.error(e.message); | 24 log.error(e.message); |
| 28 log.error('Run "pub help" to see available options.'); | 25 log.error('Run "pub help" to see available options.'); |
| 29 flushThenExit(exit_codes.USAGE); | 26 flushThenExit(exit_codes.USAGE); |
| 30 return; | 27 return; |
| 31 } | 28 } |
| 32 | 29 |
| 33 if (options['version']) { | 30 if (options['version']) { |
| 34 log.message('Pub ${sdk.version}'); | 31 log.message('Pub ${sdk.version}'); |
| 35 return; | 32 return; |
| 36 } | 33 } |
| 37 | 34 |
| 38 if (options['help']) { | 35 if (options['help']) { |
| 39 printUsage(); | 36 PubCommand.printGlobalUsage(); |
| 40 return; | 37 return; |
| 41 } | 38 } |
| 42 | 39 |
| 43 if (options.command == null) { | 40 if (options.command == null) { |
| 44 if (options.rest.isEmpty) { | 41 if (options.rest.isEmpty) { |
| 45 // No command was chosen. | 42 // No command was chosen. |
| 46 printUsage(); | 43 PubCommand.printGlobalUsage(); |
| 47 } else { | 44 } else { |
| 48 log.error('Could not find a command named "${options.rest[0]}".'); | 45 log.error('Could not find a command named "${options.rest[0]}".'); |
| 49 log.error('Run "pub help" to see available commands.'); | 46 log.error('Run "pub help" to see available commands.'); |
| 50 flushThenExit(exit_codes.USAGE); | 47 flushThenExit(exit_codes.USAGE); |
| 51 } | 48 } |
| 52 return; | 49 return; |
| 53 } | 50 } |
| 54 | 51 |
| 55 if (options['trace']) { | 52 if (options['trace']) { |
| 56 log.recordTranscript(); | 53 log.recordTranscript(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 81 cacheDir = path.join(appData, 'Pub', 'Cache'); | 78 cacheDir = path.join(appData, 'Pub', 'Cache'); |
| 82 } else { | 79 } else { |
| 83 cacheDir = '${Platform.environment['HOME']}/.pub-cache'; | 80 cacheDir = '${Platform.environment['HOME']}/.pub-cache'; |
| 84 } | 81 } |
| 85 | 82 |
| 86 validatePlatform().then((_) { | 83 validatePlatform().then((_) { |
| 87 PubCommand.commands[options.command.name].run(cacheDir, options, arguments); | 84 PubCommand.commands[options.command.name].run(cacheDir, options, arguments); |
| 88 }); | 85 }); |
| 89 } | 86 } |
| 90 | 87 |
| 91 ArgParser initArgParser() { | |
| 92 var argParser = new ArgParser(); | |
| 93 | |
| 94 // Add the global options. | |
| 95 argParser.addFlag('help', abbr: 'h', negatable: false, | |
| 96 help: 'Print this usage information.'); | |
| 97 argParser.addFlag('version', negatable: false, | |
| 98 help: 'Print pub version.'); | |
| 99 argParser.addFlag('trace', | |
| 100 help: 'Print debugging information when an error occurs.'); | |
| 101 argParser.addOption('verbosity', | |
| 102 help: 'Control output verbosity.', | |
| 103 allowed: ['normal', 'io', 'solver', 'all'], | |
| 104 allowedHelp: { | |
| 105 'normal': 'Show errors, warnings, and user messages.', | |
| 106 'io': 'Also show IO operations.', | |
| 107 'solver': 'Show steps during version resolution.', | |
| 108 'all': 'Show all output including internal tracing messages.' | |
| 109 }); | |
| 110 argParser.addFlag('verbose', abbr: 'v', negatable: false, | |
| 111 help: 'Shortcut for "--verbosity=all".'); | |
| 112 | |
| 113 // Register the commands. | |
| 114 PubCommand.commands.forEach((name, command) { | |
| 115 argParser.addCommand(name, command.commandParser); | |
| 116 }); | |
| 117 | |
| 118 return argParser; | |
| 119 } | |
| 120 | |
| 121 /// Checks that pub is running on a supported platform. If it isn't, it prints | 88 /// Checks that pub is running on a supported platform. If it isn't, it prints |
| 122 /// an error message and exits. Completes when the validation is done. | 89 /// an error message and exits. Completes when the validation is done. |
| 123 Future validatePlatform() { | 90 Future validatePlatform() { |
| 124 return syncFuture(() { | 91 return syncFuture(() { |
| 125 if (Platform.operatingSystem != 'windows') return null; | 92 if (Platform.operatingSystem != 'windows') return null; |
| 126 | 93 |
| 127 return runProcess('ver', []).then((result) { | 94 return runProcess('ver', []).then((result) { |
| 128 if (result.stdout.join('\n').contains('XP')) { | 95 if (result.stdout.join('\n').contains('XP')) { |
| 129 log.error('Sorry, but pub is not supported on Windows XP.'); | 96 log.error('Sorry, but pub is not supported on Windows XP.'); |
| 130 return flushThenExit(exit_codes.USAGE); | 97 return flushThenExit(exit_codes.USAGE); |
| 131 } | 98 } |
| 132 }); | 99 }); |
| 133 }); | 100 }); |
| 134 } | 101 } |
| 135 | |
| 136 /// Displays usage information for the app. | |
| 137 void printUsage([String description = 'Pub is a package manager for Dart.']) { | |
| 138 // Build up a buffer so it shows up as a single log entry. | |
| 139 var buffer = new StringBuffer(); | |
| 140 buffer.write(description); | |
| 141 buffer.write('\n\n'); | |
| 142 buffer.write('Usage: pub command [arguments]\n\n'); | |
| 143 buffer.write('Global options:\n'); | |
| 144 buffer.write('${pubArgParser.getUsage()}\n\n'); | |
| 145 | |
| 146 // Show the commands sorted. | |
| 147 buffer.write('Available commands:\n'); | |
| 148 | |
| 149 // TODO(rnystrom): A sorted map would be nice. | |
| 150 int length = 0; | |
| 151 var names = <String>[]; | |
| 152 for (var command in PubCommand.commands.keys) { | |
| 153 // Hide aliases. | |
| 154 if (PubCommand.commands[command].aliases.indexOf(command) >= 0) continue; | |
| 155 | |
| 156 // Hide undocumented commands. | |
| 157 if (PubCommand.commands[command].hidden) continue; | |
| 158 | |
| 159 length = math.max(length, command.length); | |
| 160 names.add(command); | |
| 161 } | |
| 162 | |
| 163 names.sort((a, b) => a.compareTo(b)); | |
| 164 | |
| 165 for (var name in names) { | |
| 166 buffer.write(' ${padRight(name, length)} ' | |
| 167 '${PubCommand.commands[name].description}\n'); | |
| 168 } | |
| 169 | |
| 170 buffer.write('\n'); | |
| 171 buffer.write( | |
| 172 'Use "pub help [command]" for more information about a command.'); | |
| 173 log.message(buffer.toString()); | |
| 174 } | |
| OLD | NEW |