| OLD | NEW |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 1 import 'dart:async'; | 5 import 'dart:async'; |
| 2 import 'dart:io'; | 6 import 'dart:io'; |
| 7 |
| 3 import 'package:args/args.dart'; | 8 import 'package:args/args.dart'; |
| 4 import 'package:http/http.dart' as http; | 9 import 'package:http/http.dart' as http; |
| 5 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| 6 import 'package:stack_trace/stack_trace.dart'; | 11 import 'package:stack_trace/stack_trace.dart'; |
| 12 |
| 7 import '../lib/src/command.dart'; | 13 import '../lib/src/command.dart'; |
| 8 import '../lib/src/exceptions.dart'; | 14 import '../lib/src/exceptions.dart'; |
| 9 import '../lib/src/exit_codes.dart' as exit_codes; | 15 import '../lib/src/exit_codes.dart' as exit_codes; |
| 10 import '../lib/src/http.dart'; | 16 import '../lib/src/http.dart'; |
| 11 import '../lib/src/io.dart'; | 17 import '../lib/src/io.dart'; |
| 12 import '../lib/src/log.dart' as log; | 18 import '../lib/src/log.dart' as log; |
| 13 import '../lib/src/sdk.dart' as sdk; | 19 import '../lib/src/sdk.dart' as sdk; |
| 14 import '../lib/src/solver/version_solver.dart'; | 20 import '../lib/src/solver/version_solver.dart'; |
| 15 import '../lib/src/utils.dart'; | 21 import '../lib/src/utils.dart'; |
| 22 |
| 16 void main(List<String> arguments) { | 23 void main(List<String> arguments) { |
| 17 ArgResults options; | 24 ArgResults options; |
| 25 |
| 18 try { | 26 try { |
| 19 options = PubCommand.pubArgParser.parse(arguments); | 27 options = PubCommand.pubArgParser.parse(arguments); |
| 20 } on FormatException catch (e) { | 28 } on FormatException catch (e) { |
| 21 log.error(e.message); | 29 log.error(e.message); |
| 22 log.error('Run "pub help" to see available options.'); | 30 log.error('Run "pub help" to see available options.'); |
| 23 flushThenExit(exit_codes.USAGE); | 31 flushThenExit(exit_codes.USAGE); |
| 24 return; | 32 return; |
| 25 } | 33 } |
| 34 |
| 26 log.withPrejudice = options['with-prejudice']; | 35 log.withPrejudice = options['with-prejudice']; |
| 36 |
| 27 if (options['version']) { | 37 if (options['version']) { |
| 28 log.message('Pub ${sdk.version}'); | 38 log.message('Pub ${sdk.version}'); |
| 29 return; | 39 return; |
| 30 } | 40 } |
| 41 |
| 31 if (options['help']) { | 42 if (options['help']) { |
| 32 PubCommand.printGlobalUsage(); | 43 PubCommand.printGlobalUsage(); |
| 33 return; | 44 return; |
| 34 } | 45 } |
| 46 |
| 35 if (options['trace']) { | 47 if (options['trace']) { |
| 36 log.recordTranscript(); | 48 log.recordTranscript(); |
| 37 } | 49 } |
| 50 |
| 38 switch (options['verbosity']) { | 51 switch (options['verbosity']) { |
| 39 case 'normal': | 52 case 'normal': |
| 40 log.verbosity = log.Verbosity.NORMAL; | 53 log.verbosity = log.Verbosity.NORMAL; |
| 41 break; | 54 break; |
| 42 case 'io': | 55 case 'io': |
| 43 log.verbosity = log.Verbosity.IO; | 56 log.verbosity = log.Verbosity.IO; |
| 44 break; | 57 break; |
| 45 case 'solver': | 58 case 'solver': |
| 46 log.verbosity = log.Verbosity.SOLVER; | 59 log.verbosity = log.Verbosity.SOLVER; |
| 47 break; | 60 break; |
| 48 case 'all': | 61 case 'all': |
| 49 log.verbosity = log.Verbosity.ALL; | 62 log.verbosity = log.Verbosity.ALL; |
| 50 break; | 63 break; |
| 51 default: | 64 default: |
| 65 // No specific verbosity given, so check for the shortcut. |
| 52 if (options['verbose']) { | 66 if (options['verbose']) { |
| 53 log.verbosity = log.Verbosity.ALL; | 67 log.verbosity = log.Verbosity.ALL; |
| 54 } | 68 } |
| 55 break; | 69 break; |
| 56 } | 70 } |
| 71 |
| 57 log.fine('Pub ${sdk.version}'); | 72 log.fine('Pub ${sdk.version}'); |
| 73 |
| 58 var cacheDir; | 74 var cacheDir; |
| 59 if (Platform.environment.containsKey('PUB_CACHE')) { | 75 if (Platform.environment.containsKey('PUB_CACHE')) { |
| 60 cacheDir = Platform.environment['PUB_CACHE']; | 76 cacheDir = Platform.environment['PUB_CACHE']; |
| 61 } else if (Platform.operatingSystem == 'windows') { | 77 } else if (Platform.operatingSystem == 'windows') { |
| 62 var appData = Platform.environment['APPDATA']; | 78 var appData = Platform.environment['APPDATA']; |
| 63 cacheDir = path.join(appData, 'Pub', 'Cache'); | 79 cacheDir = path.join(appData, 'Pub', 'Cache'); |
| 64 } else { | 80 } else { |
| 65 cacheDir = '${Platform.environment['HOME']}/.pub-cache'; | 81 cacheDir = '${Platform.environment['HOME']}/.pub-cache'; |
| 66 } | 82 } |
| 83 |
| 67 validatePlatform().then((_) => runPub(cacheDir, options, arguments)); | 84 validatePlatform().then((_) => runPub(cacheDir, options, arguments)); |
| 68 } | 85 } |
| 86 |
| 87 /// Runs the appropriate pub command whose [arguments] have been parsed to |
| 88 /// [options] using the system cache in [cacheDir]. |
| 89 /// |
| 90 /// Handles and correctly reports any errors that occur while running. |
| 69 void runPub(String cacheDir, ArgResults options, List<String> arguments) { | 91 void runPub(String cacheDir, ArgResults options, List<String> arguments) { |
| 70 var captureStackChains = | 92 var captureStackChains = |
| 71 options['trace'] || | 93 options['trace'] || |
| 72 options['verbose'] || | 94 options['verbose'] || |
| 73 options['verbosity'] == 'all'; | 95 options['verbosity'] == 'all'; |
| 96 |
| 74 captureErrors( | 97 captureErrors( |
| 75 () => invokeCommand(cacheDir, options), | 98 () => invokeCommand(cacheDir, options), |
| 76 captureStackChains: captureStackChains).catchError((error, Chain chain) { | 99 captureStackChains: captureStackChains).catchError((error, Chain chain) { |
| 77 log.exception(error, chain); | 100 log.exception(error, chain); |
| 101 |
| 78 if (options['trace']) { | 102 if (options['trace']) { |
| 79 log.dumpTranscript(); | 103 log.dumpTranscript(); |
| 80 } else if (!isUserFacingException(error)) { | 104 } else if (!isUserFacingException(error)) { |
| 81 log.error(""" | 105 log.error(""" |
| 82 This is an unexpected error. Please run | 106 This is an unexpected error. Please run |
| 83 | 107 |
| 84 pub --trace ${arguments.map((arg) => "'$arg'").join(' ')} | 108 pub --trace ${arguments.map((arg) => "'$arg'").join(' ')} |
| 85 | 109 |
| 86 and include the results in a bug report on http://dartbug.com/new. | 110 and include the results in a bug report on http://dartbug.com/new. |
| 87 """); | 111 """); |
| 88 } | 112 } |
| 113 |
| 89 return flushThenExit(chooseExitCode(error)); | 114 return flushThenExit(chooseExitCode(error)); |
| 90 }).then((_) { | 115 }).then((_) { |
| 116 // Explicitly exit on success to ensure that any dangling dart:io handles |
| 117 // don't cause the process to never terminate. |
| 91 return flushThenExit(exit_codes.SUCCESS); | 118 return flushThenExit(exit_codes.SUCCESS); |
| 92 }); | 119 }); |
| 93 } | 120 } |
| 121 |
| 122 /// Returns the appropriate exit code for [exception], falling back on 1 if no |
| 123 /// appropriate exit code could be found. |
| 94 int chooseExitCode(exception) { | 124 int chooseExitCode(exception) { |
| 95 while (exception is WrappedException) exception = exception.innerError; | 125 while (exception is WrappedException) exception = exception.innerError; |
| 126 |
| 96 if (exception is HttpException || | 127 if (exception is HttpException || |
| 97 exception is http.ClientException || | 128 exception is http.ClientException || |
| 98 exception is SocketException || | 129 exception is SocketException || |
| 99 exception is PubHttpException || | 130 exception is PubHttpException || |
| 100 exception is DependencyNotFoundException) { | 131 exception is DependencyNotFoundException) { |
| 101 return exit_codes.UNAVAILABLE; | 132 return exit_codes.UNAVAILABLE; |
| 102 } else if (exception is FormatException || exception is DataException) { | 133 } else if (exception is FormatException || exception is DataException) { |
| 103 return exit_codes.DATA; | 134 return exit_codes.DATA; |
| 104 } else if (exception is UsageException) { | 135 } else if (exception is UsageException) { |
| 105 return exit_codes.USAGE; | 136 return exit_codes.USAGE; |
| 106 } else { | 137 } else { |
| 107 return 1; | 138 return 1; |
| 108 } | 139 } |
| 109 } | 140 } |
| 141 |
| 142 /// Walks the command tree and runs the selected pub command. |
| 110 Future invokeCommand(String cacheDir, ArgResults mainOptions) { | 143 Future invokeCommand(String cacheDir, ArgResults mainOptions) { |
| 111 final completer0 = new Completer(); | 144 final completer0 = new Completer(); |
| 112 scheduleMicrotask(() { | 145 scheduleMicrotask(() { |
| 113 try { | 146 try { |
| 114 var commands = PubCommand.mainCommands; | 147 var commands = PubCommand.mainCommands; |
| 115 var command; | 148 var command; |
| 116 var commandString = "pub"; | 149 var commandString = "pub"; |
| 117 var options = mainOptions; | 150 var options = mainOptions; |
| 118 break0(x2) { | 151 break0() { |
| 119 join0() { | 152 join0() { |
| 120 join1(x0) { | 153 join1() { |
| 121 completer0.complete(null); | 154 completer0.complete(); |
| 122 } | 155 } |
| 123 finally0(cont0, v0) { | 156 finally0(cont0) { |
| 124 command.cache.deleteTempDir(); | 157 command.cache.deleteTempDir(); |
| 125 cont0(v0); | 158 cont0(); |
| 126 } | 159 } |
| 127 catch0(e0) { | 160 catch0(e1, s1) { |
| 128 finally0(join1, null); | 161 finally0(() => completer0.completeError(e1, s1)); |
| 129 } | 162 } |
| 130 try { | 163 try { |
| 131 finally0((v1) { | 164 final v0 = command.run(cacheDir, mainOptions, options); |
| 132 completer0.complete(v1); | 165 finally0(() { |
| 133 }, command.run(cacheDir, mainOptions, options)); | 166 completer0.complete(v0); |
| 134 } catch (e1) { | 167 }); |
| 135 catch0(e1); | 168 } catch (e2, s2) { |
| 169 catch0(e2, s2); |
| 136 } | 170 } |
| 137 } | 171 } |
| 138 if (!command.takesArguments && options.rest.isNotEmpty) { | 172 if (!command.takesArguments && options.rest.isNotEmpty) { |
| 139 command.usageError( | 173 command.usageError( |
| 140 'Command "${options.name}" does not take any arguments.'); | 174 'Command "${options.name}" does not take any arguments.'); |
| 141 join0(); | 175 join0(); |
| 142 } else { | 176 } else { |
| 143 join0(); | 177 join0(); |
| 144 } | 178 } |
| 145 } | 179 } |
| 146 continue0(x3) { | 180 var trampoline0; |
| 181 continue0() { |
| 182 trampoline0 = null; |
| 147 if (commands.isNotEmpty) { | 183 if (commands.isNotEmpty) { |
| 148 Future.wait([]).then((x1) { | 184 join2() { |
| 149 join2() { | 185 options = options.command; |
| 150 options = options.command; | 186 command = commands[options.name]; |
| 151 command = commands[options.name]; | 187 commands = command.subcommands; |
| 152 commands = command.subcommands; | 188 commandString += " ${options.name}"; |
| 153 commandString += " ${options.name}"; | 189 join3() { |
| 154 join3() { | 190 trampoline0 = continue0; |
| 155 continue0(null); | 191 } |
| 192 if (options['help']) { |
| 193 command.printUsage(); |
| 194 completer0.complete(new Future.value()); |
| 195 } else { |
| 196 join3(); |
| 197 } |
| 198 } |
| 199 if (options.command == null) { |
| 200 join4() { |
| 201 join2(); |
| 202 } |
| 203 if (options.rest.isEmpty) { |
| 204 join5() { |
| 205 command.usageError( |
| 206 'Missing subcommand for "${commandString}".'); |
| 207 join4(); |
| 156 } | 208 } |
| 157 if (options['help']) { | 209 if (command == null) { |
| 158 command.printUsage(); | 210 PubCommand.printGlobalUsage(); |
| 159 completer0.complete(new Future.value()); | 211 completer0.complete(new Future.value()); |
| 160 } else { | 212 } else { |
| 161 join3(); | 213 join5(); |
| 214 } |
| 215 } else { |
| 216 join6() { |
| 217 command.usageError( |
| 218 'Could not find a subcommand named ' |
| 219 '"${options.rest[0]}" for "${commandString}".'); |
| 220 join4(); |
| 221 } |
| 222 if (command == null) { |
| 223 PubCommand.usageErrorWithCommands( |
| 224 commands, |
| 225 'Could not find a command named "${options.rest[0]}".'); |
| 226 join6(); |
| 227 } else { |
| 228 join6(); |
| 162 } | 229 } |
| 163 } | 230 } |
| 164 if (options.command == null) { | 231 } else { |
| 165 join4() { | 232 join2(); |
| 166 join2(); | 233 } |
| 167 } | |
| 168 if (options.rest.isEmpty) { | |
| 169 join5() { | |
| 170 command.usageError( | |
| 171 'Missing subcommand for "${commandString}".'); | |
| 172 join4(); | |
| 173 } | |
| 174 if (command == null) { | |
| 175 PubCommand.printGlobalUsage(); | |
| 176 completer0.complete(new Future.value()); | |
| 177 } else { | |
| 178 join5(); | |
| 179 } | |
| 180 } else { | |
| 181 join6() { | |
| 182 command.usageError( | |
| 183 'Could not find a subcommand named ' | |
| 184 '"${options.rest[0]}" for "${commandString}".'); | |
| 185 join4(); | |
| 186 } | |
| 187 if (command == null) { | |
| 188 PubCommand.usageErrorWithCommands( | |
| 189 commands, | |
| 190 'Could not find a command named "${options.rest[0]}".'); | |
| 191 join6(); | |
| 192 } else { | |
| 193 join6(); | |
| 194 } | |
| 195 } | |
| 196 } else { | |
| 197 join2(); | |
| 198 } | |
| 199 }); | |
| 200 } else { | 234 } else { |
| 201 break0(null); | 235 break0(); |
| 202 } | 236 } |
| 203 } | 237 } |
| 204 continue0(null); | 238 trampoline0 = continue0; |
| 205 } catch (e2) { | 239 do trampoline0(); while (trampoline0 != null); |
| 206 completer0.completeError(e2); | 240 } catch (e, s) { |
| 241 completer0.completeError(e, s); |
| 207 } | 242 } |
| 208 }); | 243 }); |
| 209 return completer0.future; | 244 return completer0.future; |
| 210 } | 245 } |
| 246 |
| 247 /// Checks that pub is running on a supported platform. |
| 248 /// |
| 249 /// If it isn't, it prints an error message and exits. Completes when the |
| 250 /// validation is done. |
| 211 Future validatePlatform() { | 251 Future validatePlatform() { |
| 212 final completer0 = new Completer(); | 252 final completer0 = new Completer(); |
| 213 scheduleMicrotask(() { | 253 scheduleMicrotask(() { |
| 214 try { | 254 try { |
| 215 join0() { | 255 join0() { |
| 216 runProcess('ver', []).then((x0) { | 256 runProcess('ver', []).then((x0) { |
| 217 try { | 257 try { |
| 218 var result = x0; | 258 var result = x0; |
| 219 join1() { | 259 join1() { |
| 220 completer0.complete(null); | 260 completer0.complete(); |
| 221 } | 261 } |
| 222 if (result.stdout.join('\n').contains('XP')) { | 262 if (result.stdout.join('\n').contains('XP')) { |
| 223 log.error('Sorry, but pub is not supported on Windows XP.'); | 263 log.error('Sorry, but pub is not supported on Windows XP.'); |
| 224 flushThenExit(exit_codes.USAGE).then((x1) { | 264 flushThenExit(exit_codes.USAGE).then((x1) { |
| 225 try { | 265 try { |
| 226 x1; | 266 x1; |
| 227 join1(); | 267 join1(); |
| 228 } catch (e1) { | 268 } catch (e0, s0) { |
| 229 completer0.completeError(e1); | 269 completer0.completeError(e0, s0); |
| 230 } | 270 } |
| 231 }, onError: (e2) { | 271 }, onError: completer0.completeError); |
| 232 completer0.completeError(e2); | |
| 233 }); | |
| 234 } else { | 272 } else { |
| 235 join1(); | 273 join1(); |
| 236 } | 274 } |
| 237 } catch (e0) { | 275 } catch (e1, s1) { |
| 238 completer0.completeError(e0); | 276 completer0.completeError(e1, s1); |
| 239 } | 277 } |
| 240 }, onError: (e3) { | 278 }, onError: completer0.completeError); |
| 241 completer0.completeError(e3); | |
| 242 }); | |
| 243 } | 279 } |
| 244 if (Platform.operatingSystem != 'windows') { | 280 if (Platform.operatingSystem != 'windows') { |
| 245 completer0.complete(null); | 281 completer0.complete(null); |
| 246 } else { | 282 } else { |
| 247 join0(); | 283 join0(); |
| 248 } | 284 } |
| 249 } catch (e4) { | 285 } catch (e, s) { |
| 250 completer0.completeError(e4); | 286 completer0.completeError(e, s); |
| 251 } | 287 } |
| 252 }); | 288 }); |
| 253 return completer0.future; | 289 return completer0.future; |
| 254 } | 290 } |
| OLD | NEW |