|
1 | 1 | package org.javacs.kt
|
2 | 2 |
|
3 |
| -import org.javacs.kt.util.filePath |
4 |
| -import java.io.File |
5 | 3 | import java.net.URI
|
6 | 4 | import java.nio.file.FileSystems
|
7 | 5 | import java.nio.file.Path
|
8 |
| -import java.nio.file.Paths |
| 6 | +import org.javacs.kt.util.filePath |
9 | 7 |
|
10 | 8 | // TODO: Read exclusions from gitignore/settings.json/... instead of
|
11 | 9 | // hardcoding them
|
12 | 10 | class SourceExclusions(
|
13 | 11 | private val workspaceRoots: Collection<Path>,
|
14 |
| - private val scriptsConfig: ScriptsConfiguration |
| 12 | + scriptsConfig: ScriptsConfiguration, |
15 | 13 | ) {
|
16 |
| - val excludedPatterns = (listOf( |
17 |
| - ".git", ".hg", ".svn", // Version control systems |
18 |
| - ".idea", ".idea_modules", ".vs", ".vscode", ".code-workspace", ".settings", // IDEs |
19 |
| - "bazel-*", "bin", "build", "node_modules", "target", // Build systems |
20 |
| - ) + when { |
21 |
| - !scriptsConfig.enabled -> listOf("*.kts") |
22 |
| - !scriptsConfig.buildScriptsEnabled -> listOf("*.gradle.kts") |
23 |
| - else -> emptyList() |
24 |
| - }) |
| 14 | + val excludedPatterns = |
| 15 | + (listOf( |
| 16 | + ".git", |
| 17 | + ".hg", |
| 18 | + ".svn", // Version control systems |
| 19 | + ".idea", |
| 20 | + ".idea_modules", |
| 21 | + ".vs", |
| 22 | + ".vscode", |
| 23 | + ".code-workspace", |
| 24 | + ".settings", // IDEs |
| 25 | + "bazel-*", |
| 26 | + "bin", |
| 27 | + "build", |
| 28 | + "node_modules", |
| 29 | + ) + |
| 30 | + when { |
| 31 | + !scriptsConfig.enabled -> listOf("*.kts") |
| 32 | + !scriptsConfig.buildScriptsEnabled -> listOf("*.gradle.kts") |
| 33 | + else -> emptyList() |
| 34 | + }) |
25 | 35 |
|
26 | 36 | private val exclusionMatchers = excludedPatterns
|
| 37 | + .filter { !it.startsWith("!") } |
27 | 38 | .map { FileSystems.getDefault().getPathMatcher("glob:$it") }
|
28 | 39 |
|
29 |
| - /** Finds all non-excluded files recursively. */ |
30 |
| - fun walkIncluded(): Sequence<Path> = workspaceRoots.asSequence().flatMap { root -> |
31 |
| - root.toFile() |
32 |
| - .walk() |
33 |
| - .onEnter { isPathIncluded(it.toPath()) } |
34 |
| - .map { it.toPath() } |
35 |
| - } |
| 40 | + fun walkIncluded(): Sequence<Path> = |
| 41 | + workspaceRoots.asSequence().flatMap { root -> |
| 42 | + root.toFile().walk().onEnter { isPathIncluded(it.toPath()) }.map { it.toPath() } |
| 43 | + } |
36 | 44 |
|
37 |
| - /** Tests whether the given URI is not excluded. */ |
38 | 45 | fun isURIIncluded(uri: URI) = uri.filePath?.let(this::isPathIncluded) ?: false
|
39 | 46 |
|
40 |
| - /** Tests whether the given path is not excluded. */ |
41 |
| - fun isPathIncluded(file: Path): Boolean = workspaceRoots.any { file.startsWith(it) } |
42 |
| - && exclusionMatchers.none { matcher -> |
43 |
| - workspaceRoots |
44 |
| - .mapNotNull { if (file.startsWith(it)) it.relativize(file) else null } |
45 |
| - .flatMap { it } // Extract path segments |
46 |
| - .any(matcher::matches) |
| 47 | + fun isPathIncluded(file: Path): Boolean { |
| 48 | + if (!workspaceRoots.any { file.startsWith(it) }) { |
| 49 | + return false |
| 50 | + } |
| 51 | + |
| 52 | + val relativePaths = workspaceRoots |
| 53 | + .mapNotNull { if (file.startsWith(it)) it.relativize(file) else null } |
| 54 | + .flatten() |
| 55 | + |
| 56 | + // Check if we're in a target directory |
| 57 | + if (relativePaths.contains(Path.of("target"))) { |
| 58 | + val pathList = relativePaths.toList() |
| 59 | + val targetIndex = pathList.indexOf(Path.of("target")) |
| 60 | + |
| 61 | + // Allow only target directory itself or if next directory is generated-sources |
| 62 | + return pathList.size <= targetIndex + 1 || |
| 63 | + pathList[targetIndex + 1] == Path.of("generated-sources") |
| 64 | + } |
| 65 | + |
| 66 | + // If path matches any exclusion pattern, exclude it |
| 67 | + if (exclusionMatchers.any { matcher -> |
| 68 | + relativePaths.any(matcher::matches) |
| 69 | + }) { |
| 70 | + return false |
47 | 71 | }
|
| 72 | + |
| 73 | + // Include paths outside target directory by default |
| 74 | + return true |
| 75 | + } |
| 76 | + |
48 | 77 | }
|
| 78 | + |
0 commit comments