OLD | NEW |
| (Empty) |
1 #!/usr/bin/env dart | |
2 | |
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 | |
5 // BSD-style license that can be found in the LICENSE file. | |
6 | |
7 /** The entry point for the analyzer. */ | |
8 library analyzer; | |
9 | |
10 import 'dart:async'; | |
11 import 'dart:convert'; | |
12 import 'dart:io'; | |
13 | |
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/java_core.dart' show JavaSys
tem; | |
17 import 'package:analyzer_experimental/options.dart'; | |
18 | |
19 import 'package:analyzer_experimental/src/analyzer_impl.dart'; | |
20 import 'package:analyzer_experimental/src/error_formatter.dart'; | |
21 | |
22 void main(args) { | |
23 var options = CommandLineOptions.parse(args); | |
24 if (options.shouldBatch) { | |
25 BatchRunner.runAsBatch(args, (List<String> args) { | |
26 var options = CommandLineOptions.parse(args); | |
27 return _runAnalyzer(options); | |
28 }); | |
29 } else { | |
30 int startTime = JavaSystem.currentTimeMillis(); | |
31 | |
32 ErrorSeverity result = _runAnalyzer(options); | |
33 | |
34 if (options.perf) { | |
35 int totalTime = JavaSystem.currentTimeMillis() - startTime; | |
36 print("scan:${PerformanceStatistics.scan.result}"); | |
37 print("parse:${PerformanceStatistics.parse.result}"); | |
38 print("resolve:${PerformanceStatistics.resolve.result}"); | |
39 print("errors:${PerformanceStatistics.errors.result}"); | |
40 print("hints:${PerformanceStatistics.hints.result}"); | |
41 print("total:$totalTime"); | |
42 } | |
43 | |
44 exit(result.ordinal); | |
45 } | |
46 } | |
47 | |
48 ErrorSeverity _runAnalyzer(CommandLineOptions options) { | |
49 if (!options.machineFormat) { | |
50 stdout.writeln("Analyzing ${options.sourceFiles}..."); | |
51 } | |
52 ErrorSeverity allResult = ErrorSeverity.NONE; | |
53 String sourcePath = options.sourceFiles[0]; | |
54 sourcePath = sourcePath.trim(); | |
55 // check that file exists | |
56 if (!new File(sourcePath).existsSync()) { | |
57 print('File not found: $sourcePath'); | |
58 return ErrorSeverity.ERROR; | |
59 } | |
60 // check that file is Dart file | |
61 if (!AnalysisEngine.isDartFileName(sourcePath)) { | |
62 print('$sourcePath is not a Dart file'); | |
63 return ErrorSeverity.ERROR; | |
64 } | |
65 // do analyze | |
66 ErrorFormatter formatter = new ErrorFormatter(options.machineFormat ? stderr :
stdout, options); | |
67 AnalyzerImpl analyzer = new AnalyzerImpl(options); | |
68 analyzer.analyze(sourcePath); | |
69 // print errors | |
70 formatter.formatErrors(analyzer.errorInfos); | |
71 // prepare status | |
72 ErrorSeverity status = analyzer.maxErrorSeverity; | |
73 if (status == ErrorSeverity.WARNING && options.warningsAreFatal) { | |
74 status = ErrorSeverity.ERROR; | |
75 } | |
76 return status; | |
77 } | |
78 | |
79 typedef ErrorSeverity BatchRunnerHandler(List<String> args); | |
80 | |
81 /// Provides a framework to read command line options from stdin and feed them t
o a callback. | |
82 class BatchRunner { | |
83 /** | |
84 * Run the tool in 'batch' mode, receiving command lines through stdin and ret
urning pass/fail | |
85 * status through stdout. This feature is intended for use in unit testing. | |
86 */ | |
87 static ErrorSeverity runAsBatch(List<String> sharedArgs, BatchRunnerHandler ha
ndler) { | |
88 stdout.writeln('>>> BATCH START'); | |
89 Stopwatch stopwatch = new Stopwatch(); | |
90 stopwatch.start(); | |
91 int testsFailed = 0; | |
92 int totalTests = 0; | |
93 ErrorSeverity batchResult = ErrorSeverity.NONE; | |
94 // read line from stdin | |
95 Stream cmdLine = stdin | |
96 .transform(UTF8.decoder) | |
97 .transform(new LineSplitter()); | |
98 var subscription = cmdLine.listen((String line) { | |
99 // may be finish | |
100 if (line.isEmpty) { | |
101 var time = stopwatch.elapsedMilliseconds; | |
102 stdout.writeln('>>> BATCH END (${totalTests - testsFailed}/$totalTests)
${time}ms'); | |
103 exit(batchResult.ordinal); | |
104 } | |
105 // prepare aruments | |
106 var args; | |
107 { | |
108 var lineArgs = line.split(new RegExp('\\s+')); | |
109 args = new List<String>(); | |
110 args.addAll(sharedArgs); | |
111 args.addAll(lineArgs); | |
112 args.remove('-b'); | |
113 args.remove('--batch'); | |
114 // TODO(scheglov) https://code.google.com/p/dart/issues/detail?id=11061 | |
115 args.remove('-batch'); | |
116 } | |
117 // analyze single set of arguments | |
118 try { | |
119 totalTests++; | |
120 ErrorSeverity result = handler(args); | |
121 bool resultPass = result != ErrorSeverity.ERROR; | |
122 if (!resultPass) { | |
123 testsFailed++; | |
124 } | |
125 batchResult = batchResult.max(result); | |
126 // Write stderr end token and flush. | |
127 stderr.writeln('>>> EOF STDERR'); | |
128 String resultPassString = resultPass ? 'PASS' : 'FAIL'; | |
129 stdout.writeln('>>> TEST $resultPassString ${stopwatch.elapsedMillisecon
ds}ms'); | |
130 } catch (e, stackTrace) { | |
131 stderr.writeln(e); | |
132 stderr.writeln(stackTrace); | |
133 stderr.writeln('>>> EOF STDERR'); | |
134 stdout.writeln('>>> TEST CRASH'); | |
135 } | |
136 }); | |
137 } | |
138 } | |
OLD | NEW |