|
| 1 | +package com.sourcegraph.packagehub |
| 2 | + |
| 3 | +import java.nio.file.Path |
| 4 | +import java.nio.file.Paths |
| 5 | + |
| 6 | +import scala.util.control.NonFatal |
| 7 | + |
| 8 | +import com.sourcegraph.lsif_java.Dependencies |
| 9 | +import coursier.core.Dependency |
| 10 | +import coursier.core.Module |
| 11 | +import coursier.core.ModuleName |
| 12 | +import coursier.core.Organization |
| 13 | +import ujson.Obj |
| 14 | + |
| 15 | +/** |
| 16 | + * Package represents a published library such as a Java artifact, or the JDK. |
| 17 | + * |
| 18 | + * @param id |
| 19 | + * unique representation for this package that does not include the forward |
| 20 | + * slash character. Can be used as the primary key in a relational database. |
| 21 | + * Should ideally be human-readable and be easy to parse. |
| 22 | + * @param path |
| 23 | + * relative URL of this package. |
| 24 | + */ |
| 25 | +sealed abstract class Package( |
| 26 | + val id: String, |
| 27 | + val path: String, |
| 28 | + val version: String |
| 29 | +) { |
| 30 | + def toJsonRepo: Obj = Obj("Name" -> path, "URI" -> s"/repos/$path") |
| 31 | + def relativePath: Path = Paths.get(path) |
| 32 | +} |
| 33 | +object Package { |
| 34 | + def jdk(version: String): JdkPackage = { |
| 35 | + JdkPackage(version) |
| 36 | + } |
| 37 | + def maven(org: String, name: String, version: String): MavenPackage = { |
| 38 | + MavenPackage( |
| 39 | + Dependency( |
| 40 | + Module(Organization(org), ModuleName(name), Map.empty), |
| 41 | + version |
| 42 | + ) |
| 43 | + ) |
| 44 | + } |
| 45 | + def parse(value: String): Package = { |
| 46 | + value match { |
| 47 | + case s"jdk:$version" => |
| 48 | + JdkPackage(version) |
| 49 | + case s"maven:$library" => |
| 50 | + val Right(dep) = Dependencies.parseDependencyEither(library) |
| 51 | + MavenPackage(dep) |
| 52 | + } |
| 53 | + } |
| 54 | + def fromPath(path: List[String]): Option[(Package, List[String])] = |
| 55 | + path match { |
| 56 | + case "maven" :: org :: name :: version :: requestPath => |
| 57 | + Some(Package.maven(org, name, version) -> requestPath) |
| 58 | + case "jdk" :: version :: requestPath => |
| 59 | + Some(Package.jdk(version) -> requestPath) |
| 60 | + case _ => |
| 61 | + None |
| 62 | + } |
| 63 | + def fromString(value: String, coursier: String): Either[String, Package] = { |
| 64 | + value match { |
| 65 | + case s"jdk:$version" => |
| 66 | + val exit = os |
| 67 | + .proc(coursier, "java-home", "--jvm", version) |
| 68 | + .call(check = false) |
| 69 | + if (exit.exitCode == 0) |
| 70 | + Right(JdkPackage(version)) |
| 71 | + else |
| 72 | + Left(exit.out.trim()) |
| 73 | + case s"maven:$library" => |
| 74 | + Dependencies |
| 75 | + .parseDependencyEither(library) |
| 76 | + .flatMap { dep => |
| 77 | + try { |
| 78 | + // Report an error if the dependency can't be resolved. |
| 79 | + Dependencies.resolveProvidedDeps(dep) |
| 80 | + Right(MavenPackage(dep)) |
| 81 | + } catch { |
| 82 | + case NonFatal(e) => |
| 83 | + Left(e.getMessage()) |
| 84 | + } |
| 85 | + } |
| 86 | + case other => |
| 87 | + Left( |
| 88 | + s"unsupported package '$other'. To fix this problem, use a valid syntax " + |
| 89 | + s"such as 'maven:ORGANIZATION:ARTIFACT_NAME_VERSION' for Java libraries." |
| 90 | + ) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * A Java library that is published "Maven style". |
| 97 | + * |
| 98 | + * The most widely used Maven package host is "Maven Central" |
| 99 | + * https://search.maven.org/. Most companies self-host an Artifactory instance |
| 100 | + * to publish internal libraries and to proxy Maven Central. |
| 101 | + */ |
| 102 | +case class MavenPackage(dep: Dependency) |
| 103 | + extends Package( |
| 104 | + s"maven:${dep.module.repr}:${dep.version}", |
| 105 | + s"maven/${dep.module.organization.value}/${dep.module.name.value}/${dep.version}", |
| 106 | + dep.version |
| 107 | + ) { |
| 108 | + def repr = id.stripPrefix("maven:") |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * The Java standard library. |
| 113 | + * |
| 114 | + * The sources of the Java standard library are typically available under |
| 115 | + * JAVA_HOME. |
| 116 | + */ |
| 117 | +case class JdkPackage(override val version: String) |
| 118 | + extends Package(s"jdk:${version}", s"jdk/${version}", version) { |
| 119 | + def repr = id.stripPrefix("jdk:") |
| 120 | +} |
0 commit comments