@@ -104,8 +104,8 @@ class FormatCommand extends PackageCommand {
104
104
print ('These files are not formatted correctly (see diff below):' );
105
105
LineSplitter .split (stdout).map ((String line) => ' $line ' ).forEach (print);
106
106
107
- print ('\n To fix run "pub global activate flutter_plugin_tools && '
108
- 'pub global run flutter_plugin_tools format" or copy-paste '
107
+ print ('\n To fix run "dart pub global activate flutter_plugin_tools && '
108
+ 'dart pub global run flutter_plugin_tools format" or copy-paste '
109
109
'this command into your terminal:' );
110
110
111
111
final io.ProcessResult diff = await processRunner.run (
@@ -128,16 +128,11 @@ class FormatCommand extends PackageCommand {
128
128
final Iterable <String > clangFiles = _getPathsWithExtensions (
129
129
files, < String > {'.h' , '.m' , '.mm' , '.cc' , '.cpp' });
130
130
if (clangFiles.isNotEmpty) {
131
- final String clangFormat = getStringArg ('clang-format' );
132
- if (! await _hasDependency (clangFormat)) {
133
- printError ('Unable to run "clang-format". Make sure that it is in your '
134
- 'path, or provide a full path with --clang-format.' );
135
- throw ToolExit (_exitDependencyMissing);
136
- }
131
+ final String clangFormat = await _findValidClangFormat ();
137
132
138
133
print ('Formatting .cc, .cpp, .h, .m, and .mm files...' );
139
134
final int exitCode = await _runBatched (
140
- getStringArg ( 'clang-format' ) , < String > ['-i' , '--style=file' ],
135
+ clangFormat , < String > ['-i' , '--style=file' ],
141
136
files: clangFiles);
142
137
if (exitCode != 0 ) {
143
138
printError (
@@ -147,6 +142,26 @@ class FormatCommand extends PackageCommand {
147
142
}
148
143
}
149
144
145
+ Future <String > _findValidClangFormat () async {
146
+ final String clangFormatArg = getStringArg ('clang-format' );
147
+ if (await _hasDependency (clangFormatArg)) {
148
+ return clangFormatArg;
149
+ }
150
+
151
+ // There is a known issue where "chromium/depot_tools/clang-format"
152
+ // fails with "Problem while looking for clang-format in Chromium source tree".
153
+ // Loop through all "clang-format"s in PATH until a working one is found,
154
+ // for example "/usr/local/bin/clang-format" or a "brew" installed version.
155
+ for (final String clangFormatPath in await _whichAll ('clang-format' )) {
156
+ if (await _hasDependency (clangFormatPath)) {
157
+ return clangFormatPath;
158
+ }
159
+ }
160
+ printError ('Unable to run "clang-format". Make sure that it is in your '
161
+ 'path, or provide a full path with --clang-format.' );
162
+ throw ToolExit (_exitDependencyMissing);
163
+ }
164
+
150
165
Future <void > _formatJava (
151
166
Iterable <String > files, String googleFormatterPath) async {
152
167
final Iterable <String > javaFiles =
@@ -279,6 +294,26 @@ class FormatCommand extends PackageCommand {
279
294
return true ;
280
295
}
281
296
297
+ /// Returns all instances of [command] executable found on user path.
298
+ Future <List <String >> _whichAll (String command) async {
299
+ try {
300
+ final io.ProcessResult result =
301
+ await processRunner.run ('which' , < String > ['-a' , command]);
302
+
303
+ if (result.exitCode != 0 ) {
304
+ return < String > [];
305
+ }
306
+
307
+ final String stdout = result.stdout.trim () as String ;
308
+ if (stdout.isEmpty) {
309
+ return < String > [];
310
+ }
311
+ return LineSplitter .split (stdout).toList ();
312
+ } on io.ProcessException {
313
+ return < String > [];
314
+ }
315
+ }
316
+
282
317
/// Runs [command] on [arguments] on all of the files in [files] , batched as
283
318
/// necessary to avoid OS command-line length limits.
284
319
///
0 commit comments