This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 265
/
analyze_command.dart
199 lines (174 loc) · 5.83 KB
/
analyze_command.dart
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// ignore_for_file: public_member_api_docs
import 'dart:io';
import '../../analyzers/lint_analyzer/lint_analyzer.dart';
import '../../analyzers/lint_analyzer/metrics/metrics_factory.dart';
import '../../analyzers/lint_analyzer/metrics/models/metric_value_level.dart';
import '../../analyzers/lint_analyzer/models/lint_file_report.dart';
import '../../analyzers/lint_analyzer/models/severity.dart';
import '../../analyzers/lint_analyzer/reporters/lint_report_params.dart';
import '../../analyzers/lint_analyzer/reporters/reporters_list/json/lint_json_reporter.dart';
import '../../analyzers/lint_analyzer/utils/report_utils.dart';
import '../../config_builder/config_builder.dart';
import '../../logger/logger.dart';
import '../models/flag_names.dart';
import '../models/parsed_arguments.dart';
import 'base_command.dart';
class AnalyzeCommand extends BaseCommand {
final LintAnalyzer _analyzer;
final Logger _logger;
@override
String get name => 'analyze';
@override
String get description =>
'Collect code metrics, rules and anti-patterns violations.';
@override
String get invocation =>
'${runner?.executableName} $name [arguments] <directories>';
AnalyzeCommand(this._logger) : _analyzer = LintAnalyzer(_logger) {
_addFlags();
}
@override
Future<void> runCommand() async {
_logger
..isSilent = isNoCongratulate
..isVerbose = isVerbose
..progress.start('Analyzing');
final parsedArgs = ParsedArguments.fromArgs(argResults);
final config = ConfigBuilder.getLintConfigFromArgs(parsedArgs);
final lintAnalyzerResult = await _analyzer.runCliAnalysis(
argResults.rest,
parsedArgs.rootFolder,
config,
sdkPath: findSdkPath(),
);
_logger.progress.complete('Analysis is completed. Preparing the results:');
final jsonReportPath = parsedArgs.jsonReportPath;
if (jsonReportPath != null) {
final jsonReporter =
LintJsonReporter.toFile(jsonReportPath, parsedArgs.rootFolder);
await jsonReporter.report(
lintAnalyzerResult,
additionalParams: LintReportParams(
congratulate: true,
summary: _analyzer.getSummary(lintAnalyzerResult),
),
);
}
await _analyzer
.getReporter(
name: argResults[FlagNames.reporter] as String,
output: stdout,
reportFolder: argResults[FlagNames.reportFolder] as String,
)
?.report(
lintAnalyzerResult,
additionalParams: LintReportParams(
congratulate: !isNoCongratulate,
summary: _analyzer.getSummary(lintAnalyzerResult),
),
);
_checkSeverity(lintAnalyzerResult);
}
void _checkSeverity(Iterable<LintFileReport> lintAnalyzerResult) {
if (hasIssueWithSeverity(lintAnalyzerResult, Severity.error)) {
exit(3);
} else if ((argResults[FlagNames.fatalWarnings] as bool) &&
hasIssueWithSeverity(lintAnalyzerResult, Severity.warning)) {
exit(2);
}
final maximumAllowedLevel = MetricValueLevel.fromString(
argResults[FlagNames.setExitOnViolationLevel] as String?,
);
if (maximumAllowedLevel != null &&
maxMetricViolationLevel(lintAnalyzerResult) >= maximumAllowedLevel) {
exit(2);
}
if (((argResults[FlagNames.fatalPerformance] as bool) &&
hasIssueWithSeverity(lintAnalyzerResult, Severity.performance)) ||
((argResults[FlagNames.fatalStyle] as bool) &&
hasIssueWithSeverity(lintAnalyzerResult, Severity.style))) {
exit(1);
}
}
void _addFlags() {
_usesReporterOption();
_usesMetricsThresholdOptions();
addCommonFlags();
_usesExitOption();
}
void _usesReporterOption() {
argParser
..addSeparator('')
..addOption(
FlagNames.reporter,
abbr: 'r',
help: 'The format of the output of the analysis.',
valueHelp: FlagNames.consoleReporter,
allowed: [
FlagNames.consoleReporter,
FlagNames.consoleVerboseReporter,
FlagNames.checkstyleReporter,
FlagNames.codeClimateReporter,
FlagNames.githubReporter,
FlagNames.gitlabCodeClimateReporter,
FlagNames.htmlReporter,
FlagNames.jsonReporter,
],
defaultsTo: FlagNames.consoleReporter,
)
..addOption(
FlagNames.reportFolder,
abbr: 'o',
help: 'Write HTML output to OUTPUT.',
valueHelp: 'OUTPUT',
defaultsTo: 'metrics',
)
..addOption(
FlagNames.jsonReportPath,
help: 'Path to the JSON file with the output of the analysis.',
valueHelp: 'path/to/file.json',
defaultsTo: null,
);
}
void _usesMetricsThresholdOptions() {
argParser.addSeparator('');
for (final metric in getMetrics(config: {})) {
argParser.addOption(
metric.id,
help: '${metric.documentation.name} threshold.',
valueHelp: '${metric.documentation.recommendedThreshold}',
callback: (i) {
if (i != null && int.tryParse(i) == null) {
print(
"'$i' invalid value for argument ${metric.documentation.name}",
);
}
},
);
}
}
void _usesExitOption() {
argParser
..addSeparator('')
..addOption(
FlagNames.setExitOnViolationLevel,
allowed: ['noted', 'warning', 'alarm'],
valueHelp: 'warning',
help:
'Set exit code 2 if code violations same or higher level than selected are detected.',
)
..addFlag(
FlagNames.fatalStyle,
help: 'Treat style level issues as fatal.',
)
..addFlag(
FlagNames.fatalPerformance,
help: 'Treat performance level issues as fatal.',
)
..addFlag(
FlagNames.fatalWarnings,
help: 'Treat warning level issues as fatal.',
defaultsTo: true,
);
}
}