[gen_l10n] Absolute project path parameter (#63368)
This commit is contained in:
@@ -120,6 +120,14 @@ void main(List<String> arguments) {
|
||||
'\n\n'
|
||||
'When null, the JSON file will not be generated.'
|
||||
);
|
||||
parser.addOption(
|
||||
'project-dir',
|
||||
valueHelp: 'absolute/path/to/flutter/project',
|
||||
help: 'When specified, the tool uses the path passed into this option '
|
||||
'as the directory of the root Flutter project.'
|
||||
'\n\n'
|
||||
'When null, the relative path to the present working directory will be used.'
|
||||
);
|
||||
|
||||
final argslib.ArgResults results = parser.parse(arguments);
|
||||
if (results['help'] == true) {
|
||||
@@ -140,6 +148,7 @@ void main(List<String> arguments) {
|
||||
final String headerFile = results['header-file'] as String;
|
||||
final bool useDeferredLoading = results['use-deferred-loading'] as bool;
|
||||
final String inputsAndOutputsListPath = results['gen-inputs-and-outputs-list'] as String;
|
||||
final String projectPathString = results['project-dir'] as String;
|
||||
|
||||
const local.LocalFileSystem fs = local.LocalFileSystem();
|
||||
final LocalizationsGenerator localizationsGenerator = LocalizationsGenerator(fs);
|
||||
@@ -157,6 +166,7 @@ void main(List<String> arguments) {
|
||||
headerFile: headerFile,
|
||||
useDeferredLoading: useDeferredLoading,
|
||||
inputsAndOutputsListPath: inputsAndOutputsListPath,
|
||||
projectPathString: projectPathString,
|
||||
)
|
||||
..loadResources()
|
||||
..writeOutputFiles()
|
||||
|
||||
@@ -403,6 +403,11 @@ class LocalizationsGenerator {
|
||||
/// This directory is specified with the [initialize] method.
|
||||
Directory inputDirectory;
|
||||
|
||||
/// The Flutter project's root directory.
|
||||
///
|
||||
/// This directory is specified with the [initialize] method.
|
||||
Directory projectDirectory;
|
||||
|
||||
/// The directory to generate the project's localizations files in.
|
||||
///
|
||||
/// It is assumed that all output files (e.g. The localizations
|
||||
@@ -518,7 +523,9 @@ class LocalizationsGenerator {
|
||||
String headerFile,
|
||||
bool useDeferredLoading = false,
|
||||
String inputsAndOutputsListPath,
|
||||
String projectPathString,
|
||||
}) {
|
||||
setProjectDir(projectPathString);
|
||||
setInputDirectory(inputPathString);
|
||||
setOutputDirectory(outputPathString ?? inputPathString);
|
||||
setTemplateArbFile(templateArbFileName);
|
||||
@@ -544,22 +551,44 @@ class LocalizationsGenerator {
|
||||
return !(statString[1] == 'w' || statString[4] == 'w' || statString[7] == 'w');
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
void setProjectDir(String projectPathString) {
|
||||
if (projectPathString == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Directory directory = _fs.directory(projectPathString);
|
||||
if (!directory.existsSync()) {
|
||||
throw L10nException(
|
||||
'Directory does not exist: $directory.\n'
|
||||
'Please select a directory that contains the project\'s localizations '
|
||||
'resource files.'
|
||||
);
|
||||
}
|
||||
projectDirectory = directory;
|
||||
}
|
||||
|
||||
/// Sets the reference [Directory] for [inputDirectory].
|
||||
@visibleForTesting
|
||||
void setInputDirectory(String inputPathString) {
|
||||
if (inputPathString == null)
|
||||
throw L10nException('inputPathString argument cannot be null');
|
||||
inputDirectory = _fs.directory(inputPathString);
|
||||
inputDirectory = _fs.directory(
|
||||
projectDirectory != null
|
||||
? _getAbsoluteProjectPath(inputPathString)
|
||||
: inputPathString
|
||||
);
|
||||
|
||||
if (!inputDirectory.existsSync())
|
||||
throw FileSystemException(
|
||||
"The 'input-dir' directory, '$inputDirectory', does not exist.\n"
|
||||
"The 'arb-dir' directory, '$inputDirectory', does not exist.\n"
|
||||
'Make sure that the correct path was provided.'
|
||||
);
|
||||
|
||||
final FileStat fileStat = inputDirectory.statSync();
|
||||
if (_isNotReadable(fileStat) || _isNotWritable(fileStat))
|
||||
throw FileSystemException(
|
||||
"The 'input-dir' directory, '$inputDirectory', doesn't allow reading and writing.\n"
|
||||
"The 'arb-dir' directory, '$inputDirectory', doesn't allow reading and writing.\n"
|
||||
'Please ensure that the user has read and write permissions.'
|
||||
);
|
||||
}
|
||||
@@ -569,7 +598,11 @@ class LocalizationsGenerator {
|
||||
void setOutputDirectory(String outputPathString) {
|
||||
if (outputPathString == null)
|
||||
throw L10nException('outputPathString argument cannot be null');
|
||||
outputDirectory = _fs.directory(outputPathString);
|
||||
outputDirectory = _fs.directory(
|
||||
projectDirectory != null
|
||||
? _getAbsoluteProjectPath(outputPathString)
|
||||
: outputPathString
|
||||
);
|
||||
}
|
||||
|
||||
/// Sets the reference [File] for [templateArbFile].
|
||||
@@ -667,6 +700,8 @@ class LocalizationsGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
String _getAbsoluteProjectPath(String relativePath) => _fs.path.join(projectDirectory.path, relativePath);
|
||||
|
||||
void _setUseDeferredLoading(bool useDeferredLoading) {
|
||||
if (useDeferredLoading == null) {
|
||||
throw L10nException('useDeferredLoading argument cannot be null.');
|
||||
|
||||
Reference in New Issue
Block a user