forked from fwcd/kotlin-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindClassPath.kt
253 lines (206 loc) · 8.94 KB
/
findClassPath.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package org.javacs.kt.classpath
import java.util.logging.Level
import org.javacs.kt.LOG
import org.javacs.kt.util.winCompatiblePathOf
import org.javacs.kt.util.tryResolving
import org.javacs.kt.util.firstNonNull
import org.javacs.kt.util.KotlinLSException
import org.jetbrains.kotlin.utils.ifEmpty
import java.io.File
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.attribute.BasicFileAttributes
import java.util.stream.Collectors
import java.util.function.BiPredicate
import java.util.Comparator
import java.util.concurrent.TimeUnit
fun findClassPath(workspaceRoots: Collection<Path>): Set<Path> {
return ensureStdlibInPaths(
workspaceRoots
.flatMap { projectFiles(it) }
.flatMap { readProjectFile(it) }
.toSet()
).ifEmpty(::backupClassPath)
}
private fun ensureStdlibInPaths(paths: Set<Path>): Set<Path> {
// Ensure that there is exactly one kotlin-stdlib present
val isStdlib: ((Path) -> Boolean) = { it.toString().contains("kotlin-stdlib") }
val stdlib = paths.firstOrNull(isStdlib) ?: findKotlinStdlib()
return paths.filterNot(isStdlib).union(listOf(stdlib).filterNotNull())
}
private fun backupClassPath() =
listOfNotNull(findKotlinStdlib()).toSet()
private fun projectFiles(workspaceRoot: Path): Set<Path> {
return Files.walk(workspaceRoot)
.filter { isMavenBuildFile(it) || isGradleBuildFile(it) }
.collect(Collectors.toSet())
}
private fun readProjectFile(file: Path): Set<Path> {
if (isMavenBuildFile(file)) {
// Project uses a Maven model
return readPom(file)
} else if (isGradleBuildFile(file)) {
// Project uses a Gradle model
return readBuildGradle(file)
} else {
throw IllegalArgumentException("$file is not a valid project configuration file (pom.xml or build.gradle)")
}
}
private fun isMavenBuildFile(file: Path) = file.endsWith("pom.xml")
private fun isGradleBuildFile(file: Path) = file.endsWith("build.gradle") || file.endsWith("build.gradle.kts")
private fun readPom(pom: Path): Set<Path> {
val mavenOutput = generateMavenDependencyList(pom)
val artifacts = mavenOutput?.let(::readMavenDependencyList) ?: throw KotlinLSException("No artifacts could be read from $pom")
when {
artifacts.isEmpty() -> LOG.warning("No artifacts found in $pom")
artifacts.size < 5 -> LOG.info("Found ${artifacts.joinToString(", ")} in $pom")
else -> LOG.info("Found ${artifacts.size} artifacts in $pom")
}
return artifacts.mapNotNull { findMavenArtifact(it, false) }.toSet()
}
private fun generateMavenDependencyList(pom: Path): Path? {
val mavenOutput = Files.createTempFile("deps", ".txt")
val workingDirectory = pom.toAbsolutePath().parent.toFile()
val cmd = "${mvnCommand()} dependency:list -DincludeScope=test -DoutputFile=$mavenOutput"
LOG.info("Run ${cmd} in $workingDirectory")
val process = Runtime.getRuntime().exec(cmd, null, workingDirectory)
process.inputStream.bufferedReader().use { reader ->
while (process.isAlive()) {
val line = reader.readLine()?.trim()
if (line == null) break
if ((line.length > 0) && !line.startsWith("Progress")) {
LOG.info("Maven: $line")
}
}
}
return mavenOutput
}
private val artifact = ".*:.*:.*:.*:.*".toRegex()
private fun readMavenDependencyList(mavenOutput: Path): Set<Artifact> =
mavenOutput.toFile()
.readLines()
.filter { it.matches(artifact) }
.map { parseArtifact(it) }
.toSet()
fun parseArtifact(rawArtifact: String, version: String? = null): Artifact {
val parts = rawArtifact.trim().split(':')
return when (parts.size) {
3 -> Artifact(parts[0], parts[1], version ?: parts[2])
5 -> Artifact(parts[0], parts[1], version ?: parts[3])
else -> throw IllegalArgumentException("$rawArtifact is not a properly formed Maven/Gradle artifact")
}
}
data class Artifact(val group: String, val artifact: String, val version: String) {
override fun toString() = "$group:$artifact:$version"
}
private val userHome = Paths.get(System.getProperty("user.home"))
val mavenHome = userHome.resolve(".m2")
val gradleHome = userHome.resolve(".gradle")
// TODO: Resolve the gradleCaches dynamically instead of hardcoding this path
val gradleCaches by lazy {
gradleHome.resolve("caches")
.resolveStartingWith("modules")
.resolveStartingWith("files")
}
private fun Path.resolveStartingWith(prefix: String) = Files.list(this).filter { it.fileName.toString().startsWith(prefix) }.findFirst().orElse(null)
fun findKotlinStdlib(): Path? {
return findLocalArtifact("org.jetbrains.kotlin", "kotlin-stdlib")
}
private data class LocalArtifactDirectoryResolution(val artifactDir: Path?, val buildTool: String)
private fun findLocalArtifact(group: String, artifact: String) = firstNonNull<Path>(
{ tryResolving("$artifact using Maven") { tryFindingLocalArtifactUsing(group, artifact, findLocalArtifactDirUsingMaven(group, artifact)) } },
{ tryResolving("$artifact using Gradle") { tryFindingLocalArtifactUsing(group, artifact, findLocalArtifactDirUsingGradle(group, artifact)) } }
)
private fun tryFindingLocalArtifactUsing(group: String, artifact: String, artifactDirResolution: LocalArtifactDirectoryResolution): Path? {
val isCorrectArtifact = BiPredicate<Path, BasicFileAttributes> { file, _ ->
val name = file.fileName.toString()
when (artifactDirResolution.buildTool) {
"Maven" -> {
val version = file.parent.fileName.toString()
val expected = "${artifact}-${version}.jar"
name == expected
}
else -> name.startsWith(artifact) && name.endsWith(".jar")
}
}
return Files.list(artifactDirResolution.artifactDir)
.sorted(::compareVersions)
.findFirst()
.orElse(null)
?.let {
Files.find(artifactDirResolution.artifactDir, 3, isCorrectArtifact)
.findFirst()
.orElse(null)
}
}
private fun Path.existsOrNull() =
if (Files.exists(this)) this else null
private fun findLocalArtifactDirUsingMaven(group: String, artifact: String) =
LocalArtifactDirectoryResolution(mavenHome.resolve("repository")
?.resolve(group.replace('.', File.separatorChar))
?.resolve(artifact)
?.existsOrNull(), "Maven")
private fun findLocalArtifactDirUsingGradle(group: String, artifact: String) =
LocalArtifactDirectoryResolution(gradleCaches
?.resolve(group)
?.resolve(artifact)
?.existsOrNull(), "Gradle")
private fun compareVersions(left: Path, right: Path): Int {
val leftVersion = extractVersion(left)
val rightVersion = extractVersion(right)
for (i in 0 until Math.min(leftVersion.size, rightVersion.size)) {
val leftRev = leftVersion[i].reversed()
val rightRev = rightVersion[i].reversed()
val compare = leftRev.compareTo(rightRev)
if (compare != 0)
return -compare
}
return -leftVersion.size.compareTo(rightVersion.size)
}
private fun extractVersion(artifactVersionDir: Path): List<String> {
return artifactVersionDir.toString().split(".")
}
private fun findMavenArtifact(a: Artifact, source: Boolean): Path? {
val result = mavenHome.resolve("repository")
.resolve(a.group.replace('.', File.separatorChar))
.resolve(a.artifact)
.resolve(a.version)
.resolve(mavenJarName(a, source))
if (Files.exists(result))
return result
else {
LOG.warning("Couldn't find $a in $result")
return null
}
}
private fun mavenJarName(a: Artifact, source: Boolean) =
if (source) "${a.artifact}-${a.version}-sources.jar"
else "${a.artifact}-${a.version}.jar"
private var cacheMvnCommand: Path? = null
private fun mvnCommand(): Path {
if (cacheMvnCommand == null)
cacheMvnCommand = doMvnCommand()
return cacheMvnCommand!!
}
private fun isOSWindows() = (File.separatorChar == '\\')
private fun doMvnCommand() = findCommandOnPath("mvn")
fun findCommandOnPath(name: String): Path? =
if (isOSWindows()) windowsCommand(name)
else unixCommand(name)
private fun windowsCommand(name: String) =
findExecutableOnPath("$name.cmd")
?: findExecutableOnPath("$name.bat")
?: findExecutableOnPath("$name.exe")
private fun unixCommand(name: String) = findExecutableOnPath(name)
private fun findExecutableOnPath(fileName: String): Path? {
for (dir in System.getenv("PATH").split(File.pathSeparator)) {
val file = File(dir, fileName)
if (file.isFile && file.canExecute()) {
LOG.info("Found $fileName at ${file.absolutePath}")
return Paths.get(file.absolutePath)
}
}
return null
}