Skip to content

Commit 8f48d8c

Browse files
committed
List Kotlin DSL accessors and split into two classpath finders
Print dynamically generated Kotlin DSL accessors (e.g. the 'compile' configuration method) when resolving the classpath of Kotlin DSL projects. Also move the Kotlin DSL classpath resolution logic into a separate script.
1 parent 2fef42d commit 8f48d8c

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import org.gradle.kotlin.dsl.accessors.AccessorsClassPathKt
2+
import org.gradle.internal.classpath.ClassPath
3+
4+
allprojects { project ->
5+
task kotlinLSPKotlinDSLDeps {
6+
def pattern = ~/^(?:gradle-(?:kotlin-dsl|core)|kotlin-(?:compiler|stdlib)).*\.jar/
7+
doLast {
8+
(fileTree("$gradle.gradleHomeDir/lib") + fileTree("$gradle.gradleUserHomeDir/caches/$gradle.gradleVersion/generated-gradle-jars"))
9+
.findAll { it.name =~ pattern }
10+
.forEach { System.out.println "kotlin-lsp-gradle $it" }
11+
12+
// List dynamically generated Kotlin DSL accessors (e.g. the 'compile' configuration method)
13+
def accessors = AccessorsClassPathKt.projectAccessorsClassPath(project, ClassPath.EMPTY)
14+
accessors.bin.asFiles
15+
.forEach { System.out.println "kotlin-lsp-gradle $it" }
16+
}
17+
}
18+
}

server/src/main/resources/classpathFinder.gradle renamed to server/src/main/resources/projectClassPathFinder.gradle

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,4 @@ allprojects { project ->
6969
.forEach { System.out.println "kotlin-lsp-gradle $it" }
7070
}
7171
}
72-
73-
task kotlinLSPKotlinDSLDeps {
74-
def pattern = ~/^(?:gradle-(?:kotlin-dsl|core)|kotlin-(?:compiler|stdlib)).*\.jar/
75-
doLast {
76-
(fileTree("$gradle.gradleHomeDir/lib") + fileTree("$gradle.gradleUserHomeDir/caches/$gradle.gradleVersion/generated-gradle-jars"))
77-
.findAll { it.name =~ pattern }
78-
.forEach { System.out.println "kotlin-lsp-gradle $it" }
79-
}
80-
}
8172
}

shared/src/main/kotlin/org/javacs/kt/classpath/GradleClassPathResolver.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ internal class GradleClassPathResolver(private val path: Path, private val inclu
1616
override val resolverType: String = "Gradle"
1717
override val classpath: Set<Path> get() {
1818
val projectDirectory = path.getParent()
19-
val tasks = listOf("kotlinLSPProjectDeps") + (if (includeKotlinDSL) listOf("kotlinLSPKotlinDSLDeps") else emptySet())
20-
return readDependenciesViaGradleCLI(projectDirectory, tasks)
19+
val scripts = listOf("projectClassPathFinder.gradle") + listOf("kotlinDSLClassPathFinder.gradle").takeIf { includeKotlinDSL }.orEmpty()
20+
val tasks = listOf("kotlinLSPProjectDeps") + listOf("kotlinLSPKotlinDSLDeps").takeIf { includeKotlinDSL }.orEmpty()
21+
22+
return readDependenciesViaGradleCLI(projectDirectory, scripts, tasks)
2123
.apply { if (isNotEmpty()) LOG.info("Successfully resolved dependencies for '${projectDirectory.fileName}' using Gradle") }
2224
}
2325

@@ -29,7 +31,7 @@ internal class GradleClassPathResolver(private val path: Path, private val inclu
2931
}
3032
}
3133

32-
private fun createTemporaryGradleFile(deleteOnExit: Boolean = false): File {
34+
private fun gradleScriptToTempFile(scriptName: String, deleteOnExit: Boolean = false): File {
3335
val config = File.createTempFile("classpath", ".gradle")
3436
if (deleteOnExit) {
3537
config.deleteOnExit()
@@ -38,7 +40,7 @@ private fun createTemporaryGradleFile(deleteOnExit: Boolean = false): File {
3840
LOG.debug("Creating temporary gradle file {}", config.absolutePath)
3941

4042
config.bufferedWriter().use { configWriter ->
41-
ClassLoader.getSystemResourceAsStream("classpathFinder.gradle").bufferedReader().use { configReader ->
43+
ClassLoader.getSystemResourceAsStream(scriptName).bufferedReader().use { configReader ->
4244
configReader.copyTo(configWriter)
4345
}
4446
}
@@ -58,15 +60,18 @@ private fun getGradleCommand(workspace: Path): Path {
5860
}
5961
}
6062

61-
private fun readDependenciesViaGradleCLI(projectDirectory: Path, gradleTasks: List<String>): Set<Path> {
63+
private fun readDependenciesViaGradleCLI(projectDirectory: Path, gradleScripts: List<String>, gradleTasks: List<String>): Set<Path> {
6264
LOG.info("Resolving dependencies for '{}' through Gradle's CLI using tasks {}...", projectDirectory.fileName, gradleTasks)
63-
val tmpFile = createTemporaryGradleFile(deleteOnExit = false).toPath()
65+
66+
val tmpScripts = gradleScripts.map { gradleScriptToTempFile(it, deleteOnExit = false).toPath().toAbsolutePath() }
6467
val gradle = getGradleCommand(projectDirectory)
65-
val command = "$gradle -I ${tmpFile.toAbsolutePath()} ${gradleTasks.joinToString(separator = " ")} --console=plain"
68+
69+
val command = "$gradle ${tmpScripts.map { "-I $it" }.joinToString(" ")} ${gradleTasks.joinToString(" ")} --console=plain"
6670
val dependencies = findGradleCLIDependencies(command, projectDirectory)
6771
?.also { LOG.debug("Classpath for task {}", it) }
6872
.orEmpty()
69-
Files.delete(tmpFile)
73+
74+
tmpScripts.forEach(Files::delete)
7075
return dependencies
7176
}
7277

0 commit comments

Comments
 (0)