@@ -14,18 +14,29 @@ import com.sourcegraph.lsif_semanticdb.JavaVersion
1414import moped .cli .Command
1515import moped .cli .CommandParser
1616import moped .annotations .DeprecatedName
17+ import moped .annotations .Hidden
18+ import com .sourcegraph .io .DeleteVisitor
1719
1820final 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 )
0 commit comments