| OLD | NEW |
| 1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
| 2 | 2 |
| 3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 3 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 4 // for details. All rights reserved. Use of this source code is governed by a | 4 // for details. All rights reserved. Use of this source code is governed by a |
| 5 // BSD-style license that can be found in the LICENSE file. | 5 // BSD-style license that can be found in the LICENSE file. |
| 6 | 6 |
| 7 /** The entry point for the analyzer. */ | 7 /** The entry point for the analyzer. */ |
| 8 library analyzer; | 8 library analyzer; |
| 9 | 9 |
| 10 import 'dart:async'; | 10 import 'dart:async'; |
| 11 import 'dart:io'; | 11 import 'dart:io'; |
| 12 | 12 |
| 13 import 'package:analyzer_experimental/src/generated/java_io.dart'; |
| 14 import 'package:analyzer_experimental/src/generated/engine.dart'; |
| 15 import 'package:analyzer_experimental/src/generated/error.dart'; |
| 16 import 'package:analyzer_experimental/src/generated/source_io.dart'; |
| 17 import 'package:analyzer_experimental/src/generated/sdk.dart'; |
| 18 import 'package:analyzer_experimental/src/generated/sdk_io.dart'; |
| 19 import 'package:analyzer_experimental/src/generated/ast.dart'; |
| 20 import 'package:analyzer_experimental/src/generated/element.dart'; |
| 13 import 'package:analyzer_experimental/options.dart'; | 21 import 'package:analyzer_experimental/options.dart'; |
| 14 | 22 |
| 15 // Exit status codes. | 23 part 'package:analyzer_experimental/analyzer.dart'; |
| 16 const OK_EXIT = 0; | 24 part 'package:analyzer_experimental/error_formatter.dart'; |
| 17 const ERROR_EXIT = 1; | |
| 18 | 25 |
| 19 void main() { | 26 void main() { |
| 20 run(new Options().arguments).then((result) { | 27 var args = new Options().arguments; |
| 21 exit(result.error ? ERROR_EXIT : OK_EXIT); | 28 var options = CommandLineOptions.parse(args); |
| 22 }); | 29 if (options.shouldBatch) { |
| 30 BatchRunner.runAsBatch(args, (List<String> args) { |
| 31 var options = CommandLineOptions.parse(args); |
| 32 return _runAnalyzer(options); |
| 33 }); |
| 34 } else { |
| 35 ErrorSeverity result = _runAnalyzer(options); |
| 36 exit(result.ordinal); |
| 37 } |
| 23 } | 38 } |
| 24 | 39 |
| 25 /** The result of an analysis. */ | 40 ErrorSeverity _runAnalyzer(CommandLineOptions options) { |
| 26 class AnalysisResult { | 41 for (String sourcePath in options.sourceFiles) { |
| 27 final bool error; | 42 sourcePath = sourcePath.trim(); |
| 28 AnalysisResult.forFailure() : error = true; | 43 // check that file exists |
| 29 AnalysisResult.forSuccess() : error = false; | 44 if (!new File(sourcePath).existsSync()) { |
| 45 print('File not found: $sourcePath'); |
| 46 return ErrorSeverity.ERROR; |
| 47 } |
| 48 // check that file is Dart file |
| 49 if (!AnalysisEngine.isDartFileName(sourcePath)) { |
| 50 print('$sourcePath is not a Dart file'); |
| 51 return ErrorSeverity.ERROR; |
| 52 } |
| 53 // start analysis |
| 54 _ErrorFormatter formatter = new _ErrorFormatter(options.machineFormat ? stde
rr : stdout, options); |
| 55 formatter.startAnalysis(); |
| 56 // do analyze |
| 57 _AnalyzerImpl analyzer = new _AnalyzerImpl(options); |
| 58 analyzer.analyze(sourcePath); |
| 59 // pring errors |
| 60 formatter.formatErrors(analyzer.errorInfos); |
| 61 // prepare status |
| 62 ErrorSeverity status = analyzer.maxErrorSeverity; |
| 63 if (status == ErrorSeverity.WARNING && options.warningsAreFatal) { |
| 64 status = ErrorSeverity.ERROR; |
| 65 } |
| 66 return status; |
| 67 } |
| 30 } | 68 } |
| 31 | 69 |
| 70 typedef ErrorSeverity BatchRunnerHandler(List<String> args); |
| 32 | 71 |
| 33 /** | 72 /// Provides a framework to read command line options from stdin and feed them t
o a callback. |
| 34 * Runs the dart analyzer with the command-line options in [args]. | 73 class BatchRunner { |
| 35 * See [CommandLineOptions] for a list of valid arguments. | 74 /** |
| 36 */ | 75 * Run the tool in 'batch' mode, receiving command lines through stdin and ret
urning pass/fail |
| 37 Future<AnalysisResult> run(List<String> args) { | 76 * status through stdout. This feature is intended for use in unit testing. |
| 38 | 77 */ |
| 39 var options = new CommandLineOptions.parse(args); | 78 static ErrorSeverity runAsBatch(List<String> sharedArgs, BatchRunnerHandler ha
ndler) { |
| 40 if (options == null) { | 79 stdout.writeln('>>> BATCH START'); |
| 41 return new Future.value(new AnalysisResult.forFailure()); | 80 Stopwatch stopwatch = new Stopwatch(); |
| 81 stopwatch.start(); |
| 82 int testsFailed = 0; |
| 83 int totalTests = 0; |
| 84 ErrorSeverity batchResult = ErrorSeverity.NONE; |
| 85 // read line from stdin |
| 86 Stream cmdLine = stdin |
| 87 .transform(new StringDecoder()) |
| 88 .transform(new LineTransformer()); |
| 89 var subscription = cmdLine.listen((String line) { |
| 90 // may be finish |
| 91 if (line.isEmpty) { |
| 92 stdout.writeln('>>> BATCH END (${totalTests - testsFailed}/$totalTests)
${stopwatch.elapsedMilliseconds}ms'); |
| 93 exit(batchResult.ordinal); |
| 94 } |
| 95 // prepare aruments |
| 96 var args; |
| 97 { |
| 98 var lineArgs = line.split(new RegExp('\\s+')); |
| 99 args = new List<String>(); |
| 100 args.addAll(sharedArgs); |
| 101 args.addAll(lineArgs); |
| 102 args.remove('-b'); |
| 103 args.remove('--batch'); |
| 104 } |
| 105 // analyze single set of arguments |
| 106 try { |
| 107 totalTests++; |
| 108 ErrorSeverity result = handler(args); |
| 109 bool resultPass = result != ErrorSeverity.ERROR; |
| 110 if (!resultPass) { |
| 111 testsFailed++; |
| 112 } |
| 113 batchResult = batchResult.max(result); |
| 114 // Write stderr end token and flush. |
| 115 stderr.writeln('>>> EOF STDERR'); |
| 116 String resultPassString = resultPass ? 'PASS' : 'FAIL'; |
| 117 stdout.writeln('>>> TEST $resultPassString ${stopwatch.elapsedMillisecon
ds}ms'); |
| 118 } catch (e, stackTrace) { |
| 119 stderr.writeln(e); |
| 120 stderr.writeln(stackTrace); |
| 121 stderr.writeln('>>> EOF STDERR'); |
| 122 stdout.writeln('>>> TEST CRASH'); |
| 123 } |
| 124 }); |
| 42 } | 125 } |
| 43 | 126 } |
| 44 //TODO(pquitslund): call out to analyzer... | |
| 45 | |
| 46 } | |
| OLD | NEW |