Stop using ---/pair in SbtLauncherPlugin
I'd like to remove '---' and 'pair' in sbt 2 so I'm inlining the logic where
I find it. The '---' method is trivially implemented with a filter on
the sequence of files and filtering the output will not require io,
unlike '---'. For pair, I get confused every time I see it in the code
and it is rarely saving more than a line. While I understand that it may
have been convenient when the code using pair was originally written, I
don't think it is worth the maintenance cost. My specific issue is that
to me pair means tuple2, full stop. The definition of pair is:
def pair[T](mapper: File => Option[T], errorIfNone: Boolean = true): Seq[(File, T)]
First of all, it's not at all obvious when seen inline in the code that
it has the side effect of evaluating PathFinder.get. Moreover, it
doesn't return a general pair, it's a very specific pair with a File in
the first position. I just don't see how using pair improves upon, say:
val func: File => Option[(File, String)] = ???
globs.all.flatMap(func)
or
val func: File => Option[(File, String)] = ???
globs.all.map(f => func(f) match {
case Some(r) => r
case None => throw new IllegalStateException("Couldn't evaluate func for $f")
}) // or just define `func = File => (File, String)` and throw on an error
2019-01-14 01:04:58 +01:00
|
|
|
import sbt.Keys._
|
2015-03-24 21:12:51 +01:00
|
|
|
import sbt._
|
2017-07-20 04:00:42 +02:00
|
|
|
import sbt.io.CopyOptions
|
2015-03-24 21:12:51 +01:00
|
|
|
|
|
|
|
|
object SbtLauncherPlugin extends AutoPlugin {
|
|
|
|
|
override def requires = plugins.IvyPlugin
|
|
|
|
|
|
|
|
|
|
object autoImport {
|
|
|
|
|
val SbtLaunchConfiguration = config("sbt-launch")
|
|
|
|
|
val sbtLaunchJar = taskKey[File]("constructs an sbt-launch.jar for this version of sbt.")
|
2017-04-21 09:14:31 +02:00
|
|
|
val rawSbtLaunchJar =
|
|
|
|
|
taskKey[File]("The released version of the sbt-launcher we use to bundle this application.")
|
2015-03-24 21:12:51 +01:00
|
|
|
}
|
|
|
|
|
import autoImport._
|
|
|
|
|
|
|
|
|
|
override def projectConfigurations: Seq[Configuration] = Seq(SbtLaunchConfiguration)
|
|
|
|
|
override def projectSettings: Seq[Setting[_]] = Seq(
|
|
|
|
|
libraryDependencies += Dependencies.rawLauncher % SbtLaunchConfiguration.name,
|
|
|
|
|
rawSbtLaunchJar := {
|
|
|
|
|
Classpaths.managedJars(SbtLaunchConfiguration, Set("jar"), update.value).headOption match {
|
|
|
|
|
case Some(jar) => jar.data
|
2017-04-21 09:14:31 +02:00
|
|
|
case None =>
|
|
|
|
|
sys.error(
|
|
|
|
|
s"Could not resolve sbt launcher!, dependencies := ${libraryDependencies.value}")
|
2015-03-24 21:12:51 +01:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
sbtLaunchJar := {
|
|
|
|
|
val propFiles = (resources in Compile).value
|
|
|
|
|
val propFileLocations =
|
2017-04-21 09:14:31 +02:00
|
|
|
for (file <- propFiles; if file.getName != "resources") yield {
|
|
|
|
|
if (file.getName == "sbt.boot.properties") "sbt/sbt.boot.properties" -> file
|
|
|
|
|
else file.getName -> file
|
|
|
|
|
}
|
2015-03-24 21:12:51 +01:00
|
|
|
// TODO - We need to inject the appropriate boot.properties file for this version of sbt.
|
|
|
|
|
rebundle(rawSbtLaunchJar.value, propFileLocations.toMap, target.value / "sbt-launch.jar")
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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-20 04:00:42 +02:00
|
|
|
IO.copy(overrides.map({ case (n, f) => (f, dir / n) }), CopyOptions().withOverwrite(true))
|
2015-03-24 21:12:51 +01:00
|
|
|
// TODO - is the ok for creating a jar?
|
Stop using ---/pair in SbtLauncherPlugin
I'd like to remove '---' and 'pair' in sbt 2 so I'm inlining the logic where
I find it. The '---' method is trivially implemented with a filter on
the sequence of files and filtering the output will not require io,
unlike '---'. For pair, I get confused every time I see it in the code
and it is rarely saving more than a line. While I understand that it may
have been convenient when the code using pair was originally written, I
don't think it is worth the maintenance cost. My specific issue is that
to me pair means tuple2, full stop. The definition of pair is:
def pair[T](mapper: File => Option[T], errorIfNone: Boolean = true): Seq[(File, T)]
First of all, it's not at all obvious when seen inline in the code that
it has the side effect of evaluating PathFinder.get. Moreover, it
doesn't return a general pair, it's a very specific pair with a File in
the first position. I just don't see how using pair improves upon, say:
val func: File => Option[(File, String)] = ???
globs.all.flatMap(func)
or
val func: File => Option[(File, String)] = ???
globs.all.map(f => func(f) match {
case Some(r) => r
case None => throw new IllegalStateException("Couldn't evaluate func for $f")
}) // or just define `func = File => (File, String)` and throw on an error
2019-01-14 01:04:58 +01:00
|
|
|
val rebase: File => Seq[(File, String)] = {
|
|
|
|
|
val path = dir.toPath
|
|
|
|
|
f =>
|
|
|
|
|
if (f != dir) f -> path.relativize(f.toPath).toString :: Nil else Nil
|
|
|
|
|
}
|
|
|
|
|
IO.zip(dir.allPaths.get().flatMap(rebase), target)
|
2015-03-24 21:12:51 +01:00
|
|
|
}
|
|
|
|
|
target
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-10 11:53:48 +02:00
|
|
|
}
|