1import org.gradle.api.file.FileTreeElement
2
3import java.util.regex.Pattern
4
5class ExcludeUtils {
6    /**
7     * Returns true if f should be excluded.
8     * f is excluded only if:
9     * - its absolute path contains [pathMustContain] AND
10     * - its path matches one of the regex in [regexToKeep].
11     */
12    static boolean excludeIfNotIn(
13            String pathMustContain,
14            ArrayList<String> regexToKeep,
15            FileTreeElement f) {
16        if (f.isDirectory()) return false
17        def absolutePath = f.file.absolutePath
18
19        if (!absolutePath.contains(pathMustContain)) return false
20
21        // keeping only those in regexToKeep
22        def toRemove = !regexToKeep.any { absolutePath =~ Pattern.compile(it) }
23        // To debug: println("file: ${f.getName()} to remove: ${toRemove}")
24        return toRemove
25    }
26
27    /**
28     * Returns true if f should be excluded.
29     * f is excluded only if:
30     * - its absolute path contains [pathMustContain] AND
31     * - its path matches one of the regex in [regexToExclude].
32     */
33    static boolean excludeIfIn(
34            String pathMustContain,
35            ArrayList<String> regexToExclude,
36            FileTreeElement f) {
37        if (f.isDirectory()) return false
38        def absolutePath = f.file.absolutePath
39
40        if (!absolutePath.contains(pathMustContain)) return false
41
42        // keeping only those in regexToKeep
43        def toRemove = regexToExclude.any { absolutePath =~ Pattern.compile(it) }
44        // To debug: println("file: ${f.getName()} to remove: ${toRemove}")
45        return toRemove
46    }
47}