Skip to content

Commit 695d69f

Browse files
authored
Strip sbt-native-packager universal zip's (fixes #41) (#43)
1 parent 60e4b7d commit 695d69f

File tree

10 files changed

+94
-8
lines changed

10 files changed

+94
-8
lines changed

build.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ libraryDependencies += "net.bzzt" % "reproducible-build" % "0.2"
1919
libraryDependencies += "com.jsuereth" % "sbt-pgp" % sbtPgpVersion
2020
libraryDependencies += "io.spray" %% "spray-json" % "1.3.4"
2121

22+
// Optional integration:
23+
addSbtPlugin("com.typesafe.sbt" %% "sbt-native-packager" % "1.3.9" % Provided)
24+
2225
// Dogfood^WChampagne time!
2326
import net.bzzt.reproduciblebuilds.ReproducibleBuildsPlugin._
2427
reproducibleBuildsUploadPrefix := uri("http://pi.bzzt.net:8081/")

src/main/scala/ReproducibleBuildsPlugin.scala

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ import io.github.zlika.reproducible._
1717
import sbt.io.syntax.{URI, uri}
1818
import sbt.librarymanagement.Http.http
1919

20-
import scala.util.Success
20+
import scala.util.{Success, Try}
2121
import spray.json._
22+
2223
import scala.concurrent.ExecutionContext.Implicits.global
2324

2425
object ReproducibleBuildsPlugin extends AutoPlugin {
2526
// To make sure we're loaded after the defaults
27+
val universalPluginOnClasspath =
28+
Try(getClass.getClassLoader.loadClass("com.typesafe.sbt.packager.universal.UniversalPlugin")).isSuccess
29+
2630
override def requires: Plugins = JvmPlugin
2731

2832
val reproducibleBuildsPackageName = taskKey[String]("Package name of this build, including version but excluding disambiguation string")
@@ -124,16 +128,23 @@ object ReproducibleBuildsPlugin extends AutoPlugin {
124128
})
125129
}
126130
}
131+
) ++ (
132+
if (universalPluginOnClasspath) SbtNativePackagerHelpers.settings
133+
else Seq.empty
127134
)
128135

129-
def postProcessJar(jar: File): File = {
130-
val dir = jar.getParentFile.toPath.resolve("stripped")
131-
dir.toFile.mkdir()
132-
val out = dir.resolve(jar.getName).toFile
133-
new ZipStripper()
136+
def postProcessJar(jar: File): File = postProcessWith(jar, new ZipStripper()
134137
.addFileStripper("META-INF/MANIFEST.MF", new ManifestStripper())
135-
.addFileStripper("META-INF/maven/\\S*/pom.properties", new PomPropertiesStripper())
136-
.strip(jar, out)
138+
.addFileStripper("META-INF/maven/\\S*/pom.properties", new PomPropertiesStripper()))
139+
140+
def postProcessZip(zip: File): File = postProcessWith(zip, new ZipStripper())
141+
142+
// TODO make the signature `Stripper`
143+
private def postProcessWith(file: File, stripper: ZipStripper): File = {
144+
val dir = file.getParentFile.toPath.resolve("stripped")
145+
dir.toFile.mkdir()
146+
val out = dir.resolve(file.getName).toFile
147+
stripper.strip(file, out)
137148
out
138149
}
139150

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.bzzt.reproduciblebuilds
2+
3+
import com.typesafe.sbt.packager.universal.UniversalPlugin
4+
import com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport._
5+
import sbt._
6+
import sbt.Keys._
7+
8+
/**
9+
* Helper code for sbt-native-packager integration.
10+
*
11+
* The main ReproducibleBuildsPlugin code should not rely on any
12+
* sbt-native-packager code, since this plugin should be usable without
13+
* sbt-native-packager and not introduce it to projects not already using it.
14+
*
15+
* Any code dependent on sbt-native-packager classes
16+
* should go here, and only be called in case it is indeed available.
17+
*
18+
* To avoid 'leaking' references to sbt-native-packager classes into other
19+
* classes, care should be taken that none appear in parameter or return
20+
* value types either.
21+
*/
22+
object SbtNativePackagerHelpers {
23+
val plugin: Plugins.Basic = UniversalPlugin
24+
25+
val none: File = new File("none")
26+
27+
val settings: Seq[Setting[_]] = Seq(
28+
// Make sure there's always a value defined, even when the Universal plugin isn't loaded:
29+
packageBin in Global := none,
30+
packageBin in Universal := {
31+
val upstream = (packageBin in Universal).value
32+
// If the value is still `none`, the Universal plugin isn't loaded.
33+
if (upstream == none) none
34+
else ReproducibleBuildsPlugin.postProcessZip(upstream)
35+
}
36+
)
37+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import net.bzzt.reproduciblebuilds.ReproducibleBuildsPlugin.disambiguation
2+
3+
enablePlugins(ReproducibleBuildsPlugin)
4+
enablePlugins(JavaAppPackaging)
5+
6+
// Universal plugin settings:
7+
maintainer := "[email protected]"
8+
9+
// Make the filename static for easier validation:
10+
disambiguation in Compile := (_ => Some("STATIC"))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.2.1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sys.props.get("plugin.version") match {
2+
case Some(x) => addSbtPlugin("net.bzzt" % "sbt-reproducible-builds" % sys.props("plugin.version"))
3+
case _ => sys.error("""|The system property 'plugin.version' is not defined.
4+
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
5+
}
6+
7+
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.9")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.bzzt
2+
3+
object Main extends App {
4+
println("Hello, Reproducible World")
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
> package
2+
$ exists target/scala-2.12/stripped/native-packager_2.12-0.1.0-SNAPSHOT.jar
3+
$ 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
4+
> reproducibleBuildsCertification
5+
$ exists target/scala-2.12/native-packager_2.12_0.1.0-SNAPSHOT_all_STATIC.buildinfo
6+
# Not on travis:
7+
#> signedReproducibleBuildsCertification
8+
#> reproducibleBuildsUploadCertification
9+
#> reproducibleBuildsCheckCertification

src/sbt-test/sbt-reproducible-builds/simple/project/plugins.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ sys.props.get("plugin.version") match {
33
case _ => sys.error("""|The system property 'plugin.version' is not defined.
44
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
55
}
6+
7+
// Included but not used, to catch problems with that combination:
8+
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.9")

0 commit comments

Comments
 (0)