Skip to content

Commit

Permalink
More relevant suggestion for --target value when failed to find appro…
Browse files Browse the repository at this point in the history
…priate file for import.

Fixed invalid "--diffs" flag name.
  • Loading branch information
greymag committed Jun 11, 2024
1 parent 08b0340 commit 12257b1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[L10n] `import_xml`:
* More relevant suggestion for `--target` value when failed to find appropriate file for import.
* **Fixed**: Invalid `--diff` flag name in suggestion (should be `--diffs`).

## 1.6.13

* **Fixed**: Program always have 0 exit code, even when execution failed.
Expand Down
45 changes: 40 additions & 5 deletions lib/commands/l10n/import_xml_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class ImportXmlCommand extends L10nCommandBase {
: (fileForImport == null
? config.getMainXmlFileName()
: _getFilenameByBaseName(fileForImport));
// TODO: define dest file by name of imported file (change help for _argFile)
final destFilename = importAll
? null
: (targetFileName == null
Expand Down Expand Up @@ -298,10 +299,6 @@ class ImportXmlCommand extends L10nCommandBase {
.firstWhereOrNull((e) => e.startsWith(filenamePrefix));

final String? suggestFile;
final suggestTarget = destFilename != null
? path.basenameWithoutExtension(destFilename)
: null;

if (candidateFile != null) {
final invalidName = path
.basenameWithoutExtension(candidateFile)
Expand All @@ -312,6 +309,44 @@ class ImportXmlCommand extends L10nCommandBase {
suggestFile = null;
}

String? suggestTarget;
if (targetFileName == null && suggestFile != null) {
// check for appropriate target file
// common error, than filename includes base or "en" locale
final baseLocale = config.baseLocaleForXml;
final junkSuffixes = {null, baseLocale, 'en'};

for (final suffix in junkSuffixes) {
final String guessName;
if (suffix == null) {
guessName = suggestFile;
} else {
final nameEnd = '_$suffix';
if (!suggestFile.endsWith(nameEnd)) continue;
guessName = suggestFile.substring(
0, suggestFile.length - nameEnd.length);
}

// check if exist
final guessFile = File(
path.join(
config.getXmlFilesPath(baseLocale),
_getFilenameByBaseName(guessName),
),
);

if (guessFile.existsSync()) {
suggestTarget =
path.basenameWithoutExtension(guessFile.path);
break;
}
}
}

if (suggestTarget == null && destFilename != null) {
suggestTarget = path.basenameWithoutExtension(destFilename);
}

sb
..writeln('')
..writeln('')
Expand All @@ -336,7 +371,7 @@ class ImportXmlCommand extends L10nCommandBase {

sb
..writeln('')
..write('--diff - If you are importing diffs file.');
..write('--$_argDiffs - If you are importing diffs file.');
}

throw RunException.err(sb.toString());
Expand Down

0 comments on commit 12257b1

Please sign in to comment.