Skip to content

Commit bd66a66

Browse files
authored
fix: ignore pattern with language mapping in config (#753)
1 parent e08595e commit bd66a66

File tree

5 files changed

+72
-37
lines changed

5 files changed

+72
-37
lines changed

src/main/java/com/crowdin/cli/commands/actions/UploadSourcesAction.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
117117
throw e;
118118
}
119119

120-
List<String> sources = SourcesUtils.getFiles(pb.getBasePath(), file.getSource(), file.getIgnore(), placeholderUtil)
120+
LanguageMapping localLanguageMapping = LanguageMapping.fromConfigFileLanguageMapping(file.getLanguagesMapping());
121+
LanguageMapping serverLanguageMapping = project.getLanguageMapping();
122+
LanguageMapping languageMapping = LanguageMapping.populate(localLanguageMapping, serverLanguageMapping);
123+
List<String> sources = SourcesUtils.getFiles(pb.getBasePath(), file.getSource(), file.getIgnore(), placeholderUtil, languageMapping)
121124
.map(File::getAbsolutePath)
122125
.collect(Collectors.toList());
123126
String commonPath =

src/main/java/com/crowdin/cli/commands/functionality/SourcesUtils.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.crowdin.cli.commands.functionality;
22

3+
import com.crowdin.cli.client.LanguageMapping;
34
import com.crowdin.cli.properties.helper.FileHelper;
45
import com.crowdin.cli.utils.PlaceholderUtil;
56
import com.crowdin.cli.utils.Utils;
@@ -19,19 +20,23 @@
1920
import static java.util.Arrays.asList;
2021

