OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library analyzer_cli.src.build_mode; | 5 library analyzer_cli.src.build_mode; |
6 | 6 |
7 import 'dart:core' hide Resource; | 7 import 'dart:core' hide Resource; |
8 import 'dart:io' as io; | 8 import 'dart:io' as io; |
9 | 9 |
10 import 'package:analyzer/dart/ast/ast.dart' show CompilationUnit; | 10 import 'package:analyzer/dart/ast/ast.dart' show CompilationUnit; |
(...skipping 13 matching lines...) Expand all Loading... |
24 import 'package:analyzer/src/summary/summarize_ast.dart'; | 24 import 'package:analyzer/src/summary/summarize_ast.dart'; |
25 import 'package:analyzer/src/summary/summarize_elements.dart'; | 25 import 'package:analyzer/src/summary/summarize_elements.dart'; |
26 import 'package:analyzer/task/dart.dart'; | 26 import 'package:analyzer/task/dart.dart'; |
27 import 'package:analyzer_cli/src/analyzer_impl.dart'; | 27 import 'package:analyzer_cli/src/analyzer_impl.dart'; |
28 import 'package:analyzer_cli/src/driver.dart'; | 28 import 'package:analyzer_cli/src/driver.dart'; |
29 import 'package:analyzer_cli/src/error_formatter.dart'; | 29 import 'package:analyzer_cli/src/error_formatter.dart'; |
30 import 'package:analyzer_cli/src/options.dart'; | 30 import 'package:analyzer_cli/src/options.dart'; |
31 import 'package:bazel_worker/bazel_worker.dart'; | 31 import 'package:bazel_worker/bazel_worker.dart'; |
32 | 32 |
33 /** | 33 /** |
| 34 * Persistent Bazel worker. |
| 35 */ |
| 36 class AnalyzerWorkerLoop extends SyncWorkerLoop { |
| 37 final StringBuffer errorBuffer = new StringBuffer(); |
| 38 final StringBuffer outBuffer = new StringBuffer(); |
| 39 |
| 40 final String dartSdkPath; |
| 41 |
| 42 AnalyzerWorkerLoop(SyncWorkerConnection connection, {this.dartSdkPath}) |
| 43 : super(connection: connection); |
| 44 |
| 45 factory AnalyzerWorkerLoop.std( |
| 46 {io.Stdin stdinStream, io.Stdout stdoutStream, String dartSdkPath}) { |
| 47 SyncWorkerConnection connection = new StdSyncWorkerConnection( |
| 48 stdinStream: stdinStream, stdoutStream: stdoutStream); |
| 49 return new AnalyzerWorkerLoop(connection, dartSdkPath: dartSdkPath); |
| 50 } |
| 51 |
| 52 /** |
| 53 * Performs analysis with given [options]. |
| 54 */ |
| 55 void analyze(CommandLineOptions options) { |
| 56 new BuildMode(options, new AnalysisStats()).analyze(); |
| 57 AnalysisEngine.instance.clearCaches(); |
| 58 } |
| 59 |
| 60 /** |
| 61 * Perform a single loop step. |
| 62 */ |
| 63 WorkResponse performRequest(WorkRequest request) { |
| 64 errorBuffer.clear(); |
| 65 outBuffer.clear(); |
| 66 try { |
| 67 // Add in the dart-sdk argument if `dartSdkPath` is not null, otherwise it |
| 68 // will try to find the currently installed sdk. |
| 69 var arguments = new List.from(request.arguments); |
| 70 if (dartSdkPath != null && |
| 71 !arguments.any((arg) => arg.startsWith('--dart-sdk'))) { |
| 72 arguments.add('--dart-sdk=$dartSdkPath'); |
| 73 } |
| 74 // Prepare options. |
| 75 CommandLineOptions options = |
| 76 CommandLineOptions.parse(arguments, (String msg) { |
| 77 throw new ArgumentError(msg); |
| 78 }); |
| 79 // Analyze and respond. |
| 80 analyze(options); |
| 81 String msg = _getErrorOutputBuffersText(); |
| 82 return new WorkResponse() |
| 83 ..exitCode = EXIT_CODE_OK |
| 84 ..output = msg; |
| 85 } catch (e, st) { |
| 86 String msg = _getErrorOutputBuffersText(); |
| 87 msg += '$e\n$st'; |
| 88 return new WorkResponse() |
| 89 ..exitCode = EXIT_CODE_ERROR |
| 90 ..output = msg; |
| 91 } |
| 92 } |
| 93 |
| 94 /** |
| 95 * Run the worker loop. |
| 96 */ |
| 97 @override |
| 98 void run() { |
| 99 errorSink = errorBuffer; |
| 100 outSink = outBuffer; |
| 101 exitHandler = (int exitCode) { |
| 102 return throw new StateError('Exit called: $exitCode'); |
| 103 }; |
| 104 super.run(); |
| 105 } |
| 106 |
| 107 String _getErrorOutputBuffersText() { |
| 108 String msg = ''; |
| 109 if (errorBuffer.isNotEmpty) { |
| 110 msg += errorBuffer.toString() + '\n'; |
| 111 } |
| 112 if (outBuffer.isNotEmpty) { |
| 113 msg += outBuffer.toString() + '\n'; |
| 114 } |
| 115 return msg; |
| 116 } |
| 117 } |
| 118 |
| 119 /** |
34 * Analyzer used when the "--build-mode" option is supplied. | 120 * Analyzer used when the "--build-mode" option is supplied. |
35 */ | 121 */ |
36 class BuildMode { | 122 class BuildMode { |
37 final CommandLineOptions options; | 123 final CommandLineOptions options; |
38 final AnalysisStats stats; | 124 final AnalysisStats stats; |
39 | 125 |
40 final ResourceProvider resourceProvider = PhysicalResourceProvider.INSTANCE; | 126 final ResourceProvider resourceProvider = PhysicalResourceProvider.INSTANCE; |
41 SummaryDataStore summaryDataStore; | 127 SummaryDataStore summaryDataStore; |
42 InternalAnalysisContext context; | 128 InternalAnalysisContext context; |
43 Map<Uri, JavaFile> uriToFileMap; | 129 Map<Uri, JavaFile> uriToFileMap; |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 'Illegal input file (must be "\$uri|\$path"): $sourceFile'); | 351 'Illegal input file (must be "\$uri|\$path"): $sourceFile'); |
266 return null; | 352 return null; |
267 } | 353 } |
268 Uri uri = Uri.parse(sourceFile.substring(0, pipeIndex)); | 354 Uri uri = Uri.parse(sourceFile.substring(0, pipeIndex)); |
269 String path = sourceFile.substring(pipeIndex + 1); | 355 String path = sourceFile.substring(pipeIndex + 1); |
270 uriToFileMap[uri] = new JavaFile(path); | 356 uriToFileMap[uri] = new JavaFile(path); |
271 } | 357 } |
272 return uriToFileMap; | 358 return uriToFileMap; |
273 } | 359 } |
274 } | 360 } |
275 | |
276 /** | |
277 * Persistent Bazel worker. | |
278 */ | |
279 class AnalyzerWorkerLoop extends SyncWorkerLoop { | |
280 final StringBuffer errorBuffer = new StringBuffer(); | |
281 final StringBuffer outBuffer = new StringBuffer(); | |
282 | |
283 final String dartSdkPath; | |
284 | |
285 AnalyzerWorkerLoop(SyncWorkerConnection connection, {this.dartSdkPath}) | |
286 : super(connection: connection); | |
287 | |
288 factory AnalyzerWorkerLoop.std( | |
289 {io.Stdin stdinStream, io.Stdout stdoutStream, String dartSdkPath}) { | |
290 SyncWorkerConnection connection = new StdSyncWorkerConnection( | |
291 stdinStream: stdinStream, stdoutStream: stdoutStream); | |
292 return new AnalyzerWorkerLoop(connection, dartSdkPath: dartSdkPath); | |
293 } | |
294 | |
295 /** | |
296 * Performs analysis with given [options]. | |
297 */ | |
298 void analyze(CommandLineOptions options) { | |
299 new BuildMode(options, new AnalysisStats()).analyze(); | |
300 AnalysisEngine.instance.clearCaches(); | |
301 } | |
302 | |
303 /** | |
304 * Perform a single loop step. | |
305 */ | |
306 WorkResponse performRequest(WorkRequest request) { | |
307 errorBuffer.clear(); | |
308 outBuffer.clear(); | |
309 try { | |
310 // Add in the dart-sdk argument if `dartSdkPath` is not null, otherwise it | |
311 // will try to find the currently installed sdk. | |
312 var arguments = new List.from(request.arguments); | |
313 if (dartSdkPath != null && | |
314 !arguments.any((arg) => arg.startsWith('--dart-sdk'))) { | |
315 arguments.add('--dart-sdk=$dartSdkPath'); | |
316 } | |
317 // Prepare options. | |
318 CommandLineOptions options = | |
319 CommandLineOptions.parse(arguments, (String msg) { | |
320 throw new ArgumentError(msg); | |
321 }); | |
322 // Analyze and respond. | |
323 analyze(options); | |
324 String msg = _getErrorOutputBuffersText(); | |
325 return new WorkResponse() | |
326 ..exitCode = EXIT_CODE_OK | |
327 ..output = msg; | |
328 } catch (e, st) { | |
329 String msg = _getErrorOutputBuffersText(); | |
330 msg += '$e\n$st'; | |
331 return new WorkResponse() | |
332 ..exitCode = EXIT_CODE_ERROR | |
333 ..output = msg; | |
334 } | |
335 } | |
336 | |
337 /** | |
338 * Run the worker loop. | |
339 */ | |
340 @override | |
341 void run() { | |
342 errorSink = errorBuffer; | |
343 outSink = outBuffer; | |
344 exitHandler = (int exitCode) { | |
345 return throw new StateError('Exit called: $exitCode'); | |
346 }; | |
347 super.run(); | |
348 } | |
349 | |
350 String _getErrorOutputBuffersText() { | |
351 String msg = ''; | |
352 if (errorBuffer.isNotEmpty) { | |
353 msg += errorBuffer.toString() + '\n'; | |
354 } | |
355 if (outBuffer.isNotEmpty) { | |
356 msg += outBuffer.toString() + '\n'; | |
357 } | |
358 return msg; | |
359 } | |
360 } | |
OLD | NEW |