[flutter_tools] generate a synthetic flutter_gen package on pub get (#61261)

Allow configuring the flutter_manifest to support a synthetic package, this is done through flutter: generate: true.

When running pub get, insert a flutter_gen entry into the packages if it does not already exist. This points to .dart_tool/flutter_gen, which can be updated to contain the generated intl sources (But doesn't currently)

Adds an integration test that verifies this code can be run and imported when enabled.

Part of #60914
This commit is contained in:
Jonah Williams
2020-07-15 10:12:52 -07:00
committed by GitHub
parent 3631adc99a
commit d8b6fa153a
23 changed files with 254 additions and 25 deletions

View File

@@ -104,6 +104,7 @@ class BuildRunner extends CodeGenerator {
directory: generatedDirectory.path,
upgrade: false,
checkLastModified: false,
generateSyntheticPackage: false,
);
if (!scriptIdFile.existsSync()) {
scriptIdFile.createSync(recursive: true);

View File

@@ -448,7 +448,8 @@ class _ResidentWebRunner extends ResidentWebRunner {
// This will result in a NoSuchMethodError thrown by injected_handler.darts
await pub.get(
context: PubContext.pubGet,
directory: globals.fs.path.join(Cache.flutterRoot, 'packages', 'flutter_tools')
directory: globals.fs.path.join(Cache.flutterRoot, 'packages', 'flutter_tools'),
generateSyntheticPackage: false,
);
final ExpressionCompiler expressionCompiler =

View File

@@ -211,6 +211,7 @@ class BuildDaemonCreator {
await pub.get(
context: PubContext.pubGet,
directory: globals.fs.file(buildScriptPackages).parent.path,
generateSyntheticPackage: false,
);
}
final String flutterWebSdk = globals.artifacts.getArtifactPath(Artifact.flutterWebSdk);

View File

@@ -543,6 +543,7 @@ To edit platform code in an IDE see https://flutter.dev/developing-packages/#edi
context: PubContext.create,
directory: directory.path,
offline: boolArg('offline'),
generateSyntheticPackage: false,
);
final FlutterProject project = FlutterProject.fromDirectory(directory);
await project.ensureReadyForPlatformSpecificTooling(checkProjects: false);
@@ -562,6 +563,7 @@ To edit platform code in an IDE see https://flutter.dev/developing-packages/#edi
context: PubContext.createPackage,
directory: directory.path,
offline: boolArg('offline'),
generateSyntheticPackage: false,
);
}
return generatedCount;
@@ -606,6 +608,7 @@ To edit platform code in an IDE see https://flutter.dev/developing-packages/#edi
context: PubContext.createPlugin,
directory: directory.path,
offline: boolArg('offline'),
generateSyntheticPackage: false,
);
}
@@ -674,7 +677,12 @@ https://flutter.dev/docs/development/packages-and-plugins/developing-packages#pl
}
if (boolArg('pub')) {
await pub.get(context: PubContext.create, directory: directory.path, offline: boolArg('offline'));
await pub.get(
context: PubContext.create,
directory: directory.path,
offline: boolArg('offline'),
generateSyntheticPackage: false,
);
await project.ensureReadyForPlatformSpecificTooling(checkProjects: pluginExampleApp);
}
if (templateContext['android'] == true) {

View File

@@ -88,7 +88,7 @@ class PackagesGetCommand extends FlutterCommand {
return usageValues;
}
Future<void> _runPubGet(String directory) async {
Future<void> _runPubGet(String directory, FlutterProject flutterProject) async {
final Stopwatch pubGetTimer = Stopwatch()..start();
try {
await pub.get(context: PubContext.pubGet,
@@ -96,6 +96,7 @@ class PackagesGetCommand extends FlutterCommand {
upgrade: upgrade ,
offline: boolArg('offline'),
checkLastModified: false,
generateSyntheticPackage: flutterProject.manifest.generateSyntheticPackage,
);
pubGetTimer.stop();
globals.flutterUsage.sendTiming('pub', 'get', pubGetTimer.elapsed, label: 'success');
@@ -121,15 +122,15 @@ class PackagesGetCommand extends FlutterCommand {
'${ workingDirectory ?? "current working directory" }.'
);
}
await _runPubGet(target);
final FlutterProject rootProject = FlutterProject.fromPath(target);
await _runPubGet(target, rootProject);
await rootProject.ensureReadyForPlatformSpecificTooling(checkProjects: true);
// Get/upgrade packages in example app as well
if (rootProject.hasExampleApp) {
final FlutterProject exampleProject = rootProject.example;
await _runPubGet(exampleProject.directory.path);
await _runPubGet(exampleProject.directory.path, exampleProject);
await exampleProject.ensureReadyForPlatformSpecificTooling(checkProjects: true);
}

View File

@@ -163,15 +163,19 @@ class TestCommand extends FlutterCommand {
"called *_test.dart and must reside in the package's 'test' "
'directory (or one of its subdirectories).');
}
final FlutterProject flutterProject = FlutterProject.current();
if (shouldRunPub) {
await pub.get(context: PubContext.getVerifyContext(name), skipPubspecYamlCheck: true);
await pub.get(
context: PubContext.getVerifyContext(name),
skipPubspecYamlCheck: true,
generateSyntheticPackage: flutterProject.manifest.generateSyntheticPackage,
);
}
final bool buildTestAssets = boolArg('test-assets');
final List<String> names = stringsArg('name');
final List<String> plainNames = stringsArg('plain-name');
final String tags = stringArg('tags');
final String excludeTags = stringArg('exclude-tags');
final FlutterProject flutterProject = FlutterProject.current();
if (buildTestAssets && flutterProject.manifest.assets.isNotEmpty) {
await _buildTestAsset();
@@ -222,7 +226,7 @@ class TestCommand extends FlutterCommand {
final bool machine = boolArg('machine');
CoverageCollector collector;
if (boolArg('coverage') || boolArg('merge-coverage')) {
final String projectName = FlutterProject.current().manifest.appName;
final String projectName = flutterProject.manifest.appName;
collector = CoverageCollector(
verbose: !machine,
libraryPredicate: (String libraryName) => libraryName.contains(projectName),

View File

@@ -308,6 +308,7 @@ class UpdatePackagesCommand extends FlutterCommand {
flutterRootOverride: upgrade
? temporaryFlutterSdk.path
: null,
generateSyntheticPackage: false,
);
// Cleanup the temporary SDK
try {
@@ -387,6 +388,7 @@ class UpdatePackagesCommand extends FlutterCommand {
directory: dir.path,
checkLastModified: false,
offline: offline,
generateSyntheticPackage: false,
);
count += 1;
}

View File

@@ -294,7 +294,13 @@ class UpgradeCommandRunner {
final String projectRoot = findProjectRoot();
if (projectRoot != null) {
globals.printStatus('');
await pub.get(context: PubContext.pubUpgrade, directory: projectRoot, upgrade: true, checkLastModified: false);
await pub.get(
context: PubContext.pubUpgrade,
directory: projectRoot,
upgrade: true,
checkLastModified: false,
generateSyntheticPackage: false,
);
}
}

View File

@@ -154,6 +154,7 @@ class VersionCommand extends FlutterCommand {
directory: projectRoot,
upgrade: true,
checkLastModified: false,
generateSyntheticPackage: false,
);
}

View File

@@ -207,6 +207,7 @@ Future<T> runInContext<T>(
botDetector: globals.botDetector,
platform: globals.platform,
usage: globals.flutterUsage,
toolStampFile: globals.cache.getStampFileFor('flutter_tools'),
),
ShutdownHooks: () => ShutdownHooks(logger: globals.logger),
Stdio: () => Stdio(),

View File

@@ -5,6 +5,7 @@
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:package_config/package_config.dart';
import 'package:process/process.dart';
import '../base/bot_detector.dart';
@@ -16,6 +17,7 @@ import '../base/logger.dart';
import '../base/platform.dart';
import '../base/process.dart';
import '../cache.dart';
import '../dart/package_map.dart';
import '../reporting/reporting.dart';
/// The [Pub] instance.
@@ -39,7 +41,7 @@ class PubContext {
for (final String item in _values) {
if (!_validContext.hasMatch(item)) {
throw ArgumentError.value(
_values, 'value', 'Must match RegExp ${_validContext.pattern}');
_values, 'value', 'Must match RegExp ${_validContext.pattern}');
}
}
}
@@ -80,6 +82,7 @@ abstract class Pub {
@required Platform platform,
@required BotDetector botDetector,
@required Usage usage,
File toolStampFile,
}) = _DefaultPub;
/// Runs `pub get`.
@@ -94,6 +97,7 @@ abstract class Pub {
bool offline = false,
bool checkLastModified = true,
bool skipPubspecYamlCheck = false,
bool generateSyntheticPackage = false,
String flutterRootOverride,
});
@@ -139,7 +143,9 @@ class _DefaultPub implements Pub {
@required Platform platform,
@required BotDetector botDetector,
@required Usage usage,
}) : _fileSystem = fileSystem,
File toolStampFile,
}) : _toolStampFile = toolStampFile,
_fileSystem = fileSystem,
_logger = logger,
_platform = platform,
_botDetector = botDetector,
@@ -155,6 +161,7 @@ class _DefaultPub implements Pub {
final Platform _platform;
final BotDetector _botDetector;
final Usage _usage;
final File _toolStampFile;
@override
Future<void> get({
@@ -165,6 +172,7 @@ class _DefaultPub implements Pub {
bool offline = false,
bool checkLastModified = true,
bool skipPubspecYamlCheck = false,
bool generateSyntheticPackage = false,
String flutterRootOverride,
}) async {
directory ??= _fileSystem.currentDirectory.path;
@@ -173,6 +181,7 @@ class _DefaultPub implements Pub {
_fileSystem.path.join(directory, 'pubspec.yaml'));
final File packageConfigFile = _fileSystem.file(
_fileSystem.path.join(directory, '.dart_tool', 'package_config.json'));
final Directory generatedDirectory = _fileSystem.directory(_fileSystem.path.join(directory, '.dart_tool', 'flutter_gen'));
if (!skipPubspecYamlCheck && !pubSpecYaml.existsSync()) {
if (!skipIfAbsent) {
@@ -242,6 +251,10 @@ class _DefaultPub implements Pub {
'The time now is: $now'
);
}
// Insert references to synthetic flutter package.
if (generateSyntheticPackage) {
await _updatePackageConfig(packageConfigFile, generatedDirectory);
}
}
@override
@@ -387,6 +400,12 @@ class _DefaultPub implements Pub {
if (pubSpecYaml.lastModifiedSync().isAfter(dotPackagesLastModified)) {
return true;
}
if (_toolStampFile != null &&
_toolStampFile.existsSync() &&
_toolStampFile.lastModifiedSync().isAfter(dotPackagesLastModified)) {
return true;
}
return false;
}
@@ -439,4 +458,27 @@ class _DefaultPub implements Pub {
}
return environment;
}
/// Insert the flutter_gen synthetic package into the package configuration file if
/// there is an l10n.yaml.
Future<void> _updatePackageConfig(File packageConfigFile, Directory generatedDirectory) async {
if (!packageConfigFile.existsSync()) {
return;
}
final PackageConfig packageConfig = await loadPackageConfigWithLogging(packageConfigFile, logger: _logger);
final Package flutterGen = Package('flutter_gen', generatedDirectory.uri, languageVersion: LanguageVersion(2, 8));
if (packageConfig.packages.any((Package package) => package.name == 'flutter_gen')) {
return;
}
final PackageConfig newPackageConfig = PackageConfig(
<Package>[
...packageConfig.packages,
flutterGen,
],
);
// There is no current API for saving a package_config without hitting the real filesystem.
if (packageConfigFile.fileSystem is LocalFileSystem) {
await savePackageConfig(newPackageConfig, packageConfigFile.parent.parent);
}
}
}

View File

@@ -279,6 +279,26 @@ class FlutterManifest {
}
return fonts;
}
/// Whether a synthetic flutter_gen package should be generated.
///
/// This can be provided to the [Pub] interface to inject a new entry
/// into the package_config.json file which points to `.dart_tool/flutter_gen`.
///
/// This allows generated source code to be imported using a package
/// alias.
bool get generateSyntheticPackage => _generateSyntheticPackage ??= _computeGenerateSyntheticPackage();
bool _generateSyntheticPackage;
bool _computeGenerateSyntheticPackage() {
if (!_flutterDescriptor.containsKey('generate')) {
return false;
}
final Object value = _flutterDescriptor['generate'];
if (value is! bool) {
return false;
}
return value as bool;
}
}
class Font {
@@ -438,6 +458,8 @@ void _validateFlutter(YamlMap yaml, List<String> errors) {
final List<String> pluginErrors = Plugin.validatePluginYaml(kvp.value as YamlMap);
errors.addAll(pluginErrors);
break;
case 'generate':
break;
default:
errors.add('Unexpected child "${kvp.key}" found under "flutter".');
break;

View File

@@ -845,10 +845,13 @@ abstract class FlutterCommand extends Command<void> {
await validateCommand();
if (shouldRunPub) {
await pub.get(context: PubContext.getVerifyContext(name));
final FlutterProject project = FlutterProject.current();
await pub.get(
context: PubContext.getVerifyContext(name),
generateSyntheticPackage: project.manifest.generateSyntheticPackage,
);
// All done updating dependencies. Release the cache lock.
Cache.releaseLock();
final FlutterProject project = FlutterProject.current();
await project.ensureReadyForPlatformSpecificTooling(checkProjects: true);
} else {
Cache.releaseLock();

View File

@@ -301,5 +301,6 @@ Future<void> _ensurePackageDependencies(String packagePath) async {
await pub.get(
context: PubContext.pubGet,
directory: packagePath,
generateSyntheticPackage: false,
);
}