Skip to content

Commit 9974c1f

Browse files
committed
FIX #405: Script replacements doesn't work with jdeb packaging
1 parent 60c5e90 commit 9974c1f

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

src/main/scala/com/typesafe/sbt/packager/debian/JDebPackaging.scala

+39-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package com.typesafe.sbt
22
package packager
33
package debian
44

5+
6+
import com.typesafe.sbt.packager.archetypes.TemplateWriter
7+
import com.typesafe.sbt.packager.universal.Archives
58
import sbt._
69
import sbt.Keys.{ target, normalizedName, version, streams, mappings, packageBin }
710
import linux.{ LinuxSymlink, LinuxPackageMapping, LinuxFileMetaData }
8-
import linux.LinuxPlugin.autoImport.{ linuxPackageMappings, linuxPackageSymlinks, packageArchitecture }
11+
import linux.LinuxPlugin.autoImport.{ linuxPackageMappings, linuxPackageSymlinks, packageArchitecture, linuxScriptReplacements }
912
import scala.collection.JavaConversions._
1013
import org.vafer.jdeb.{ DebMaker, DataProducer }
1114
import org.vafer.jdeb.mapping._
@@ -67,9 +70,14 @@ object JDebPackaging extends AutoPlugin with DebianPluginLike {
6770
val controlDir = targetDir / Names.Debian
6871
val control = debianControlFile.value
6972
val conffile = debianConffilesFile.value
73+
val replacements = debianMakeChownReplacements.value +: linuxScriptReplacements.value
7074

7175
val controlScripts = debianMaintainerScripts.value
72-
controlScripts foreach { case (file, script) => IO.copyFile(file, controlDir / script) }
76+
for ((file, name) <- controlScripts) {
77+
val targetFile = controlDir / name
78+
copyFiles(file, targetFile, LinuxFileMetaData())
79+
filterFiles(targetFile, replacements, LinuxFileMetaData())
80+
}
7381

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

100+
101+
/**
102+
* The same as [[DebianPluginLike.copyAndFixPerms]] except chmod invocation (for windows compatibility).
103+
* Permissions will be handled by jDeb packager itself.
104+
*/
105+
private[this] def copyFiles(from: File, to: File, perms: LinuxFileMetaData, zipped: Boolean = false): Unit = {
106+
if (zipped) {
107+
IO.withTemporaryDirectory { dir =>
108+
val tmp = dir / from.getName
109+
IO.copyFile(from, tmp)
110+
val zipped = Archives.gzip(tmp)
111+
IO.copyFile(zipped, to, true)
112+
}
113+
} else IO.copyFile(from, to, true)
114+
}
115+
116+
117+
/**
118+
* The same as [[DebianPluginLike.filterAndFixPerms]] except chmod invocation (for windows compatibility).
119+
* Permissions will be handled by jDeb packager itself.
120+
*/
121+
private[this] final def filterFiles(script: File, replacements: Seq[(String, String)], perms: LinuxFileMetaData): File = {
122+
val filtered = TemplateWriter.generateScript(script.toURI.toURL, replacements)
123+
IO.delete(script)
124+
IO.write(script, filtered)
125+
script
126+
}
127+
128+
92129
/**
93130
* Creating file and directory producers. These "produce" the
94131
* files for the debian packaging.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
enablePlugins(JavaServerAppPackaging, JDebPackaging)
2+
3+
name := "debian-test"
4+
5+
version := "0.1.0"
6+
7+
maintainer := "Josh Suereth <[email protected]>"
8+
9+
packageSummary := "Test debian package"
10+
11+
packageDescription := """A fun package description of our software,
12+
with multiple lines."""
13+
14+
debianPackageDependencies in Debian ++= Seq("java2-runtime", "bash (>= 2.05a-11)")
15+
16+
debianPackageRecommends in Debian += "git"
17+
18+
TaskKey[Unit]("check-control-files") <<= (target, streams) map { (target, out) =>
19+
val header = "#!/bin/sh"
20+
val extracted = target / "extracted"
21+
println(extracted.getAbsolutePath)
22+
Seq("dpkg-deb", "-R", (target / "debian-test_0.1.0_all.deb").absolutePath, extracted.absolutePath).!
23+
val preinst = extracted / "DEBIAN/preinst"
24+
val postinst = extracted / "DEBIAN/postinst"
25+
val prerm = extracted / "DEBIAN/prerm"
26+
val postrm = extracted / "DEBIAN/postrm"
27+
Seq(preinst, postinst, prerm, postrm) foreach { script =>
28+
val content = IO.read(script)
29+
assert(content.startsWith(header), "script doesn't start with #!/bin/sh header:\n" + script)
30+
assert(header.r.findAllIn(content).length == 1, "script contains more than one header line:\n" + script)
31+
}
32+
out.log.success("Successfully tested systemV control files")
33+
()
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Run the debian packaging.
2+
$ mkdir src/resources/empty
3+
> debian:package-bin
4+
$ exists target/debian-test_0.1.0_all.deb
5+
> check-control-files

0 commit comments

Comments
 (0)