Skip to content

Commit

Permalink
fix: download sources with double asterisk (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
yzerk authored Mar 30, 2023
1 parent 8e91f31 commit 88ae567
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ static Stream<Arguments> 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%"),
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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%";
Expand Down

0 comments on commit 88ae567

Please sign in to comment.