2122
public class SourcesUtils {
22-
public static Stream<File> getFiles(String basePath, String sourcePattern, List<String> ignorePattern, PlaceholderUtil placeholderUtil) {
23+
public static Stream<File> getFiles(String basePath, String sourcePattern, List<String> ignorePattern, PlaceholderUtil placeholderUtil, LanguageMapping languageMapping) {
2324
if (basePath == null || sourcePattern == null || placeholderUtil == null) {
2425
throw new NullPointerException("null args in SourceUtils.getFiles");
2526
}
2627
FileHelper fileHelper = new FileHelper(basePath);
2728
String relativePath = StringUtils.removeStart(sourcePattern, basePath);
2829
List<File> sources = fileHelper.getFiles(relativePath);
29-
List<String> formattedIgnores = placeholderUtil.format(sources, ignorePattern, false);
30+
List<String> formattedIgnores = placeholderUtil.format(sources, ignorePattern, languageMapping);
3031
return fileHelper.filterOutIgnoredFiles(sources, formattedIgnores)
3132
.stream()
3233
.filter(File::isFile);
3334
}
3435

36+
public static Stream<File> getFiles(String basePath, String sourcePattern, List<String> ignorePattern, PlaceholderUtil placeholderUtil) {
37+
return SourcesUtils.getFiles(basePath, sourcePattern, ignorePattern, placeholderUtil, null);
38+
}
39+
3540
public static List<String> filterProjectFiles(
3641
List<String> filePaths, String sourcePattern, List<String> ignorePatterns, boolean preserveHierarchy, PlaceholderUtil placeholderUtil
3742
) {
@@ -44,7 +49,7 @@ public static List<String> filterProjectFiles(
4449
Predicate<String> ignorePredicate;
4550
if (preserveHierarchy) {
4651
sourcePredicate = Pattern.compile("^" + PlaceholderUtil.formatSourcePatternForRegex(sourcePattern) + "$").asPredicate();
47-
ignorePredicate = placeholderUtil.formatForRegex(ignorePatterns, false).stream()
52+
ignorePredicate = placeholderUtil.formatForRegex(ignorePatterns).stream()
4853
.map(Pattern::compile)
4954
.map(Pattern::asPredicate)
5055
.map(Predicate::negate)
@@ -71,7 +76,7 @@ public static List<String> filterProjectFiles(
7176
};
7277
ignorePredicate = ignorePatterns.stream()
7378
.map(ignorePattern -> {
74-
List<String> ignorePatternPaths = placeholderUtil.formatForRegex(asList(ignorePattern.split(Pattern.quote(File.separator))), false);
79+
List<String> ignorePatternPaths = placeholderUtil.formatForRegex(asList(ignorePattern.split(Pattern.quote(File.separator))));
7580
Collections.reverse(ignorePatternPaths);
7681
return ignorePatternPaths;
7782
})

src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java

+32-18
Original file line numberDiff line numberDiff line change
@@ -80,31 +80,46 @@ public PlaceholderUtil(List<Language> supportedLanguages, List<com.crowdin.clien
8080
this.basePath = basePath;
8181
}
8282

83-
public List<String> format(List<File> sources, List<String> toFormat, boolean onProjectLangs) {
83+
public List<String> format(List<File> sources, List<String> toFormat) {
8484
if (sources == null || toFormat == null) {
8585
return new ArrayList<>();
8686
}
8787
List<String> res = new ArrayList<>();
8888
for (String str : toFormat) {
89-
res.addAll(this.format(sources, str, onProjectLangs));
89+
res.addAll(this.format(sources, str, null));
9090
}
9191
return res;
9292
}
9393

94-
public Set<String> format(List<File> sources, String toFormat, boolean onProjectLangs) {
94+
public List<String> format(List<File> sources, List<String> toFormat, LanguageMapping languageMapping) {
9595
if (sources == null || toFormat == null) {
96-
return new HashSet<>();
96+
return new ArrayList<>();
97+
}
98+
List<String> res = new ArrayList<>();
99+
for (String str : toFormat) {
100+
res.addAll(this.format(sources, str, languageMapping));
97101
}
102+
return res;
103+
}
98104

99-
List<Language> languages = (onProjectLangs ? projectLanguages : supportedLanguages);
105+
public Set<String> format(List<File> sources, String toFormat, LanguageMapping languageMapping) {
106+
if (sources == null || toFormat == null) {
107+
return new HashSet<>();
108+
}
100109

101-
return languages.stream()
102-
.map(lang -> this.replaceLanguageDependentPlaceholders(toFormat, lang))
110+
return supportedLanguages.stream()
111+
.map(lang -> languageMapping == null
112+
? this.replaceLanguageDependentPlaceholders(toFormat, lang)
113+
: this.replaceLanguageDependentPlaceholders(toFormat, languageMapping, lang))
103114
.flatMap(changedToFormat -> sources.stream()
104115
.map(source -> this.replaceFileDependentPlaceholders(changedToFormat, source)))
105116
.collect(Collectors.toSet());
106117
}
107118

119+
public Set<String> format(List<File> sources, String toFormat) {
120+
return this.format(sources, toFormat, null);
121+
}
122+
108123
public String replaceLanguageDependentPlaceholders(String toFormat, Language lang) {
109124
if (toFormat == null || lang == null) {
110125
throw new NullPointerException("null args in replaceLanguageDependentPlaceholders()");
@@ -206,18 +221,17 @@ public String replaceFileDependentPlaceholders(String toFormat, File file) {
206221
return StringUtils.removeStart(toFormat, Utils.PATH_SEPARATOR);
207222
}
208223

209-
public List<String> formatForRegex(List<String> toFormat, boolean onProjectLangs) {
210-
List<Language> langs = (onProjectLangs) ? this.projectLanguages : this.supportedLanguages;
211-
String langIds = langs.stream().map(Language::getId).collect(Collectors.joining("|", "(", ")"));
212-
String langNames = langs.stream().map(Language::getName).collect(Collectors.joining("|", "(", ")"));
213-
String langLocales = langs.stream().map(Language::getLocale).collect(Collectors.joining("|", "(", ")"));
214-
String langLocalesWithUnderscore = langs.stream().map(Language::getLocale).map(s -> s.replace("-", "_"))
224+
public List<String> formatForRegex(List<String> toFormat) {
225+
String langIds = supportedLanguages.stream().map(Language::getId).collect(Collectors.joining("|", "(", ")"));
226+
String langNames = supportedLanguages.stream().map(Language::getName).collect(Collectors.joining("|", "(", ")"));
227+
String langLocales = supportedLanguages.stream().map(Language::getLocale).collect(Collectors.joining("|", "(", ")"));
228+
String langLocalesWithUnderscore = supportedLanguages.stream().map(Language::getLocale).map(s -> s.replace("-", "_"))
215229
.collect(Collectors.joining("|", "(", ")"));
216-
String langTwoLettersCodes = langs.stream().map(Language::getTwoLettersCode).collect(Collectors.joining("|", "(", ")"));
217-
String langThreeLettersCodes = langs.stream().map(Language::getThreeLettersCode).collect(Collectors.joining("|", "(", ")"));
218-
String langAndroidCodes = langs.stream().map(Language::getAndroidCode).collect(Collectors.joining("|", "(", ")"));
219-
String langOsxLocales = langs.stream().map(Language::getOsxLocale).collect(Collectors.joining("|", "(", ")"));
220-
String langOsxCodes = langs.stream().map(Language::getOsxCode).collect(Collectors.joining("|", "(", ")"));
230+
String langTwoLettersCodes = supportedLanguages.stream().map(Language::getTwoLettersCode).collect(Collectors.joining("|", "(", ")"));
231+
String langThreeLettersCodes = supportedLanguages.stream().map(Language::getThreeLettersCode).collect(Collectors.joining("|", "(", ")"));
232+
String langAndroidCodes = supportedLanguages.stream().map(Language::getAndroidCode).collect(Collectors.joining("|", "(", ")"));
233+
String langOsxLocales = supportedLanguages.stream().map(Language::getOsxLocale).collect(Collectors.joining("|", "(", ")"));
234+
String langOsxCodes = supportedLanguages.stream().map(Language::getOsxCode).collect(Collectors.joining("|", "(", ")"));
221235
return toFormat.stream()
222236
.map(PlaceholderUtil::formatSourcePatternForRegex)
223237
.map(s -> s

src/test/java/com/crowdin/cli/commands/functionality/SourcesUtilsTest.java

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.crowdin.cli.commands.functionality;
22

3+
import com.crowdin.cli.client.LanguageMapping;
34
import com.crowdin.cli.properties.helper.TempProject;
45
import com.crowdin.cli.utils.PlaceholderUtilBuilder;
56
import com.crowdin.cli.utils.Utils;
@@ -16,9 +17,7 @@
1617
import java.io.File;
1718
import java.nio.file.Path;
1819
import java.nio.file.Paths;
19-
import java.util.Arrays;
20-
import java.util.Collections;
21-
import java.util.List;
20+
import java.util.*;
2221
import java.util.stream.Collectors;
2322
import java.util.stream.Stream;
2423

@@ -91,6 +90,27 @@ static Stream<Arguments> testGetFiles1() {
9190
);
9291
}
9392

93+
@Test
94+
public void testGetFiles_LanguageMapping() {
95+
tempProject.addFile("folder/first.xml");
96+
tempProject.addFile("folder/second.eng.txt");
97+
tempProject.addFile("folder/second.xml");
98+
HashMap<String, Map<String, String>> mapping = new HashMap<String, Map<String, String>>() {{
99+
put("two_letters_code", new HashMap<String, String>() {{
100+
put("en", "eng");
101+
}}
102+
);
103+
}};
104+
LanguageMapping languageMapping = LanguageMapping.fromConfigFileLanguageMapping(mapping);
105+
Stream<File> sources = SourcesUtils.getFiles(
106+
tempProject.getBasePath(),
107+
"/folder/*",
108+
Collections.singletonList("/folder/**/*.%two_letters_code%.*"),
109+
PlaceholderUtilBuilder.STANDART.build(tempProject.getBasePath()),
110+
languageMapping);
111+
assertEquals(2, sources.count());
112+
}
113+
94114
@ParameterizedTest
95115
@MethodSource
96116
@DisabledOnOs(OS.WINDOWS)

src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java

+4-11
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void testMainFunctionality(
3939
"/proj/path/"
4040
);
4141

42-
Set<String> result = placeholderUtil.format(Arrays.asList(sources), toFormat, true);
42+
Set<String> result = placeholderUtil.format(Arrays.asList(sources), toFormat);
4343

4444
assertEquals(new HashSet<>(Arrays.asList(expected)), result);
4545
}
@@ -53,13 +53,6 @@ static Stream<Arguments> testMainFunctionality() {
5353
Utils.normalizePath("resources/%two_letters_code%_%original_file_name%"),
5454
new String[] {Utils.normalizePath("resources/ua_messages.xml"), Utils.normalizePath("resources/ru_messages.xml")}
5555
),
56-
arguments(// Must be only de_messages
57-
new Language[] { LanguageBuilder.DEU.build(), LanguageBuilder.ENG.build() },
58-
new Language[] { LanguageBuilder.DEU.build() },
59-
new File[] {new File("resources/messages.xml")},
60-
Utils.normalizePath("resources/%two_letters_code%_%original_file_name%"),
61-
new String[] {Utils.normalizePath("resources/de_messages.xml")}
62-
),
6356
arguments(// How to treat double asterisks
6457
new Language[] {LanguageBuilder.ENG.build()},
6558
new Language[] {LanguageBuilder.ENG.build()},
@@ -92,7 +85,7 @@ public void testMainFunctionalityWithLists(
9285
"/proj/path/"
9386
);
9487

95-
List<String> result = placeholderUtil.format(Arrays.asList(sources), Arrays.asList(toFormat), true);
88+
List<String> result = placeholderUtil.format(Arrays.asList(sources), Arrays.asList(toFormat));
9689

9790
assertEquals(new HashSet<>(Arrays.asList(expected)), new HashSet<>(result));
9891
}
@@ -122,8 +115,8 @@ public void testForNpe() {
122115
assertThrows(NullPointerException.class, () -> new PlaceholderUtil(null, null, null));
123116
PlaceholderUtil placeholderUtil = new PlaceholderUtil(new ArrayList<>(), new ArrayList<>(), "/here/it/goes/");
124117

125-
assertEquals(new ArrayList<String>(), placeholderUtil.format(null, new ArrayList<>(), false));
126-
assertEquals(new HashSet<>(), placeholderUtil.format(null, "", false));
118+
assertEquals(new ArrayList<String>(), placeholderUtil.format(null, new ArrayList<>()));
119+
assertEquals(new HashSet<>(), placeholderUtil.format(null, ""));
127120

128121
assertThrows(NullPointerException.class, () -> placeholderUtil.replaceLanguageDependentPlaceholders(null, new Language()));
129122
assertThrows(NullPointerException.class, () -> placeholderUtil.replaceLanguageDependentPlaceholders(null, null, null));

0 commit comments

Comments
 (0)