flutter_flutter/dev/bots/prepare_package.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-26 01:10:39 +00:00

163 lines
5.1 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:io' show exit, stderr;
import 'package:args/args.dart';
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'prepare_package/archive_creator.dart';
import 'prepare_package/archive_publisher.dart';
import 'prepare_package/common.dart';
const FileSystem fs = LocalFileSystem();
/// Prepares a flutter git repo to be packaged up for distribution. It mainly
/// serves to populate the .pub-preload-cache with any appropriate Dart
/// packages, and the flutter cache in bin/cache with the appropriate
/// dependencies and snapshots.
///
/// Archives contain the executables and customizations for the platform that
/// they are created on.
Future<void> main(List<String> rawArguments) async {
final argParser = ArgParser();
argParser.addOption(
'temp_dir',
help:
'A location where temporary files may be written. Defaults to a '
'directory in the system temp folder. Will write a few GiB of data, '
'so it should have sufficient free space. If a temp_dir is not '
'specified, then the default temp_dir will be created, used, and '
'removed automatically.',
);
argParser.addOption(
'revision',
help:
'The Flutter git repo revision to build the '
'archive with. Must be the full 40-character hash. Required.',
);
argParser.addOption(
'branch',
allowed: Branch.values.map<String>((Branch branch) => branch.name),
help: 'The Flutter branch to build the archive with. Required.',
);
argParser.addOption(
'output',
help:
'The path to the directory where the output archive should be '
'written. If --output is not specified, the archive will be written to '
"the current directory. If the output directory doesn't exist, it, and "
'the path to it, will be created.',
);
argParser.addFlag(
'publish',
help:
'If set, will publish the archive to Google Cloud Storage upon '
'successful creation of the archive. Will publish under this '
'directory: $baseUrl$releaseFolder',
);
argParser.addFlag('force', abbr: 'f', help: 'Overwrite a previously uploaded package.');
argParser.addFlag(
'dry_run',
negatable: false,
help: 'Prints gsutil commands instead of executing them.',
);
argParser.addFlag('help', negatable: false, help: 'Print help for this command.');
final ArgResults parsedArguments = argParser.parse(rawArguments);
if (parsedArguments['help'] as bool) {
print(argParser.usage);
exit(0);
}
void errorExit(String message, {int exitCode = -1}) {
stderr.write('Error: $message\n\n');
stderr.write('${argParser.usage}\n');
exit(exitCode);
}
if (!parsedArguments.wasParsed('revision')) {
errorExit('Invalid argument: --revision must be specified.');
}
final revision = parsedArguments['revision'] as String;
if (revision.length != 40) {
errorExit('Invalid argument: --revision must be the entire hash, not just a prefix.');
}
if (!parsedArguments.wasParsed('branch')) {
errorExit('Invalid argument: --branch must be specified.');
}
final tempDirArg = parsedArguments['temp_dir'] as String?;
final Directory tempDir;
var removeTempDir = false;
if (tempDirArg == null || tempDirArg.isEmpty) {
tempDir = fs.systemTempDirectory.createTempSync('flutter_package.');
removeTempDir = true;
} else {
tempDir = fs.directory(tempDirArg);
if (!tempDir.existsSync()) {
errorExit("Temporary directory $tempDirArg doesn't exist.");
}
}
final Directory outputDir;
if (parsedArguments['output'] == null) {
outputDir = tempDir;
} else {
outputDir = fs.directory(parsedArguments['output'] as String);
if (!outputDir.existsSync()) {
outputDir.createSync(recursive: true);
}
}
final publish = parsedArguments['publish'] as bool;
final dryRun = parsedArguments['dry_run'] as bool;
final Branch branch = Branch.values.byName(parsedArguments['branch'] as String);
final creator = ArchiveCreator(
tempDir,
outputDir,
revision,
branch,
fs: fs,
strict: publish && !dryRun,
);
var exitCode = 0;
late String message;
try {
final Map<String, String> version = await creator.initializeRepo();
final File outputFile = await creator.createArchive();
final publisher = ArchivePublisher(
tempDir,
revision,
branch,
version,
outputFile,
dryRun,
fs: fs,
);
await publisher.generateLocalMetadata();
if (parsedArguments['publish'] as bool) {
await publisher.publishArchive(parsedArguments['force'] as bool);
}
} on PreparePackageException catch (e) {
exitCode = e.exitCode;
message = e.message;
} catch (e) {
exitCode = -1;
message = e.toString();
} finally {
if (removeTempDir) {
tempDir.deleteSync(recursive: true);
}
if (exitCode != 0) {
errorExit(message, exitCode: exitCode);
}
exit(0);
}
}