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