From 88ae5673ad6589abca849fcfa0559f242c532cc8 Mon Sep 17 00:00:00 2001 From: yzerk Date: Thu, 30 Mar 2023 15:34:10 +0300 Subject: [PATCH] fix: download sources with double asterisk (#538) --- .../cli/commands/actions/DownloadSourcesAction.java | 2 +- .../cli/commands/functionality/TranslationsUtils.java | 3 ++- .../java/com/crowdin/cli/utils/PlaceholderUtil.java | 4 +++- .../commands/functionality/TranslationsUtilsTest.java | 5 +++++ .../com/crowdin/cli/utils/PlaceholderUtilTest.java | 10 ++++++++++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/crowdin/cli/commands/actions/DownloadSourcesAction.java b/src/main/java/com/crowdin/cli/commands/actions/DownloadSourcesAction.java index fc32a35c4..4d00418ad 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/DownloadSourcesAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/DownloadSourcesAction.java @@ -131,7 +131,7 @@ public void act(Outputter out, PropertiesWithFiles properties, ProjectClient cli for (String filePathKey : filePaths.keySet()) { String exportPattern = Utils.normalizePath(ProjectFilesUtils.getExportPattern(((File) filePaths.get(filePathKey)).getExportOptions())); String translationPattern = TranslationsUtils.replaceDoubleAsterisk(fileBean.getSource(), fileBean.getTranslation(), filePathKey); - if (exportPattern == null || translationPattern.equals(exportPattern)) { + if (exportPattern == null || translationPattern.endsWith(exportPattern)) { filePaths2.add(filePathKey); } } diff --git a/src/main/java/com/crowdin/cli/commands/functionality/TranslationsUtils.java b/src/main/java/com/crowdin/cli/commands/functionality/TranslationsUtils.java index 72cc4abf9..59656c063 100644 --- a/src/main/java/com/crowdin/cli/commands/functionality/TranslationsUtils.java +++ b/src/main/java/com/crowdin/cli/commands/functionality/TranslationsUtils.java @@ -28,7 +28,8 @@ public static String replaceDoubleAsterisk(String sourcePattern, String translat String[] sourceNodes = sourcePattern.split("\\*\\*"); for (int i = 0; i < sourceNodes.length; i++) { if (sourceFile.contains(sourceNodes[i])) { - sourceFile = sourceFile.replaceFirst(Utils.regexPath(sourceNodes[i]), ""); + sourceFile = StringUtils.substring(sourceFile, sourceFile.indexOf(Utils.regexPath(sourceNodes[i])), sourceFile.length() - 1) + .replaceFirst(Utils.regexPath(sourceNodes[i]), ""); } else if (sourceNodes.length - 1 == i) { if (sourceNodes[i].contains(Utils.PATH_SEPARATOR)) { String[] sourceNodesTmp = sourceNodes[i].split(Utils.PATH_SEPARATOR_REGEX); diff --git a/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java b/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java index 68dd91752..1c4deebf9 100644 --- a/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java +++ b/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java @@ -187,8 +187,10 @@ public String replaceFileDependentPlaceholders(String toFormat, File file) { toFormat = toFormat.contains(PLACEHOLDER_ORIGINAL_PATH) ? toFormat.replace(PLACEHOLDER_ORIGINAL_PATH, fileParent) : toFormat; if (toFormat.contains("**")) { + String prefix = StringUtils.substringBefore(toFormat, "**"); + prefix = prefix.length() > 1 && file.getPath().contains(prefix) ? StringUtils.substringBefore(fileParent,prefix) : ""; String doubleAsterisks = - StringUtils.removeStart(fileParent, Utils.noSepAtEnd(Utils.noSepAtStart(StringUtils.substringBefore(toFormat, "**")))); + StringUtils.removeStart(Utils.noSepAtStart(StringUtils.removeStart(fileParent, prefix)), Utils.noSepAtEnd(Utils.noSepAtStart(StringUtils.substringBefore(toFormat, "**")))); toFormat = toFormat.replace("**", doubleAsterisks); } diff --git a/src/test/java/com/crowdin/cli/commands/functionality/TranslationsUtilsTest.java b/src/test/java/com/crowdin/cli/commands/functionality/TranslationsUtilsTest.java index 6196eb4a1..91ab6aa6c 100644 --- a/src/test/java/com/crowdin/cli/commands/functionality/TranslationsUtilsTest.java +++ b/src/test/java/com/crowdin/cli/commands/functionality/TranslationsUtilsTest.java @@ -41,6 +41,11 @@ static Stream testReplaceDoubleAsterisk() { Utils.normalizePath("/%locale%/%original_file_name%"), Utils.normalizePath("f1/android.xml"), Utils.normalizePath("/%locale%/%original_file_name%")), + arguments( + Utils.normalizePath("/folder1/folder2/**/messages.properties"), + Utils.normalizePath("/folder1/folder2/**/%file_name%_%two_letters_code%.properties"), + Utils.normalizePath("/folder_on_crowdin/folder1/folder2/folder3/folder4/messages.properties"), + Utils.normalizePath("/folder1/folder2/folder3/folder4/%file_name%_%two_letters_code%.properties")), arguments( Utils.normalizePath("/home/daanya/Documents/**/*.txt"), Utils.normalizePath("/**/%locale%/%original_file_name%"), diff --git a/src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java b/src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java index 0637df92b..7b07ec36d 100644 --- a/src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java +++ b/src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java @@ -135,6 +135,16 @@ public void testReplaceLanguageDependentPlaceholders() { assertEquals(expected, result); } + @Test + public void testDoubleAsteriskInWildCard() { + PlaceholderUtil placeholderUtil = new PlaceholderUtil(new ArrayList<>(), new ArrayList<>(), "./"); + String source = "folder1/folder2/**/messages.properties"; + String expected = "folder1/folder2/folder3/folder4/messages.properties"; + File crowdinFile = new File("folder_on_crowdin/folder1/folder2/folder3/folder4/messages.properties"); + assertEquals(expected, placeholderUtil.replaceFileDependentPlaceholders(source, crowdinFile)); + } + + @Test public void testReplaceLanguageDependentPlaceholdersLang() { String toFormat = "path/to/%two_letters_code%/%language%/%file_name%";