Skip to content

Commit 1dc2a75

Browse files
authored
Merge pull request #331 from olafurpg/dep-repro
Add index-dependency command to troubleshoot package repo issues.
2 parents 8848cf4 + 2ba348f commit 1dc2a75

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

lsif-java/src/main/scala/com/sourcegraph/lsif_java/LsifJava.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.sourcegraph.lsif_java
33
import java.io.PrintStream
44

55
import com.sourcegraph.lsif_java.commands.IndexCommand
6+
import com.sourcegraph.lsif_java.commands.IndexDependencyCommand
67
import com.sourcegraph.lsif_java.commands.IndexSemanticdbCommand
78
import com.sourcegraph.lsif_java.commands.SnapshotCommand
89
import com.sourcegraph.lsif_java.commands.SnapshotLsifCommand
@@ -21,6 +22,7 @@ object LsifJava {
2122
CommandParser[VersionCommand],
2223
CommandParser[IndexCommand],
2324
CommandParser[IndexSemanticdbCommand],
25+
CommandParser[IndexDependencyCommand],
2426
CommandParser[SnapshotCommand],
2527
CommandParser[SnapshotLsifCommand]
2628
)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.sourcegraph.lsif_java.commands
2+
3+
import java.nio.charset.StandardCharsets
4+
import java.nio.file.Files
5+
import java.nio.file.Path
6+
import java.nio.file.Paths
7+
import java.nio.file.StandardCopyOption
8+
import java.nio.file.StandardOpenOption
9+
import java.util.jar.JarFile
10+
11+
import com.sourcegraph.io.AbsolutePath
12+
import com.sourcegraph.lsif_java.Dependencies
13+
import com.sourcegraph.lsif_semanticdb.JavaVersion
14+
import moped.cli.Command
15+
import moped.cli.CommandParser
16+
17+
final case class IndexDependencyCommand(
18+
target: Path = Paths.get("maven"),
19+
index: IndexCommand = IndexCommand(),
20+
dependency: String = ""
21+
) extends Command {
22+
def app = index.app
23+
private val absoluteTarget = AbsolutePath
24+
.of(target, app.env.workingDirectory)
25+
.resolve(dependency.replace(":", "__"))
26+
def run(): Int = {
27+
if (dependency == "") {
28+
app.reporter.error("dependency can't be empty")
29+
1
30+
} else {
31+
val deps = Dependencies
32+
.resolveDependencies(List(dependency), transitive = false)
33+
deps.sources.headOption match {
34+
case Some(sources) =>
35+
unzipJar(sources)
36+
deps.classpath.headOption match {
37+
case Some(classpath) =>
38+
Option(
39+
JavaVersion.classfileJvmVersion(classpath).orElse(8)
40+
) match {
41+
case Some(jvmVersion) =>
42+
val config =
43+
s"""{"kind":"maven","jvm":"$jvmVersion","dependencies":["$dependency"]}"""
44+
Files.createDirectories(absoluteTarget)
45+
Files.write(
46+
absoluteTarget.resolve("lsif-java.json"),
47+
config.getBytes(StandardCharsets.UTF_8),
48+
StandardOpenOption.CREATE,
49+
StandardOpenOption.TRUNCATE_EXISTING
50+
)
51+
index
52+
.copy(
53+
buildTool = Some("lsif"),
54+
app = app
55+
.withEnv(app.env.withWorkingDirectory(absoluteTarget))
56+
)
57+
.run()
58+
case None =>
59+
app
60+
.reporter
61+
.error(
62+
s"failed to infer JVM version for classpath '$classpath'"
63+
)
64+
1
65+
}
66+
case None =>
67+
app.reporter.error(s"no classpath for dependency '$dependency'")
68+
1
69+
}
70+
case None =>
71+
app.reporter.error(s"no sources for dependency '$dependency'")
72+
1
73+
}
74+
1
75+
}
76+
}
77+
78+
private def unzipJar(file: Path): Unit = {
79+
val jar = new JarFile(file.toFile())
80+
try {
81+
val entries = jar.entries()
82+
while (entries.hasMoreElements()) {
83+
val entry = entries.nextElement()
84+
if (!entry.isDirectory()) {
85+
val out = absoluteTarget.resolve(entry.getName())
86+
Files.createDirectories(out.getParent())
87+
val in = jar.getInputStream(entry)
88+
try Files.copy(in, out, StandardCopyOption.REPLACE_EXISTING)
89+
finally in.close()
90+
}
91+
}
92+
} finally {
93+
jar.close()
94+
}
95+
}
96+
}
97+
98+
object IndexDependencyCommand {
99+
implicit val parser = CommandParser.derive(IndexDependencyCommand())
100+
}

0 commit comments

Comments
 (0)