Skip to content

Commit 7279e06

Browse files
committed
Use --snapshot flag for index-dependency command.
1 parent c92b4c9 commit 7279e06

File tree

2 files changed

+66
-48
lines changed

2 files changed

+66
-48
lines changed

lsif-java/src/main/scala/com/sourcegraph/lsif_java/commands/IndexDependencyCommand.scala

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,29 @@ import com.sourcegraph.lsif_semanticdb.JavaVersion
1414
import moped.cli.Command
1515
import moped.cli.CommandParser
1616
import moped.annotations.DeprecatedName
17+
import moped.annotations.Hidden
18+
import com.sourcegraph.io.DeleteVisitor
1719

1820
final case class IndexDependencyCommand(
1921
@DeprecatedName("target", "Use --output instead", "0.6.10") output: Path =
2022
Paths.get("maven"),
2123
index: IndexCommand = IndexCommand(),
24+
@Hidden
25+
snapshotCommand: SnapshotLsifCommand = SnapshotLsifCommand(),
2226
dependency: String = "",
23-
provided: List[String] = Nil
27+
provided: List[String] = Nil,
28+
snapshot: Boolean = false
2429
) extends Command {
2530
def app = index.app
2631
private val absoluteTarget = AbsolutePath
2732
.of(output, app.env.workingDirectory)
2833
.resolve(dependency.replace(":", "__"))
34+
private val indexTarget =
35+
if (!snapshot)
36+
absoluteTarget
37+
else
38+
Files.createTempDirectory("lsif-java-index")
39+
private val snapshotTarget = absoluteTarget
2940
def run(): Int = {
3041
if (dependency == "") {
3142
app.reporter.error("dependency can't be empty")
@@ -34,59 +45,78 @@ final case class IndexDependencyCommand(
3445
val deps = Dependencies
3546
.resolveDependencies(dependency :: provided, transitive = false)
3647
deps.sources.headOption match {
48+
case None =>
49+
app.reporter.error(s"no sources for dependency '$dependency'")
50+
1
3751
case Some(sources) =>
3852
unzipJar(sources)
3953
deps.classpath.headOption match {
54+
case None =>
55+
app.reporter.error(s"no classpath for dependency '$dependency'")
56+
1
4057
case Some(classpath) =>
41-
Option(
42-
JavaVersion.classfileJvmVersion(classpath).orElse(8)
43-
) match {
44-
case Some(jvmVersion) =>
45-
val roundedVersion = JavaVersion
46-
.roundToNearestStableRelease(jvmVersion)
47-
val config =
48-
s"""{"kind":"maven","jvm":"${roundedVersion}","dependencies":["$dependency"]}"""
49-
Files.createDirectories(absoluteTarget)
50-
Files.write(
51-
absoluteTarget.resolve("lsif-java.json"),
52-
config.getBytes(StandardCharsets.UTF_8),
53-
StandardOpenOption.CREATE,
54-
StandardOpenOption.TRUNCATE_EXISTING
55-
)
56-
index
57-
.copy(
58-
buildTool = Some("lsif"),
59-
app = app
60-
.withEnv(app.env.withWorkingDirectory(absoluteTarget))
61-
)
62-
.run()
58+
inferJvmVersion(classpath) match {
6359
case None =>
6460
app
6561
.reporter
6662
.error(
6763
s"failed to infer JVM version for classpath '$classpath'"
6864
)
6965
1
66+
case Some(jvmVersion) =>
67+
val exit = indexJar(jvmVersion)
68+
if (exit == 0 && snapshot) {
69+
try {
70+
snapshotCommand
71+
.copy(
72+
output = snapshotTarget,
73+
app = app
74+
.withEnv(app.env.withWorkingDirectory(indexTarget))
75+
)
76+
.run()
77+
} finally {
78+
Files.walkFileTree(indexTarget, new DeleteVisitor())
79+
}
80+
} else {
81+
exit
82+
}
7083
}
71-
case None =>
72-
app.reporter.error(s"no classpath for dependency '$dependency'")
73-
1
7484
}
75-
case None =>
76-
app.reporter.error(s"no sources for dependency '$dependency'")
77-
1
7885
}
7986
}
8087
}
8188

89+
private def inferJvmVersion(jar: Path): Option[Int] = {
90+
Option(JavaVersion.classfileJvmVersion(jar).orElse(8))
91+
.map(JavaVersion.roundToNearestStableRelease(_))
92+
}
93+
94+
private def indexJar(jvmVersion: Int): Int = {
95+
val config =
96+
s"""{"kind":"maven","jvm":"$jvmVersion","dependencies":["$dependency"]}"""
97+
Files.createDirectories(indexTarget)
98+
Files.write(
99+
indexTarget.resolve("lsif-java.json"),
100+
config.getBytes(StandardCharsets.UTF_8),
101+
StandardOpenOption.CREATE,
102+
StandardOpenOption.TRUNCATE_EXISTING
103+
)
104+
index
105+
.copy(
106+
buildTool = Some("lsif"),
107+
app = app.withEnv(app.env.withWorkingDirectory(indexTarget))
108+
)
109+
.run()
110+
}
111+
82112
private def unzipJar(file: Path): Unit = {
83113
val jar = new JarFile(file.toFile())
84114
try {
85115
val entries = jar.entries()
86116
while (entries.hasMoreElements()) {
87117
val entry = entries.nextElement()
88118
if (!entry.isDirectory()) {
89-
val out = absoluteTarget.resolve(entry.getName())
119+
val out = indexTarget.resolve(entry.getName())
90120
Files.createDirectories(out.getParent())
91121
val in = jar.getInputStream(entry)
92122
try Files.copy(in, out, StandardCopyOption.REPLACE_EXISTING)

tests/snapshots/src/main/scala/tests/LibrarySnapshotGenerator.scala

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,40 +66,28 @@ class LibrarySnapshotGenerator extends SnapshotGenerator {
6666
println(s"indexing library '$name'")
6767
val providedArguments = provided.flatMap(p => List("--provided", p))
6868
val targetroot = Files.createTempDirectory("semanticdb-javac")
69-
val indexDir = Files.createTempDirectory("semanticdb-javac")
69+
val snapshotDir = Files.createTempDirectory("semanticdb-javac")
7070
runLsifJava(
7171
List(
7272
"index-dependency",
73+
"--snapshot",
7374
"--dependency",
7475
name,
7576
"--output",
76-
indexDir.toString()
77+
snapshotDir.toString()
7778
) ++ providedArguments
7879
)
79-
val snapshotDir = Files.createTempDirectory("semanticdb-javac")
80-
val dumpDir = indexDir.toFile().listFiles().head.toPath
81-
val outputDir = snapshotDir.resolve(dumpDir.getFileName())
82-
runLsifJava(
83-
List(
84-
"snapshot-lsif",
85-
"--cwd",
86-
dumpDir.toString(),
87-
"--output",
88-
outputDir.toString()
89-
)
90-
)
80+
val root = snapshotDir.toFile().listFiles().head.toPath()
9181
Files.walkFileTree(
92-
outputDir,
82+
root,
9383
new SimpleFileVisitor[Path] {
9484
override def visitFile(
9585
file: Path,
9686
attrs: BasicFileAttributes
9787
): FileVisitResult = {
9888
val print =
9989
new String(Files.readAllBytes(file), StandardCharsets.UTF_8)
100-
val out = context
101-
.expectDirectory
102-
.resolve(outputDir.relativize(file))
90+
val out = context.expectDirectory.resolve(root.relativize(file))
10391
handler.onSnapshotTest(context, out, () => print)
10492
super.visitFile(file, attrs)
10593
}

0 commit comments

Comments
 (0)