Skip to content

Commit 0c30db1

Browse files
committed
Use the EclipseModel directly to fetch dependencies
1 parent 4bdd672 commit 0c30db1

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ internal class GradleClassPathResolver(private val path: Path, private val inclu
1919

2020
return readDependenciesViaGradleCLI(projectDirectory, scripts, tasks)
2121
.apply { if (isNotEmpty()) LOG.info("Successfully resolved dependencies for '${projectDirectory.fileName}' using Gradle") }
22-
.map { ClassPathEntry(it, null) }.toSet()
2322
}
24-
override val buildScriptClasspath: Set<Path> get() {
23+
override val buildScriptClasspath: Set<Path>
24+
get() {
2525
return if (includeKotlinDSL) {
2626
val scripts = listOf("kotlinDSLClassPathFinder.gradle")
2727
val tasks = listOf("kotlinLSPKotlinDSLDeps")
2828

2929
return readDependenciesViaGradleCLI(projectDirectory, scripts, tasks)
3030
.apply { if (isNotEmpty()) LOG.info("Successfully resolved build script dependencies for '${projectDirectory.fileName}' using Gradle") }
31+
.map {
32+
it.compiledJar
33+
}.toSet()
3134
} else {
3235
emptySet()
3336
}
@@ -70,24 +73,20 @@ private fun getGradleCommand(workspace: Path): Path {
7073
}
7174
}
7275

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> {
7477
LOG.info("Resolving dependencies for '{}' through Gradle's CLI using tasks {}...", projectDirectory.fileName, gradleTasks)
7578

7679
val tmpScripts = gradleScripts.map { gradleScriptToTempFile(it, deleteOnExit = false).toPath().toAbsolutePath() }
7780
val gradle = getGradleCommand(projectDirectory)
7881

7982
val command = listOf(gradle.toString()) + tmpScripts.flatMap { listOf("-I", it.toString()) } + gradleTasks + listOf("--console=plain")
8083
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()
8584

8685
tmpScripts.forEach(Files::delete)
8786
return dependencies
8887
}
8988

90-
private fun findGradleCLIDependencies(command: List<String>, projectDirectory: Path): Set<Path>? {
89+
private fun findGradleCLIDependencies(command: List<String>, projectDirectory: Path): Set<ClassPathEntry> {
9190
val (result, errors) = execAndReadStdoutAndStderr(command, projectDirectory)
9291
if ("FAILURE: Build failed" in errors) {
9392
LOG.warn("Gradle task failed: {}", errors)
@@ -101,13 +100,22 @@ private fun findGradleCLIDependencies(command: List<String>, projectDirectory: P
101100
return parseGradleCLIDependencies(result)
102101
}
103102

104-
private val artifactPattern by lazy { "kotlin-lsp-gradle (.+)(?:\r?\n)".toRegex() }
103+
private val artifactPattern by lazy { "kotlin-lsp-gradle path:(.+) source:(.+)".toRegex() }
105104
private val gradleErrorWherePattern by lazy { "\\*\\s+Where:[\r\n]+(\\S\\.*)".toRegex() }
106105

107-
private fun parseGradleCLIDependencies(output: String): Set<Path>? {
106+
private fun parseGradleCLIDependencies(output: String): Set<ClassPathEntry> {
108107
LOG.debug(output)
109108
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+
112120
return artifacts.toSet()
113121
}

shared/src/main/resources/projectClassPathFinder.gradle

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import org.gradle.plugins.ide.eclipse.model.ClasspathEntry
2+
import org.gradle.plugins.ide.eclipse.model.EclipseModel
3+
import org.gradle.plugins.ide.eclipse.model.Library
4+
15
allprojects { project ->
6+
apply plugin: "eclipse"
7+
28
task kotlinLSPProjectDeps { task ->
39
doLast {
10+
def classpaths = new HashMap()
411
System.out.println ""
512
System.out.println "gradle-version $gradleVersion"
613
System.out.println "kotlin-lsp-project ${project.name}"
@@ -28,33 +35,35 @@ allprojects { project ->
2835
File.separator + variantBase +
2936
File.separator + "classes"
3037

31-
System.out.println "kotlin-lsp-gradle $buildClasses"
38+
classpaths[buildClasses.toString()] = null
3239

3340
def userClasses = project.getBuildDir().absolutePath +
3441
File.separator + "intermediates" +
3542
File.separator + "javac" +
3643
File.separator + variantBase +
3744
File.separator + "compile" + variantBase.capitalize() + "JavaWithJavac" + File.separator + "classes"
3845

39-
System.out.println "kotlin-lsp-gradle $userClasses"
46+
classpaths[userClasses.toString()] = null
4047

4148
def userVariantClasses = project.getBuildDir().absolutePath +
4249
File.separator + "intermediates" +
4350
File.separator + "javac" +
4451
File.separator + variantBase +
4552
File.separator + "classes"
4653

47-
System.out.println "kotlin-lsp-gradle $userVariantClasses"
54+
classpaths[userVariantClasses.toString()] = null
4855

4956
variant.getCompileClasspath().each {
50-
System.out.println "kotlin-lsp-gradle $it"
57+
classpaths[it.toString()] = null
5158
}
5259
}
60+
61+
5362
} else if (project.hasProperty('sourceSets')) {
5463
// Print the list of all dependencies jar files.
5564
sourceSets.forEach {
5665
it.compileClasspath.forEach {
57-
System.out.println "kotlin-lsp-gradle $it"
66+
classpaths[it.toString()] = null
5867
}
5968
}
6069
}
@@ -69,18 +78,32 @@ allprojects { project ->
6978
kotlinExtension.targets.names.each {
7079
def classpath = configurations["${it}CompileClasspath"]
7180
classpath.files.each {
72-
System.out.println "kotlin-lsp-gradle $it"
81+
classpaths[it.toString()] = null
7382
}
7483
}
7584
}
85+
86+
EclipseModel eclipseModel = project.extensions.getByType(EclipseModel.class)
87+
88+
for (ClasspathEntry dep : (eclipseModel.getClasspath().resolveDependencies())) {
89+
if (dep instanceof Library) {
90+
if (classpaths.get(dep.getPath()) == null){
91+
classpaths[dep.getPath()] = dep.getSourcePath()?.getPath()
92+
}
93+
}
94+
}
95+
96+
for (e in classpaths) {
97+
System.out.println "kotlin-lsp-gradle path:$e.key source:$e.value"
98+
}
7699
}
77100
}
78101

79102
task kotlinLSPAllGradleDeps {
80103
doLast {
81104
fileTree("$gradle.gradleHomeDir/lib")
82105
.findAll { it.toString().endsWith '.jar' }
83-
.forEach { System.out.println "kotlin-lsp-gradle $it" }
106+
.forEach { System.out.println "kotlin-lsp-gradle path:$it source:null" }
84107
}
85108
}
86109
}

0 commit comments

Comments
 (0)