Skip to content

Commit

Permalink
Merge pull request #646 from damirv/master
Browse files Browse the repository at this point in the history
overridable bash and bat templates
  • Loading branch information
muuki88 committed Aug 10, 2015
2 parents 1aa3666 + f87edb9 commit f62896f
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 26 deletions.
41 changes: 23 additions & 18 deletions src/main/scala/com/typesafe/sbt/packager/archetypes/JavaApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,17 @@ object JavaAppPackaging extends AutoPlugin with JavaAppStartScript {
} yield JavaAppBashScript.makeDefines(cn, appClasspath = cp, extras = extras, configFile = config)
hasMain getOrElse Nil
},
// TODO - Overridable bash template.
makeBashScript <<= (bashScriptDefines, target in Universal, executableScriptName, sourceDirectory) map makeUniversalBinScript(bashTemplate),
bashScriptTemplateLocation := (sourceDirectory.value / "templates" / bashTemplate),
makeBashScript <<= (bashScriptTemplateLocation, bashScriptDefines, target in Universal, executableScriptName, sourceDirectory) map makeUniversalBinScript,
batScriptExtraDefines := Nil,
batScriptReplacements <<= (packageName, Keys.mainClass in (Compile, batScriptReplacements), scriptClasspath in batScriptReplacements, batScriptExtraDefines) map { (name, mainClass, cp, extras) =>
mainClass map { mc =>
JavaAppBatScript.makeReplacements(name = name, mainClass = mc, appClasspath = cp, extras = extras)
} getOrElse Nil

},
makeBatScript <<= (batScriptReplacements, target in Universal, executableScriptName, sourceDirectory) map makeUniversalBatScript(batTemplate),
batScriptTemplateLocation := (sourceDirectory.value / "templates" / batTemplate),
makeBatScript <<= (batScriptTemplateLocation, batScriptReplacements, target in Universal, executableScriptName, sourceDirectory) map makeUniversalBatScript,
mappings in Universal <++= (makeBashScript, executableScriptName) map { (script, name) =>
for {
s <- script.toSeq
Expand Down Expand Up @@ -281,16 +282,10 @@ object JavaAppPackaging extends AutoPlugin with JavaAppStartScript {
*/
trait JavaAppStartScript {

def makeUniversalBinScript(bashTemplate: String)(defines: Seq[String], tmpDir: File, name: String, sourceDir: File): Option[File] =
def makeUniversalBinScript(defaultTemplateLocation: File, defines: Seq[String], tmpDir: File, name: String, sourceDir: File): Option[File] =
if (defines.isEmpty) None
else {
val defaultTemplateLocation = sourceDir / "templates" / bashTemplate
val defaultTemplateSource = getClass getResource bashTemplate

val template = if (defaultTemplateLocation.exists)
defaultTemplateLocation.toURI.toURL
else defaultTemplateSource

val template = resolveTemplate(defaultTemplateLocation)
val scriptBits = JavaAppBashScript.generateScript(defines, template)
val script = tmpDir / "tmp" / "bin" / name
IO.write(script, scriptBits)
Expand All @@ -299,19 +294,29 @@ trait JavaAppStartScript {
Some(script)
}

def makeUniversalBatScript(batTemplate: String)(replacements: Seq[(String, String)], tmpDir: File, name: String, sourceDir: File): Option[File] =
def makeUniversalBinScript(bashTemplate: String)(defines: Seq[String], tmpDir: File, name: String, sourceDir: File): Option[File] = {
makeUniversalBinScript(sourceDir / "templates" / bashTemplate, defines, tmpDir, name, sourceDir)
}

def makeUniversalBatScript(batTemplate: String)(replacements: Seq[(String, String)], tmpDir: File, name: String, sourceDir: File): Option[File] = {
makeUniversalBatScript(sourceDir / "templates" / batTemplate, replacements, tmpDir, name, sourceDir)
}

def makeUniversalBatScript(defaultTemplateLocation: File, replacements: Seq[(String, String)], tmpDir: File, name: String, sourceDir: File): Option[File] =
if (replacements.isEmpty) None
else {
val defaultTemplateLocation = sourceDir / "templates" / batTemplate
val defaultTemplateSource = getClass.getResource(batTemplate)
val template = if (defaultTemplateLocation.exists)
defaultTemplateLocation.toURI.toURL
else defaultTemplateSource

val template = resolveTemplate(defaultTemplateLocation)
val scriptBits = JavaAppBatScript.generateScript(replacements, template)
val script = tmpDir / "tmp" / "bin" / (name + ".bat")
IO.write(script, scriptBits)
Some(script)
}

private def resolveTemplate(defaultTemplateLocation: File): URL = {
if (defaultTemplateLocation.exists)
defaultTemplateLocation.toURI.toURL
else
getClass.getResource(defaultTemplateLocation.getName)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import sbt._
trait JavaAppKeys {

val makeBashScript = TaskKey[Option[File]]("makeBashScript", "Creates or discovers the bash script used by this project.")
val bashScriptTemplateLocation = TaskKey[File]("bashScriptTemplateLocation", "The location of the bash script template.")
val bashScriptDefines = TaskKey[Seq[String]]("bashScriptDefines", "A list of definitions that should be written to the bash file template.")
val bashScriptExtraDefines = TaskKey[Seq[String]]("bashScriptExtraDefines", "A list of extra definitions that should be written to the bash file template.")
val bashScriptConfigLocation = TaskKey[Option[String]]("bashScriptConfigLocation", "The location where the bash script will load default argument configuration from.")
Expand All @@ -18,6 +19,7 @@ trait JavaAppKeys {
val projectDependencyArtifacts = TaskKey[Seq[Attributed[File]]]("projectDependencyArtifacts", "The set of exported artifacts from our dependent projects.")
val scriptClasspath = TaskKey[Seq[String]]("scriptClasspath", "A list of relative filenames (to the lib/ folder in the distribution) of what to include on the classpath.")
val makeBatScript = TaskKey[Option[File]]("makeBatScript", "Creates or discovers the bat script used by this project.")
val batScriptTemplateLocation = TaskKey[File]("batScriptTemplateLocation", "The location of the bat script template.")
val batScriptReplacements = TaskKey[Seq[(String, String)]](
"batScriptReplacements",
"""|Replacements of template parameters used in the windows bat script.
Expand Down
26 changes: 26 additions & 0 deletions src/sbt-test/bash/override-templates/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import scala.io.Source

enablePlugins(JavaAppPackaging)

name := "override-templates"

version := "0.1.0"

bashScriptTemplateLocation := baseDirectory.value / "custom-templates" / "custom-bash-template"

batScriptTemplateLocation := baseDirectory.value / "custom-templates" / "custom-bat-template"

TaskKey[Unit]("run-check-bash") := {
val cwd = (stagingDirectory in Universal).value
val source = scala.io.Source.fromFile((cwd / "bin" / packageName.value).getAbsolutePath)
val contents = try source.getLines mkString "\n" finally source.close()
assert(contents contains "this is the custom bash template", "Bash template didn't contain the right text: \n" + contents)
}

TaskKey[Unit]("run-check-bat") := {
val cwd = (stagingDirectory in Universal).value
val batFilename = packageName.value + ".bat"
val source = scala.io.Source.fromFile((cwd / "bin" / batFilename).getAbsolutePath)
val contents = try source.getLines mkString "\n" finally source.close()
assert(contents contains "this is the custom bat template", "Bat template didn't contain the right text: \n" + contents)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

# this is the custom bash template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@REM this is the custom bat template
1 change: 1 addition & 0 deletions src/sbt-test/bash/override-templates/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object MainApp extends App {
println("SUCCESS!")
}
4 changes: 4 additions & 0 deletions src/sbt-test/bash/override-templates/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Run the staging and check the script.
> stage
> run-check-bash
> run-check-bat
16 changes: 8 additions & 8 deletions src/sphinx/archetypes/java_app/customize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ to ``bashScriptExtraDefines`` as other stages in the pipeline may be include lin
Overriding Templates (Bash/Bat)
-------------------------------

In order to override full templates, like the default bash script, create a file in ``src/templates/bash-template``
In order to override full templates, like the default bash script, you can create a file in ``src/templates/bash-template``.
Alternatively, you can use a different file location by setting ``bashScriptTemplateLocation``.

.. code-block:: bash
Expand Down Expand Up @@ -297,7 +298,8 @@ In order to override full templates, like the default bash script, create a file
exec java -cp $app_classpath $app_mainclass $@
Similarly the windows BAT template can be overridden by placing a new template in ``src/templates/bat-template``
Similarly the windows BAT template can be overridden by placing a new template in ``src/templates/bat-template``.
You can also use a different file location by setting ``batScriptTemplateLocation``.

.. code-block:: bat
Expand Down Expand Up @@ -325,11 +327,10 @@ While we just replaced the default templates with simpler templates, this should
In general, the templates are intended to provide enough utility that customization is only necessary for truly custom scripts.


``src/templates/bat-template``
Overriding bat templates (``src/templates/bat-template`` or a custom path using ``batScriptTemplateLocation``)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Creating a file here will override the default template used to
generate the ``.bat`` script for windows distributions.
This will override the default template used to generate the ``.bat`` script for windows distributions.

**Syntax**

Expand All @@ -342,11 +343,10 @@ generate the ``.bat`` script for windows distributions.

You can define additional variable definitions using ``batScriptExtraDefines``.

``src/templates/bash-template``
Overriding bash templates (``src/templates/bash-template`` or a custom path using ``bashScriptTemplateLocation``)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Creating a file here will override the default template used to
generate the BASH start script found in ``bin/<application>`` in the
This will override the default template used to generate the BASH start script found in ``bin/<application>`` in the
universal distribution

**Syntax**
Expand Down

0 comments on commit f62896f

Please sign in to comment.