From bc1b2948eb358b905c798ce73285cda40d201489 Mon Sep 17 00:00:00 2001 From: danielsutts Date: Fri, 30 Sep 2022 22:36:23 +0300 Subject: [PATCH] fixed regex to work correctly for Windows machines --- .../crowdin/cli/commands/functionality/SourcesUtils.java | 7 ++++--- src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java | 2 +- src/main/java/com/crowdin/cli/utils/Utils.java | 4 ++++ 3 files changed, 9 insertions(+), 4 deletions(-) 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 6b30344fc..694e038bb 100644 --- a/src/main/java/com/crowdin/cli/commands/functionality/SourcesUtils.java +++ b/src/main/java/com/crowdin/cli/commands/functionality/SourcesUtils.java @@ -54,10 +54,11 @@ public static Stream getFiles(String basePath, String sourcePattern, List< public static List filterProjectFiles( List filePaths, String sourcePattern, List ignorePatterns, boolean preserveHierarchy, PlaceholderUtil placeholderUtil ) { - filePaths = filePaths.stream().map(Utils::unixPath).map(Utils::noSepAtStart).collect(Collectors.toList()); - sourcePattern = Utils.noSepAtStart(Utils.unixPath(sourcePattern)); + filePaths = filePaths.stream().map((Utils.isWindows() ? Utils::windowsPath : Utils::unixPath)).map(Utils::noSepAtStart).collect(Collectors.toList()); + sourcePattern = Utils.noSepAtStart(Utils.isWindows() ? Utils.windowsPath(sourcePattern) : Utils.unixPath(sourcePattern)); ignorePatterns = (ignorePatterns != null) - ? ignorePatterns.stream().map(Utils::unixPath).map(Utils::noSepAtStart).collect(Collectors.toList()) : Collections.emptyList(); + ? ignorePatterns.stream().map((Utils.isWindows() ? Utils::windowsPath : Utils::unixPath)).map(Utils::noSepAtStart).collect(Collectors.toList()) : Collections.emptyList(); + Predicate sourcePredicate; Predicate ignorePredicate; if (preserveHierarchy) { diff --git a/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java b/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java index 7d72b32f4..5eba5de42 100644 --- a/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java +++ b/src/main/java/com/crowdin/cli/utils/PlaceholderUtil.java @@ -236,6 +236,7 @@ public static String formatSourcePatternForRegex(String toFormat) { if (Utils.isWindows()) { toFormat = toFormat .replace("**", ".+") + .replace("\\", "\\\\") .replace(ASTERISK, "[^/]+"); } else { toFormat = toFormat @@ -247,7 +248,6 @@ public static String formatSourcePatternForRegex(String toFormat) { .replace(ASTERISK, "[^/]+") .replace(ESCAPE_ASTERISK_PLACEHOLDER, ESCAPE_ASTERISK); } - toFormat = toFormat .replace(ROUND_BRACKET_OPEN, ESCAPE_ROUND_BRACKET_OPEN) diff --git a/src/main/java/com/crowdin/cli/utils/Utils.java b/src/main/java/com/crowdin/cli/utils/Utils.java index 728260810..b633538fa 100755 --- a/src/main/java/com/crowdin/cli/utils/Utils.java +++ b/src/main/java/com/crowdin/cli/utils/Utils.java @@ -90,6 +90,10 @@ public static String unixPath(String path) { return (path != null) ? path.replaceAll("[\\\\/]+", "/") : null; } + public static String windowsPath(String path) { + return (path != null) ? path.replaceAll("[\\\\/]+", "\\\\") : null; + } + public static String normalizePath(String path) { return (path != null) ? path.replaceAll("[\\\\/]+", Utils.PATH_SEPARATOR_REGEX) : null; }