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:
Michael Goderbauer
2024-12-19 12:06:21 -08:00
committed by GitHub
parent 8e0993eda8
commit 5491c8c146
4411 changed files with 455108 additions and 415991 deletions

View File

@@ -47,14 +47,8 @@ class ArchiveCreator {
processManager: processManager,
subprocessOutput: subprocessOutput,
platform: platform,
)..environment['PUB_CACHE'] = path.join(
tempDir.path, '.pub-cache',
);
final String flutterExecutable = path.join(
flutterRoot.absolute.path,
'bin',
'flutter',
);
)..environment['PUB_CACHE'] = path.join(tempDir.path, '.pub-cache');
final String flutterExecutable = path.join(flutterRoot.absolute.path, 'bin', 'flutter');
final String dartExecutable = path.join(
flutterRoot.absolute.path,
'bin',
@@ -93,11 +87,10 @@ class ArchiveCreator {
required this.revision,
required this.strict,
required this.tempDir,
}) :
assert(revision.length == 40),
_processRunner = processRunner,
_flutter = flutterExecutable,
_dart = dartExecutable;
}) : assert(revision.length == 40),
_processRunner = processRunner,
_flutter = flutterExecutable,
_dart = dartExecutable;
/// The platform to use for the environment and determining which
/// platform we're running on.
@@ -140,11 +133,13 @@ class ArchiveCreator {
late String _flutter;
late String _dart;
late final Future<String> _dartArch = (() async {
// Parse 'arch' out of a string like '... "os_arch"\n'.
return (await _runDart(<String>['--version']))
.trim().split(' ').last.replaceAll('"', '').split('_')[1];
})();
late final Future<String> _dartArch =
(() async {
// Parse 'arch' out of a string like '... "os_arch"\n'.
return (await _runDart(<String>[
'--version',
])).trim().split(' ').last.replaceAll('"', '').split('_')[1];
})();
/// Returns a default archive name when given a Git revision.
/// Used when an output filename is not given.
@@ -179,10 +174,7 @@ class ArchiveCreator {
/// Performs all of the steps needed to create an archive.
Future<File> createArchive() async {
assert(_version.isNotEmpty, 'Must run initializeRepo before createArchive');
final File outputFile = fs.file(path.join(
outputDir.absolute.path,
await _archiveName,
));
final File outputFile = fs.file(path.join(outputDir.absolute.path, await _archiveName));
await _installMinGitIfNeeded();
await _populateCaches();
await _validate();
@@ -202,19 +194,14 @@ class ArchiveCreator {
// Validate that the dart binary is codesigned
try {
// TODO(fujino): Use the conductor https://github.com/flutter/flutter/issues/81701
await _processRunner.runProcess(
<String>[
'codesign',
'-vvvv',
'--check-notarization',
_dart,
],
workingDirectory: flutterRoot,
);
await _processRunner.runProcess(<String>[
'codesign',
'-vvvv',
'--check-notarization',
_dart,
], workingDirectory: flutterRoot);
} on PreparePackageException catch (e) {
throw PreparePackageException(
'The binary $_dart was not codesigned!\n${e.message}',
);
throw PreparePackageException('The binary $_dart was not codesigned!\n${e.message}');
}
}
@@ -240,7 +227,7 @@ class ArchiveCreator {
throw PreparePackageException(
'Git error when checking for a version tag attached to revision $revision.\n'
'Perhaps there is no tag at that revision?:\n'
'$exception'
'$exception',
);
}
} else {
@@ -284,7 +271,9 @@ class ArchiveCreator {
final File gitFile = fs.file(path.join(tempDir.absolute.path, 'mingit.zip'));
await gitFile.writeAsBytes(data, flush: true);
final Directory minGitPath = fs.directory(path.join(flutterRoot.absolute.path, 'bin', 'mingit'));
final Directory minGitPath = fs.directory(
path.join(flutterRoot.absolute.path, 'bin', 'mingit'),
);
await minGitPath.create(recursive: true);
await _unzipArchive(gitFile, workingDirectory: minGitPath);
}
@@ -303,37 +292,46 @@ class ArchiveCreator {
final http.Client client = http.Client();
final Directory preloadCache = fs.directory(path.join(flutterRoot.path, '.pub-preload-cache'));
preloadCache.createSync(recursive: true);
/// Fetch a single package.
Future<void> fetchPackageArchive(String name, String version) async {
await pool.withResource(() async {
stderr.write('Fetching package archive for $name-$version.\n');
int retries = 7;
while (true) {
retries-=1;
retries -= 1;
try {
final Uri packageListingUrl = Uri.parse('https://pub.dev/api/packages/$name');
// Fetch the package listing to obtain the package download url.
final http.Response packageListingResponse = await client.get(packageListingUrl);
if (packageListingResponse.statusCode != 200) {
throw Exception('Downloading $packageListingUrl failed. Status code ${packageListingResponse.statusCode}.');
throw Exception(
'Downloading $packageListingUrl failed. Status code ${packageListingResponse.statusCode}.',
);
}
final dynamic decodedPackageListing = json.decode(packageListingResponse.body);
if (decodedPackageListing is! Map) {
throw const FormatException('Package listing should be a map');
}
final dynamic versions = decodedPackageListing['versions'];
final dynamic versions = decodedPackageListing['versions'];
if (versions is! List) {
throw const FormatException('.versions should be a list');
}
final Map<String, dynamic> versionDescription = versions.firstWhere(
(dynamic description) {
if (description is! Map) {
throw const FormatException('.versions elements should be maps');
}
return description['version'] == version;
},
orElse: () => throw FormatException('Could not find $name-$version in package listing')
) as Map<String, dynamic>;
final Map<String, dynamic> versionDescription =
versions.firstWhere(
(dynamic description) {
if (description is! Map) {
throw const FormatException('.versions elements should be maps');
}
return description['version'] == version;
},
orElse:
() =>
throw FormatException(
'Could not find $name-$version in package listing',
),
)
as Map<String, dynamic>;
final dynamic downloadUrl = versionDescription['archive_url'];
if (downloadUrl is! String) {
throw const FormatException('archive_url should be a string');
@@ -345,11 +343,11 @@ class ArchiveCreator {
final http.Request request = http.Request('get', Uri.parse(downloadUrl));
final http.StreamedResponse response = await client.send(request);
if (response.statusCode != 200) {
throw Exception('Downloading ${request.url} failed. Status code ${response.statusCode}.');
throw Exception(
'Downloading ${request.url} failed. Status code ${response.statusCode}.',
);
}
final File archiveFile = fs.file(
path.join(preloadCache.path, '$name-$version.tar.gz'),
);
final File archiveFile = fs.file(path.join(preloadCache.path, '$name-$version.tar.gz'));
await response.stream.pipe(archiveFile.openWrite());
final Stream<List<int>> archiveStream = archiveFile.openRead();
final Digest r = await sha256.bind(archiveStream).first;
@@ -370,7 +368,9 @@ class ArchiveCreator {
}
});
}
final Map<String, dynamic> cacheDescription = json.decode(await _runFlutter(<String>['pub', 'cache', 'list'])) as Map<String, dynamic>;
final Map<String, dynamic> cacheDescription =
json.decode(await _runFlutter(<String>['pub', 'cache', 'list'])) as Map<String, dynamic>;
final Map<String, dynamic> packages = cacheDescription['packages'] as Map<String, dynamic>;
final List<Future<void>> downloads = <Future<void>>[];
for (final MapEntry<String, dynamic> package in packages.entries) {
@@ -416,24 +416,23 @@ class ArchiveCreator {
'--',
'**/.packages',
]);
/// Remove package_config files and any contents in .dart_tool
await _runGit(<String>[
'clean',
'-f',
'-x',
'--',
'**/.dart_tool/',
]);
await _runGit(<String>['clean', '-f', '-x', '--', '**/.dart_tool/']);
// Ensure the above commands do not clean out the cache
final Directory flutterCache = fs.directory(path.join(flutterRoot.absolute.path, 'bin', 'cache'));
final Directory flutterCache = fs.directory(
path.join(flutterRoot.absolute.path, 'bin', 'cache'),
);
if (!flutterCache.existsSync()) {
throw Exception('The flutter cache was not found at ${flutterCache.path}!');
}
/// Remove git subfolder from .pub-cache, this contains the flutter goldens
/// and new flutter_gallery.
final Directory gitCache = fs.directory(path.join(flutterRoot.absolute.path, '.pub-cache', 'git'));
final Directory gitCache = fs.directory(
path.join(flutterRoot.absolute.path, '.pub-cache', 'git'),
);
if (gitCache.existsSync()) {
gitCache.deleteSync(recursive: true);
}
@@ -449,24 +448,24 @@ class ArchiveCreator {
}
Future<String> _runDart(List<String> args, {Directory? workingDirectory}) {
return _processRunner.runProcess(
<String>[_dart, ...args],
workingDirectory: workingDirectory ?? flutterRoot,
);
return _processRunner.runProcess(<String>[
_dart,
...args,
], workingDirectory: workingDirectory ?? flutterRoot);
}
Future<String> _runFlutter(List<String> args, {Directory? workingDirectory}) {
return _processRunner.runProcess(
<String>[_flutter, ...args],
workingDirectory: workingDirectory ?? flutterRoot,
);
return _processRunner.runProcess(<String>[
_flutter,
...args,
], workingDirectory: workingDirectory ?? flutterRoot);
}
Future<String> _runGit(List<String> args, {Directory? workingDirectory}) {
return _processRunner.runProcess(
<String>['git', ...args],
workingDirectory: workingDirectory ?? flutterRoot,
);
return _processRunner.runProcess(<String>[
'git',
...args,
], workingDirectory: workingDirectory ?? flutterRoot);
}
/// Unpacks the given zip file into the currentDirectory (if set), or the
@@ -475,16 +474,9 @@ class ArchiveCreator {
workingDirectory ??= fs.directory(path.dirname(archive.absolute.path));
List<String> commandLine;
if (platform.isWindows) {
commandLine = <String>[
'7za',
'x',
archive.absolute.path,
];
commandLine = <String>['7za', 'x', archive.absolute.path];
} else {
commandLine = <String>[
'unzip',
archive.absolute.path,
];
commandLine = <String>['unzip', archive.absolute.path];
}
return _processRunner.runProcess(commandLine, workingDirectory: workingDirectory);
}
@@ -494,10 +486,11 @@ class ArchiveCreator {
List<String> commandLine;
if (platform.isWindows) {
// Unhide the .git folder, https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/attrib.
await _processRunner.runProcess(
<String>['attrib', '-h', '.git'],
workingDirectory: fs.directory(source.absolute.path),
);
await _processRunner.runProcess(<String>[
'attrib',
'-h',
'.git',
], workingDirectory: fs.directory(source.absolute.path));
commandLine = <String>[
'7za',
'a',

View File

@@ -26,13 +26,13 @@ class ArchivePublisher {
bool subprocessOutput = true,
required this.fs,
this.platform = const LocalPlatform(),
}) : assert(revision.length == 40),
platformName = platform.operatingSystem.toLowerCase(),
metadataGsPath = '$gsReleaseFolder/${getMetadataFilename(platform)}',
_processRunner = ProcessRunner(
processManager: processManager,
subprocessOutput: subprocessOutput,
);
}) : assert(revision.length == 40),
platformName = platform.operatingSystem.toLowerCase(),
metadataGsPath = '$gsReleaseFolder/${getMetadataFilename(platform)}',
_processRunner = ProcessRunner(
processManager: processManager,
subprocessOutput: subprocessOutput,
);
final Platform platform;
final FileSystem fs;
@@ -45,8 +45,10 @@ class ArchivePublisher {
final File outputFile;
final ProcessRunner _processRunner;
final bool dryRun;
String get destinationArchivePath => '${branch.name}/$platformName/${path.basename(outputFile.path)}';
static String getMetadataFilename(Platform platform) => 'releases_${platform.operatingSystem.toLowerCase()}.json';
String get destinationArchivePath =>
'${branch.name}/$platformName/${path.basename(outputFile.path)}';
static String getMetadataFilename(Platform platform) =>
'releases_${platform.operatingSystem.toLowerCase()}.json';
Future<String> _getChecksum(File archiveFile) async {
final AccumulatorSink<Digest> digestSink = AccumulatorSink<Digest>();
@@ -68,15 +70,10 @@ class ArchivePublisher {
final String destGsPath = '$gsReleaseFolder/$destinationArchivePath';
if (!forceUpload) {
if (await _cloudPathExists(destGsPath) && !dryRun) {
throw PreparePackageException(
'File $destGsPath already exists on cloud storage!',
);
throw PreparePackageException('File $destGsPath already exists on cloud storage!');
}
}
await _cloudCopy(
src: outputFile.absolute.path,
dest: destGsPath,
);
await _cloudCopy(src: outputFile.absolute.path, dest: destGsPath);
assert(tempDir.existsSync());
final String gcsPath = '$gsReleaseFolder/${getMetadataFilename(platform)}';
await _publishMetadata(gcsPath);
@@ -179,7 +176,12 @@ class ArchivePublisher {
return '';
}
return _processRunner.runProcess(
<String>['python3', path.join(platform.environment['DEPOT_TOOLS']!, 'gsutil.py'), '--', ...args],
<String>[
'python3',
path.join(platform.environment['DEPOT_TOOLS']!, 'gsutil.py'),
'--',
...args,
],
workingDirectory: workingDirectory,
failOk: failOk,
);
@@ -188,9 +190,7 @@ class ArchivePublisher {
/// Determine if a file exists at a given [cloudPath].
Future<bool> _cloudPathExists(String cloudPath) async {
try {
await _runGsUtil(
<String>['stat', cloudPath],
);
await _runGsUtil(<String>['stat', cloudPath]);
} on PreparePackageException {
// `gsutil stat gs://path/to/file` will exit with 1 if file does not exist
return false;
@@ -198,11 +198,7 @@ class ArchivePublisher {
return true;
}
Future<String> _cloudCopy({
required String src,
required String dest,
int? cacheSeconds,
}) async {
Future<String> _cloudCopy({required String src, required String dest, int? cacheSeconds}) async {
// We often don't have permission to overwrite, but
// we have permission to remove, so that's what we do.
await _runGsUtil(<String>['rm', dest], failOk: true);

View File

@@ -6,7 +6,8 @@ import 'dart:io' hide Platform;
const String gobMirror = 'https://flutter.googlesource.com/mirrors/flutter';
const String githubRepo = 'https://github.com/flutter/flutter.git';
const String mingitForWindowsUrl = 'https://storage.googleapis.com/flutter_infra_release/mingit/'
const String mingitForWindowsUrl =
'https://storage.googleapis.com/flutter_infra_release/mingit/'
'603511c649b00bbef0a6122a827ac419b656bc19/mingit.zip';
const String releaseFolder = '/releases';
const String gsBase = 'gs://flutter_infra_release';
@@ -17,12 +18,7 @@ const String frameworkVersionTag = 'frameworkVersionFromGit';
const String dartVersionTag = 'dartSdkVersion';
const String dartTargetArchTag = 'dartTargetArch';
enum Branch {
beta,
stable,
master,
main;
}
enum Branch { beta, stable, master, main }
/// Exception class for when a process fails to run, so we can catch
/// it and provide something more readable than a stack trace.

View File

@@ -74,38 +74,35 @@ class ProcessRunner {
workingDirectory: workingDirectory.absolute.path,
environment: environment,
);
process.stdout.listen(
(List<int> event) {
output.addAll(event);
if (subprocessOutput) {
stdout.add(event);
}
},
onDone: () async => stdoutComplete.complete(),
);
process.stdout.listen((List<int> event) {
output.addAll(event);
if (subprocessOutput) {
stdout.add(event);
}
}, onDone: () async => stdoutComplete.complete());
if (subprocessOutput) {
process.stderr.listen(
(List<int> event) {
stderr.add(event);
},
onDone: () async => stderrComplete.complete(),
);
process.stderr.listen((List<int> event) {
stderr.add(event);
}, onDone: () async => stderrComplete.complete());
} else {
stderrComplete.complete();
}
} on ProcessException catch (e) {
final String message = 'Running "${commandLine.join(' ')}" in ${workingDirectory.path} '
final String message =
'Running "${commandLine.join(' ')}" in ${workingDirectory.path} '
'failed with:\n$e';
throw PreparePackageException(message);
} on ArgumentError catch (e) {
final String message = 'Running "${commandLine.join(' ')}" in ${workingDirectory.path} '
final String message =
'Running "${commandLine.join(' ')}" in ${workingDirectory.path} '
'failed with:\n$e';
throw PreparePackageException(message);
}
final int exitCode = await allComplete();
if (exitCode != 0 && !failOk) {
final String message = 'Running "${commandLine.join(' ')}" in ${workingDirectory.path} failed';
final String message =
'Running "${commandLine.join(' ')}" in ${workingDirectory.path} failed';
throw PreparePackageException(
message,
ProcessResult(0, exitCode, null, 'returned $exitCode'),