-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathSourceExclusions.kt
More file actions
49 lines (43 loc) · 1.91 KB
/
Copy pathSourceExclusions.kt
File metadata and controls
49 lines (43 loc) · 1.91 KB
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
package org.javacs.kt
import org.javacs.kt.util.filePath
import java.io.File
import java.net.URI
import java.nio.file.FileSystems
import java.nio.file.Path
import java.nio.file.Paths
// TODO: Read exclusions from gitignore/settings.json/... instead of
// hardcoding them
class SourceExclusions(
private val workspaceRoots: Collection<Path>,
private val scriptsConfig: ScriptsConfiguration,
private val additionalSourceExclusions: List<String>
) {
val excludedPatterns = (listOf(
".git", ".hg", ".svn", // Version control systems
".idea", ".idea_modules", ".vs", ".vscode", ".code-workspace", ".settings", // IDEs
"bazel-*", "bin", "build", "node_modules", "target", // Build systems
) + additionalSourceExclusions + when {
!scriptsConfig.enabled -> listOf("*.kts")
!scriptsConfig.buildScriptsEnabled -> listOf("*.gradle.kts")
else -> emptyList()
})
private val exclusionMatchers = excludedPatterns
.map { FileSystems.getDefault().getPathMatcher("glob:$it") }
/** Finds all non-excluded files recursively. */
fun walkIncluded(): Sequence<Path> = workspaceRoots.asSequence().flatMap { root ->
root.toFile()
.walk()
.onEnter { isPathIncluded(it.toPath()) }
.map { it.toPath() }
}
/** Tests whether the given URI is not excluded. */
fun isURIIncluded(uri: URI) = uri.filePath?.let(this::isPathIncluded) ?: false
/** Tests whether the given path is not excluded. */
fun isPathIncluded(file: Path): Boolean = workspaceRoots.any { file.startsWith(it) }
&& exclusionMatchers.none { matcher ->
workspaceRoots
.mapNotNull { if (file.startsWith(it)) it.relativize(file) else null }
.flatMap { it } // Extract path segments
.any(matcher::matches)
}
}