-
Notifications
You must be signed in to change notification settings - Fork 87
/
SourcesUtils.java
158 lines (146 loc) · 7.61 KB
/
SourcesUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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;
import lombok.NonNull;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
public class SourcesUtils {
public static Stream<File> getFiles(String basePath, String sourcePattern, List<String> 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<File> sources = fileHelper.getFiles(relativePath);
List<String> formattedIgnores = placeholderUtil.format(sources, ignorePattern, languageMapping);
return fileHelper.filterOutIgnoredFiles(sources, formattedIgnores)
.stream()
.filter(File::isFile);
}
public static Stream<File> getFiles(String basePath, String sourcePattern, List<String> ignorePattern, PlaceholderUtil placeholderUtil) {
return SourcesUtils.getFiles(basePath, sourcePattern, ignorePattern, placeholderUtil, null);
}
public static List<String> filterProjectFiles(
List<String> filePaths, String sourcePattern, List<String> ignorePatterns, boolean preserveHierarchy, PlaceholderUtil placeholderUtil
) {
filePaths = filePaths.stream().map((Utils.isWindows() ? Utils::toWindowsPath : Utils::toUnixPath)).map(Utils::noSepAtStart).collect(Collectors.toList());
sourcePattern = Utils.noSepAtStart(Utils.isWindows() ? Utils.toWindowsPath(sourcePattern) : Utils.toUnixPath(sourcePattern));
ignorePatterns = (ignorePatterns != null)
? ignorePatterns.stream().map((Utils.isWindows() ? Utils::toWindowsPath : Utils::toUnixPath)).map(Utils::noSepAtStart).collect(Collectors.toList()) : Collections.emptyList();
Predicate<String> sourcePredicate;
Predicate<String> ignorePredicate;
if (preserveHierarchy) {
sourcePredicate = Pattern.compile("^" + PlaceholderUtil.formatSourcePatternForRegex(sourcePattern) + "$").asPredicate();
ignorePredicate = placeholderUtil.formatForRegex(ignorePatterns).stream()
.map(Pattern::compile)
.map(Pattern::asPredicate)
.map(Predicate::negate)
.reduce((s) -> true, Predicate::and);
} else {
List<Pattern> patternPaths = Arrays.stream(sourcePattern.split(Pattern.quote(File.separator)))
.map(pathSplit -> Pattern.compile("^" + PlaceholderUtil.formatSourcePatternForRegex(pathSplit) + "$"))
.collect(Collectors.toList());
Collections.reverse(patternPaths);
sourcePredicate = (filePath) -> {
List<String> filePathSplit = asList(filePath.split("/+"));
Collections.reverse(filePathSplit);
for (int i = 0; true; i++) {
if (i >= filePathSplit.size()) {
return true;
} else if (i >= patternPaths.size()) {
return false;
} else if (patternPaths.get(i).pattern().equals("^.+$")) {
return true;
} else if (!patternPaths.get(i).matcher(filePathSplit.get(i)).matches()) {
return false;
}
}
};
ignorePredicate = ignorePatterns.stream()
.map(ignorePattern -> {
List<String> ignorePatternPaths = placeholderUtil.formatForRegex(asList(ignorePattern.split(Pattern.quote(File.separator))));
Collections.reverse(ignorePatternPaths);
return ignorePatternPaths;
})
.map(path -> path.stream().map(Pattern::compile).collect(Collectors.toList()))
.map(ignorePatternPaths -> (Predicate<String>) (filePath) -> {
List<String> filePathSplit = asList(filePath.split("[\\\\/]+"));
Collections.reverse(filePathSplit);
for (int i = 0; true; i++) {
if (i > filePathSplit.size()) {
return true;
} else if (i >= ignorePatternPaths.size()) {
return true;
} else if (ignorePatternPaths.get(i).pattern().equals("^.+$")) {
return false;
} else if (i >= filePathSplit.size()) {
return true;
}
if (!ignorePatternPaths.get(i).matcher(filePathSplit.get(i)).matches()) {
return true;
} else if (ignorePatternPaths.size() - 1 == i) {
return false;
}
}
})
.reduce((s) -> true, Predicate::and);
}
return filePaths.stream()
.filter(sourcePredicate)
.filter(ignorePredicate)
.map(Utils::normalizePath)
.collect(Collectors.toList());
}
/**
* Try to replace ‘*’ with ‘source’ param and project file path.
* If project file path (or part of it) does not match the pattern, do nothing.
* @param sourcePattern should contain '*'
* @param projectFile file path
* @return source pattern with replaced ‘*’
*/
public static String replaceUnaryAsterisk(@NonNull String sourcePattern, @NonNull String projectFile) {
String[] parts = Utils.splitPath(sourcePattern);
String [] fileParts = Utils.splitPath(projectFile);
for (int i = 1; i <= parts.length; i++) {
if (!parts[parts.length-i].equals("**")
&& fileParts.length >= i
&& Pattern.matches(PlaceholderUtil.formatSourcePatternForRegex(parts[parts.length-i]), fileParts[fileParts.length-i])) {
parts[parts.length-i] = fileParts[fileParts.length-i];
}
}
return Utils.joinPaths(parts);
}
public static boolean containsPattern(String sourcePattern) {
if (sourcePattern == null) {
return false;
}
return sourcePattern.contains("**")
|| sourcePattern.contains("*")
|| sourcePattern.contains("?")
|| (sourcePattern.contains("[") && sourcePattern.contains("]"))
|| (sourcePattern.contains("\\") && !Utils.isWindows());
}
public static String getCommonPath(List<String> sources, String basePath) {
String commonPrefix = StringUtils.getCommonPrefix(sources.toArray(new String[0]));
String result = commonPrefix.substring(0, commonPrefix.lastIndexOf(Utils.PATH_SEPARATOR) + 1);
result = StringUtils.removeStart(result, basePath);
return result;
}
public static boolean isFileProperties(File source) {
return FilenameUtils.isExtension(source.getName(), "properties");
}
public static boolean isFileJavaScript(File source) {
return FilenameUtils.isExtension(source.getName(), "js");
}
}