From 2cdda9e6797a973f7fa07650a3ce655321553407 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Mon, 13 Jun 2016 13:29:10 -0700 Subject: [PATCH] Provide a way to save the analyzer results to disk (#4541) --- .../lib/src/commands/analyze.dart | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/analyze.dart b/packages/flutter_tools/lib/src/commands/analyze.dart index 6823967a0a..1f5ca0dd2e 100644 --- a/packages/flutter_tools/lib/src/commands/analyze.dart +++ b/packages/flutter_tools/lib/src/commands/analyze.dart @@ -31,6 +31,7 @@ class AnalyzeCommand extends FlutterCommand { argParser.addFlag('preamble', help: 'Display the number of files that will be analyzed.', defaultsTo: true); argParser.addFlag('congratulate', help: 'Show output even when there are no errors, warnings, hints, or lints.', defaultsTo: true); argParser.addFlag('watch', help: 'Run analysis continuously, watching the filesystem for changes.', negatable: false); + argParser.addOption('write', help: 'Also output the results to a file. This is useful with --watch if you want a file to always contain the latest results.'); argParser.addOption('dart-sdk', help: 'The path to the Dart SDK.', hide: true); // Hidden option to enable a benchmarking mode. @@ -202,7 +203,7 @@ class AnalyzeCommand extends FlutterCommand { options.analysisOptionsFile = path.join(Cache.flutterRoot, 'packages', 'flutter_tools', 'flutter_analysis_options'); AnalysisDriver analyzer = new AnalysisDriver(options); - //TODO (pq): consider error handling + // TODO(pq): consider error handling List errors = analyzer.analyze(dartFiles); int errorCount = 0; @@ -221,7 +222,7 @@ class AnalyzeCommand extends FlutterCommand { shouldIgnore = true; } } - //TODO(ianh): Fix the Dart mojom compiler + // TODO(ianh): Fix the Dart mojom compiler if (error.source.fullName.endsWith('.mojom.dart')) shouldIgnore = true; if (shouldIgnore) @@ -229,6 +230,7 @@ class AnalyzeCommand extends FlutterCommand { printError(error.asString()); errorCount += 1; } + _dumpErrors(errors.map/**/((AnalysisErrorDescription error) => error.asString())); stopwatch.stop(); String elapsed = (stopwatch.elapsedMilliseconds / 1000.0).toStringAsFixed(1); @@ -253,6 +255,22 @@ class AnalyzeCommand extends FlutterCommand { return 0; } + void _dumpErrors(Iterable errors) { + if (argResults['write'] != null) { + try { + final RandomAccessFile resultsFile = new File(argResults['write']).openSync(mode: FileMode.WRITE); + try { + resultsFile.lockSync(); + resultsFile.writeStringSync(errors.join('\n')); + } finally { + resultsFile.close(); + } + } catch (e) { + printError('Failed to save output to "${argResults['write']}": $e'); + } + } + } + List _collectDartFiles(Directory dir, List collected, {FileFilter exclude}) { // Bail out in case of a .dartignore. if (FileSystemEntity.isFileSync(path.join(path.dirname(dir.path), '.dartignore'))) @@ -337,6 +355,8 @@ class AnalyzeCommand extends FlutterCommand { printTrace('error code: ${error.code}'); } + _dumpErrors(errors.map/**/((AnalysisError error) => error.toLegacyString())); + // Print an analysis summary. String errorsMessage; @@ -651,4 +671,8 @@ class AnalysisError implements Comparable { String relativePath = path.relative(file); return '${severity.toLowerCase().padLeft(7)} • $message • $relativePath:$startLine:$startColumn'; } + + String toLegacyString() { + return '[${severity.toLowerCase()}] $message ($file:$startLine:$startColumn)'; + } }