Skip to content

Commit

Permalink
Strip sbt-native-packager universal zip's (fixes #41) (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
raboof authored Sep 27, 2018
1 parent 60e4b7d commit 695d69f
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 8 deletions.
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ libraryDependencies += "net.bzzt" % "reproducible-build" % "0.2"
libraryDependencies += "com.jsuereth" % "sbt-pgp" % sbtPgpVersion
libraryDependencies += "io.spray" %% "spray-json" % "1.3.4"

// Optional integration:
addSbtPlugin("com.typesafe.sbt" %% "sbt-native-packager" % "1.3.9" % Provided)

// Dogfood^WChampagne time!
import net.bzzt.reproduciblebuilds.ReproducibleBuildsPlugin._
reproducibleBuildsUploadPrefix := uri("http://pi.bzzt.net:8081/")
Expand Down
27 changes: 19 additions & 8 deletions src/main/scala/ReproducibleBuildsPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ import io.github.zlika.reproducible._
import sbt.io.syntax.{URI, uri}
import sbt.librarymanagement.Http.http

import scala.util.Success
import scala.util.{Success, Try}
import spray.json._

import scala.concurrent.ExecutionContext.Implicits.global

object ReproducibleBuildsPlugin extends AutoPlugin {
// To make sure we're loaded after the defaults
val universalPluginOnClasspath =
Try(getClass.getClassLoader.loadClass("com.typesafe.sbt.packager.universal.UniversalPlugin")).isSuccess

override def requires: Plugins = JvmPlugin

val reproducibleBuildsPackageName = taskKey[String]("Package name of this build, including version but excluding disambiguation string")
Expand Down Expand Up @@ -124,16 +128,23 @@ object ReproducibleBuildsPlugin extends AutoPlugin {
})
}
}
) ++ (
if (universalPluginOnClasspath) SbtNativePackagerHelpers.settings
else Seq.empty
)

def postProcessJar(jar: File): File = {
val dir = jar.getParentFile.toPath.resolve("stripped")
dir.toFile.mkdir()
val out = dir.resolve(jar.getName).toFile
new ZipStripper()
def postProcessJar(jar: File): File = postProcessWith(jar, new ZipStripper()
.addFileStripper("META-INF/MANIFEST.MF", new ManifestStripper())
.addFileStripper("META-INF/maven/\\S*/pom.properties", new PomPropertiesStripper())
.strip(jar, out)
.addFileStripper("META-INF/maven/\\S*/pom.properties", new PomPropertiesStripper()))

def postProcessZip(zip: File): File = postProcessWith(zip, new ZipStripper())

// TODO make the signature `Stripper`
private def postProcessWith(file: File, stripper: ZipStripper): File = {
val dir = file.getParentFile.toPath.resolve("stripped")
dir.toFile.mkdir()
val out = dir.resolve(file.getName).toFile
stripper.strip(file, out)
out
}

Expand Down
37 changes: 37 additions & 0 deletions src/main/scala/SbtNativePackagerHelpers.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.bzzt.reproduciblebuilds

import com.typesafe.sbt.packager.universal.UniversalPlugin
import com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport._
import sbt._
import sbt.Keys._

/**
* Helper code for sbt-native-packager integration.
*
* The main ReproducibleBuildsPlugin code should not rely on any
* sbt-native-packager code, since this plugin should be usable without
* sbt-native-packager and not introduce it to projects not already using it.
*
* Any code dependent on sbt-native-packager classes
* should go here, and only be called in case it is indeed available.
*
* To avoid 'leaking' references to sbt-native-packager classes into other
* classes, care should be taken that none appear in parameter or return
* value types either.
*/
object SbtNativePackagerHelpers {
val plugin: Plugins.Basic = UniversalPlugin

val none: File = new File("none")

val settings: Seq[Setting[_]] = Seq(
// Make sure there's always a value defined, even when the Universal plugin isn't loaded:
packageBin in Global := none,
packageBin in Universal := {
val upstream = (packageBin in Universal).value
// If the value is still `none`, the Universal plugin isn't loaded.
if (upstream == none) none
else ReproducibleBuildsPlugin.postProcessZip(upstream)
}
)
}
10 changes: 10 additions & 0 deletions src/sbt-test/sbt-reproducible-builds/native-packager/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import net.bzzt.reproduciblebuilds.ReproducibleBuildsPlugin.disambiguation

enablePlugins(ReproducibleBuildsPlugin)
enablePlugins(JavaAppPackaging)

// Universal plugin settings:
maintainer := "[email protected]"

// Make the filename static for easier validation:
disambiguation in Compile := (_ => Some("STATIC"))
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.2.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sys.props.get("plugin.version") match {
case Some(x) => addSbtPlugin("net.bzzt" % "sbt-reproducible-builds" % sys.props("plugin.version"))
case _ => sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
}

addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.9")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.bzzt

object Main extends App {
println("Hello, Reproducible World")
}
9 changes: 9 additions & 0 deletions src/sbt-test/sbt-reproducible-builds/native-packager/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
> package
$ exists target/scala-2.12/stripped/native-packager_2.12-0.1.0-SNAPSHOT.jar
$ must-mirror target/scala-2.12/stripped/native-packager_2.12-0.1.0-SNAPSHOT.jar expected/native-packager_2.12-0.1.0-SNAPSHOT.jar
> reproducibleBuildsCertification
$ exists target/scala-2.12/native-packager_2.12_0.1.0-SNAPSHOT_all_STATIC.buildinfo
# Not on travis:
#> signedReproducibleBuildsCertification
#> reproducibleBuildsUploadCertification
#> reproducibleBuildsCheckCertification
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ sys.props.get("plugin.version") match {
case _ => sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
}

// Included but not used, to catch problems with that combination:
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.9")

0 comments on commit 695d69f

Please sign in to comment.