2021-11-23 11:51:17 +03:00
|
|
|
import sbt._
|
|
|
|
import sbt.util.CacheStoreFactory
|
|
|
|
|
|
|
|
/** A helper for generating manifests for bundled libraries. */
|
|
|
|
object LibraryManifestGenerator {
|
|
|
|
|
|
|
|
/** Represents a library that will be bundled with the engine and needs to
|
|
|
|
* have its manifest generated.
|
|
|
|
*/
|
|
|
|
case class BundledLibrary(name: String, version: String)
|
|
|
|
|
|
|
|
/** Generates manifests for the provided libraries.
|
|
|
|
*
|
|
|
|
* It assumes that the engine-runner/assembly task is up to date (as it uses
|
|
|
|
* its artifacts).
|
2023-11-17 21:02:36 +03:00
|
|
|
*
|
|
|
|
* @param javaOpts The java options to pass to the manifest generator.
|
2021-11-23 11:51:17 +03:00
|
|
|
*/
|
|
|
|
def generateManifests(
|
|
|
|
libraries: Seq[BundledLibrary],
|
|
|
|
distributionRoot: File,
|
|
|
|
log: Logger,
|
2023-11-17 21:02:36 +03:00
|
|
|
javaOpts: Seq[String],
|
2021-11-23 11:51:17 +03:00
|
|
|
cacheStoreFactory: CacheStoreFactory
|
|
|
|
): Unit =
|
|
|
|
for (BundledLibrary(qualifiedName, version) <- libraries) {
|
|
|
|
val (namespace, name) = qualifiedName.split('.') match {
|
|
|
|
case Array(namespace, name) => (namespace, name)
|
|
|
|
case _ =>
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
s"Invalid library name [$qualifiedName]."
|
|
|
|
)
|
|
|
|
}
|
|
|
|
val projectPath =
|
|
|
|
distributionRoot / "lib" / namespace / name / version
|
|
|
|
|
|
|
|
val store =
|
|
|
|
cacheStoreFactory.make(s"library-manifest-$namespace-$name-$version")
|
2023-10-12 11:19:45 +03:00
|
|
|
val sources = (projectPath / "src").allPaths.get
|
2021-11-23 11:51:17 +03:00
|
|
|
Tracked.diffInputs(store, FileInfo.hash)(sources.toSet) { diff =>
|
|
|
|
def manifestExists = (projectPath / "manifest.yaml").exists()
|
|
|
|
if (diff.modified.nonEmpty || !manifestExists) {
|
|
|
|
log.info(s"Regenerating manifest for [$projectPath].")
|
2023-11-17 21:02:36 +03:00
|
|
|
runGenerator(projectPath, javaOpts, log)
|
2021-11-23 11:51:17 +03:00
|
|
|
} else {
|
|
|
|
log.debug(s"[$projectPath] manifest is up to date.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 21:02:36 +03:00
|
|
|
private def runGenerator(
|
|
|
|
projectPath: File,
|
|
|
|
javaOpts: Seq[String],
|
|
|
|
log: Logger
|
|
|
|
): Unit = {
|
2024-07-31 12:43:17 +03:00
|
|
|
val canonicalPath = projectPath.getCanonicalFile
|
2021-11-23 11:51:17 +03:00
|
|
|
val javaCommand =
|
|
|
|
ProcessHandle.current().info().command().asScala.getOrElse("java")
|
|
|
|
val command = Seq(
|
2023-11-17 21:02:36 +03:00
|
|
|
javaCommand
|
|
|
|
) ++ javaOpts ++ Seq(
|
2021-11-23 11:51:17 +03:00
|
|
|
"--update-manifest",
|
|
|
|
"--in-project",
|
2024-07-31 12:43:17 +03:00
|
|
|
canonicalPath.toString
|
2021-11-23 11:51:17 +03:00
|
|
|
)
|
|
|
|
|
2024-05-02 10:36:10 +03:00
|
|
|
val commandText = command.mkString(" ")
|
|
|
|
log.debug(s"Running [$commandText].")
|
2021-11-23 11:51:17 +03:00
|
|
|
val exitCode = sys.process
|
|
|
|
.Process(
|
|
|
|
command,
|
2024-07-31 12:43:17 +03:00
|
|
|
cwd = Some(canonicalPath.getParentFile),
|
2021-11-23 11:51:17 +03:00
|
|
|
"ENSO_EDITION_PATH" -> file("distribution/editions").getCanonicalPath
|
|
|
|
)
|
|
|
|
.!
|
|
|
|
if (exitCode != 0) {
|
2024-05-02 10:36:10 +03:00
|
|
|
val message = s"Command [$commandText] has failed with code $exitCode."
|
2021-11-23 11:51:17 +03:00
|
|
|
log.error(message)
|
|
|
|
throw new RuntimeException(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|