File size: 4,661 Bytes
a4ff84b b0336a8 a4ff84b b0336a8 a4ff84b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | // Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../dart/analysis.dart';
import 'analyze_base.dart';
class AnalyzeContinuously extends AnalyzeBase {
AnalyzeContinuously(
super.argResults,
List<Directory> repoPackages, {
required super.fileSystem,
required super.logger,
required super.terminal,
required super.platform,
required super.processManager,
required super.artifacts,
required super.suppressAnalytics,
}) : super(repoPackages: repoPackages);
String? analysisTarget;
bool firstAnalysis = true;
Set<String> analyzedPaths = <String>{};
Map<String, List<AnalysisError>> analysisErrors = <String, List<AnalysisError>>{};
final Stopwatch analysisTimer = Stopwatch();
int lastErrorCount = 0;
Status? analysisStatus;
@override
Future<void> analyze() async {
List<String> directories;
if (isFlutterRepo) {
final PackageDependencyTracker dependencies = PackageDependencyTracker();
dependencies.checkForConflictingDependencies(repoPackages, dependencies);
directories = <String>[flutterRoot];
analysisTarget = 'Flutter repository';
logger.printTrace('Analyzing Flutter repository:');
} else {
directories = <String>[fileSystem.currentDirectory.path];
analysisTarget = fileSystem.currentDirectory.path;
}
final AnalysisServer server = AnalysisServer(
sdkPath,
directories,
fileSystem: fileSystem,
logger: logger,
platform: platform,
processManager: processManager,
terminal: terminal,
protocolTrafficLog: protocolTrafficLog,
suppressAnalytics: suppressAnalytics,
);
server.onAnalyzing.listen((bool isAnalyzing) => _handleAnalysisStatus(server, isAnalyzing));
server.onErrors.listen(_handleAnalysisErrors);
await server.start();
final int? exitCode = await server.onExit;
final String message = 'Analysis server exited with code $exitCode.';
if (exitCode != 0) {
throwToolExit(message, exitCode: exitCode);
}
logger.printStatus(message);
if (server.didServerErrorOccur) {
throwToolExit('Server error(s) occurred.');
}
}
void _handleAnalysisStatus(AnalysisServer server, bool isAnalyzing) {
if (isAnalyzing) {
analysisStatus?.cancel();
if (!firstAnalysis) {
logger.printStatus('\n');
}
analysisStatus = logger.startProgress('Analyzing $analysisTarget...');
analyzedPaths.clear();
analysisTimer.start();
} else {
analysisStatus?.stop();
analysisStatus = null;
analysisTimer.stop();
logger.printStatus(terminal.clearScreen(), newline: false);
// Remove errors for deleted files, sort, and print errors.
final List<AnalysisError> sortedErrors = <AnalysisError>[];
final List<String> pathsToRemove = <String>[];
analysisErrors.forEach((String path, List<AnalysisError> errors) {
if (fileSystem.isFileSync(path)) {
sortedErrors.addAll(errors);
} else {
pathsToRemove.add(path);
}
});
analysisErrors.removeWhere((String path, _) => pathsToRemove.contains(path));
sortedErrors.sort();
for (final AnalysisError error in sortedErrors) {
logger.printStatus(error.toString());
logger.printTrace('error code: ${error.code}');
}
dumpErrors(sortedErrors.map<String>((AnalysisError error) => error.toLegacyString()));
final int issueCount = sortedErrors.length;
final int issueDiff = issueCount - lastErrorCount;
lastErrorCount = issueCount;
final String seconds = (analysisTimer.elapsedMilliseconds / 1000.0).toStringAsFixed(2);
final String errorsMessage = AnalyzeBase.generateErrorsMessage(
issueCount: issueCount,
issueDiff: issueDiff,
files: analyzedPaths.length,
seconds: seconds,
);
logger.printStatus(errorsMessage);
if (firstAnalysis && isBenchmarking) {
writeBenchmark(analysisTimer, issueCount);
server.dispose().whenComplete(() {
exit(issueCount > 0 ? 1 : 0);
});
}
firstAnalysis = false;
}
}
void _handleAnalysisErrors(FileAnalysisErrors fileErrors) {
fileErrors.errors.removeWhere((AnalysisError error) => error.type == 'TODO');
analyzedPaths.add(fileErrors.file);
analysisErrors[fileErrors.file] = fileErrors.errors;
}
}
|