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,13 +94,15 @@ 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.' ,
90107 )
91108 ..addFlag (
@@ -112,6 +129,20 @@ class Options {
112129 help: 'Use the given path as the source of compile_commands.json. This '
113130 'file is created by running tools/gn' ,
114131 )
132+ ..addOption (
133+ 'target-variant' ,
134+ aliases: < String > ['variant' ],
135+ help: 'The engine variant directory containing compile_commands.json '
136+ 'created by running tools/gn. Ignored if --compile-commands is also passed.' ,
137+ valueHelp: 'host_debug|android_debug_unopt|ios_debug|ios_debug_sim_unopt' ,
138+ defaultsTo: 'host_debug' ,
139+ )
140+ ..addOption (
141+ 'src-dir' ,
142+ help: 'Path to the engine src directory. Ignored if --compile-commands is also passed.' ,
143+ valueHelp: 'path/to/engine/src' ,
144+ defaultsTo: path.dirname (_engineRoot),
145+ )
115146 ..addOption (
116147 'checks' ,
117148 help: 'Perform the given checks on the code. Defaults to the empty '
@@ -156,26 +187,21 @@ class Options {
156187 _errSink.writeln (message);
157188 }
158189 _errSink.writeln (
159- 'Usage: bin/main.dart [--help] [--lint-all] [--fix] [--verbose] [--diff-branch]' ,
190+ 'Usage: bin/main.dart [--help] [--lint-all] [--fix] [--verbose] [--diff-branch] [--target-variant variant] [--src-dir path/to/engine/src] ' ,
160191 );
161192 _errSink.writeln (_argParser.usage);
162193 }
163194
164195 /// Command line argument validation.
165- static String ? _checkArguments (ArgResults argResults) {
196+ static String ? _checkArguments (ArgResults argResults, io. File buildCommandsPath ) {
166197 if (argResults.wasParsed ('help' )) {
167198 return null ;
168199 }
169200
170- if (! argResults.wasParsed ('compile-commands' )) {
171- return 'ERROR: The --compile-commands argument is required.' ;
172- }
173-
174201 if (! argResults.wasParsed ('repo' )) {
175202 return 'ERROR: The --repo argument is required.' ;
176203 }
177204
178- final io.File buildCommandsPath = io.File (argResults['compile-commands' ] as String );
179205 if (! buildCommandsPath.existsSync ()) {
180206 return "ERROR: Build commands path ${buildCommandsPath .absolute .path } doesn't exist." ;
181207 }
0 commit comments