diff --git a/src/main/java/com/crowdin/cli/commands/actions/UploadSourcesAction.java b/src/main/java/com/crowdin/cli/commands/actions/UploadSourcesAction.java index 4f741a4f..33f44d17 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/UploadSourcesAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/UploadSourcesAction.java @@ -117,7 +117,10 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) { throw e; } - List sources = SourcesUtils.getFiles(pb.getBasePath(), file.getSource(), file.getIgnore(), placeholderUtil) + LanguageMapping localLanguageMapping = LanguageMapping.fromConfigFileLanguageMapping(file.getLanguagesMapping()); + LanguageMapping serverLanguageMapping = project.getLanguageMapping(); + LanguageMapping languageMapping = LanguageMapping.populate(localLanguageMapping, serverLanguageMapping); + List sources = SourcesUtils.getFiles(pb.getBasePath(), file.getSource(), file.getIgnore(), placeholderUtil, languageMapping) .map(File::getAbsolutePath) .collect(Collectors.toList()); String commonPath = diff --git a/src/main/java/com/crowdin/cli/commands/functionality/SourcesUtils.java b/src/main/java/com/crowdin/cli/commands/functionality/SourcesUtils.java index 87d2da1d..4469253e 100644 --- a/src/main/java/com/crowdin/cli/commands/functionality/SourcesUtils.java +++ b/src/main/java/com/crowdin/cli/commands/functionality/SourcesUtils.java @@ -1,5 +1,6 @@ package com.crowdin.cli.commands.functionality; +import com.crowdin.cli.client.LanguageMapping; import com.crowdin.cli.properties.helper.FileHelper; import com.crowdin.cli.utils.PlaceholderUtil; import com.crowdin.cli.utils.Utils; @@ -19,19 +20,23 @@ import static java.util.Arrays.asList; public class SourcesUtils { - public static Stream getFiles(String basePath, String sourcePattern, List ignorePattern, PlaceholderUtil placeholderUtil) { + public static Stream getFiles(String basePath, String sourcePattern, List ignorePattern, PlaceholderUtil placeholderUtil, LanguageMapping languageMapping) { if (basePath == null || sourcePattern == null || placeholderUtil == null) { throw new NullPointerException("null args in SourceUtils.getFiles"); } FileHelper fileHelper = new FileHelper(basePath); String relativePath = StringUtils.removeStart(sourcePattern, basePath); List sources = fileHelper.getFiles(relativePath); - List formattedIgnores = placeholderUtil.format(sources, ignorePattern, false); + List formattedIgnores = placeholderUtil.format(sources, ignorePattern, languageMapping); return fileHelper.filterOutIgnoredFiles(sources, formattedIgnores) .stream() .filter(File::isFile); } + public static Stream getFiles(String basePath, String sourcePattern, List ignorePattern, PlaceholderUtil placeholderUtil) { + return SourcesUtils.getFiles(basePath, sourcePattern, ignorePattern, placeholderUtil, null); + } + public static List filterProjectFiles( List filePaths, String sourcePattern, List ignorePatterns, boolean preserveHierarchy, PlaceholderUtil placeholderUtil ) { @@ -44,7 +49,7 @@ public static List filterProjectFiles( Predicate ignorePredicate; if (preserveHierarchy) { sourcePredicate = Pattern.compile("^" + PlaceholderUtil.formatSourcePatternForRegex(sourcePattern) + "$").asPredicate(); - ignorePredicate = placeholderUtil.formatForRegex(ignorePatterns, false).stream() + ignorePredicate = placeholderUtil.formatForRegex(ignorePatterns).stream() .map(Pattern::compile) .map(Pattern::asPredicate) .map(Predicate::negate) @@ -71,7 +76,7 @@ public static List filterProjectFiles( }; ignorePredicate = ignorePatterns.stream() .map(ignorePattern -> { - List ignorePatternPaths = placeholderUtil.formatForRegex(asList(ignorePattern.split(Pattern.quote(File.separator))), false); + List ignorePatternPaths = placeholderUtil.formatForRegex(asList(ignorePattern.split(Pattern.quote(File.separator)))); Collections.reverse(ignorePatternPaths); return ignorePatternPaths; }) diff --git a/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java b/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java index e5cdabb8..975b6b5a 100644 --- a/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java +++ b/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java @@ -80,31 +80,46 @@ public PlaceholderUtil(List supportedLanguages, List format(List sources, List toFormat, boolean onProjectLangs) { + public List format(List sources, List toFormat) { if (sources == null || toFormat == null) { return new ArrayList<>(); } List res = new ArrayList<>(); for (String str : toFormat) { - res.addAll(this.format(sources, str, onProjectLangs)); + res.addAll(this.format(sources, str, null)); } return res; } - public Set format(List sources, String toFormat, boolean onProjectLangs) { + public List format(List sources, List toFormat, LanguageMapping languageMapping) { if (sources == null || toFormat == null) { - return new HashSet<>(); + return new ArrayList<>(); + } + List res = new ArrayList<>(); + for (String str : toFormat) { + res.addAll(this.format(sources, str, languageMapping)); } + return res; + } - List languages = (onProjectLangs ? projectLanguages : supportedLanguages); + public Set format(List sources, String toFormat, LanguageMapping languageMapping) { + if (sources == null || toFormat == null) { + return new HashSet<>(); + } - return languages.stream() - .map(lang -> this.replaceLanguageDependentPlaceholders(toFormat, lang)) + return supportedLanguages.stream() + .map(lang -> languageMapping == null + ? this.replaceLanguageDependentPlaceholders(toFormat, lang) + : this.replaceLanguageDependentPlaceholders(toFormat, languageMapping, lang)) .flatMap(changedToFormat -> sources.stream() .map(source -> this.replaceFileDependentPlaceholders(changedToFormat, source))) .collect(Collectors.toSet()); } + public Set format(List sources, String toFormat) { + return this.format(sources, toFormat, null); + } + public String replaceLanguageDependentPlaceholders(String toFormat, Language lang) { if (toFormat == null || lang == null) { throw new NullPointerException("null args in replaceLanguageDependentPlaceholders()"); @@ -206,18 +221,17 @@ public String replaceFileDependentPlaceholders(String toFormat, File file) { return StringUtils.removeStart(toFormat, Utils.PATH_SEPARATOR); } - public List formatForRegex(List toFormat, boolean onProjectLangs) { - List langs = (onProjectLangs) ? this.projectLanguages : this.supportedLanguages; - String langIds = langs.stream().map(Language::getId).collect(Collectors.joining("|", "(", ")")); - String langNames = langs.stream().map(Language::getName).collect(Collectors.joining("|", "(", ")")); - String langLocales = langs.stream().map(Language::getLocale).collect(Collectors.joining("|", "(", ")")); - String langLocalesWithUnderscore = langs.stream().map(Language::getLocale).map(s -> s.replace("-", "_")) + public List formatForRegex(List toFormat) { + String langIds = supportedLanguages.stream().map(Language::getId).collect(Collectors.joining("|", "(", ")")); + String langNames = supportedLanguages.stream().map(Language::getName).collect(Collectors.joining("|", "(", ")")); + String langLocales = supportedLanguages.stream().map(Language::getLocale).collect(Collectors.joining("|", "(", ")")); + String langLocalesWithUnderscore = supportedLanguages.stream().map(Language::getLocale).map(s -> s.replace("-", "_")) .collect(Collectors.joining("|", "(", ")")); - String langTwoLettersCodes = langs.stream().map(Language::getTwoLettersCode).collect(Collectors.joining("|", "(", ")")); - String langThreeLettersCodes = langs.stream().map(Language::getThreeLettersCode).collect(Collectors.joining("|", "(", ")")); - String langAndroidCodes = langs.stream().map(Language::getAndroidCode).collect(Collectors.joining("|", "(", ")")); - String langOsxLocales = langs.stream().map(Language::getOsxLocale).collect(Collectors.joining("|", "(", ")")); - String langOsxCodes = langs.stream().map(Language::getOsxCode).collect(Collectors.joining("|", "(", ")")); + String langTwoLettersCodes = supportedLanguages.stream().map(Language::getTwoLettersCode).collect(Collectors.joining("|", "(", ")")); + String langThreeLettersCodes = supportedLanguages.stream().map(Language::getThreeLettersCode).collect(Collectors.joining("|", "(", ")")); + String langAndroidCodes = supportedLanguages.stream().map(Language::getAndroidCode).collect(Collectors.joining("|", "(", ")")); + String langOsxLocales = supportedLanguages.stream().map(Language::getOsxLocale).collect(Collectors.joining("|", "(", ")")); + String langOsxCodes = supportedLanguages.stream().map(Language::getOsxCode).collect(Collectors.joining("|", "(", ")")); return toFormat.stream() .map(PlaceholderUtil::formatSourcePatternForRegex) .map(s -> s diff --git a/src/test/java/com/crowdin/cli/commands/functionality/SourcesUtilsTest.java b/src/test/java/com/crowdin/cli/commands/functionality/SourcesUtilsTest.java index 6a070906..181a16dc 100644 --- a/src/test/java/com/crowdin/cli/commands/functionality/SourcesUtilsTest.java +++ b/src/test/java/com/crowdin/cli/commands/functionality/SourcesUtilsTest.java @@ -1,5 +1,6 @@ package com.crowdin.cli.commands.functionality; +import com.crowdin.cli.client.LanguageMapping; import com.crowdin.cli.properties.helper.TempProject; import com.crowdin.cli.utils.PlaceholderUtilBuilder; import com.crowdin.cli.utils.Utils; @@ -16,9 +17,7 @@ import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -91,6 +90,27 @@ static Stream testGetFiles1() { ); } + @Test + public void testGetFiles_LanguageMapping() { + tempProject.addFile("folder/first.xml"); + tempProject.addFile("folder/second.eng.txt"); + tempProject.addFile("folder/second.xml"); + HashMap> mapping = new HashMap>() {{ + put("two_letters_code", new HashMap() {{ + put("en", "eng"); + }} + ); + }}; + LanguageMapping languageMapping = LanguageMapping.fromConfigFileLanguageMapping(mapping); + Stream sources = SourcesUtils.getFiles( + tempProject.getBasePath(), + "/folder/*", + Collections.singletonList("/folder/**/*.%two_letters_code%.*"), + PlaceholderUtilBuilder.STANDART.build(tempProject.getBasePath()), + languageMapping); + assertEquals(2, sources.count()); + } + @ParameterizedTest @MethodSource @DisabledOnOs(OS.WINDOWS) diff --git a/src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java b/src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java index 400b985f..bbe9d012 100644 --- a/src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java +++ b/src/test/java/com/crowdin/cli/utils/PlaceholderUtilTest.java @@ -39,7 +39,7 @@ public void testMainFunctionality( "/proj/path/" ); - Set result = placeholderUtil.format(Arrays.asList(sources), toFormat, true); + Set result = placeholderUtil.format(Arrays.asList(sources), toFormat); assertEquals(new HashSet<>(Arrays.asList(expected)), result); } @@ -53,13 +53,6 @@ static Stream testMainFunctionality() { Utils.normalizePath("resources/%two_letters_code%_%original_file_name%"), new String[] {Utils.normalizePath("resources/ua_messages.xml"), Utils.normalizePath("resources/ru_messages.xml")} ), - arguments(// Must be only de_messages - new Language[] { LanguageBuilder.DEU.build(), LanguageBuilder.ENG.build() }, - new Language[] { LanguageBuilder.DEU.build() }, - new File[] {new File("resources/messages.xml")}, - Utils.normalizePath("resources/%two_letters_code%_%original_file_name%"), - new String[] {Utils.normalizePath("resources/de_messages.xml")} - ), arguments(// How to treat double asterisks new Language[] {LanguageBuilder.ENG.build()}, new Language[] {LanguageBuilder.ENG.build()}, @@ -92,7 +85,7 @@ public void testMainFunctionalityWithLists( "/proj/path/" ); - List result = placeholderUtil.format(Arrays.asList(sources), Arrays.asList(toFormat), true); + List result = placeholderUtil.format(Arrays.asList(sources), Arrays.asList(toFormat)); assertEquals(new HashSet<>(Arrays.asList(expected)), new HashSet<>(result)); } @@ -122,8 +115,8 @@ public void testForNpe() { assertThrows(NullPointerException.class, () -> new PlaceholderUtil(null, null, null)); PlaceholderUtil placeholderUtil = new PlaceholderUtil(new ArrayList<>(), new ArrayList<>(), "/here/it/goes/"); - assertEquals(new ArrayList(), placeholderUtil.format(null, new ArrayList<>(), false)); - assertEquals(new HashSet<>(), placeholderUtil.format(null, "", false)); + assertEquals(new ArrayList(), placeholderUtil.format(null, new ArrayList<>())); + assertEquals(new HashSet<>(), placeholderUtil.format(null, "")); assertThrows(NullPointerException.class, () -> placeholderUtil.replaceLanguageDependentPlaceholders(null, new Language())); assertThrows(NullPointerException.class, () -> placeholderUtil.replaceLanguageDependentPlaceholders(null, null, null));