From 312ef541156c0b0c8553626678e37e551b99383a Mon Sep 17 00:00:00 2001 From: Tae Hyung Kim Date: Fri, 18 Aug 2023 15:01:09 +0000 Subject: [PATCH] [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. --- .../lib/src/commands/packages.dart | 28 +++++++ .../permeable/packages_test.dart | 75 +++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/packages/flutter_tools/lib/src/commands/packages.dart b/packages/flutter_tools/lib/src/commands/packages.dart index 096bd72cd4..824077cd8c 100644 --- a/packages/flutter_tools/lib/src/commands/packages.dart +++ b/packages/flutter_tools/lib/src/commands/packages.dart @@ -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((ExceptionMeasurement e) => e.exception).join('\n\n')}', + ); + } } } final String? relativeTarget = target == null ? null : globals.fs.path.relative(target); diff --git a/packages/flutter_tools/test/commands.shard/permeable/packages_test.dart b/packages/flutter_tools/test/commands.shard/permeable/packages_test.dart index 0a9be004be..df14535004 100644 --- a/packages/flutter_tools/test/commands.shard/permeable/packages_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/packages_test.dart @@ -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: ['--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: { + 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: ['--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: { + 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: ['--no-pub', '--template=module']);