@@ -19,15 +19,18 @@ internal class GradleClassPathResolver(private val path: Path, private val inclu
19
19
20
20
return readDependenciesViaGradleCLI(projectDirectory, scripts, tasks)
21
21
.apply { if (isNotEmpty()) LOG .info(" Successfully resolved dependencies for '${projectDirectory.fileName} ' using Gradle" ) }
22
- .map { ClassPathEntry (it, null ) }.toSet()
23
22
}
24
- override val buildScriptClasspath: Set <Path > get() {
23
+ override val buildScriptClasspath: Set <Path >
24
+ get() {
25
25
return if (includeKotlinDSL) {
26
26
val scripts = listOf (" kotlinDSLClassPathFinder.gradle" )
27
27
val tasks = listOf (" kotlinLSPKotlinDSLDeps" )
28
28
29
29
return readDependenciesViaGradleCLI(projectDirectory, scripts, tasks)
30
30
.apply { if (isNotEmpty()) LOG .info(" Successfully resolved build script dependencies for '${projectDirectory.fileName} ' using Gradle" ) }
31
+ .map {
32
+ it.compiledJar
33
+ }.toSet()
31
34
} else {
32
35
emptySet()
33
36
}
@@ -70,24 +73,20 @@ private fun getGradleCommand(workspace: Path): Path {
70
73
}
71
74
}
72
75
73
- private fun readDependenciesViaGradleCLI (projectDirectory : Path , gradleScripts : List <String >, gradleTasks : List <String >): Set <Path > {
76
+ private fun readDependenciesViaGradleCLI (projectDirectory : Path , gradleScripts : List <String >, gradleTasks : List <String >): Set <ClassPathEntry > {
74
77
LOG .info(" Resolving dependencies for '{}' through Gradle's CLI using tasks {}..." , projectDirectory.fileName, gradleTasks)
75
78
76
79
val tmpScripts = gradleScripts.map { gradleScriptToTempFile(it, deleteOnExit = false ).toPath().toAbsolutePath() }
77
80
val gradle = getGradleCommand(projectDirectory)
78
81
79
82
val command = listOf (gradle.toString()) + tmpScripts.flatMap { listOf (" -I" , it.toString()) } + gradleTasks + listOf (" --console=plain" )
80
83
val dependencies = findGradleCLIDependencies(command, projectDirectory)
81
- ?.also { LOG .debug(" Classpath for task {}" , it) }
82
- .orEmpty()
83
- .filter { it.toString().lowercase().endsWith(" .jar" ) || Files .isDirectory(it) } // Some Gradle plugins seem to cause this to output POMs, therefore filter JARs
84
- .toSet()
85
84
86
85
tmpScripts.forEach(Files ::delete)
87
86
return dependencies
88
87
}
89
88
90
- private fun findGradleCLIDependencies (command : List <String >, projectDirectory : Path ): Set <Path > ? {
89
+ private fun findGradleCLIDependencies (command : List <String >, projectDirectory : Path ): Set <ClassPathEntry > {
91
90
val (result, errors) = execAndReadStdoutAndStderr(command, projectDirectory)
92
91
if (" FAILURE: Build failed" in errors) {
93
92
LOG .warn(" Gradle task failed: {}" , errors)
@@ -101,13 +100,22 @@ private fun findGradleCLIDependencies(command: List<String>, projectDirectory: P
101
100
return parseGradleCLIDependencies(result)
102
101
}
103
102
104
- private val artifactPattern by lazy { " kotlin-lsp-gradle (.+)(?: \r ? \n )" .toRegex() }
103
+ private val artifactPattern by lazy { " kotlin-lsp-gradle path: (.+) source:(.+ )" .toRegex() }
105
104
private val gradleErrorWherePattern by lazy { " \\ *\\ s+Where:[\r\n ]+(\\ S\\ .*)" .toRegex() }
106
105
107
- private fun parseGradleCLIDependencies (output : String ): Set <Path > ? {
106
+ private fun parseGradleCLIDependencies (output : String ): Set <ClassPathEntry > {
108
107
LOG .debug(output)
109
108
val artifacts = artifactPattern.findAll(output)
110
- .mapNotNull { Paths .get(it.groups[1 ]?.value) }
111
- .filterNotNull()
109
+ .map {
110
+ val path = it.groups[1 ]?.value
111
+ val source = it.groups[2 ]?.value
112
+ val jarPath = if (path == " null" || path == null ) null else Path .of(path)
113
+ val sourceJarPath = if (source == " null" || source == null ) null else Path .of(source)
114
+ if (jarPath != null && (path!! .lowercase().endsWith(" .jar" ) || Files .isDirectory(jarPath))) {
115
+ LOG .debug { " Adding path:$jarPath source: $sourceJarPath to classpath" }
116
+ return @map ClassPathEntry (jarPath, sourceJarPath)
117
+ } else return @map null
118
+ }.filterNotNull()
119
+
112
120
return artifacts.toSet()
113
121
}
0 commit comments