mirror of
https://github.com/flutter/packages.git
synced 2025-08-14 18:12:30 +08:00
Switch script/tools over to the new analysis options (#3777)
Removes the legacy analysis options override and fixes all resulting issues. This is a combination of dart fix and manual changes (mostly mechanical, but some small restructuring to address warnings more cleanly, such as creating typed structs from args when they are used repeatedly to avoid repeated casting, or making things that were unnecessarily public private). One small opportunistic extra cleanup is that the handling of null-safety prerelease versions is removed, as any new plugin would be written null-safe from the start, so we no longer need to allow those versions. Part of flutter/flutter#76229
This commit is contained in:
@ -16,7 +16,9 @@ import 'common.dart';
|
||||
const String _googleFormatterUrl =
|
||||
'https://github.com/google/google-java-format/releases/download/google-java-format-1.3/google-java-format-1.3-all-deps.jar';
|
||||
|
||||
/// A command to format all package code.
|
||||
class FormatCommand extends PluginCommand {
|
||||
/// Creates an instance of the format command.
|
||||
FormatCommand(
|
||||
Directory packagesDir,
|
||||
FileSystem fileSystem, {
|
||||
@ -38,15 +40,14 @@ class FormatCommand extends PluginCommand {
|
||||
'your path.';
|
||||
|
||||
@override
|
||||
Future<Null> run() async {
|
||||
checkSharding();
|
||||
Future<void> run() async {
|
||||
final String googleFormatterPath = await _getGoogleFormatterPath();
|
||||
|
||||
await _formatDart();
|
||||
await _formatJava(googleFormatterPath);
|
||||
await _formatCppAndObjectiveC();
|
||||
|
||||
if (argResults['fail-on-change']) {
|
||||
if (argResults['fail-on-change'] == true) {
|
||||
final bool modified = await _didModifyAnything();
|
||||
if (modified) {
|
||||
throw ToolExit(1);
|
||||
@ -61,15 +62,14 @@ class FormatCommand extends PluginCommand {
|
||||
|
||||
print('\n\n');
|
||||
|
||||
if (modifiedFiles.stdout.isEmpty) {
|
||||
final String stdout = modifiedFiles.stdout as String;
|
||||
if (stdout.isEmpty) {
|
||||
print('All files formatted correctly.');
|
||||
return false;
|
||||
}
|
||||
|
||||
print('These files are not formatted correctly (see diff below):');
|
||||
LineSplitter.split(modifiedFiles.stdout)
|
||||
.map((String line) => ' $line')
|
||||
.forEach(print);
|
||||
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 '
|
||||
@ -83,33 +83,34 @@ class FormatCommand extends PluginCommand {
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<Null> _formatCppAndObjectiveC() async {
|
||||
Future<void> _formatCppAndObjectiveC() async {
|
||||
print('Formatting all .cc, .cpp, .mm, .m, and .h files...');
|
||||
final Iterable<String> allFiles = <String>[]
|
||||
..addAll(await _getFilesWithExtension('.h'))
|
||||
..addAll(await _getFilesWithExtension('.m'))
|
||||
..addAll(await _getFilesWithExtension('.mm'))
|
||||
..addAll(await _getFilesWithExtension('.cc'))
|
||||
..addAll(await _getFilesWithExtension('.cpp'));
|
||||
final Iterable<String> allFiles = <String>[
|
||||
...await _getFilesWithExtension('.h'),
|
||||
...await _getFilesWithExtension('.m'),
|
||||
...await _getFilesWithExtension('.mm'),
|
||||
...await _getFilesWithExtension('.cc'),
|
||||
...await _getFilesWithExtension('.cpp'),
|
||||
];
|
||||
// Split this into multiple invocations to avoid a
|
||||
// 'ProcessException: Argument list too long'.
|
||||
final Iterable<List<String>> batches = partition(allFiles, 100);
|
||||
for (List<String> batch in batches) {
|
||||
await processRunner.runAndStream(argResults['clang-format'],
|
||||
<String>['-i', '--style=Google']..addAll(batch),
|
||||
for (final List<String> batch in batches) {
|
||||
await processRunner.runAndStream(argResults['clang-format'] as String,
|
||||
<String>['-i', '--style=Google', ...batch],
|
||||
workingDir: packagesDir, exitOnError: true);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Null> _formatJava(String googleFormatterPath) async {
|
||||
Future<void> _formatJava(String googleFormatterPath) async {
|
||||
print('Formatting all .java files...');
|
||||
final Iterable<String> javaFiles = await _getFilesWithExtension('.java');
|
||||
await processRunner.runAndStream('java',
|
||||
<String>['-jar', googleFormatterPath, '--replace']..addAll(javaFiles),
|
||||
<String>['-jar', googleFormatterPath, '--replace', ...javaFiles],
|
||||
workingDir: packagesDir, exitOnError: true);
|
||||
}
|
||||
|
||||
Future<Null> _formatDart() async {
|
||||
Future<void> _formatDart() async {
|
||||
// This actually should be fine for non-Flutter Dart projects, no need to
|
||||
// specifically shell out to dartfmt -w in that case.
|
||||
print('Formatting all .dart files...');
|
||||
@ -119,7 +120,7 @@ class FormatCommand extends PluginCommand {
|
||||
'No .dart files to format. If you set the `--exclude` flag, most likey they were skipped');
|
||||
} else {
|
||||
await processRunner.runAndStream(
|
||||
'flutter', <String>['format']..addAll(dartFiles),
|
||||
'flutter', <String>['format', ...dartFiles],
|
||||
workingDir: packagesDir, exitOnError: true);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user