55import 'dart:io' as io show Directory, File, Platform, stderr;
66
77import 'package:args/args.dart' ;
8+ import 'package:path/path.dart' as path;
9+
10+ // Path to root of the flutter/engine repository containing this script.
11+ final String _engineRoot = path.dirname (path.dirname (path.dirname (path.dirname (path.fromUri (io.Platform .script)))));
812
913/// A class for organizing the options to the Engine linter, and the files
1014/// that it operates on.
1115class Options {
1216 /// Builds an instance of [Options] from the arguments.
1317 Options ({
1418 required this .buildCommandsPath,
15- required this .repoPath,
1619 this .help = false ,
1720 this .verbose = false ,
1821 this .checksArg = '' ,
@@ -30,7 +33,6 @@ class Options {
3033 return Options (
3134 errorMessage: message,
3235 buildCommandsPath: io.File ('none' ),
33- repoPath: io.Directory ('none' ),
3436 errSink: errSink,
3537 );
3638 }
@@ -41,21 +43,20 @@ class Options {
4143 return Options (
4244 help: true ,
4345 buildCommandsPath: io.File ('none' ),
44- repoPath: io.Directory ('none' ),
4546 errSink: errSink,
4647 );
4748 }
4849
4950 /// Builds an [Options] instance with an [ArgResults] instance.
5051 factory Options ._fromArgResults (
5152 ArgResults options, {
53+ required io.File buildCommandsPath,
5254 StringSink ? errSink,
5355 }) {
5456 return Options (
5557 help: options['help' ] as bool ,
5658 verbose: options['verbose' ] as bool ,
57- buildCommandsPath: io.File (options['compile-commands' ] as String ),
58- repoPath: io.Directory (options['repo' ] as String ),
59+ buildCommandsPath: buildCommandsPath,
5960 checksArg: options.wasParsed ('checks' ) ? options['checks' ] as String : '' ,
6061 lintAll: io.Platform .environment['FLUTTER_LINT_ALL' ] != null ||
6162 options['lint-all' ] as bool ,
@@ -70,7 +71,17 @@ class Options {
7071 StringSink ? errSink,
7172 }) {
7273 final ArgResults argResults = _argParser.parse (arguments);
73- final String ? message = _checkArguments (argResults);
74+
75+ String ? buildCommandsPath = argResults['compile-commands' ] as String ? ;
76+ // path/to/engine/src/out/variant/compile_commands.json
77+ buildCommandsPath ?? = path.join (
78+ argResults['src-dir' ] as String ,
79+ 'out' ,
80+ argResults['target-variant' ] as String ,
81+ 'compile_commands.json' ,
82+ );
83+ final io.File buildCommands = io.File (buildCommandsPath);
84+ final String ? message = _checkArguments (argResults, buildCommands);
7485 if (message != null ) {
7586 return Options ._error (message, errSink: errSink);
7687 }
@@ -79,14 +90,17 @@ class Options {
7990 }
8091 return Options ._fromArgResults (
8192 argResults,
93+ buildCommandsPath: buildCommands,
8294 errSink: errSink,
8395 );
8496 }
8597
8698 static final ArgParser _argParser = ArgParser ()
8799 ..addFlag (
88100 'help' ,
101+ abbr: 'h' ,
89102 help: 'Print help.' ,
103+ negatable: false ,
90104 )
91105 ..addFlag (
92106 'lint-all' ,
@@ -103,14 +117,25 @@ class Options {
103117 help: 'Print verbose output.' ,
104118 defaultsTo: false ,
105119 )
106- ..addOption (
107- 'repo' ,
108- help: 'Use the given path as the repo path' ,
109- )
110120 ..addOption (
111121 'compile-commands' ,
112122 help: 'Use the given path as the source of compile_commands.json. This '
113- 'file is created by running tools/gn' ,
123+ 'file is created by running "tools/gn". Cannot be used with --target-variant '
124+ 'or --src-dir.' ,
125+ )
126+ ..addOption (
127+ 'target-variant' ,
128+ aliases: < String > ['variant' ],
129+ help: 'The engine variant directory containing compile_commands.json '
130+ 'created by running "tools/gn". Cannot be used with --compile-commands.' ,
131+ valueHelp: 'host_debug|android_debug_unopt|ios_debug|ios_debug_sim_unopt' ,
132+ defaultsTo: 'host_debug' ,
133+ )
134+ ..addOption (
135+ 'src-dir' ,
136+ help: 'Path to the engine src directory. Cannot be used with --compile-commands.' ,
137+ valueHelp: 'path/to/engine/src' ,
138+ defaultsTo: path.dirname (_engineRoot),
114139 )
115140 ..addOption (
116141 'checks' ,
@@ -129,7 +154,7 @@ class Options {
129154 final io.File buildCommandsPath;
130155
131156 /// The root of the flutter/engine repository.
132- final io.Directory repoPath;
157+ final io.Directory repoPath = io. Directory (_engineRoot) ;
133158
134159 /// Arguments to plumb through to clang-tidy formatted as a command line
135160 /// argument.
@@ -156,35 +181,30 @@ class Options {
156181 _errSink.writeln (message);
157182 }
158183 _errSink.writeln (
159- 'Usage: bin/main.dart [--help] [--lint-all] [--fix] [--verbose] [--diff-branch]' ,
184+ 'Usage: bin/main.dart [--help] [--lint-all] [--fix] [--verbose] [--diff-branch] [--target-variant variant] [--src-dir path/to/engine/src] ' ,
160185 );
161186 _errSink.writeln (_argParser.usage);
162187 }
163188
164189 /// Command line argument validation.
165- static String ? _checkArguments (ArgResults argResults) {
190+ static String ? _checkArguments (ArgResults argResults, io. File buildCommandsPath ) {
166191 if (argResults.wasParsed ('help' )) {
167192 return null ;
168193 }
169194
170- if (! argResults.wasParsed ('compile-commands' )) {
171- return 'ERROR: The --compile-commands argument is required.' ;
195+ final bool compileCommandsParsed = argResults.wasParsed ('compile-commands' );
196+ if (compileCommandsParsed && argResults.wasParsed ('target-variant' )) {
197+ return 'ERROR: --compile-commands option cannot be used with --target-variant.' ;
172198 }
173199
174- if (! argResults.wasParsed ('repo ' )) {
175- return 'ERROR: The --repo argument is required .' ;
200+ if (compileCommandsParsed && argResults.wasParsed ('src-dir ' )) {
201+ return 'ERROR: --compile-commands option cannot be used with --src-dir .' ;
176202 }
177203
178- final io.File buildCommandsPath = io.File (argResults['compile-commands' ] as String );
179204 if (! buildCommandsPath.existsSync ()) {
180205 return "ERROR: Build commands path ${buildCommandsPath .absolute .path } doesn't exist." ;
181206 }
182207
183- final io.Directory repoPath = io.Directory (argResults['repo' ] as String );
184- if (! repoPath.existsSync ()) {
185- return "ERROR: Repo path ${repoPath .absolute .path } doesn't exist." ;
186- }
187-
188208 return null ;
189209 }
190210}
0 commit comments