[flutter_tools] Generate localizations on flutter pub get (#132172)

Currently, flutter pub get generates localizations if there exists an l10n.yaml file where synthetic-package is not false. However, for any user who needs to turn off synthetic-package, their localizations are not generated. This PR should make the behavior more consistent. (Also it seems good to make it so that running flutter pub get once resolves all the dependencies so that people can get to work without running flutter gen-l10n manually.)

Fixes https://github.com/flutter/flutter/issues/84979.
This commit is contained in:
Tae Hyung Kim
2023-08-18 15:01:09 +00:00
committed by GitHub
parent 633b1ce402
commit 312ef54115
2 changed files with 103 additions and 0 deletions

View File

@@ -6,8 +6,10 @@ import 'package:args/args.dart';
import '../base/common.dart';
import '../base/os.dart';
import '../base/utils.dart';
import '../build_info.dart';
import '../build_system/build_system.dart';
import '../build_system/targets/localizations.dart';
import '../cache.dart';
import '../dart/generate_synthetic_packages.dart';
import '../dart/pub.dart';
@@ -304,6 +306,32 @@ class PackagesGetCommand extends FlutterCommand {
environment: environment,
buildSystem: globals.buildSystem,
);
} else if (rootProject.directory.childFile('l10n.yaml').existsSync()) {
final Environment environment = Environment(
artifacts: globals.artifacts!,
logger: globals.logger,
cacheDir: globals.cache.getRoot(),
engineVersion: globals.flutterVersion.engineRevision,
fileSystem: globals.fs,
flutterRootDir: globals.fs.directory(Cache.flutterRoot),
outputDir: globals.fs.directory(getBuildDirectory()),
processManager: globals.processManager,
platform: globals.platform,
usage: globals.flutterUsage,
projectDir: rootProject.directory,
generateDartPluginRegistry: true,
);
final BuildResult result = await globals.buildSystem.build(
const GenerateLocalizationsTarget(),
environment,
);
if (result.hasException) {
throwToolExit(
'Generating synthetic localizations package failed with ${result.exceptions.length} ${pluralize('error', result.exceptions.length)}:'
'\n\n'
'${result.exceptions.values.map<Object?>((ExceptionMeasurement e) => e.exception).join('\n\n')}',
);
}
}
}
final String? relativeTarget = target == null ? null : globals.fs.path.relative(target);

View File

@@ -255,6 +255,81 @@ void main() {
),
});
testUsingContext('get generates synthetic package when l10n.yaml has synthetic-package: true', () async {
final String projectPath = await createProject(tempDir,
arguments: <String>['--no-pub', '--template=module']);
final Directory projectDir = globals.fs.directory(projectPath);
projectDir
.childDirectory('lib')
.childDirectory('l10n')
.childFile('app_en.arb')
..createSync(recursive: true)
..writeAsStringSync('{ "hello": "Hello world!" }');
String pubspecFileContent = projectDir.childFile('pubspec.yaml').readAsStringSync();
pubspecFileContent = pubspecFileContent.replaceFirst(RegExp(r'\nflutter\:'), '''
flutter:
generate: true
''');
projectDir
.childFile('pubspec.yaml')
.writeAsStringSync(pubspecFileContent);
projectDir
.childFile('l10n.yaml')
.writeAsStringSync('synthetic-package: true');
await runCommandIn(projectPath, 'get');
expect(
projectDir
.childDirectory('.dart_tool')
.childDirectory('flutter_gen')
.childDirectory('gen_l10n')
.childFile('app_localizations.dart')
.existsSync(),
true
);
}, overrides: <Type, Generator>{
Pub: () => Pub(
fileSystem: globals.fs,
logger: globals.logger,
processManager: globals.processManager,
usage: globals.flutterUsage,
botDetector: globals.botDetector,
platform: globals.platform,
),
});
testUsingContext('get generates normal files when l10n.yaml has synthetic-package: false', () async {
final String projectPath = await createProject(tempDir,
arguments: <String>['--no-pub', '--template=module']);
final Directory projectDir = globals.fs.directory(projectPath);
projectDir
.childDirectory('lib')
.childDirectory('l10n')
.childFile('app_en.arb')
..createSync(recursive: true)
..writeAsStringSync('{ "hello": "Hello world!" }');
projectDir
.childFile('l10n.yaml')
.writeAsStringSync('synthetic-package: false');
await runCommandIn(projectPath, 'get');
expect(
projectDir
.childDirectory('lib')
.childDirectory('l10n')
.childFile('app_localizations.dart')
.existsSync(),
true
);
}, overrides: <Type, Generator>{
Pub: () => Pub(
fileSystem: globals.fs,
logger: globals.logger,
processManager: globals.processManager,
usage: globals.flutterUsage,
botDetector: globals.botDetector,
platform: globals.platform,
),
});
testUsingContext('set no plugins as usage value', () async {
final String projectPath = await createProject(tempDir,
arguments: <String>['--no-pub', '--template=module']);