mirror of
https://github.com/flutter/packages.git
synced 2025-08-15 03:52:36 +08:00
[flutter_plugin_tools] If clang-format
does not run, fall back to other executables in PATH (#6853)
* If clang-format does not run, fall back to other executables in PATH * Review edits
This commit is contained in:
@ -104,8 +104,8 @@ class FormatCommand extends PackageCommand {
|
||||
print('These files are not formatted correctly (see diff below):');
|
||||
LineSplitter.split(stdout).map((String line) => ' $line').forEach(print);
|
||||
|
||||
print('\nTo fix run "pub global activate flutter_plugin_tools && '
|
||||
'pub global run flutter_plugin_tools format" or copy-paste '
|
||||
print('\nTo fix run "dart pub global activate flutter_plugin_tools && '
|
||||
'dart pub global run flutter_plugin_tools format" or copy-paste '
|
||||
'this command into your terminal:');
|
||||
|
||||
final io.ProcessResult diff = await processRunner.run(
|
||||
@ -128,16 +128,11 @@ class FormatCommand extends PackageCommand {
|
||||
final Iterable<String> clangFiles = _getPathsWithExtensions(
|
||||
files, <String>{'.h', '.m', '.mm', '.cc', '.cpp'});
|
||||
if (clangFiles.isNotEmpty) {
|
||||
final String clangFormat = getStringArg('clang-format');
|
||||
if (!await _hasDependency(clangFormat)) {
|
||||
printError('Unable to run "clang-format". Make sure that it is in your '
|
||||
'path, or provide a full path with --clang-format.');
|
||||
throw ToolExit(_exitDependencyMissing);
|
||||
}
|
||||
final String clangFormat = await _findValidClangFormat();
|
||||
|
||||
print('Formatting .cc, .cpp, .h, .m, and .mm files...');
|
||||
final int exitCode = await _runBatched(
|
||||
getStringArg('clang-format'), <String>['-i', '--style=file'],
|
||||
clangFormat, <String>['-i', '--style=file'],
|
||||
files: clangFiles);
|
||||
if (exitCode != 0) {
|
||||
printError(
|
||||
@ -147,6 +142,26 @@ class FormatCommand extends PackageCommand {
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _findValidClangFormat() async {
|
||||
final String clangFormatArg = getStringArg('clang-format');
|
||||
if (await _hasDependency(clangFormatArg)) {
|
||||
return clangFormatArg;
|
||||
}
|
||||
|
||||
// There is a known issue where "chromium/depot_tools/clang-format"
|
||||
// fails with "Problem while looking for clang-format in Chromium source tree".
|
||||
// Loop through all "clang-format"s in PATH until a working one is found,
|
||||
// for example "/usr/local/bin/clang-format" or a "brew" installed version.
|
||||
for (final String clangFormatPath in await _whichAll('clang-format')) {
|
||||
if (await _hasDependency(clangFormatPath)) {
|
||||
return clangFormatPath;
|
||||
}
|
||||
}
|
||||
printError('Unable to run "clang-format". Make sure that it is in your '
|
||||
'path, or provide a full path with --clang-format.');
|
||||
throw ToolExit(_exitDependencyMissing);
|
||||
}
|
||||
|
||||
Future<void> _formatJava(
|
||||
Iterable<String> files, String googleFormatterPath) async {
|
||||
final Iterable<String> javaFiles =
|
||||
@ -279,6 +294,26 @@ class FormatCommand extends PackageCommand {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Returns all instances of [command] executable found on user path.
|
||||
Future<List<String>> _whichAll(String command) async {
|
||||
try {
|
||||
final io.ProcessResult result =
|
||||
await processRunner.run('which', <String>['-a', command]);
|
||||
|
||||
if (result.exitCode != 0) {
|
||||
return <String>[];
|
||||
}
|
||||
|
||||
final String stdout = result.stdout.trim() as String;
|
||||
if (stdout.isEmpty) {
|
||||
return <String>[];
|
||||
}
|
||||
return LineSplitter.split(stdout).toList();
|
||||
} on io.ProcessException {
|
||||
return <String>[];
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs [command] on [arguments] on all of the files in [files], batched as
|
||||
/// necessary to avoid OS command-line length limits.
|
||||
///
|
||||
|
Reference in New Issue
Block a user