Skip to content

Commit

Permalink
FIX #405: Script replacements doesn't work with jdeb packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
kardapoltsev committed Nov 11, 2014
1 parent 60c5e90 commit 9974c1f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
Expand Up @@ -2,10 +2,13 @@ package com.typesafe.sbt
package packager
package debian


import com.typesafe.sbt.packager.archetypes.TemplateWriter
import com.typesafe.sbt.packager.universal.Archives
import sbt._
import sbt.Keys.{ target, normalizedName, version, streams, mappings, packageBin }
import linux.{ LinuxSymlink, LinuxPackageMapping, LinuxFileMetaData }
import linux.LinuxPlugin.autoImport.{ linuxPackageMappings, linuxPackageSymlinks, packageArchitecture }
import linux.LinuxPlugin.autoImport.{ linuxPackageMappings, linuxPackageSymlinks, packageArchitecture, linuxScriptReplacements }
import scala.collection.JavaConversions._
import org.vafer.jdeb.{ DebMaker, DataProducer }
import org.vafer.jdeb.mapping._
Expand Down Expand Up @@ -67,9 +70,14 @@ object JDebPackaging extends AutoPlugin with DebianPluginLike {
val controlDir = targetDir / Names.Debian
val control = debianControlFile.value
val conffile = debianConffilesFile.value
val replacements = debianMakeChownReplacements.value +: linuxScriptReplacements.value

val controlScripts = debianMaintainerScripts.value
controlScripts foreach { case (file, script) => IO.copyFile(file, controlDir / script) }
for ((file, name) <- controlScripts) {
val targetFile = controlDir / name
copyFiles(file, targetFile, LinuxFileMetaData())
filterFiles(targetFile, replacements, LinuxFileMetaData())
}

log.info("Building debian package with java based implementation 'jdeb'")
val console = new JDebConsole(log)
Expand All @@ -89,6 +97,35 @@ object JDebPackaging extends AutoPlugin with DebianPluginLike {
debianFile
})


/**
* The same as [[DebianPluginLike.copyAndFixPerms]] except chmod invocation (for windows compatibility).
* Permissions will be handled by jDeb packager itself.
*/
private[this] def copyFiles(from: File, to: File, perms: LinuxFileMetaData, zipped: Boolean = false): Unit = {
if (zipped) {
IO.withTemporaryDirectory { dir =>
val tmp = dir / from.getName
IO.copyFile(from, tmp)
val zipped = Archives.gzip(tmp)
IO.copyFile(zipped, to, true)
}
} else IO.copyFile(from, to, true)
}


/**
* The same as [[DebianPluginLike.filterAndFixPerms]] except chmod invocation (for windows compatibility).
* Permissions will be handled by jDeb packager itself.
*/
private[this] final def filterFiles(script: File, replacements: Seq[(String, String)], perms: LinuxFileMetaData): File = {
val filtered = TemplateWriter.generateScript(script.toURI.toURL, replacements)
IO.delete(script)
IO.write(script, filtered)
script
}


/**
* Creating file and directory producers. These "produce" the
* files for the debian packaging.
Expand Down
34 changes: 34 additions & 0 deletions src/sbt-test/debian/jdeb-script-replacements/build.sbt
@@ -0,0 +1,34 @@
enablePlugins(JavaServerAppPackaging, JDebPackaging)

name := "debian-test"

version := "0.1.0"

maintainer := "Josh Suereth <joshua.suereth@typesafe.com>"

packageSummary := "Test debian package"

packageDescription := """A fun package description of our software,
with multiple lines."""

debianPackageDependencies in Debian ++= Seq("java2-runtime", "bash (>= 2.05a-11)")

debianPackageRecommends in Debian += "git"

TaskKey[Unit]("check-control-files") <<= (target, streams) map { (target, out) =>
val header = "#!/bin/sh"
val extracted = target / "extracted"
println(extracted.getAbsolutePath)
Seq("dpkg-deb", "-R", (target / "debian-test_0.1.0_all.deb").absolutePath, extracted.absolutePath).!
val preinst = extracted / "DEBIAN/preinst"
val postinst = extracted / "DEBIAN/postinst"
val prerm = extracted / "DEBIAN/prerm"
val postrm = extracted / "DEBIAN/postrm"
Seq(preinst, postinst, prerm, postrm) foreach { script =>
val content = IO.read(script)
assert(content.startsWith(header), "script doesn't start with #!/bin/sh header:\n" + script)
assert(header.r.findAllIn(content).length == 1, "script contains more than one header line:\n" + script)
}
out.log.success("Successfully tested systemV control files")
()
}
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
5 changes: 5 additions & 0 deletions src/sbt-test/debian/jdeb-script-replacements/test
@@ -0,0 +1,5 @@
# Run the debian packaging.
$ mkdir src/resources/empty
> debian:package-bin
$ exists target/debian-test_0.1.0_all.deb
> check-control-files

0 comments on commit 9974c1f

Please sign in to comment.