| 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; | 7 import 'dart:math' as math; |
| 8 | 8 |
| 9 import 'package:args/args.dart'; | 9 import 'package:args/args.dart'; |
| 10 import 'package:pathos/path.dart' as path; | 10 import 'package:pathos/path.dart' as path; |
| 11 | 11 |
| 12 import '../lib/src/command.dart'; | 12 import '../lib/src/command.dart'; |
| 13 import '../lib/src/exit_codes.dart' as exit_codes; | 13 import '../lib/src/exit_codes.dart' as exit_codes; |
| 14 import '../lib/src/io.dart'; | 14 import '../lib/src/io.dart'; |
| 15 import '../lib/src/log.dart' as log; | 15 import '../lib/src/log.dart' as log; |
| 16 import '../lib/src/sdk.dart' as sdk; | 16 import '../lib/src/sdk.dart' as sdk; |
| 17 import '../lib/src/system_cache.dart'; | 17 import '../lib/src/system_cache.dart'; |
| 18 import '../lib/src/utils.dart'; | 18 import '../lib/src/utils.dart'; |
| 19 | 19 |
| 20 /// The parser for arguments that are global to Pub rather than specific to a | 20 final pubArgParser = initArgParser(); |
| 21 /// single command. | |
| 22 ArgParser get pubArgParser { | |
| 23 var parser = new ArgParser(); | |
| 24 parser.addFlag('help', abbr: 'h', negatable: false, | |
| 25 help: 'Print this usage information.'); | |
| 26 parser.addFlag('version', negatable: false, | |
| 27 help: 'Print pub version.'); | |
| 28 parser.addFlag('trace', | |
| 29 help: 'Print debugging information when an error occurs.'); | |
| 30 parser.addOption('verbosity', | |
| 31 help: 'Control output verbosity.', | |
| 32 allowed: ['normal', 'io', 'solver', 'all'], | |
| 33 allowedHelp: { | |
| 34 'normal': 'Show errors, warnings, and user messages.', | |
| 35 'io': 'Also show IO operations.', | |
| 36 'solver': 'Show steps during version resolution.', | |
| 37 'all': 'Show all output including internal tracing messages.' | |
| 38 }); | |
| 39 parser.addFlag('verbose', abbr: 'v', negatable: false, | |
| 40 help: 'Shortcut for "--verbosity=all".'); | |
| 41 return parser; | |
| 42 } | |
| 43 | 21 |
| 44 void main() { | 22 void main() { |
| 45 var globalOptions; | 23 ArgResults options; |
| 24 |
| 46 try { | 25 try { |
| 47 globalOptions = pubArgParser.parse(new Options().arguments); | 26 options = pubArgParser.parse(new Options().arguments); |
| 48 } on FormatException catch (e) { | 27 } on FormatException catch (e) { |
| 49 log.error(e.message); | 28 log.error(e.message); |
| 50 log.error('Run "pub help" to see available options.'); | 29 log.error('Run "pub help" to see available options.'); |
| 51 exit(exit_codes.USAGE); | 30 exit(exit_codes.USAGE); |
| 52 } | 31 } |
| 53 | 32 |
| 54 if (globalOptions['version']) { | 33 if (options['version']) { |
| 55 log.message('Pub ${sdk.version}'); | 34 log.message('Pub ${sdk.version}'); |
| 56 return; | 35 return; |
| 57 } | 36 } |
| 58 | 37 |
| 59 if (globalOptions['help'] || globalOptions.rest.isEmpty) { | 38 if (options['help']) { |
| 60 printUsage(); | 39 printUsage(); |
| 61 return; | 40 return; |
| 62 } | 41 } |
| 63 | 42 |
| 64 if (globalOptions['trace']) { | 43 if (options.command == null) { |
| 44 if (options.rest.isEmpty) { |
| 45 // No command was chosen. |
| 46 printUsage(); |
| 47 } else { |
| 48 log.error('Could not find a command named "${options.rest[0]}".'); |
| 49 log.error('Run "pub help" to see available commands.'); |
| 50 exit(exit_codes.USAGE); |
| 51 } |
| 52 return; |
| 53 } |
| 54 |
| 55 if (options['trace']) { |
| 65 log.recordTranscript(); | 56 log.recordTranscript(); |
| 66 } | 57 } |
| 67 | 58 |
| 68 switch (globalOptions['verbosity']) { | 59 switch (options['verbosity']) { |
| 69 case 'normal': log.showNormal(); break; | 60 case 'normal': log.showNormal(); break; |
| 70 case 'io': log.showIO(); break; | 61 case 'io': log.showIO(); break; |
| 71 case 'solver': log.showSolver(); break; | 62 case 'solver': log.showSolver(); break; |
| 72 case 'all': log.showAll(); break; | 63 case 'all': log.showAll(); break; |
| 73 default: | 64 default: |
| 74 // No specific verbosity given, so check for the shortcut. | 65 // No specific verbosity given, so check for the shortcut. |
| 75 if (globalOptions['verbose']) { | 66 if (options['verbose']) { |
| 76 log.showAll(); | 67 log.showAll(); |
| 77 } else { | 68 } else { |
| 78 log.showNormal(); | 69 log.showNormal(); |
| 79 } | 70 } |
| 80 break; | 71 break; |
| 81 } | 72 } |
| 82 | 73 |
| 83 log.fine('Pub ${sdk.version}'); | 74 log.fine('Pub ${sdk.version}'); |
| 84 | 75 |
| 85 var cacheDir; | 76 var cacheDir; |
| 86 if (Platform.environment.containsKey('PUB_CACHE')) { | 77 if (Platform.environment.containsKey('PUB_CACHE')) { |
| 87 cacheDir = Platform.environment['PUB_CACHE']; | 78 cacheDir = Platform.environment['PUB_CACHE']; |
| 88 } else if (Platform.operatingSystem == 'windows') { | 79 } else if (Platform.operatingSystem == 'windows') { |
| 89 var appData = Platform.environment['APPDATA']; | 80 var appData = Platform.environment['APPDATA']; |
| 90 cacheDir = path.join(appData, 'Pub', 'Cache'); | 81 cacheDir = path.join(appData, 'Pub', 'Cache'); |
| 91 } else { | 82 } else { |
| 92 cacheDir = '${Platform.environment['HOME']}/.pub-cache'; | 83 cacheDir = '${Platform.environment['HOME']}/.pub-cache'; |
| 93 } | 84 } |
| 94 | 85 |
| 95 validatePlatform().then((_) { | 86 validatePlatform().then((_) { |
| 96 // Select the command. | 87 PubCommand.commands[options.command.name].run(cacheDir, options); |
| 97 var command = PubCommand.commands[globalOptions.rest[0]]; | |
| 98 if (command == null) { | |
| 99 log.error('Could not find a command named "${globalOptions.rest[0]}".'); | |
| 100 log.error('Run "pub help" to see available commands.'); | |
| 101 exit(exit_codes.USAGE); | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 var commandArgs = globalOptions.rest.sublist(1); | |
| 106 command.run(cacheDir, globalOptions, commandArgs); | |
| 107 }); | 88 }); |
| 108 } | 89 } |
| 109 | 90 |
| 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 |
| 110 /// Checks that pub is running on a supported platform. If it isn't, it prints | 121 /// Checks that pub is running on a supported platform. If it isn't, it prints |
| 111 /// an error message and exits. Completes when the validation is done. | 122 /// an error message and exits. Completes when the validation is done. |
| 112 Future validatePlatform() { | 123 Future validatePlatform() { |
| 113 return new Future.sync(() { | 124 return new Future.sync(() { |
| 114 if (Platform.operatingSystem != 'windows') return; | 125 if (Platform.operatingSystem != 'windows') return; |
| 115 | 126 |
| 116 return runProcess('ver', []).then((result) { | 127 return runProcess('ver', []).then((result) { |
| 117 if (result.stdout.join('\n').contains('XP')) { | 128 if (result.stdout.join('\n').contains('XP')) { |
| 118 log.error('Sorry, but pub is not supported on Windows XP.'); | 129 log.error('Sorry, but pub is not supported on Windows XP.'); |
| 119 exit(exit_codes.USAGE); | 130 exit(exit_codes.USAGE); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 150 for (var name in names) { | 161 for (var name in names) { |
| 151 buffer.write(' ${padRight(name, length)} ' | 162 buffer.write(' ${padRight(name, length)} ' |
| 152 '${PubCommand.commands[name].description}\n'); | 163 '${PubCommand.commands[name].description}\n'); |
| 153 } | 164 } |
| 154 | 165 |
| 155 buffer.write('\n'); | 166 buffer.write('\n'); |
| 156 buffer.write( | 167 buffer.write( |
| 157 'Use "pub help [command]" for more information about a command.'); | 168 'Use "pub help [command]" for more information about a command.'); |
| 158 log.message(buffer.toString()); | 169 log.message(buffer.toString()); |
| 159 } | 170 } |
| OLD | NEW |