Auto-format Framework (#160545)
This auto-formats all *.dart files in the repository outside of the `engine` subdirectory and enforces that these files stay formatted with a presubmit check. **Reviewers:** Please carefully review all the commits except for the one titled "formatted". The "formatted" commit was auto-generated by running `dev/tools/format.sh -a -f`. The other commits were hand-crafted to prepare the repo for the formatting change. I recommend reviewing the commits one-by-one via the "Commits" tab and avoiding Github's "Files changed" tab as it will likely slow down your browser because of the size of this PR. --------- Co-authored-by: Kate Lovett <katelovett@google.com> Co-authored-by: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
8e0993eda8
commit
5491c8c146
@@ -11,13 +11,18 @@ import 'package:process_runner/process_runner.dart';
|
||||
Future<int> main(List<String> arguments) async {
|
||||
final ArgParser parser = ArgParser();
|
||||
parser.addFlag('help', help: 'Print help.', abbr: 'h');
|
||||
parser.addFlag('fix',
|
||||
abbr: 'f',
|
||||
help: 'Instead of just checking for formatting errors, fix them in place.');
|
||||
parser.addFlag('all-files',
|
||||
abbr: 'a',
|
||||
help: 'Instead of just checking for formatting errors in changed files, '
|
||||
'check for them in all files.');
|
||||
parser.addFlag(
|
||||
'fix',
|
||||
abbr: 'f',
|
||||
help: 'Instead of just checking for formatting errors, fix them in place.',
|
||||
);
|
||||
parser.addFlag(
|
||||
'all-files',
|
||||
abbr: 'a',
|
||||
help:
|
||||
'Instead of just checking for formatting errors in changed files, '
|
||||
'check for them in all files.',
|
||||
);
|
||||
|
||||
late final ArgResults options;
|
||||
try {
|
||||
@@ -34,10 +39,12 @@ Future<int> main(List<String> arguments) async {
|
||||
final File script = File.fromUri(Platform.script).absolute;
|
||||
final Directory flutterRoot = script.parent.parent.parent.parent;
|
||||
|
||||
final bool result = (await DartFormatChecker(
|
||||
flutterRoot: flutterRoot,
|
||||
allFiles: options['all-files'] as bool,
|
||||
).check(fix: options['fix'] as bool)) == 0;
|
||||
final bool result =
|
||||
(await DartFormatChecker(
|
||||
flutterRoot: flutterRoot,
|
||||
allFiles: options['all-files'] as bool,
|
||||
).check(fix: options['fix'] as bool)) ==
|
||||
0;
|
||||
|
||||
exit(result ? 0 : 1);
|
||||
}
|
||||
@@ -49,12 +56,8 @@ void _usage(ArgParser parser, {int exitCode = 1}) {
|
||||
}
|
||||
|
||||
class DartFormatChecker {
|
||||
DartFormatChecker({
|
||||
required this.flutterRoot,
|
||||
required this.allFiles,
|
||||
}) : processRunner = ProcessRunner(
|
||||
defaultWorkingDirectory: flutterRoot,
|
||||
);
|
||||
DartFormatChecker({required this.flutterRoot, required this.allFiles})
|
||||
: processRunner = ProcessRunner(defaultWorkingDirectory: flutterRoot);
|
||||
|
||||
final Directory flutterRoot;
|
||||
final bool allFiles;
|
||||
@@ -67,10 +70,7 @@ class DartFormatChecker {
|
||||
allFiles: allFiles,
|
||||
baseGitRef: baseGitRef,
|
||||
);
|
||||
return _checkFormat(
|
||||
filesToCheck: filesToCheck,
|
||||
fix: fix,
|
||||
);
|
||||
return _checkFormat(filesToCheck: filesToCheck, fix: fix);
|
||||
}
|
||||
|
||||
Future<String> _getDiffBaseRevision() async {
|
||||
@@ -89,10 +89,12 @@ class DartFormatChecker {
|
||||
// This is the preferred command to use, but developer checkouts often do
|
||||
// not have a clear fork point, so we fall back to just the regular
|
||||
// merge-base in that case.
|
||||
result = await _runGit(
|
||||
<String>['merge-base', '--fork-point', 'FETCH_HEAD', 'HEAD'],
|
||||
processRunner,
|
||||
);
|
||||
result = await _runGit(<String>[
|
||||
'merge-base',
|
||||
'--fork-point',
|
||||
'FETCH_HEAD',
|
||||
'HEAD',
|
||||
], processRunner);
|
||||
} on ProcessRunnerException {
|
||||
result = await _runGit(<String>['merge-base', 'FETCH_HEAD', 'HEAD'], processRunner);
|
||||
}
|
||||
@@ -100,14 +102,14 @@ class DartFormatChecker {
|
||||
}
|
||||
|
||||
Future<String> _runGit(
|
||||
List<String> args,
|
||||
ProcessRunner processRunner, {
|
||||
bool failOk = false,
|
||||
}) async {
|
||||
final ProcessRunnerResult result = await processRunner.runProcess(
|
||||
<String>['git', ...args],
|
||||
failOk: failOk,
|
||||
);
|
||||
List<String> args,
|
||||
ProcessRunner processRunner, {
|
||||
bool failOk = false,
|
||||
}) async {
|
||||
final ProcessRunnerResult result = await processRunner.runProcess(<String>[
|
||||
'git',
|
||||
...args,
|
||||
], failOk: failOk);
|
||||
return result.stdout;
|
||||
}
|
||||
|
||||
@@ -118,11 +120,7 @@ class DartFormatChecker {
|
||||
}) async {
|
||||
String output;
|
||||
if (allFiles) {
|
||||
output = await _runGit(<String>[
|
||||
'ls-files',
|
||||
'--',
|
||||
...types,
|
||||
], processRunner);
|
||||
output = await _runGit(<String>['ls-files', '--', ...types], processRunner);
|
||||
} else {
|
||||
output = await _runGit(<String>[
|
||||
'diff',
|
||||
@@ -135,13 +133,13 @@ class DartFormatChecker {
|
||||
...types,
|
||||
], processRunner);
|
||||
}
|
||||
return output.split('\n').where((String line) => line.isNotEmpty).toList();
|
||||
return output
|
||||
.split('\n')
|
||||
.where((String line) => line.isNotEmpty && !line.startsWith('engine'))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<int> _checkFormat({
|
||||
required List<String> filesToCheck,
|
||||
required bool fix,
|
||||
}) async {
|
||||
Future<int> _checkFormat({required List<String> filesToCheck, required bool fix}) async {
|
||||
final List<String> cmd = <String>[
|
||||
path.join(flutterRoot.path, 'bin', 'dart'),
|
||||
'format',
|
||||
@@ -170,19 +168,16 @@ class DartFormatChecker {
|
||||
errorJobs.add(completedJob);
|
||||
} else if (completedJob.result.exitCode == 1) {
|
||||
diffJobs.add(
|
||||
WorkerJob(
|
||||
<String>[
|
||||
'git',
|
||||
'diff',
|
||||
'--no-index',
|
||||
'--no-color',
|
||||
'--ignore-cr-at-eol',
|
||||
'--',
|
||||
completedJob.command.last,
|
||||
'-',
|
||||
],
|
||||
stdinRaw: _codeUnitsAsStream(completedJob.result.stdoutRaw),
|
||||
),
|
||||
WorkerJob(<String>[
|
||||
'git',
|
||||
'diff',
|
||||
'--no-index',
|
||||
'--no-color',
|
||||
'--ignore-cr-at-eol',
|
||||
'--',
|
||||
completedJob.command.last,
|
||||
'-',
|
||||
], stdinRaw: _codeUnitsAsStream(completedJob.result.stdoutRaw)),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -210,23 +205,31 @@ class DartFormatChecker {
|
||||
if (incorrect.isNotEmpty) {
|
||||
final bool plural = incorrect.length > 1;
|
||||
if (fix) {
|
||||
stdout.writeln('Fixing ${incorrect.length} dart file${plural ? 's' : ''}'
|
||||
' which ${plural ? 'were' : 'was'} formatted incorrectly.');
|
||||
stdout.writeln(
|
||||
'Fixing ${incorrect.length} dart file${plural ? 's' : ''}'
|
||||
' which ${plural ? 'were' : 'was'} formatted incorrectly.',
|
||||
);
|
||||
} else {
|
||||
stderr.writeln('Found ${incorrect.length} Dart file${plural ? 's' : ''}'
|
||||
' which ${plural ? 'were' : 'was'} formatted incorrectly.');
|
||||
final String fileList = incorrect.map(
|
||||
(WorkerJob job) => job.command[job.command.length - 2]
|
||||
).join(' ');
|
||||
stderr.writeln(
|
||||
'Found ${incorrect.length} Dart file${plural ? 's' : ''}'
|
||||
' which ${plural ? 'were' : 'was'} formatted incorrectly.',
|
||||
);
|
||||
final String fileList = incorrect
|
||||
.map((WorkerJob job) => job.command[job.command.length - 2])
|
||||
.join(' ');
|
||||
stdout.writeln();
|
||||
stdout.writeln('To fix, run `dart format $fileList` or:');
|
||||
stdout.writeln();
|
||||
stdout.writeln('git apply <<DONE');
|
||||
for (final WorkerJob job in incorrect) {
|
||||
stdout.write(job.result.stdout
|
||||
.replaceFirst('b/-', 'b/${job.command[job.command.length - 2]}')
|
||||
.replaceFirst('b/-', 'b/${job.command[job.command.length - 2]}')
|
||||
.replaceFirst(RegExp('\\+Formatted \\d+ files? \\(\\d+ changed\\) in \\d+.\\d+ seconds.\n'), '')
|
||||
stdout.write(
|
||||
job.result.stdout
|
||||
.replaceFirst('b/-', 'b/${job.command[job.command.length - 2]}')
|
||||
.replaceFirst('b/-', 'b/${job.command[job.command.length - 2]}')
|
||||
.replaceFirst(
|
||||
RegExp('\\+Formatted \\d+ files? \\(\\d+ changed\\) in \\d+.\\d+ seconds.\n'),
|
||||
'',
|
||||
),
|
||||
);
|
||||
}
|
||||
stdout.writeln('DONE');
|
||||
@@ -244,7 +247,9 @@ class DartFormatChecker {
|
||||
void _printErrorJobs(List<WorkerJob> errorJobs) {
|
||||
if (errorJobs.isNotEmpty) {
|
||||
final bool plural = errorJobs.length > 1;
|
||||
stderr.writeln('The formatter failed to run on ${errorJobs.length} Dart file${plural ? 's' : ''}.');
|
||||
stderr.writeln(
|
||||
'The formatter failed to run on ${errorJobs.length} Dart file${plural ? 's' : ''}.',
|
||||
);
|
||||
stdout.writeln();
|
||||
for (final WorkerJob job in errorJobs) {
|
||||
stdout.writeln('--> ${job.command.last} produced the following error:');
|
||||
@@ -257,19 +262,20 @@ class DartFormatChecker {
|
||||
|
||||
ProcessPoolProgressReporter _namedReport(String name) {
|
||||
return (int total, int completed, int inProgress, int pending, int failed) {
|
||||
final String percent =
|
||||
total == 0 ? '100' : ((100 * completed) ~/ total).toString().padLeft(3);
|
||||
final String percent = total == 0 ? '100' : ((100 * completed) ~/ total).toString().padLeft(3);
|
||||
final String completedStr = completed.toString().padLeft(3);
|
||||
final String totalStr = total.toString().padRight(3);
|
||||
final String inProgressStr = inProgress.toString().padLeft(2);
|
||||
final String pendingStr = pending.toString().padLeft(3);
|
||||
final String failedStr = failed.toString().padLeft(3);
|
||||
|
||||
stdout.write('$name Jobs: $percent% done, '
|
||||
'$completedStr/$totalStr completed, '
|
||||
'$inProgressStr in progress, '
|
||||
'$pendingStr pending, '
|
||||
'$failedStr failed.${' ' * 20}\r');
|
||||
stdout.write(
|
||||
'$name Jobs: $percent% done, '
|
||||
'$completedStr/$totalStr completed, '
|
||||
'$inProgressStr in progress, '
|
||||
'$pendingStr pending, '
|
||||
'$failedStr failed.${' ' * 20}\r',
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user