| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 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 | 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 import 'dart:async'; | 4 import 'dart:async'; |
| 5 import 'dart:io'; | 5 import 'dart:io'; |
| 6 | 6 |
| 7 import 'package:args/args.dart'; | 7 import 'package:args/args.dart'; |
| 8 import 'package:bazel_worker/bazel_worker.dart'; | 8 import 'package:bazel_worker/bazel_worker.dart'; |
| 9 import 'package:front_end/front_end.dart' hide FileSystemException; | 9 import 'package:front_end/front_end.dart' hide FileSystemException; |
| 10 import 'package:front_end/src/fasta/command_line_reporting.dart'; | 10 import 'package:front_end/src/fasta/command_line_reporting.dart'; |
| 11 import 'package:kernel/target/targets.dart'; | 11 import 'package:kernel/target/targets.dart'; |
| 12 | 12 |
| 13 main(List<String> args) async { | 13 main(List<String> args) async { |
| 14 args = preprocessArgs(args); | 14 args = preprocessArgs(args); |
| 15 | 15 |
| 16 if (args.contains('--persistent_worker')) { | 16 if (args.contains('--persistent_worker')) { |
| 17 if (args.length != 1) { | 17 if (args.length != 1) { |
| 18 throw new StateError( | 18 throw new StateError( |
| 19 "unexpected args, expected only --persistent-worker but got: $args"); | 19 "unexpected args, expected only --persistent-worker but got: $args"); |
| 20 } | 20 } |
| 21 await new SummaryWorker().run(); | 21 await new SummaryWorker().run(); |
| 22 } else { | 22 } else { |
| 23 await computeSummary(args); | 23 var succeeded = await computeSummary(args); |
| 24 if (!succeeded) { |
| 25 exitCode = 15; |
| 26 } |
| 24 } | 27 } |
| 25 } | 28 } |
| 26 | 29 |
| 27 /// A bazel worker loop that can compute summaries. | 30 /// A bazel worker loop that can compute summaries. |
| 28 class SummaryWorker extends AsyncWorkerLoop { | 31 class SummaryWorker extends AsyncWorkerLoop { |
| 29 Future<WorkResponse> performRequest(WorkRequest request) async { | 32 Future<WorkResponse> performRequest(WorkRequest request) async { |
| 30 var outputBuffer = new StringBuffer(); | 33 var outputBuffer = new StringBuffer(); |
| 31 var response = new WorkResponse()..exitCode = 0; | 34 var response = new WorkResponse()..exitCode = 0; |
| 32 try { | 35 try { |
| 33 await computeSummary(request.arguments, | 36 var succeeded = await computeSummary(request.arguments, |
| 34 isWorker: true, outputBuffer: outputBuffer); | 37 isWorker: true, outputBuffer: outputBuffer); |
| 35 } catch (_, s) { | 38 if (!succeeded) { |
| 39 response.exitCode = 15; |
| 40 } |
| 41 } catch (e, s) { |
| 42 outputBuffer.writeln(e); |
| 36 outputBuffer.writeln(s); | 43 outputBuffer.writeln(s); |
| 37 response.exitCode = 15; | 44 response.exitCode = 15; |
| 38 } | 45 } |
| 39 response.output = outputBuffer.toString(); | 46 response.output = outputBuffer.toString(); |
| 40 return response; | 47 return response; |
| 41 } | 48 } |
| 42 } | 49 } |
| 43 | 50 |
| 44 /// If the last arg starts with `@`, this reads the file it points to and treats | 51 /// If the last arg starts with `@`, this reads the file it points to and treats |
| 45 /// each line as an additional arg. | 52 /// each line as an additional arg. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 72 ..addOption('packages-file') | 79 ..addOption('packages-file') |
| 73 ..addOption('source', allowMultiple: true) | 80 ..addOption('source', allowMultiple: true) |
| 74 ..addOption('output'); | 81 ..addOption('output'); |
| 75 | 82 |
| 76 /// Computes a kernel summary based on [args]. | 83 /// Computes a kernel summary based on [args]. |
| 77 /// | 84 /// |
| 78 /// If [isWorker] is true then exit codes will not be set on failure. | 85 /// If [isWorker] is true then exit codes will not be set on failure. |
| 79 /// | 86 /// |
| 80 /// If [outputBuffer] is provided then messages will be written to that buffer | 87 /// If [outputBuffer] is provided then messages will be written to that buffer |
| 81 /// instead of printed to the console. | 88 /// instead of printed to the console. |
| 82 Future computeSummary(List<String> args, | 89 /// |
| 90 /// Returns whether or not the summary was successfully output. |
| 91 Future<bool> computeSummary(List<String> args, |
| 83 {bool isWorker: false, StringBuffer outputBuffer}) async { | 92 {bool isWorker: false, StringBuffer outputBuffer}) async { |
| 93 bool succeeded = true; |
| 84 var parsedArgs = summaryArgsParser.parse(args); | 94 var parsedArgs = summaryArgsParser.parse(args); |
| 85 var options = new CompilerOptions() | 95 var options = new CompilerOptions() |
| 86 ..packagesFileUri = Uri.parse(parsedArgs['packages-file']) | 96 ..packagesFileUri = Uri.parse(parsedArgs['packages-file']) |
| 87 ..inputSummaries = parsedArgs['input-summary'].map(Uri.parse).toList() | 97 ..inputSummaries = parsedArgs['input-summary'].map(Uri.parse).toList() |
| 88 ..sdkSummary = Uri.parse(parsedArgs['dart-sdk-summary']) | 98 ..sdkSummary = Uri.parse(parsedArgs['dart-sdk-summary']) |
| 89 ..multiRoots = parsedArgs['multi-root'].map(Uri.parse).toList() | 99 ..multiRoots = parsedArgs['multi-root'].map(Uri.parse).toList() |
| 90 ..target = new NoneTarget(new TargetFlags()); | 100 ..target = new NoneTarget(new TargetFlags()); |
| 91 | 101 |
| 92 if (outputBuffer != null) { | 102 options.onError = (CompilationMessage error) { |
| 93 options.onError = (CompilationMessage error) { | 103 var message = new StringBuffer() |
| 94 var severityString = severityName(error.severity, capitalized: true); | 104 ..write(severityName(error.severity, capitalized: true)) |
| 95 outputBuffer.writeln('$severityString: ${error.message}'); | 105 ..write(': '); |
| 96 if (error.severity != Severity.nit) { | 106 if (error.span != null) { |
| 97 throw error; | 107 message.writeln(error.span.message(error.message)); |
| 98 } | 108 } else { |
| 99 }; | 109 message.writeln(error.message); |
| 100 } else { | 110 } |
| 101 options.throwOnWarningsForDebugging = true; | 111 if (error.tip != null) { |
| 102 } | 112 message.writeln(error.tip); |
| 113 } |
| 114 if (outputBuffer != null) { |
| 115 outputBuffer.writeln(message); |
| 116 } else { |
| 117 print(message); |
| 118 } |
| 119 if (error.severity != Severity.nit) { |
| 120 succeeded = false; |
| 121 } |
| 122 }; |
| 103 | 123 |
| 104 var sources = parsedArgs['source'].map(Uri.parse).toList(); | 124 var sources = parsedArgs['source'].map(Uri.parse).toList(); |
| 105 var program = await summaryFor(sources, options); | 125 var program = await summaryFor(sources, options); |
| 106 | 126 |
| 107 var outputFile = new File(parsedArgs['output']); | 127 var outputFile = new File(parsedArgs['output']); |
| 108 outputFile.createSync(recursive: true); | 128 outputFile.createSync(recursive: true); |
| 109 outputFile.writeAsBytesSync(program); | 129 outputFile.writeAsBytesSync(program); |
| 130 |
| 131 return succeeded; |
| 110 } | 132 } |
| OLD | NEW |