OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017, 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 import 'dart:async'; | |
5 import 'dart:io'; | |
6 | |
7 import 'package:args/args.dart'; | |
8 import 'package:bazel_worker/bazel_worker.dart'; | |
9 import 'package:front_end/front_end.dart' hide FileSystemException; | |
10 import 'package:front_end/src/fasta/command_line_reporting.dart'; | |
11 import 'package:kernel/target/targets.dart'; | |
12 | |
13 main(List<String> args) async { | |
Siggi Cherem (dart-lang)
2017/08/21 20:16:49
I'm still contemplating whether to put this here o
jakemac
2017/08/21 21:16:37
I don't feel that strongly, but analyzer/ddc both
| |
14 args = preprocessArgs(args); | |
15 | |
16 if (args.contains('--persistent_worker')) { | |
17 if (args.length != 1) { | |
18 throw new StateError( | |
19 "unexpected args, expected only --persistent-worker but got: $args"); | |
20 } | |
21 await new SummaryWorker().run(); | |
22 } else { | |
23 await computeSummary(args); | |
24 } | |
25 } | |
26 | |
27 /// A bazel worker loop that can compute summaries. | |
28 class SummaryWorker extends AsyncWorkerLoop { | |
29 Future<WorkResponse> performRequest(WorkRequest request) async { | |
30 var outputBuffer = new StringBuffer(); | |
31 var response = new WorkResponse()..exitCode = 0; | |
32 try { | |
33 await computeSummary(request.arguments, | |
34 isWorker: true, outputBuffer: outputBuffer); | |
35 } catch (_, s) { | |
36 outputBuffer.writeln(s); | |
37 response.exitCode = 15; | |
38 } | |
39 response.output = outputBuffer.toString(); | |
40 return response; | |
41 } | |
42 } | |
43 | |
44 /// If the last arg starts with `@`, this reads the file it points to and treats | |
45 /// each line as an additional arg. | |
46 /// | |
47 /// This is how individual work request args are differentiated from startup | |
48 /// args in bazel (inidividual work request args go in that file). | |
49 List<String> preprocessArgs(List<String> args) { | |
50 args = new List.from(args); | |
51 if (args.isEmpty) { | |
52 return args; | |
53 } | |
54 String lastArg = args.last; | |
55 if (lastArg.startsWith('@')) { | |
56 File argsFile = new File(lastArg.substring(1)); | |
57 try { | |
58 args.removeLast(); | |
59 args.addAll(argsFile.readAsLinesSync()); | |
60 } on FileSystemException catch (e) { | |
61 throw new Exception('Failed to read file specified by $lastArg : $e'); | |
62 } | |
63 } | |
64 return args; | |
65 } | |
66 | |
67 /// An [ArgParser] for generating kernel summaries. | |
68 final summaryArgsParser = new ArgParser() | |
69 ..addOption('dart-sdk-summary') | |
70 ..addOption('input-summary', allowMultiple: true) | |
71 ..addOption('multi-root', allowMultiple: true) | |
72 ..addOption('packages-file') | |
73 ..addOption('source', allowMultiple: true) | |
74 ..addOption('output'); | |
75 | |
76 /// Computes a kernel summary based on [args]. | |
77 /// | |
78 /// If [isWorker] is true then exit codes will not be set on failure. | |
79 /// | |
80 /// If [outputBuffer] is provided then messages will be written to that buffer | |
81 /// instead of printed to the console. | |
82 Future computeSummary(List<String> args, | |
83 {bool isWorker: false, StringBuffer outputBuffer}) async { | |
84 var parsedArgs = summaryArgsParser.parse(args); | |
85 var options = new CompilerOptions() | |
86 ..packagesFileUri = Uri.parse(parsedArgs['packages-file']) | |
87 ..inputSummaries = parsedArgs['input-summary'].map(Uri.parse).toList() | |
88 ..sdkSummary = Uri.parse(parsedArgs['dart-sdk-summary']) | |
89 ..multiRoots = parsedArgs['multi-root'].map(Uri.parse).toList() | |
90 ..target = new NoneTarget(new TargetFlags()); | |
91 | |
92 if (isWorker) { | |
93 options.setExitCodeOnProblem = false; | |
Siggi Cherem (dart-lang)
2017/08/21 20:16:49
this is already false by default :)
In your case
jakemac
2017/08/21 21:16:37
done
| |
94 } | |
95 | |
96 if (outputBuffer != null) { | |
97 options.onError = (CompilationMessage error) { | |
98 var severityString = severityName(error.severity, capitalized: true); | |
99 outputBuffer.writeln('$severityString: ${error.message}'); | |
100 if (error.severity != Severity.nit) { | |
101 throw error; | |
Siggi Cherem (dart-lang)
2017/08/21 20:16:49
`throw` might not be the most user-friendly respon
jakemac
2017/08/21 21:16:37
The bazel worker handles errors and sends a proper
| |
102 } | |
103 }; | |
104 } else { | |
105 options.throwOnWarnings = true; | |
Siggi Cherem (dart-lang)
2017/08/21 20:16:49
our default error reporting throws and prints to t
jakemac
2017/08/21 21:16:37
I think the default handling is fine
| |
106 } | |
107 | |
108 var sources = parsedArgs['source'].map(Uri.parse).toList(); | |
Siggi Cherem (dart-lang)
2017/08/21 20:16:49
we intend to fix this, but we've had issues in the
jakemac
2017/08/21 21:16:37
These will be real uris with schemes yes
| |
109 var program = await summaryFor(sources, options); | |
110 | |
111 var outputFile = new File(parsedArgs['output']); | |
112 outputFile.createSync(recursive: true); | |
113 outputFile.writeAsBytesSync(program); | |
114 } | |
OLD | NEW |