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.
@@ -49,12 +53,13 @@ class Options {
4953 /// Builds an [Options] instance with an [ArgResults] instance.
5054 factory Options ._fromArgResults (
5155 ArgResults options, {
56+ required io.File buildCommandsPath,
5257 StringSink ? errSink,
5358 }) {
5459 return Options (
5560 help: options['help' ] as bool ,
5661 verbose: options['verbose' ] as bool ,
57- buildCommandsPath: io. File (options[ 'compile-commands' ] as String ) ,
62+ buildCommandsPath: buildCommandsPath ,
5863 repoPath: io.Directory (options['repo' ] as String ),
5964 checksArg: options.wasParsed ('checks' ) ? options['checks' ] as String : '' ,
6065 lintAll: io.Platform .environment['FLUTTER_LINT_ALL' ] != null ||
@@ -70,7 +75,17 @@ class Options {
7075 StringSink ? errSink,
7176 }) {
7277 final ArgResults argResults = _argParser.parse (arguments);
73- final String ? message = _checkArguments (argResults);
78+
79+ String ? buildCommandsPath = argResults['compile-commands' ] as String ? ;
80+ // path/to/engine/src/out/variant/compile_commands.json
81+ buildCommandsPath ?? = path.join (
82+ argResults['src-dir' ] as String ,
83+ 'out' ,
84+ argResults['target-variant' ] as String ,
85+ 'compile_commands.json' ,
86+ );
87+ final io.File buildCommands = io.File (buildCommandsPath);
88+ final String ? message = _checkArguments (argResults, buildCommands);
7489 if (message != null ) {
7590 return Options ._error (message, errSink: errSink);
7691 }
@@ -79,14 +94,17 @@ class Options {
7994 }
8095 return Options ._fromArgResults (
8196 argResults,
97+ buildCommandsPath: buildCommands,
8298 errSink: errSink,
8399 );
84100 }
85101
86102 static final ArgParser _argParser = ArgParser ()
87103 ..addFlag (
88104 'help' ,
105+ abbr: 'h' ,
89106 help: 'Print help.' ,
107+ negatable: false ,
90108 )
91109 ..addFlag (
92110 'lint-all' ,
@@ -112,6 +130,20 @@ class Options {
112130 help: 'Use the given path as the source of compile_commands.json. This '
113131 'file is created by running tools/gn' ,
114132 )
133+ ..addOption (
134+ 'target-variant' ,
135+ aliases: < String > ['variant' ],
136+ help: 'The engine variant directory containing compile_commands.json '
137+ 'created by running tools/gn. Ignored if --compile-commands is also passed.' ,
138+ valueHelp: 'host_debug|android_debug_unopt|ios_debug|ios_debug_sim_unopt' ,
139+ defaultsTo: 'host_debug' ,
140+ )
141+ ..addOption (
142+ 'src-dir' ,
143+ help: 'Path to the engine src directory. Ignored if --compile-commands is also passed.' ,
144+ valueHelp: 'path/to/engine/src' ,
145+ defaultsTo: path.dirname (_engineRoot),
146+ )
115147 ..addOption (
116148 'checks' ,
117149 help: 'Perform the given checks on the code. Defaults to the empty '
@@ -156,26 +188,21 @@ class Options {
156188 _errSink.writeln (message);
157189 }
158190 _errSink.writeln (
159- 'Usage: bin/main.dart [--help] [--lint-all] [--fix] [--verbose] [--diff-branch]' ,
191+ 'Usage: bin/main.dart [--help] [--lint-all] [--fix] [--verbose] [--diff-branch] [--target-variant variant] [--src-dir path/to/engine/src] ' ,
160192 );
161193 _errSink.writeln (_argParser.usage);
162194 }
163195
164196 /// Command line argument validation.
165- static String ? _checkArguments (ArgResults argResults) {
197+ static String ? _checkArguments (ArgResults argResults, io. File buildCommandsPath ) {
166198 if (argResults.wasParsed ('help' )) {
167199 return null ;
168200 }
169201
170- if (! argResults.wasParsed ('compile-commands' )) {
171- return 'ERROR: The --compile-commands argument is required.' ;
172- }
173-
174202 if (! argResults.wasParsed ('repo' )) {
175203 return 'ERROR: The --repo argument is required.' ;
176204 }
177205
178- final io.File buildCommandsPath = io.File (argResults['compile-commands' ] as String );
179206 if (! buildCommandsPath.existsSync ()) {
180207 return "ERROR: Build commands path ${buildCommandsPath .absolute .path } doesn't exist." ;
181208 }
0 commit comments