2025-01-02 11:16:32 +09:00
|
|
|
import sbt.Keys.*
|
|
|
|
|
import sbt.*
|
2017-07-19 22:00:42 -04:00
|
|
|
import sbt.io.CopyOptions
|
2026-07-21 01:38:53 +09:00
|
|
|
import sbt.util.CacheImplicits.given
|
2015-03-24 16:12:51 -04:00
|
|
|
|
|
|
|
|
object SbtLauncherPlugin extends AutoPlugin:
|
|
|
|
|
override def requires = plugins.IvyPlugin
|
|
|
|
|
|
|
|
|
|
object autoImport:
|
|
|
|
|
val SbtLaunchConfiguration = config("sbt-launch")
|
2026-07-21 01:38:53 +09:00
|
|
|
val sbtLaunchJar =
|
|
|
|
|
taskKey[HashedVirtualFileRef]("constructs an sbt-launch.jar for this version of sbt.")
|
2015-03-24 16:12:51 -04:00
|
|
|
val rawSbtLaunchJar =
|
2026-07-21 01:38:53 +09:00
|
|
|
taskKey[HashedVirtualFileRef](
|
|
|
|
|
"The released version of the sbt-launcher we use to bundle this application."
|
|
|
|
|
)
|
2025-01-02 11:16:32 +09:00
|
|
|
import autoImport.*
|
2015-03-24 16:12:51 -04:00
|
|
|
|
|
|
|
|
override def projectConfigurations: Seq[Configuration] = Seq(SbtLaunchConfiguration)
|
2025-09-23 10:28:38 +09:00
|
|
|
override def projectSettings: Seq[Setting[?]] = Seq(
|
2015-03-24 16:12:51 -04:00
|
|
|
libraryDependencies += Dependencies.rawLauncher % SbtLaunchConfiguration.name,
|
2026-07-21 01:38:53 +09:00
|
|
|
rawSbtLaunchJar := Def.uncached {
|
|
|
|
|
val converter = fileConverter.value
|
|
|
|
|
Classpaths
|
|
|
|
|
.managedJars(SbtLaunchConfiguration, Set("jar"), update.value, converter)
|
|
|
|
|
.headOption match
|
2015-03-24 16:12:51 -04:00
|
|
|
case Some(jar) => jar.data
|
2026-05-31 16:01:15 -04:00
|
|
|
case None =>
|
2015-03-24 16:12:51 -04:00
|
|
|
sys.error(
|
2019-01-31 11:18:27 -08:00
|
|
|
s"Could not resolve sbt launcher!, dependencies := ${libraryDependencies.value}"
|
|
|
|
|
)
|
2015-03-24 16:12:51 -04:00
|
|
|
},
|
2026-07-21 01:38:53 +09:00
|
|
|
sbtLaunchJar := Def.uncached {
|
2021-05-02 23:25:23 -04:00
|
|
|
val propFiles = (Compile / resources).value
|
2015-03-24 16:12:51 -04:00
|
|
|
val propFileLocations =
|
|
|
|
|
for file <- propFiles; if file.getName != "resources"
|
|
|
|
|
yield
|
|
|
|
|
if file.getName == "sbt.boot.properties" then "sbt/sbt.boot.properties" -> file
|
|
|
|
|
else file.getName -> file
|
|
|
|
|
// TODO - We need to inject the appropriate boot.properties file for this version of sbt.
|
2026-07-21 01:38:53 +09:00
|
|
|
fileConverter.value.toVirtualFile(
|
|
|
|
|
rebundle(
|
|
|
|
|
fileConverter.value.toPath(rawSbtLaunchJar.value).toFile,
|
|
|
|
|
propFileLocations.toMap,
|
|
|
|
|
target.value / "sbt-launch.jar"
|
|
|
|
|
).toPath
|
|
|
|
|
)
|
2015-03-24 16:12:51 -04:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def rebundle(jar: File, overrides: Map[String, File], target: File): File =
|
|
|
|
|
// TODO - Check if we should rebuild the jar or not....
|
|
|
|
|
IO.withTemporaryDirectory { dir =>
|
|
|
|
|
IO.unzip(jar, dir)
|
2017-07-19 22:00:42 -04:00
|
|
|
IO.copy(overrides.map({ case (n, f) => (f, dir / n) }), CopyOptions().withOverwrite(true))
|
2015-03-24 16:12:51 -04:00
|
|
|
// TODO - is the ok for creating a jar?
|
2019-01-13 16:04:58 -08:00
|
|
|
val rebase: File => Seq[(File, String)] =
|
|
|
|
|
val path = dir.toPath
|
2019-11-30 14:18:41 -08:00
|
|
|
f => if f != dir then f -> path.relativize(f.toPath).toString :: Nil else Nil
|
2024-03-05 13:24:31 +01:00
|
|
|
IO.zip(dir.allPaths.get().flatMap(rebase), target, None)
|
2015-03-24 16:12:51 -04:00
|
|
|
}
|
|
|
|
|
target
|
2019-01-31 11:18:27 -08:00
|
|
|
end SbtLauncherPlugin
|