[gen_l10n] Optional Headers (#52125)

* Expose --header and --header-file arguments for gen_l10n
This commit is contained in:
Shi-Hao Hong
2020-03-11 13:55:54 -07:00
committed by GitHub
parent d56266375b
commit 96fd143cd7
6 changed files with 159 additions and 8 deletions

View File

@@ -56,6 +56,23 @@ Future<void> main(List<String> arguments) async {
"For example, pass in ['en_US'] if you would like your app to "
'default to American English if a device supports it.',
);
parser.addOption(
'header',
help: 'The header to prepend to the generated Dart localizations '
'files. This option takes in a string. \n\n'
'For example, pass in "/// All localized files." if you would '
'like this string prepended to the generated Dart file. \n\n'
'Alternatively, see the `header-file` option to pass in a text '
'file for longer headers.'
);
parser.addOption(
'header-file',
help: 'The header to prepend to the generated Dart localizations '
'files. The value of this option is the name of the file that '
'contains the header text. \n\n'
'Alternatively, see the `header` option to pass in a string '
'for a simpler header.'
);
final argslib.ArgResults results = parser.parse(arguments);
if (results['help'] == true) {
@@ -70,6 +87,8 @@ Future<void> main(List<String> arguments) async {
final String templateArbFileName = results['template-arb-file'] as String;
final String classNameString = results['output-class'] as String;
final String preferredSupportedLocaleString = results['preferred-supported-locales'] as String;
final String headerString = results['header'] as String;
final String headerFile = results['header-file'] as String;
const local.LocalFileSystem fs = local.LocalFileSystem();
final LocalizationsGenerator localizationsGenerator = LocalizationsGenerator(fs);
@@ -82,6 +101,8 @@ Future<void> main(List<String> arguments) async {
outputFileString: outputFileString,
classNameString: classNameString,
preferredSupportedLocaleString: preferredSupportedLocaleString,
headerString: headerString,
headerFile: headerFile,
)
..loadResources()
..writeOutputFile();

View File

@@ -262,7 +262,6 @@ class LocalizationsGenerator {
Iterable<Message> _allMessages;
AppResourceBundleCollection _allBundles;
/// The reference to the project's l10n directory.
///
/// It is assumed that all input files (e.g. [templateArbFile], arb files
@@ -322,6 +321,9 @@ class LocalizationsGenerator {
/// [l10nDirectory].
final Set<LocaleInfo> supportedLocales = <LocaleInfo>{};
/// The header to be prepended to the generated Dart localization file.
String header = '';
/// Initializes [l10nDirectory], [templateArbFile], [outputFile] and [className].
///
/// Throws an [L10nException] when a provided configuration is not allowed
@@ -335,11 +337,14 @@ class LocalizationsGenerator {
String outputFileString,
String classNameString,
String preferredSupportedLocaleString,
String headerString,
String headerFile,
}) {
setL10nDirectory(l10nDirectoryPath);
setTemplateArbFile(templateArbFileName);
setOutputFile(outputFileString);
setPreferredSupportedLocales(preferredSupportedLocaleString);
_setHeader(headerString, headerFile);
className = classNameString;
}
@@ -448,6 +453,28 @@ class LocalizationsGenerator {
}
}
void _setHeader(String headerString, String headerFile) {
if (headerString != null && headerFile != null) {
throw L10nException(
'Cannot accept both header and header file arguments. \n'
'Please make sure to define only one or the other. '
);
}
if (headerString != null) {
header = headerString;
} else if (headerFile != null) {
try {
header = _fs.file(path.join(l10nDirectory.path, headerFile)).readAsStringSync();
} on FileSystemException catch (error) {
throw L10nException (
'Failed to read header file: "$headerFile". \n'
'FileSystemException: ${error.message}'
);
}
}
}
static bool _isValidGetterAndMethodName(String name) {
// Public Dart method name must not start with an underscore
if (name[0] == '_')
@@ -525,6 +552,7 @@ class LocalizationsGenerator {
final String lookupBody = generateLookupBody(_allBundles, className);
return fileTemplate
.replaceAll('@(header)', header)
.replaceAll('@(class)', className)
.replaceAll('@(methods)', _allMessages.map(generateBaseClassMethod).join('\n'))
.replaceAll('@(importFile)', '$directory/$outputFileName')

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
const String fileTemplate = '''
@(header)
import 'dart:async';
import 'package:flutter/foundation.dart';