-
Notifications
You must be signed in to change notification settings - Fork 193
/
DefaultClassPathResolver.kt
57 lines (50 loc) · 2.34 KB
/
DefaultClassPathResolver.kt
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
package org.javacs.kt.classpath
import org.javacs.kt.LOG
import org.jetbrains.exposed.sql.Database
import java.nio.file.Path
import java.nio.file.PathMatcher
import java.nio.file.FileSystems
fun defaultClassPathResolver(workspaceRoots: Collection<Path>, db: Database? = null): ClassPathResolver {
val childResolver = WithStdlibResolver(
ShellClassPathResolver.global(workspaceRoots.firstOrNull())
.or(workspaceRoots.asSequence().flatMap { workspaceResolvers(it) }.joined)
).or(BackupClassPathResolver)
return db?.let { CachedClassPathResolver(childResolver, it) } ?: childResolver
}
/** Searches the workspace for all files that could provide classpath info. */
private fun workspaceResolvers(workspaceRoot: Path): Sequence<ClassPathResolver> {
val ignored: List<PathMatcher> = ignoredPathPatterns(workspaceRoot, workspaceRoot.resolve(".gitignore"))
return folderResolvers(workspaceRoot, ignored).asSequence()
}
/** Searches the folder for all build-files. */
private fun folderResolvers(root: Path, ignored: List<PathMatcher>): Collection<ClassPathResolver> =
root.toFile()
.walk()
.onEnter { file -> ignored.none { it.matches(file.toPath()) } }
.mapNotNull { asClassPathProvider(it.toPath()) }
.toList()
/** Tries to read glob patterns from a gitignore. */
private fun ignoredPathPatterns(root: Path, gitignore: Path): List<PathMatcher> =
gitignore.toFile()
.takeIf { it.exists() }
?.readLines()
?.map { it.trim() }
?.filter { it.isNotEmpty() && !it.startsWith("#") }
?.map { it.removeSuffix("/") }
?.let { it + listOf(
// Patterns that are ignored by default
".git"
) }
?.mapNotNull { try {
LOG.debug("Adding ignore pattern '{}' from {}", it, gitignore)
FileSystems.getDefault().getPathMatcher("glob:$root**/$it")
} catch (e: Exception) {
LOG.warn("Did not recognize gitignore pattern: '{}' ({})", it, e.message)
null
} }
?: emptyList()
/** Tries to create a classpath resolver from a file using as many sources as possible */
private fun asClassPathProvider(path: Path): ClassPathResolver? =
MavenClassPathResolver.maybeCreate(path)
?: GradleClassPathResolver.maybeCreate(path)
?: ShellClassPathResolver.maybeCreate(path)