Unverified Commit d75735ee authored by Tae Hyung Kim's avatar Tae Hyung Kim Committed by GitHub

Use platform specific line separator in gen-l10n (#130090)

Currently files are not generated with `\r\n` in windows. This PR should
fix the issue.

Fixes https://github.com/flutter/flutter/issues/109761.
parent 42924e27
...@@ -47,6 +47,9 @@ Future<LocalizationsGenerator> generateLocalizations({ ...@@ -47,6 +47,9 @@ Future<LocalizationsGenerator> generateLocalizations({
precacheLanguageAndRegionTags(); precacheLanguageAndRegionTags();
// Use \r\n if project's pubspec file contains \r\n.
final bool useCRLF = fileSystem.file('pubspec.yaml').readAsStringSync().contains('\r\n');
LocalizationsGenerator generator; LocalizationsGenerator generator;
try { try {
generator = LocalizationsGenerator( generator = LocalizationsGenerator(
...@@ -71,7 +74,7 @@ Future<LocalizationsGenerator> generateLocalizations({ ...@@ -71,7 +74,7 @@ Future<LocalizationsGenerator> generateLocalizations({
suppressWarnings: options.suppressWarnings, suppressWarnings: options.suppressWarnings,
) )
..loadResources() ..loadResources()
..writeOutputFiles(isFromYaml: true); ..writeOutputFiles(isFromYaml: true, useCRLF: useCRLF);
} on L10nException catch (e) { } on L10nException catch (e) {
throwToolExit(e.message); throwToolExit(e.message);
} }
...@@ -79,7 +82,6 @@ Future<LocalizationsGenerator> generateLocalizations({ ...@@ -79,7 +82,6 @@ Future<LocalizationsGenerator> generateLocalizations({
final List<String> outputFileList = generator.outputFileList; final List<String> outputFileList = generator.outputFileList;
final File? untranslatedMessagesFile = generator.untranslatedMessagesFile; final File? untranslatedMessagesFile = generator.untranslatedMessagesFile;
// All other post processing.
if (options.format) { if (options.format) {
final List<String> formatFileList = outputFileList.toList(); final List<String> formatFileList = outputFileList.toList();
if (untranslatedMessagesFile != null) { if (untranslatedMessagesFile != null) {
...@@ -1310,7 +1312,7 @@ The plural cases must be one of "=0", "=1", "=2", "zero", "one", "two", "few", " ...@@ -1310,7 +1312,7 @@ The plural cases must be one of "=0", "=1", "=2", "zero", "one", "two", "few", "
} }
} }
List<String> writeOutputFiles({ bool isFromYaml = false }) { List<String> writeOutputFiles({ bool isFromYaml = false, bool useCRLF = false }) {
// First, generate the string contents of all necessary files. // First, generate the string contents of all necessary files.
final String generatedLocalizationsFile = _generateCode(); final String generatedLocalizationsFile = _generateCode();
...@@ -1328,7 +1330,9 @@ The plural cases must be one of "=0", "=1", "=2", "zero", "one", "two", "few", " ...@@ -1328,7 +1330,9 @@ The plural cases must be one of "=0", "=1", "=2", "zero", "one", "two", "few", "
syntheticPackageDirectory.createSync(recursive: true); syntheticPackageDirectory.createSync(recursive: true);
final File flutterGenPubspec = syntheticPackageDirectory.childFile('pubspec.yaml'); final File flutterGenPubspec = syntheticPackageDirectory.childFile('pubspec.yaml');
if (!flutterGenPubspec.existsSync()) { if (!flutterGenPubspec.existsSync()) {
flutterGenPubspec.writeAsStringSync(emptyPubspecTemplate); flutterGenPubspec.writeAsStringSync(
useCRLF ? emptyPubspecTemplate.replaceAll('\n', '\r\n') : emptyPubspecTemplate
);
} }
} }
...@@ -1347,11 +1351,13 @@ The plural cases must be one of "=0", "=1", "=2", "zero", "one", "two", "few", " ...@@ -1347,11 +1351,13 @@ The plural cases must be one of "=0", "=1", "=2", "zero", "one", "two", "few", "
// Generate the required files for localizations. // Generate the required files for localizations.
_languageFileMap.forEach((File file, String contents) { _languageFileMap.forEach((File file, String contents) {
file.writeAsStringSync(contents); file.writeAsStringSync(useCRLF ? contents.replaceAll('\n', '\r\n') : contents);
_outputFileList.add(file.absolute.path); _outputFileList.add(file.absolute.path);
}); });
baseOutputFile.writeAsStringSync(generatedLocalizationsFile); baseOutputFile.writeAsStringSync(
useCRLF ? generatedLocalizationsFile.replaceAll('\n', '\r\n') : generatedLocalizationsFile
);
final File? messagesFile = untranslatedMessagesFile; final File? messagesFile = untranslatedMessagesFile;
if (messagesFile != null) { if (messagesFile != null) {
_generateUntranslatedMessagesFile(logger, messagesFile); _generateUntranslatedMessagesFile(logger, messagesFile);
...@@ -1387,12 +1393,12 @@ The plural cases must be one of "=0", "=1", "=2", "zero", "one", "two", "few", " ...@@ -1387,12 +1393,12 @@ The plural cases must be one of "=0", "=1", "=2", "zero", "one", "two", "few", "
if (!inputsAndOutputsListFileLocal.existsSync()) { if (!inputsAndOutputsListFileLocal.existsSync()) {
inputsAndOutputsListFileLocal.createSync(recursive: true); inputsAndOutputsListFileLocal.createSync(recursive: true);
} }
final String filesListContent = json.encode(<String, Object> {
inputsAndOutputsListFileLocal.writeAsStringSync(
json.encode(<String, Object> {
'inputs': _inputFileList, 'inputs': _inputFileList,
'outputs': _outputFileList, 'outputs': _outputFileList,
}), });
inputsAndOutputsListFileLocal.writeAsStringSync(
useCRLF ? filesListContent.replaceAll('\n', '\r\n') : filesListContent,
); );
} }
......
...@@ -78,6 +78,11 @@ void main() { ...@@ -78,6 +78,11 @@ void main() {
"description": "Sample description" "description": "Sample description"
} }
}'''); }''');
fileSystem
.file('pubspec.yaml')
.writeAsStringSync('''
flutter:
generate: true''');
final GenerateLocalizationsCommand command = GenerateLocalizationsCommand( final GenerateLocalizationsCommand command = GenerateLocalizationsCommand(
fileSystem: fileSystem, fileSystem: fileSystem,
...@@ -109,7 +114,11 @@ void main() { ...@@ -109,7 +114,11 @@ void main() {
} }
}'''); }''');
fileSystem.file('header.txt').writeAsStringSync('a header file'); fileSystem.file('header.txt').writeAsStringSync('a header file');
fileSystem
.file('pubspec.yaml')
.writeAsStringSync('''
flutter:
generate: true''');
final GenerateLocalizationsCommand command = GenerateLocalizationsCommand( final GenerateLocalizationsCommand command = GenerateLocalizationsCommand(
fileSystem: fileSystem, fileSystem: fileSystem,
logger: logger, logger: logger,
...@@ -475,7 +484,6 @@ format: true ...@@ -475,7 +484,6 @@ format: true
() async => createTestCommandRunner(command).run(<String>['gen-l10n', '--synthetic-package']), () async => createTestCommandRunner(command).run(<String>['gen-l10n', '--synthetic-package']),
throwsToolExit(message: 'Attempted to generate localizations code without having the flutter: generate flag turned on.') throwsToolExit(message: 'Attempted to generate localizations code without having the flutter: generate flag turned on.')
); );
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => fileSystem, FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
......
...@@ -59,6 +59,13 @@ void _standardFlutterDirectoryL10nSetup(FileSystem fs) { ...@@ -59,6 +59,13 @@ void _standardFlutterDirectoryL10nSetup(FileSystem fs) {
.writeAsStringSync(singleMessageArbFileString); .writeAsStringSync(singleMessageArbFileString);
l10nDirectory.childFile(esArbFileName) l10nDirectory.childFile(esArbFileName)
.writeAsStringSync(singleEsMessageArbFileString); .writeAsStringSync(singleEsMessageArbFileString);
fs.file('pubspec.yaml')
..createSync(recursive: true)
..writeAsStringSync('''
flutter:
generate: true
''');
} }
void main() { void main() {
...@@ -763,7 +770,7 @@ class FooEn extends Foo { ...@@ -763,7 +770,7 @@ class FooEn extends Foo {
_standardFlutterDirectoryL10nSetup(fs); _standardFlutterDirectoryL10nSetup(fs);
// Missing flutter: generate: true should throw exception. // Missing flutter: generate: true should throw exception.
fs.file(fs.path.join(syntheticPackagePath, 'pubspec.yaml')) fs.file('pubspec.yaml')
..createSync(recursive: true) ..createSync(recursive: true)
..writeAsStringSync(''' ..writeAsStringSync('''
flutter: flutter:
...@@ -799,6 +806,34 @@ flutter: ...@@ -799,6 +806,34 @@ flutter:
); );
}); });
testWithoutContext('uses the same line terminator as pubspec.yaml', () async {
_standardFlutterDirectoryL10nSetup(fs);
fs.file('pubspec.yaml')
..createSync(recursive: true)
..writeAsStringSync('''
flutter:\r
generate: true\r
''');
final LocalizationOptions options = LocalizationOptions(
arbDir: fs.path.join('lib', 'l10n'),
outputClass: defaultClassNameString,
outputLocalizationFile: defaultOutputFileString,
);
await generateLocalizations(
fileSystem: fs,
options: options,
logger: BufferLogger.test(),
projectDir: fs.currentDirectory,
dependenciesDir: fs.currentDirectory,
artifacts: artifacts,
processManager: processManager,
);
final String content = getGeneratedFileContent(locale: 'en');
expect(content, contains('\r\n'));
});
testWithoutContext('blank lines generated nicely', () async { testWithoutContext('blank lines generated nicely', () async {
_standardFlutterDirectoryL10nSetup(fs); _standardFlutterDirectoryL10nSetup(fs);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment