From 0508a1defd562f95f84d3a449381ba11eb2a4a54 Mon Sep 17 00:00:00 2001 From: Tae Hyung Kim Date: Wed, 31 Aug 2022 10:42:51 -0700 Subject: [PATCH] [flutter_tools] Generate Localizations on `flutter run` for web (#110526) --- .../lib/src/isolated/resident_web_runner.dart | 1 + .../general.shard/resident_runner_test.dart | 41 ++++++++++++ .../resident_web_runner_test.dart | 63 +++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/packages/flutter_tools/lib/src/isolated/resident_web_runner.dart b/packages/flutter_tools/lib/src/isolated/resident_web_runner.dart index 6852856f0cc..501606484a7 100644 --- a/packages/flutter_tools/lib/src/isolated/resident_web_runner.dart +++ b/packages/flutter_tools/lib/src/isolated/resident_web_runner.dart @@ -280,6 +280,7 @@ class ResidentWebRunner extends ResidentRunner { ); final Uri url = await device!.devFS!.create(); if (debuggingOptions.buildInfo.isDebug) { + await runSourceGenerators(); final UpdateFSReport report = await _updateDevFS(fullRestart: true); if (!report.success) { _logger!.printError('Failed to compile application.'); diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart index 24a64b6a92f..b37f4844a2e 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -1388,6 +1388,47 @@ flutter: expect(testLogger.statusText, isEmpty); })); + testUsingContext('ResidentRunner generates files when l10n.yaml exists', () => testbed.run(() async { + globals.fs.file(globals.fs.path.join('lib', 'main.dart')) + .createSync(recursive: true); + final File arbFile = globals.fs.file(globals.fs.path.join('lib', 'l10n', 'app_en.arb')) + ..createSync(recursive: true); + arbFile.writeAsStringSync(''' +{ + "helloWorld": "Hello, World!", + "@helloWorld": { + "description": "Sample description" + } +}'''); + globals.fs.file('l10n.yaml').createSync(); + globals.fs.file('pubspec.yaml').writeAsStringSync('flutter:\n generate: true\n'); + + fakeVmServiceHost = FakeVmServiceHost(requests: []); + final FakeResidentCompiler residentCompiler = FakeResidentCompiler() + ..nextOutput = const CompilerOutput('foo', 1 ,[]); + residentRunner = HotRunner( + [ + flutterDevice, + ], + stayResident: false, + debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug), + target: 'main.dart', + devtoolsHandler: createNoOpHandler, + ); + flutterDevice.generator = residentCompiler; + + await residentRunner.run(); + + final File generatedLocalizationsFile = globals.fs.directory('.dart_tool') + .childDirectory('flutter_gen') + .childDirectory('gen_l10n') + .childFile('app_localizations.dart'); + expect(generatedLocalizationsFile.existsSync(), isTrue); + + // Completing this future ensures that the daemon can exit correctly. + expect(await residentRunner.waitForAppToFinish(), 1); + })); + testUsingContext('ResidentRunner printHelpDetails hot runner', () => testbed.run(() { fakeVmServiceHost = FakeVmServiceHost(requests: []); diff --git a/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart index 6845de238dc..ffbcae8e33c 100644 --- a/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart @@ -1049,6 +1049,69 @@ void main() { ProcessManager: () => processManager, }); + testUsingContext('ResidentWebRunner generates files when l10n.yaml exists', () async { + fakeVmServiceHost = + FakeVmServiceHost(requests: kAttachExpectations.toList()); + setupMocks(); + final ResidentRunner residentWebRunner = ResidentWebRunner( + flutterDevice, + flutterProject: + FlutterProject.fromDirectoryTest(fileSystem.currentDirectory), + debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug), + ipv6: true, + stayResident: false, + fileSystem: fileSystem, + logger: BufferLogger.test(), + usage: globals.flutterUsage, + systemClock: globals.systemClock, + ); + + // Create necessary files. + globals.fs.file(globals.fs.path.join('lib', 'main.dart')) + .createSync(recursive: true); + globals.fs.file(globals.fs.path.join('lib', 'l10n', 'app_en.arb')) + ..createSync(recursive: true) + ..writeAsStringSync(''' +{ + "helloWorld": "Hello, World!", + "@helloWorld": { + "description": "Sample description" + } +}'''); + globals.fs.file('l10n.yaml').createSync(); + globals.fs.file('pubspec.yaml').writeAsStringSync(''' +flutter: + generate: true +'''); + globals.fs.directory('.dart_tool') + .childFile('package_config.json') + ..createSync(recursive: true) + ..writeAsStringSync(''' +{ + "configVersion": 2, + "packages": [ + { + "name": "path_provider_linux", + "rootUri": "../../../path_provider_linux", + "packageUri": "lib/", + "languageVersion": "2.12" + } + ] +} +'''); + expect(await residentWebRunner.run(), 0); + final File generatedLocalizationsFile = globals.fs.directory('.dart_tool') + .childDirectory('flutter_gen') + .childDirectory('gen_l10n') + .childFile('app_localizations.dart'); + expect(generatedLocalizationsFile.existsSync(), isTrue); + // Completing this future ensures that the daemon can exit correctly. + expect(fakeVmServiceHost.hasRemainingExpectations, false); + }, overrides: { + FileSystem: () => fileSystem, + ProcessManager: () => processManager, + }); + // While this file should be ignored on web, generating it here will cause a // perf regression in hot restart. testUsingContext('Does not generate dart_plugin_registrant.dart', () async {