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 '../lib/src/pub.dart'; | 5 import 'dart:async'; |
| 6 import 'dart:io'; |
| 7 import 'dart:math' as math; |
6 | 8 |
7 // TODO(nweiz): move option-parsing logic into this file. | 9 import 'package:args/args.dart'; |
8 void main() => run(); | 10 import 'package:pathos/path.dart' as path; |
| 11 |
| 12 import '../lib/src/command.dart'; |
| 13 import '../lib/src/exit_codes.dart' as exit_codes; |
| 14 import '../lib/src/log.dart' as log; |
| 15 import '../lib/src/sdk.dart' as sdk; |
| 16 import '../lib/src/system_cache.dart'; |
| 17 import '../lib/src/utils.dart'; |
| 18 |
| 19 /// The parser for arguments that are global to Pub rather than specific to a |
| 20 /// single command. |
| 21 ArgParser get pubArgParser { |
| 22 var parser = new ArgParser(); |
| 23 parser.addFlag('help', abbr: 'h', negatable: false, |
| 24 help: 'Print this usage information.'); |
| 25 parser.addFlag('version', negatable: false, |
| 26 help: 'Print pub version.'); |
| 27 parser.addFlag('trace', |
| 28 help: 'Print debugging information when an error occurs.'); |
| 29 parser.addOption('verbosity', |
| 30 help: 'Control output verbosity.', |
| 31 allowed: ['normal', 'io', 'solver', 'all'], |
| 32 allowedHelp: { |
| 33 'normal': 'Show errors, warnings, and user messages.', |
| 34 'io': 'Also show IO operations.', |
| 35 'solver': 'Show steps during version resolution.', |
| 36 'all': 'Show all output including internal tracing messages.' |
| 37 }); |
| 38 parser.addFlag('verbose', abbr: 'v', negatable: false, |
| 39 help: 'Shortcut for "--verbosity=all"'); |
| 40 return parser; |
| 41 } |
| 42 |
| 43 void main() { |
| 44 var globalOptions; |
| 45 try { |
| 46 globalOptions = pubArgParser.parse(new Options().arguments); |
| 47 } on FormatException catch (e) { |
| 48 log.error(e.message); |
| 49 log.error('Run "pub help" to see available options.'); |
| 50 exit(exit_codes.USAGE); |
| 51 } |
| 52 |
| 53 if (globalOptions['version']) { |
| 54 log.message('Pub ${sdk.version}'); |
| 55 return; |
| 56 } |
| 57 |
| 58 if (globalOptions['help'] || globalOptions.rest.isEmpty) { |
| 59 printUsage(); |
| 60 return; |
| 61 } |
| 62 |
| 63 if (globalOptions['trace']) { |
| 64 log.recordTranscript(); |
| 65 } |
| 66 |
| 67 switch (globalOptions['verbosity']) { |
| 68 case 'normal': log.showNormal(); break; |
| 69 case 'io': log.showIO(); break; |
| 70 case 'solver': log.showSolver(); break; |
| 71 case 'all': log.showAll(); break; |
| 72 default: |
| 73 // No specific verbosity given, so check for the shortcut. |
| 74 if (globalOptions['verbose']) { |
| 75 log.showAll(); |
| 76 } else { |
| 77 log.showNormal(); |
| 78 } |
| 79 break; |
| 80 } |
| 81 |
| 82 var cacheDir; |
| 83 if (Platform.environment.containsKey('PUB_CACHE')) { |
| 84 cacheDir = Platform.environment['PUB_CACHE']; |
| 85 } else if (Platform.operatingSystem == 'windows') { |
| 86 var appData = Platform.environment['APPDATA']; |
| 87 cacheDir = path.join(appData, 'Pub', 'Cache'); |
| 88 } else { |
| 89 cacheDir = '${Platform.environment['HOME']}/.pub-cache'; |
| 90 } |
| 91 |
| 92 validatePlatform().then((_) { |
| 93 var cache = new SystemCache.withSources(cacheDir); |
| 94 |
| 95 // Select the command. |
| 96 var command = PubCommand.commands[globalOptions.rest[0]]; |
| 97 if (command == null) { |
| 98 log.error('Could not find a command named "${globalOptions.rest[0]}".'); |
| 99 log.error('Run "pub help" to see available commands.'); |
| 100 exit(exit_codes.USAGE); |
| 101 return; |
| 102 } |
| 103 |
| 104 var commandArgs = globalOptions.rest.sublist(1); |
| 105 command.run(cache, globalOptions, commandArgs); |
| 106 }); |
| 107 } |
| 108 |
| 109 /// Checks that pub is running on a supported platform. If it isn't, it prints |
| 110 /// an error message and exits. Completes when the validation is done. |
| 111 Future validatePlatform() { |
| 112 return new Future.sync(() { |
| 113 if (Platform.operatingSystem != 'windows') return; |
| 114 |
| 115 return runProcess('ver', []).then((result) { |
| 116 if (result.stdout.join('\n').contains('XP')) { |
| 117 log.error('Sorry, but pub is not supported on Windows XP.'); |
| 118 exit(exit_codes.USAGE); |
| 119 } |
| 120 }); |
| 121 }); |
| 122 } |
| 123 |
| 124 /// Displays usage information for the app. |
| 125 void printUsage([String description = 'Pub is a package manager for Dart.']) { |
| 126 // Build up a buffer so it shows up as a single log entry. |
| 127 var buffer = new StringBuffer(); |
| 128 buffer.write(description); |
| 129 buffer.write('\n\n'); |
| 130 buffer.write('Usage: pub command [arguments]\n\n'); |
| 131 buffer.write('Global options:\n'); |
| 132 buffer.write('${pubArgParser.getUsage()}\n\n'); |
| 133 |
| 134 // Show the commands sorted. |
| 135 buffer.write('Available commands:\n'); |
| 136 |
| 137 // TODO(rnystrom): A sorted map would be nice. |
| 138 int length = 0; |
| 139 var names = <String>[]; |
| 140 for (var command in PubCommand.commands.keys) { |
| 141 // Hide aliases. |
| 142 if (PubCommand.commands[command].aliases.indexOf(command) >= 0) continue; |
| 143 length = math.max(length, command.length); |
| 144 names.add(command); |
| 145 } |
| 146 |
| 147 names.sort((a, b) => a.compareTo(b)); |
| 148 |
| 149 for (var name in names) { |
| 150 buffer.write(' ${padRight(name, length)} ' |
| 151 '${PubCommand.commands[name].description}\n'); |
| 152 } |
| 153 |
| 154 buffer.write('\n'); |
| 155 buffer.write( |
| 156 'Use "pub help [command]" for more information about a command.'); |
| 157 log.message(buffer.toString()); |
| 158 } |
OLD | NEW |