Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply scalariform test first time #183

Merged
merged 1 commit into from
Mar 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 27 additions & 30 deletions src/main/scala/com/typesafe/sbt/PackagerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,46 @@ import Keys.packageXzTarball
import sbt._
import sbt.Keys.packageBin

object SbtNativePackager extends Plugin
with linux.LinuxPlugin
with debian.DebianPlugin
with rpm.RpmPlugin
with windows.WindowsPlugin
with universal.UniversalPlugin
with GenericPackageSettings {
object SbtNativePackager extends Plugin
with linux.LinuxPlugin
with debian.DebianPlugin
with rpm.RpmPlugin
with windows.WindowsPlugin
with universal.UniversalPlugin
with GenericPackageSettings {

val NativePackagerKeys = packager.Keys

val NativePackagerHelper = packager.MappingsHelper

def packagerSettings = linuxSettings ++
debianSettings ++
rpmSettings ++
windowsSettings ++
universalSettings ++
Seq( // Bad defaults that let us at least not explode users who don't care about native packagers
NativePackagerKeys.maintainer := "",
NativePackagerKeys.packageDescription := "",
NativePackagerKeys.packageSummary := ""
)



def packagerSettings = linuxSettings ++
debianSettings ++
rpmSettings ++
windowsSettings ++
universalSettings ++
Seq( // Bad defaults that let us at least not explode users who don't care about native packagers
NativePackagerKeys.maintainer := "",
NativePackagerKeys.packageDescription := "",
NativePackagerKeys.packageSummary := ""
)

import SettingsHelper._
def deploymentSettings = makeDeploymentSettings(Debian, packageBin in Debian, "deb") ++
makeDeploymentSettings(Rpm, packageBin in Rpm, "rpm") ++
makeDeploymentSettings(Windows, packageBin in Windows, "msi") ++
makeDeploymentSettings(Universal, packageBin in Universal, "zip") ++
addPackage(Universal, packageZipTarball in Universal, "tgz") ++
makeDeploymentSettings(UniversalDocs, packageBin in UniversalDocs, "zip") ++
addPackage(UniversalDocs, packageXzTarball in UniversalDocs, "txz")
makeDeploymentSettings(Rpm, packageBin in Rpm, "rpm") ++
makeDeploymentSettings(Windows, packageBin in Windows, "msi") ++
makeDeploymentSettings(Universal, packageBin in Universal, "zip") ++
addPackage(Universal, packageZipTarball in Universal, "tgz") ++
makeDeploymentSettings(UniversalDocs, packageBin in UniversalDocs, "zip") ++
addPackage(UniversalDocs, packageXzTarball in UniversalDocs, "txz")

object packageArchetype {
private[this] def genericMappingSettings: Seq[Setting[_]] = packagerSettings ++ mapGenericFilesToLinux ++ mapGenericFilesToWindows
def java_application: Seq[Setting[_]] =
def java_application: Seq[Setting[_]] =
genericMappingSettings ++ archetypes.JavaAppPackaging.settings
def java_server: Seq[Setting[_]] =
genericMappingSettings ++ archetypes.JavaServerAppPackaging.settings
}


// TODO - Add a few targets that detect the current OS and build a package for that OS.

}
18 changes: 9 additions & 9 deletions src/main/scala/com/typesafe/sbt/packager/Hashing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ object Hashing {

def sha512(t: File): String =
messageDigestHex(java.security.MessageDigest.getInstance("SHA-512"))(t)

def md5Sum(t: File): String =
messageDigestHex(java.security.MessageDigest.getInstance("MD5"))(t)

def messageDigestHex(md: java.security.MessageDigest)(file: File): String = {
val in = new java.io.FileInputStream(file);
val buffer = new Array[Byte](8192)
try {
def read(): Unit = in.read(buffer) match {
case x if x <= 0 => ()
case size => md.update(buffer, 0, size); read()
}
read()
def read(): Unit = in.read(buffer) match {
case x if x <= 0 => ()
case size => md.update(buffer, 0, size); read()
}
read()
} finally in.close()
convertToHex(md.digest)
}

def convertToHex(data: Array[Byte]): String = {
//TODO - use java.lang.Integer.toHexString() ?
val buf = new StringBuffer
def byteToHex(b: Int) =
if ((0 <= b) && (b <= 9)) ('0' + b).toChar
else ('a' + (b-10)).toChar
else ('a' + (b - 10)).toChar
for (i <- 0 until data.length) {
buf append byteToHex((data(i) >>> 4) & 0x0F)
buf append byteToHex(data(i) & 0x0F)
Expand Down
6 changes: 4 additions & 2 deletions src/main/scala/com/typesafe/sbt/packager/MappingsHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ object MappingsHelper {
directory(file(sourceDir))
}

/** return a Seq of mappings which effect is to add the content of directory in the generated package,
* excluding the directory itself */
/**
* return a Seq of mappings which effect is to add the content of directory in the generated package,
* excluding the directory itself
*/
def contentOf(sourceDir: File): Seq[(File, String)] = {
(sourceDir.*** --- sourceDir) pair relativeTo(sourceDir)
}
Expand Down
28 changes: 14 additions & 14 deletions src/main/scala/com/typesafe/sbt/packager/SettingsHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import sbt._
import sbt.Keys._

object SettingsHelper {

def addPackage(config: Configuration, packageTask: TaskKey[File], extension: String, classifier: Option[String] = None): Seq[Setting[_]] =
inConfig(config)(addArtifact(
name apply (Artifact(_, extension, extension, classifier = classifier, configurations = Iterable.empty, url = None, extraAttributes = Map.empty)),
packageTask
name apply (Artifact(_, extension, extension, classifier = classifier, configurations = Iterable.empty, url = None, extraAttributes = Map.empty)),
packageTask
))
def makeDeploymentSettings(config: Configuration, packageTask: TaskKey[File], extension: String): Seq[Setting[_]] =

def makeDeploymentSettings(config: Configuration, packageTask: TaskKey[File], extension: String): Seq[Setting[_]] =
(inConfig(config)(Classpaths.publishSettings)) ++ inConfig(config)(Seq(
artifacts := Seq.empty,
packagedArtifacts := Map.empty,
projectID <<= (organization, name, version) apply { (o,n,v) => ModuleID(o,n,v) },
projectID <<= (organization, name, version) apply { (o, n, v) => ModuleID(o, n, v) },
moduleSettings <<= (projectID, projectInfo) map { (pid, pinfo) =>
InlineConfiguration(pid, pinfo, Seq.empty)
},
Expand All @@ -25,16 +25,16 @@ object SettingsHelper {
deliverConfiguration <<= deliverLocalConfiguration,
publishConfiguration <<= (packagedArtifacts, checksums, publishTo) map { (as, checks, publishTo) =>
new PublishConfiguration(ivyFile = None,
resolverName = Classpaths.getPublishTo(publishTo).name,
artifacts = as,
checksums = checks,
logging = UpdateLogging.DownloadOnly)
resolverName = Classpaths.getPublishTo(publishTo).name,
artifacts = as,
checksums = checks,
logging = UpdateLogging.DownloadOnly)
},
publishLocalConfiguration <<= (packagedArtifacts, checksums) map { (as, checks) =>
new PublishConfiguration(ivyFile = None,
resolverName = "local",
artifacts = as,
checksums = checks,
logging = UpdateLogging.DownloadOnly)
resolverName = "local",
artifacts = as,
checksums = checks,
logging = UpdateLogging.DownloadOnly)
})) ++ addPackage(config, packageTask, extension)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,48 @@ import java.net.URL

/**
* Constructs a bash script for running a java application.
*
*
* Makes use of the associated bash-template, with a few hooks
*
*
*/
object JavaAppBashScript {

private[this] def bashTemplateSource =
getClass.getResource("bash-template")

/** Creates the block of defines for a script.
*

/**
* Creates the block of defines for a script.
*
* @param mainClass The required "main" method class we use to run the program.
* @param appClasspath A sequence of relative-locations (to the lib/ folder) of jars
* to include on the classpath.
* @param configFile An (optional) filename from which the script will read arguments.
* @param extras Any additional defines/commands that should be run in this script.
* @param extras Any additional defines/commands that should be run in this script.
*/
def makeDefines(
mainClass: String,
appClasspath: Seq[String] = Seq("*"),
configFile: Option[String] = None,
extras: Seq[String] = Nil): Seq[String] =
Seq(mainClassDefine(mainClass)) ++
(configFile map configFileDefine).toSeq ++
Seq(makeClasspathDefine(appClasspath)) ++
extras
mainClass: String,
appClasspath: Seq[String] = Seq("*"),
configFile: Option[String] = None,
extras: Seq[String] = Nil): Seq[String] =
Seq(mainClassDefine(mainClass)) ++
(configFile map configFileDefine).toSeq ++
Seq(makeClasspathDefine(appClasspath)) ++
extras

private def makeClasspathDefine(cp: Seq[String]): String = {
val fullString = cp map (n => "$lib_dir/"+n) mkString ":"
"declare -r app_classpath=\""+fullString+"\"\n"
}
val fullString = cp map (n => "$lib_dir/" + n) mkString ":"
"declare -r app_classpath=\"" + fullString + "\"\n"
}
def generateScript(defines: Seq[String], template: URL = bashTemplateSource): String = {
val defineString = defines mkString "\n"
val replacements = Seq("template_declares" -> defineString)
TemplateWriter.generateScript(template, replacements)
}

def configFileDefine(configFile: String) =
"declare -r script_conf_file=\"%s\"" format (configFile)
def mainClassDefine(mainClass: String) =

def mainClassDefine(mainClass: String) =
"declare -r app_mainclass=\"%s\"\n" format (mainClass)

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.typesafe.sbt.packager.archetypes

object JavaAppBatScript {
private[this] def bashTemplateSource =
private[this] def bashTemplateSource =
getClass.getResource("bat-template")
private[this] def charset =
java.nio.charset.Charset.forName("UTF-8")

def makeEnvFriendlyName(name: String): String =
name.toUpperCase.replaceAll("\\W", "_")
name.toUpperCase.replaceAll("\\W", "_")

def makeWindowsRelativeClasspathDefine(cp: Seq[String]): String = {
def cleanPath(path: String): String = path.replaceAll("/", "\\\\")
def makeRelativePath(path: String): String =
Expand All @@ -25,13 +25,13 @@ object JavaAppBatScript {
}

def makeReplacements(
name: String,
mainClass: String,
appClasspath: Seq[String] = Seq("*"),
extras: Seq[String] = Nil): Seq[(String, String)] = {
name: String,
mainClass: String,
appClasspath: Seq[String] = Seq("*"),
extras: Seq[String] = Nil): Seq[(String, String)] = {
val replacements = Seq(
"APP_NAME" -> name,
"APP_ENV_NAME" -> makeEnvFriendlyName(name)
"APP_NAME" -> name,
"APP_ENV_NAME" -> makeEnvFriendlyName(name)
)
val defines = Seq(
makeWindowsRelativeClasspathDefine(appClasspath),
Expand All @@ -45,12 +45,12 @@ object JavaAppBatScript {
def replace(line: String, replacements: Seq[(String, String)]): String = {
replacements.foldLeft(line) {
case (line, (key, value)) =>
line.replaceAll("@@"+key+"@@", java.util.regex.Matcher.quoteReplacement(value))
line.replaceAll("@@" + key + "@@", java.util.regex.Matcher.quoteReplacement(value))
}
}

def generateScript(
replacements: Seq[(String,String)], template: java.net.URL = bashTemplateSource): String =
replacements: Seq[(String, String)], template: java.net.URL = bashTemplateSource): String =
TemplateWriter.generateScript(template, replacements, "\r\n", TemplateWriter.batFriendlyKeySurround)

}
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
package com.typesafe.sbt.packager.archetypes


object TemplateWriter {
def defaultCharset: java.nio.charset.Charset = java.nio.charset.Charset.forName("UTF-8")



def bashFriendlyKeySurround(key: String) =
"\\$\\{\\{" + key + "\\}\\}"
def batFriendlyKeySurround(key: String) =
"@@" + key + "@@"

private def replace(
line: String,
replacements: Seq[(String, String)],
keySurround: String => String): String = {
line: String,
replacements: Seq[(String, String)],
keySurround: String => String): String = {
replacements.foldLeft(line) {
case (line, (key, value)) =>
keySurround(key).r.replaceAllIn(line, java.util.regex.Matcher.quoteReplacement(value))
}
}
private def replaceValues(lines: Seq[String],
replacements: Seq[(String, String)],
eol: String,
keySurround: String => String): String = {

private def replaceValues(lines: Seq[String],
replacements: Seq[(String, String)],
eol: String,
keySurround: String => String): String = {
val sb = new StringBuilder
for(line <- lines) {
for (line <- lines) {
sb append replace(line, replacements, keySurround)
sb append eol
}
sb toString
}



def generateScript(
source: java.net.URL,
replacements: Seq[(String, String)],
eol: String = "\n",
keySurround: String => String = bashFriendlyKeySurround,
charset: java.nio.charset.Charset = defaultCharset): String = {
source: java.net.URL,
replacements: Seq[(String, String)],
eol: String = "\n",
keySurround: String => String = bashFriendlyKeySurround,
charset: java.nio.charset.Charset = defaultCharset): String = {
val lines = sbt.IO.readLinesURL(source, charset)
replaceValues(lines, replacements, eol, keySurround)
}
Expand Down
Loading