Refactor "gen-l10n" command to use same code path when "l10n.yaml" is present or not present (#125429)
I think this is a long needed change to the `gen-l10n` command. Essentially, the arguments to `flutter gen-l10n` can be provided by two different methods: via command line arguments or via the `l10n.yaml` file. The existence of a `l10n.yaml` file causes the latter approach to take precedence. However, currently, there's several differences in how the two approaches are handled, and most of the default arguments are all over the place, causing unexpected issues such as #120457 or #120023. This PR refactors the command so that * `LocalizationOptions` are more consistent with the actual argument names/yaml options. * All default values are determined in `LocalizationOptions`'s constructor (or in `argParser.addOption(...)` in the case a boolean value needs to be explicitly true). * New `parseLocalizationsOptionsFromCommand` function to parse arguments. * Parse `LocalizationOptions` at the beginning of `runCommand()` and pass it to `generateLocalizations`. Fixes #120023.
This commit is contained in:
@@ -42,6 +42,8 @@ void main() {
|
||||
"description": "Sample description"
|
||||
}
|
||||
}''');
|
||||
final File pubspecFile = fileSystem.file('pubspec.yaml')..createSync();
|
||||
pubspecFile.writeAsStringSync(BasicProjectWithFlutterGen().pubspec);
|
||||
final GenerateLocalizationsCommand command = GenerateLocalizationsCommand(
|
||||
fileSystem: fileSystem,
|
||||
logger: logger,
|
||||
@@ -192,6 +194,8 @@ void main() {
|
||||
"description": "Sample description"
|
||||
}
|
||||
}''');
|
||||
final File pubspecFile = fileSystem.file('pubspec.yaml')..createSync();
|
||||
pubspecFile.writeAsStringSync(BasicProjectWithFlutterGen().pubspec);
|
||||
processManager.addCommand(
|
||||
const FakeCommand(
|
||||
command: <String>[
|
||||
@@ -351,4 +355,83 @@ untranslated-messages-file: lib/l10n/untranslated.json
|
||||
FileSystem: () => fileSystem,
|
||||
ProcessManager: () => FakeProcessManager.any(),
|
||||
});
|
||||
|
||||
testUsingContext('throw when generate: false and uses synthetic package when run with l10n.yaml', () async {
|
||||
final File arbFile = fileSystem.file(fileSystem.path.join('lib', 'l10n', 'app_en.arb'))
|
||||
..createSync(recursive: true);
|
||||
arbFile.writeAsStringSync('''
|
||||
{
|
||||
"helloWorld": "Hello, World!",
|
||||
"@helloWorld": {
|
||||
"description": "Sample description"
|
||||
}
|
||||
}''');
|
||||
fileSystem.file('l10n.yaml').createSync();
|
||||
final File pubspecFile = fileSystem.file('pubspec.yaml')..createSync();
|
||||
pubspecFile.writeAsStringSync('''
|
||||
name: test
|
||||
environment:
|
||||
sdk: '>=3.0.0-0 <4.0.0'
|
||||
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
|
||||
flutter:
|
||||
generate: false
|
||||
''');
|
||||
final GenerateLocalizationsCommand command = GenerateLocalizationsCommand(
|
||||
fileSystem: fileSystem,
|
||||
logger: logger,
|
||||
artifacts: artifacts,
|
||||
processManager: processManager,
|
||||
);
|
||||
expect(
|
||||
() async => createTestCommandRunner(command).run(<String>['gen-l10n']),
|
||||
throwsToolExit(message: 'Attempted to generate localizations code without having the flutter: generate flag turned on.')
|
||||
);
|
||||
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => fileSystem,
|
||||
ProcessManager: () => FakeProcessManager.any(),
|
||||
});
|
||||
|
||||
testUsingContext('throw when generate: false and uses synthetic package when run via commandline options', () async {
|
||||
final File arbFile = fileSystem.file(fileSystem.path.join('lib', 'l10n', 'app_en.arb'))
|
||||
..createSync(recursive: true);
|
||||
arbFile.writeAsStringSync('''
|
||||
{
|
||||
"helloWorld": "Hello, World!",
|
||||
"@helloWorld": {
|
||||
"description": "Sample description"
|
||||
}
|
||||
}''');
|
||||
final File pubspecFile = fileSystem.file('pubspec.yaml')..createSync();
|
||||
pubspecFile.writeAsStringSync('''
|
||||
name: test
|
||||
environment:
|
||||
sdk: '>=3.0.0-0 <4.0.0'
|
||||
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
|
||||
flutter:
|
||||
generate: false
|
||||
''');
|
||||
final GenerateLocalizationsCommand command = GenerateLocalizationsCommand(
|
||||
fileSystem: fileSystem,
|
||||
logger: logger,
|
||||
artifacts: artifacts,
|
||||
processManager: processManager,
|
||||
);
|
||||
expect(
|
||||
() async => createTestCommandRunner(command).run(<String>['gen-l10n', '--synthetic-package']),
|
||||
throwsToolExit(message: 'Attempted to generate localizations code without having the flutter: generate flag turned on.')
|
||||
);
|
||||
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => fileSystem,
|
||||
ProcessManager: () => FakeProcessManager.any(),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -49,23 +49,24 @@ required-resource-attributes: false
|
||||
nullable-getter: false
|
||||
''');
|
||||
|
||||
final LocalizationOptions options = parseLocalizationsOptions(
|
||||
final LocalizationOptions options = parseLocalizationsOptionsFromYAML(
|
||||
file: configFile,
|
||||
logger: BufferLogger.test(),
|
||||
defaultArbDir: fileSystem.path.join('lib', 'l10n'),
|
||||
);
|
||||
|
||||
expect(options.arbDirectory, Uri.parse('arb'));
|
||||
expect(options.templateArbFile, Uri.parse('example.arb'));
|
||||
expect(options.outputLocalizationsFile, Uri.parse('bar'));
|
||||
expect(options.untranslatedMessagesFile, Uri.parse('untranslated'));
|
||||
expect(options.arbDir, Uri.parse('arb').path);
|
||||
expect(options.templateArbFile, Uri.parse('example.arb').path);
|
||||
expect(options.outputLocalizationFile, Uri.parse('bar').path);
|
||||
expect(options.untranslatedMessagesFile, Uri.parse('untranslated').path);
|
||||
expect(options.outputClass, 'Foo');
|
||||
expect(options.headerFile, Uri.parse('header'));
|
||||
expect(options.headerFile, Uri.parse('header').path);
|
||||
expect(options.header, 'HEADER');
|
||||
expect(options.deferredLoading, true);
|
||||
expect(options.useDeferredLoading, true);
|
||||
expect(options.preferredSupportedLocales, <String>['en_US']);
|
||||
expect(options.useSyntheticPackage, false);
|
||||
expect(options.areResourceAttributesRequired, false);
|
||||
expect(options.usesNullableGetter, false);
|
||||
expect(options.syntheticPackage, false);
|
||||
expect(options.requiredResourceAttributes, false);
|
||||
expect(options.nullableGetter, false);
|
||||
});
|
||||
|
||||
testWithoutContext('parseLocalizationsOptions handles preferredSupportedLocales as list', () async {
|
||||
@@ -74,9 +75,10 @@ nullable-getter: false
|
||||
preferred-supported-locales: ['en_US', 'de']
|
||||
''');
|
||||
|
||||
final LocalizationOptions options = parseLocalizationsOptions(
|
||||
final LocalizationOptions options = parseLocalizationsOptionsFromYAML(
|
||||
file: configFile,
|
||||
logger: BufferLogger.test(),
|
||||
defaultArbDir: fileSystem.path.join('lib', 'l10n'),
|
||||
);
|
||||
|
||||
expect(options.preferredSupportedLocales, <String>['en_US', 'de']);
|
||||
@@ -91,9 +93,10 @@ use-deferred-loading: string
|
||||
''');
|
||||
|
||||
expect(
|
||||
() => parseLocalizationsOptions(
|
||||
() => parseLocalizationsOptionsFromYAML(
|
||||
file: configFile,
|
||||
logger: BufferLogger.test(),
|
||||
defaultArbDir: fileSystem.path.join('lib', 'l10n'),
|
||||
),
|
||||
throwsException,
|
||||
);
|
||||
@@ -106,9 +109,10 @@ template-arb-file: {name}_en.arb
|
||||
''');
|
||||
|
||||
expect(
|
||||
() => parseLocalizationsOptions(
|
||||
() => parseLocalizationsOptionsFromYAML(
|
||||
file: configFile,
|
||||
logger: BufferLogger.test(),
|
||||
defaultArbDir: fileSystem.path.join('lib', 'l10n'),
|
||||
),
|
||||
throwsToolExit(),
|
||||
);
|
||||
|
||||
@@ -796,10 +796,10 @@ void main() {
|
||||
generateLocalizations(
|
||||
fileSystem: fs,
|
||||
options: LocalizationOptions(
|
||||
arbDirectory: Uri.directory(defaultL10nPathString),
|
||||
outputDirectory: Uri.directory(defaultL10nPathString, windows: false),
|
||||
templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false),
|
||||
useSyntheticPackage: false,
|
||||
arbDir: Uri.directory(defaultL10nPathString).path,
|
||||
outputDir: Uri.directory(defaultL10nPathString, windows: false).path,
|
||||
templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false).path,
|
||||
syntheticPackage: false,
|
||||
),
|
||||
logger: logger,
|
||||
projectDir: fs.currentDirectory,
|
||||
@@ -811,17 +811,17 @@ void main() {
|
||||
_standardFlutterDirectoryL10nSetup(fs);
|
||||
final LocalizationOptions options = LocalizationOptions(
|
||||
header: 'HEADER',
|
||||
arbDirectory: Uri.directory(defaultL10nPathString),
|
||||
deferredLoading: true,
|
||||
arbDir: Uri.directory(defaultL10nPathString).path,
|
||||
useDeferredLoading: true,
|
||||
outputClass: 'Foo',
|
||||
outputLocalizationsFile: Uri.file('bar.dart', windows: false),
|
||||
outputDirectory: Uri.directory(defaultL10nPathString, windows: false),
|
||||
outputLocalizationFile: Uri.file('bar.dart', windows: false).path,
|
||||
outputDir: Uri.directory(defaultL10nPathString, windows: false).path,
|
||||
preferredSupportedLocales: <String>['es'],
|
||||
templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false),
|
||||
untranslatedMessagesFile: Uri.file('untranslated', windows: false),
|
||||
useSyntheticPackage: false,
|
||||
areResourceAttributesRequired: true,
|
||||
usesNullableGetter: false,
|
||||
templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false).path,
|
||||
untranslatedMessagesFile: Uri.file('untranslated', windows: false).path,
|
||||
syntheticPackage: false,
|
||||
requiredResourceAttributes: true,
|
||||
nullableGetter: false,
|
||||
);
|
||||
|
||||
// Verify that values are correctly passed through the localizations target.
|
||||
@@ -877,14 +877,14 @@ flutter:
|
||||
|
||||
final LocalizationOptions options = LocalizationOptions(
|
||||
header: 'HEADER',
|
||||
headerFile: Uri.file('header', windows: false),
|
||||
arbDirectory: Uri.file('arb', windows: false),
|
||||
deferredLoading: true,
|
||||
headerFile: Uri.file('header', windows: false).path,
|
||||
arbDir: Uri.file('arb', windows: false).path,
|
||||
useDeferredLoading: true,
|
||||
outputClass: 'Foo',
|
||||
outputLocalizationsFile: Uri.file('bar', windows: false),
|
||||
outputLocalizationFile: Uri.file('bar', windows: false).path,
|
||||
preferredSupportedLocales: <String>['en_US'],
|
||||
templateArbFile: Uri.file('example.arb', windows: false),
|
||||
untranslatedMessagesFile: Uri.file('untranslated', windows: false),
|
||||
templateArbFile: Uri.file('example.arb', windows: false).path,
|
||||
untranslatedMessagesFile: Uri.file('untranslated', windows: false).path,
|
||||
);
|
||||
|
||||
expect(
|
||||
@@ -909,10 +909,10 @@ flutter:
|
||||
generateLocalizations(
|
||||
fileSystem: fs,
|
||||
options: LocalizationOptions(
|
||||
arbDirectory: Uri.directory(defaultL10nPathString),
|
||||
outputDirectory: Uri.directory(defaultL10nPathString, windows: false),
|
||||
templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false),
|
||||
useSyntheticPackage: false,
|
||||
arbDir: Uri.directory(defaultL10nPathString).path,
|
||||
outputDir: Uri.directory(defaultL10nPathString, windows: false).path,
|
||||
templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false).path,
|
||||
syntheticPackage: false,
|
||||
),
|
||||
logger: BufferLogger.test(),
|
||||
projectDir: fs.currentDirectory,
|
||||
@@ -936,10 +936,10 @@ class AppLocalizationsEn extends AppLocalizations {
|
||||
fileSystem: fs,
|
||||
options: LocalizationOptions(
|
||||
header: 'HEADER',
|
||||
arbDirectory: Uri.directory(defaultL10nPathString),
|
||||
outputDirectory: Uri.directory(defaultL10nPathString, windows: false),
|
||||
templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false),
|
||||
useSyntheticPackage: false,
|
||||
arbDir: Uri.directory(defaultL10nPathString).path,
|
||||
outputDir: Uri.directory(defaultL10nPathString, windows: false).path,
|
||||
templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false).path,
|
||||
syntheticPackage: false,
|
||||
),
|
||||
logger: logger,
|
||||
projectDir: fs.currentDirectory,
|
||||
|
||||
Reference in New Issue
Block a user