From 1c9b1025491498eeb0abc163a1496d3d259c7f70 Mon Sep 17 00:00:00 2001 From: Noah Santschi-Cooney Date: Tue, 20 Oct 2020 17:34:09 +0100 Subject: [PATCH 1/3] First iteration of gradle support: fetching of source directories and classpath --- build.gradle | 9 +++--- src/main/java/lsifjava/ProjectIndexer.java | 8 ++++- src/main/kotlin/lsifjava/GradleInterface.kt | 33 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/lsifjava/GradleInterface.kt diff --git a/build.gradle b/build.gradle index d6bda50b..b66b2905 100644 --- a/build.gradle +++ b/build.gradle @@ -7,12 +7,12 @@ plugins { repositories { jcenter() mavenLocal() - maven { - url = 'https://repo.maven.apache.org/maven2' - } + maven { url = 'https://repo.maven.apache.org/maven2' } + maven { url = 'https://repo.gradle.org/gradle/libs-releases' } } dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" compile files("${System.getProperty('java.home')}/../lib/tools.jar") compile 'com.google.code.gson:gson:2.8.6' compile 'commons-cli:commons-cli:1.4' @@ -23,8 +23,9 @@ dependencies { compile 'org.slf4j:slf4j-nop:1.7.30' implementation 'org.eclipse.lsp4j:org.eclipse.lsp4j:0.9.0' implementation 'com.google.guava:guava:29.0-jre' + implementation "org.gradle:gradle-tooling-api:6.6.1" + testImplementation 'junit:junit:4.13' - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" } application { diff --git a/src/main/java/lsifjava/ProjectIndexer.java b/src/main/java/lsifjava/ProjectIndexer.java index f3decd57..0d97aedc 100644 --- a/src/main/java/lsifjava/ProjectIndexer.java +++ b/src/main/java/lsifjava/ProjectIndexer.java @@ -32,7 +32,13 @@ public void index() throws IOException { var indexers = new HashMap(); var collector = new FileCollector(projectId, arguments, emitter, indexers); - Files.walkFileTree(Paths.get(arguments.projectRoot), collector); + + try(GradleInterface gradleInterface = new GradleInterface(arguments.projectRoot)) { + for (String it : gradleInterface.sourceDirectories()) { + Files.walkFileTree(Paths.get(it), collector); + } + } + var fileManager = new SourceFileManager(indexers.keySet()); for(var indexer : indexers.values()) { diff --git a/src/main/kotlin/lsifjava/GradleInterface.kt b/src/main/kotlin/lsifjava/GradleInterface.kt new file mode 100644 index 00000000..235eb475 --- /dev/null +++ b/src/main/kotlin/lsifjava/GradleInterface.kt @@ -0,0 +1,33 @@ +package lsifjava + +import org.gradle.tooling.GradleConnector +import org.gradle.tooling.model.GradleProject +import org.gradle.tooling.model.eclipse.EclipseProject +import org.gradle.tooling.model.idea.IdeaProject +import java.nio.file.Paths + +// TODO(nsc) exclusions? subprojects? inter-project dependencies? fml +class GradleInterface(private val projectDir: String): AutoCloseable { + private val projectConnection by lazy { + GradleConnector.newConnector().forProjectDirectory(Paths.get(projectDir).toFile()).connect() + } + + private val eclipseModel by lazy { + projectConnection.getModel(EclipseProject::class.java) + } + + fun classpath(): Iterable { + return eclipseModel.classpath.map { it.file.canonicalPath } + } + + fun sourceDirectories(): Iterable { + return eclipseModel.sourceDirectories.map { + Paths.get(eclipseModel.projectDirectory.toString(), it.path).toString() + } + } + + + override fun close() { + projectConnection.close() + } +} From b11a6bcb591c297d0ce1a8831a50b3299951ff69 Mon Sep 17 00:00:00 2001 From: Noah Santschi-Cooney Date: Tue, 20 Oct 2020 18:06:26 +0100 Subject: [PATCH 2/3] Using Path instead of String for source directories. Dedicated Classpath type with colon separated toString method --- src/main/java/lsifjava/ProjectIndexer.java | 4 ++-- src/main/kotlin/lsifjava/GradleInterface.kt | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/lsifjava/ProjectIndexer.java b/src/main/java/lsifjava/ProjectIndexer.java index 0d97aedc..23a5e876 100644 --- a/src/main/java/lsifjava/ProjectIndexer.java +++ b/src/main/java/lsifjava/ProjectIndexer.java @@ -34,8 +34,8 @@ public void index() throws IOException { var collector = new FileCollector(projectId, arguments, emitter, indexers); try(GradleInterface gradleInterface = new GradleInterface(arguments.projectRoot)) { - for (String it : gradleInterface.sourceDirectories()) { - Files.walkFileTree(Paths.get(it), collector); + for (Path it : gradleInterface.sourceDirectories()) { + Files.walkFileTree(it, collector); } } diff --git a/src/main/kotlin/lsifjava/GradleInterface.kt b/src/main/kotlin/lsifjava/GradleInterface.kt index 235eb475..d339b072 100644 --- a/src/main/kotlin/lsifjava/GradleInterface.kt +++ b/src/main/kotlin/lsifjava/GradleInterface.kt @@ -4,6 +4,7 @@ import org.gradle.tooling.GradleConnector import org.gradle.tooling.model.GradleProject import org.gradle.tooling.model.eclipse.EclipseProject import org.gradle.tooling.model.idea.IdeaProject +import java.nio.file.Path import java.nio.file.Paths // TODO(nsc) exclusions? subprojects? inter-project dependencies? fml @@ -16,18 +17,21 @@ class GradleInterface(private val projectDir: String): AutoCloseable { projectConnection.getModel(EclipseProject::class.java) } - fun classpath(): Iterable { - return eclipseModel.classpath.map { it.file.canonicalPath } + fun classpath(): Classpath { + return Classpath(eclipseModel.classpath.map { it.file.canonicalPath }) } - fun sourceDirectories(): Iterable { + fun sourceDirectories(): Iterable { return eclipseModel.sourceDirectories.map { - Paths.get(eclipseModel.projectDirectory.toString(), it.path).toString() + Paths.get(eclipseModel.projectDirectory.path, it.path) } } - override fun close() { projectConnection.close() } } + +class Classpath(private val classpaths: Iterable): Iterable by classpaths { + override fun toString() = classpaths.joinToString(":") +} \ No newline at end of file From d68e3b5901ef53f74219c0c31b1d93f00adedc80 Mon Sep 17 00:00:00 2001 From: Noah Santschi-Cooney Date: Tue, 20 Oct 2020 18:07:42 +0100 Subject: [PATCH 3/3] one-lining some methods in GradleInterface --- src/main/kotlin/lsifjava/GradleInterface.kt | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/lsifjava/GradleInterface.kt b/src/main/kotlin/lsifjava/GradleInterface.kt index d339b072..fddb0d06 100644 --- a/src/main/kotlin/lsifjava/GradleInterface.kt +++ b/src/main/kotlin/lsifjava/GradleInterface.kt @@ -17,19 +17,13 @@ class GradleInterface(private val projectDir: String): AutoCloseable { projectConnection.getModel(EclipseProject::class.java) } - fun classpath(): Classpath { - return Classpath(eclipseModel.classpath.map { it.file.canonicalPath }) - } + fun classpath() = Classpath(eclipseModel.classpath.map { it.file.canonicalPath }) - fun sourceDirectories(): Iterable { - return eclipseModel.sourceDirectories.map { - Paths.get(eclipseModel.projectDirectory.path, it.path) - } + fun sourceDirectories() = eclipseModel.sourceDirectories.map { + Paths.get(eclipseModel.projectDirectory.path, it.path) } - override fun close() { - projectConnection.close() - } + override fun close() = projectConnection.close() } class Classpath(private val classpaths: Iterable): Iterable by classpaths {