55import 'dart:io' as io show Directory, File, Platform, stderr;
66
77import 'package:args/args.dart' ;
8+ import 'package:engine_repo_tools/engine_repo_tools.dart' ;
89import 'package:path/path.dart' as path;
910
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)))));
12-
11+ final Engine _engineRoot = Engine .findWithin (path.dirname (path.fromUri (io.Platform .script)));
1312
1413/// Adds warnings as errors for only specific runs. This is helpful if migrating one platform at a time.
1514String ? _platformSpecificWarningsAsErrors (ArgResults options) {
@@ -91,8 +90,13 @@ class Options {
9190 factory Options .fromCommandLine (
9291 List <String > arguments, {
9392 StringSink ? errSink,
93+ Engine ? engine,
9494 }) {
95- final ArgResults argResults = _argParser.parse (arguments);
95+ // TODO(matanlurey): Refactor this further, ideally moving all of the engine
96+ // resolution logic (i.e. --src-dir, --target-variant, --compile-commands)
97+ // into a separate method, and perhaps also adding `engine.output(name)`
98+ // to engine_repo_tools instead of path manipulation inlined below.
99+ final ArgResults argResults = _argParser (defaultEngine: engine).parse (arguments);
96100
97101 String ? buildCommandsPath = argResults['compile-commands' ] as String ? ;
98102
@@ -134,74 +138,85 @@ class Options {
134138 );
135139 }
136140
137- static final ArgParser _argParser = ArgParser ()
138- ..addFlag (
139- 'help' ,
140- abbr: 'h' ,
141- help: 'Print help.' ,
142- negatable: false ,
143- )
144- ..addFlag (
145- 'lint-all' ,
146- help: 'Lint all of the sources, regardless of FLUTTER_NOLINT.' ,
147- )
148- ..addFlag (
149- 'lint-head' ,
150- help: 'Lint files changed in the tip-of-tree commit.' ,
151- )
152- ..addFlag (
153- 'fix' ,
154- help: 'Apply suggested fixes.' ,
155- )
156- ..addFlag (
157- 'verbose' ,
158- help: 'Print verbose output.' ,
159- )
160- ..addOption (
161- 'shard-id' ,
162- help: 'When used with the shard-commands option this identifies which shard will execute.' ,
163- valueHelp: 'A number less than 1 + the number of shard-commands arguments.' ,
164- )
165- ..addOption (
166- 'shard-variants' ,
167- help: 'Comma separated list of other targets, this invocation '
168- 'will only execute a subset of the intersection and the difference of the '
169- 'compile commands. Use with `shard-id`.'
170- )
171- ..addOption (
172- 'compile-commands' ,
173- help: 'Use the given path as the source of compile_commands.json. This '
174- 'file is created by running "tools/gn". Cannot be used with --target-variant '
175- 'or --src-dir.' ,
176- )
177- ..addOption (
178- 'target-variant' ,
179- aliases: < String > ['variant' ],
180- help: 'The engine variant directory containing compile_commands.json '
181- 'created by running "tools/gn". Cannot be used with --compile-commands.' ,
182- valueHelp: 'host_debug|android_debug_unopt|ios_debug|ios_debug_sim_unopt' ,
183- defaultsTo: 'host_debug' ,
184- )
185- ..addOption ('mac-host-warnings-as-errors' ,
141+ static ArgParser _argParser ({required Engine ? defaultEngine}) {
142+ defaultEngine ?? = _engineRoot;
143+ final io.Directory ? latestBuild = defaultEngine.latestOutput ()? .path;
144+ return ArgParser ()
145+ ..addFlag (
146+ 'help' ,
147+ abbr: 'h' ,
148+ help: 'Print help.' ,
149+ negatable: false ,
150+ )
151+ ..addFlag (
152+ 'lint-all' ,
153+ help: 'Lint all of the sources, regardless of FLUTTER_NOLINT.' ,
154+ )
155+ ..addFlag (
156+ 'lint-head' ,
157+ help: 'Lint files changed in the tip-of-tree commit.' ,
158+ )
159+ ..addFlag (
160+ 'fix' ,
161+ help: 'Apply suggested fixes.' ,
162+ )
163+ ..addFlag (
164+ 'verbose' ,
165+ help: 'Print verbose output.' ,
166+ )
167+ ..addOption (
168+ 'shard-id' ,
169+ help: 'When used with the shard-commands option this identifies which shard will execute.' ,
170+ valueHelp: 'A number less than 1 + the number of shard-commands arguments.' ,
171+ )
172+ ..addOption (
173+ 'shard-variants' ,
174+ help: 'Comma separated list of other targets, this invocation '
175+ 'will only execute a subset of the intersection and the difference of the '
176+ 'compile commands. Use with `shard-id`.'
177+ )
178+ ..addOption (
179+ 'compile-commands' ,
180+ help: 'Use the given path as the source of compile_commands.json. This '
181+ 'file is created by running "tools/gn". Cannot be used with --target-variant '
182+ 'or --src-dir.' ,
183+ )
184+ ..addOption (
185+ 'target-variant' ,
186+ aliases: < String > ['variant' ],
187+ help: 'The engine variant directory name containing compile_commands.json '
188+ 'created by running "tools/gn".\n\n If not provided, the default is '
189+ 'the latest build in the engine defined by --src-dir (or the '
190+ 'default path, see --src-dir for details).\n\n '
191+ 'Cannot be used with --compile-commands.' ,
192+ valueHelp: 'host_debug|android_debug_unopt|ios_debug|ios_debug_sim_unopt' ,
193+ defaultsTo: latestBuild == null ? 'host_debug' : path.basename (latestBuild.path),
194+ )
195+ ..addOption ('mac-host-warnings-as-errors' ,
196+ help:
197+ 'checks that will be treated as errors when running debug_host on mac.' )
198+ ..addOption (
199+ 'src-dir' ,
186200 help:
187- 'checks that will be treated as errors when running debug_host on mac.' )
188- ..addOption (
189- 'src-dir' ,
190- help: 'Path to the engine src directory. Cannot be used with --compile-commands.' ,
191- valueHelp: 'path/to/engine/src' ,
192- defaultsTo: path.dirname (_engineRoot),
193- )
194- ..addOption (
195- 'checks' ,
196- help: 'Perform the given checks on the code. Defaults to the empty '
197- 'string, indicating all checks should be performed.' ,
198- defaultsTo: '' ,
199- )
200- ..addFlag (
201- 'enable-check-profile' ,
202- help: 'Enable per-check timing profiles and print a report to stderr.' ,
203- negatable: false ,
204- );
201+ 'Path to the engine src directory.\n\n '
202+ 'If not provided, the default is the engine root directory that '
203+ 'contains the `clang_tidy` tool.\n\n '
204+ 'Cannot be used with --compile-commands.' ,
205+ valueHelp: 'path/to/engine/src' ,
206+ defaultsTo: _engineRoot.srcDir.path,
207+ )
208+ ..addOption (
209+ 'checks' ,
210+ help: 'Perform the given checks on the code. Defaults to the empty '
211+ 'string, indicating all checks should be performed.' ,
212+ defaultsTo: '' ,
213+ )
214+ ..addFlag (
215+ 'enable-check-profile' ,
216+ help: 'Enable per-check timing profiles and print a report to stderr.' ,
217+ negatable: false ,
218+ );
219+ }
205220
206221 /// Whether to print a help message and exit.
207222 final bool help;
@@ -219,7 +234,7 @@ class Options {
219234 final int ? shardId;
220235
221236 /// The root of the flutter/engine repository.
222- final io.Directory repoPath = io. Directory ( _engineRoot) ;
237+ final io.Directory repoPath = _engineRoot.flutterDir ;
223238
224239 /// Argument sent as `warnings-as-errors` to clang-tidy.
225240 final String ? warningsAsErrors;
@@ -249,15 +264,15 @@ class Options {
249264 final StringSink _errSink;
250265
251266 /// Print command usage with an additional message.
252- void printUsage ({String ? message}) {
267+ void printUsage ({String ? message, required Engine ? engine }) {
253268 if (message != null ) {
254269 _errSink.writeln (message);
255270 }
256271 _errSink.writeln (
257272 'Usage: bin/main.dart [--help] [--lint-all] [--lint-head] [--fix] [--verbose] '
258273 '[--diff-branch] [--target-variant variant] [--src-dir path/to/engine/src]' ,
259274 );
260- _errSink.writeln (_argParser.usage);
275+ _errSink.writeln (_argParser (defaultEngine : engine) .usage);
261276 }
262277
263278 /// Command line argument validation.
0 commit comments