@@ -8,11 +8,13 @@ import 'package:file/file.dart';
8
8
import 'package:path/path.dart' as p;
9
9
10
10
import 'common/core.dart' ;
11
- import 'common/plugin_command .dart' ;
11
+ import 'common/package_looping_command .dart' ;
12
12
import 'common/process_runner.dart' ;
13
13
14
+ const int _exitBadCustomAnalysisFile = 2 ;
15
+
14
16
/// A command to run Dart analysis on packages.
15
- class AnalyzeCommand extends PluginCommand {
17
+ class AnalyzeCommand extends PackageLoopingCommand {
16
18
/// Creates a analysis command instance.
17
19
AnalyzeCommand (
18
20
Directory packagesDir, {
@@ -32,6 +34,8 @@ class AnalyzeCommand extends PluginCommand {
32
34
33
35
static const String _analysisSdk = 'analysis-sdk' ;
34
36
37
+ late String _dartBinaryPath;
38
+
35
39
@override
36
40
final String name = 'analyze' ;
37
41
@@ -40,9 +44,10 @@ class AnalyzeCommand extends PluginCommand {
40
44
'This command requires "dart" and "flutter" to be in your path.' ;
41
45
42
46
@override
43
- Future <void > run () async {
44
- print ('Verifying analysis settings...' );
47
+ final bool hasLongOutput = false ;
45
48
49
+ /// Checks that there are no unexpected analysis_options.yaml files.
50
+ void _validateAnalysisOptions () {
46
51
final List <FileSystemEntity > files = packagesDir.listSync (recursive: true );
47
52
for (final FileSystemEntity file in files) {
48
53
if (file.basename != 'analysis_options.yaml' &&
@@ -59,52 +64,55 @@ class AnalyzeCommand extends PluginCommand {
59
64
continue ;
60
65
}
61
66
62
- print ('Found an extra analysis_options.yaml in ${file .absolute .path }.' );
63
- print (
64
- 'If this was deliberate, pass the package to the analyze command with the --$_customAnalysisFlag flag and try again.' );
65
- throw ToolExit (1 );
67
+ printError (
68
+ 'Found an extra analysis_options.yaml in ${file .absolute .path }.' );
69
+ printError (
70
+ 'If this was deliberate, pass the package to the analyze command '
71
+ 'with the --$_customAnalysisFlag flag and try again.' );
72
+ throw ToolExit (_exitBadCustomAnalysisFile);
66
73
}
74
+ }
67
75
76
+ /// Ensures that the dependent packages have been fetched for all packages
77
+ /// (including their sub-packages) that will be analyzed.
78
+ Future <void > _runPackagesGetOnTargetPackages () async {
68
79
final List <Directory > packageDirectories = await getPackages ().toList ();
69
80
final Set <String > packagePaths =
70
81
packageDirectories.map ((Directory dir) => dir.path).toSet ();
71
82
packageDirectories.removeWhere ((Directory directory) {
72
- // We remove the 'example' subdirectories - 'flutter pub get' automatically
73
- // runs 'pub get' there as part of handling the parent directory.
83
+ // Remove the 'example' subdirectories; 'flutter packages get'
84
+ // automatically runs 'pub get' there as part of handling the parent
85
+ // directory.
74
86
return directory.basename == 'example' &&
75
87
packagePaths.contains (directory.parent.path);
76
88
});
77
89
for (final Directory package in packageDirectories) {
78
90
await processRunner.runAndStream ('flutter' , < String > ['packages' , 'get' ],
79
91
workingDir: package, exitOnError: true );
80
92
}
93
+ }
94
+
95
+ @override
96
+ Future <void > initializeRun () async {
97
+ print ('Verifying analysis settings...' );
98
+ _validateAnalysisOptions ();
99
+
100
+ print ('Fetching dependencies...' );
101
+ await _runPackagesGetOnTargetPackages ();
81
102
82
103
// Use the Dart SDK override if one was passed in.
83
104
final String ? dartSdk = argResults! [_analysisSdk] as String ? ;
84
- final String dartBinary =
85
- dartSdk == null ? 'dart' : p.join (dartSdk, 'bin' , 'dart' );
86
-
87
- final List <String > failingPackages = < String > [];
88
- final List <Directory > pluginDirectories = await getPlugins ().toList ();
89
- for (final Directory package in pluginDirectories) {
90
- final int exitCode = await processRunner.runAndStream (
91
- dartBinary, < String > ['analyze' , '--fatal-infos' ],
92
- workingDir: package);
93
- if (exitCode != 0 ) {
94
- failingPackages.add (p.basename (package.path));
95
- }
96
- }
97
-
98
- print ('\n\n ' );
105
+ _dartBinaryPath = dartSdk == null ? 'dart' : p.join (dartSdk, 'bin' , 'dart' );
106
+ }
99
107
100
- if (failingPackages.isNotEmpty) {
101
- print ('The following packages have analyzer errors (see above):' );
102
- for (final String package in failingPackages) {
103
- print (' * $package ' );
104
- }
105
- throw ToolExit (1 );
108
+ @override
109
+ Future <List <String >> runForPackage (Directory package) async {
110
+ final int exitCode = await processRunner.runAndStream (
111
+ _dartBinaryPath, < String > ['analyze' , '--fatal-infos' ],
112
+ workingDir: package);
113
+ if (exitCode != 0 ) {
114
+ return PackageLoopingCommand .failure;
106
115
}
107
-
108
- print ('No analyzer errors found!' );
116
+ return PackageLoopingCommand .success;
109
117
}
110
118
}
0 commit comments