|
| 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