Skip to content

Commit

Permalink
Use sbt-native-packager to build the distribution, close pantsbuild#1
Browse files Browse the repository at this point in the history
  • Loading branch information
pdalpra committed Sep 7, 2014
1 parent 9377b11 commit 7af98ba
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 83 deletions.
75 changes: 27 additions & 48 deletions project/Dist.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,50 @@

import sbt._
import sbt.Keys._
import sbt.Project.Initialize
import sbt.classpath.ClasspathUtilities
import com.typesafe.sbt.SbtNativePackager._
import com.typesafe.sbt.packager.universal.Keys.{packageZipTarball, stage, stagingDirectory}
import com.typesafe.sbt.S3Plugin.{ S3, s3Settings }

object Dist {
val distBase = SettingKey[File]("dist-base")
val distLibs = TaskKey[Seq[(File, String)]]("dist-libs")
val distTarget = SettingKey[File]("dist-target")
val create = TaskKey[File]("create", "Create a distribution.")
val packageTgz = TaskKey[File]("package-tgz", "Create a gzipped tar of the dist.")
val distLibs = taskKey[Seq[Attributed[File]]]("List of dist jars")
val create = taskKey[File]("Create the distribution")

lazy val settings: Seq[Setting[_]] = distSettings ++ s3PublishSettings

lazy val distSettings: Seq[Setting[_]] = Seq(
distBase <<= sourceDirectory / "dist",
distLibs <<= projectLibs(zincProject),
distTarget <<= (crossTarget, version) { (dir, v) => dir / ("zinc-" + v) },
create <<= (distBase, distLibs, distTarget, streams) map createDist,
packageTgz <<= (create, crossTarget, streams) map createTgz,
Keys.`package` <<= packageTgz,
artifact in packageTgz := Artifact("zinc", "tgz", "tgz"),
lazy val distSettings: Seq[Setting[_]] = packagerSettings ++ Seq(
distLibs := (fullClasspath in Compile).value.filter(cpElem => ClasspathUtilities.isArchive(cpElem.data)),
create := {
(stage in Universal).value
(stagingDirectory in Universal).value
},
mappings in Universal ++= distLibs.value.map(named),
mappings in Universal += {
val zincJar = (packageBin in Compile).value
val zincArtifact = (artifact in Compile).value
zincJar -> filename(zincArtifact)
},
artifact in packageZipTarball in Universal := Artifact("zinc", "tgz", "tgz"),
publishMavenStyle := true,
publishArtifact in makePom := false,
publishArtifact := false,
publishTo := Some("zinc repo" at "http://typesafe.artifactoryonline.com/typesafe/zinc"),
credentials += Credentials(Path.userHome / ".ivy2" / "artifactory-credentials")
) ++ addArtifact(artifact in packageTgz, packageTgz)
) ++ addArtifact(artifact in packageZipTarball in Universal, packageZipTarball in Universal)

lazy val s3PublishSettings: Seq[Setting[_]] = s3Settings ++ Seq(
mappings in S3.upload <<= (packageTgz, version) map { (pkg, version) =>
mappings in S3.upload := {
val name = "zinc"
val file = "%s-%s.tgz" format (name, version)
val path = Seq(name, version, file) mkString "/"
Seq(pkg -> path)
val file = "%s-%s.tgz" format (name, version.value)
val path = Seq(name, version.value, file) mkString "/"
Seq((packageZipTarball in Universal).value -> path)
},
S3.host in S3.upload := "downloads.typesafe.com.s3.amazonaws.com",
S3.progress in S3.upload := true
)

def zincProject: ProjectReference = LocalProject(ZincBuild.zinc.id)

def projectLibs(project: ProjectReference): Initialize[Task[Seq[(File, String)]]] = {
(packageBin in (project, Compile), artifact in project, managedClasspath in (project, Compile)) map {
(jar, art, cp) => {
def filename(a: Artifact) = a.name + a.classifier.map("-"+_).getOrElse("") + "." + a.extension
def named(a: Attributed[File]) = (a.data, a.get(artifact.key).map(filename).getOrElse(a.data.name))
(jar, filename(art)) +: (cp map named)
}
}
}

def createDist(base: File, libs: Seq[(File, String)], target: File, s: TaskStreams): File = {
val lib = target / "lib"
IO.delete(target)
Util.copyDirectory(base, target, setExecutable = true)
IO.createDirectory(lib)
Util.copyMapped(libs map { case (file, name) => (file, lib / name) })
s.log.info("Created distribution: " + target)
target
}

def createTgz(dist: File, dir: File, s: TaskStreams): File = {
val tgz = dir / (dist.name + ".tgz")
val exitCode = Process(List("tar", "-czf", tgz.name, dist.name), dir) ! s.log
if (exitCode != 0) sys.error("Failed to create tgz.")
s.log.info("Created tgz: " + tgz)
tgz
}
def filename(a: Artifact) =
"lib/" + a.name + a.classifier.map("-"+_).getOrElse("") + "." + a.extension
def named(a: Attributed[File]) =
a.data -> a.get(artifact.key).map(filename).getOrElse(a.data.name)
}
4 changes: 2 additions & 2 deletions project/Scriptit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object Scriptit {
scriptitBase := sourceDirectory.value / "scriptit",
scriptitTestName := "test",
scriptitCommands := Commands.defaultCommands,
scriptitCommands += Commands.zincCommand((Dist.create in ZincBuild.dist).value),
scriptitCommands += Commands.zincCommand(Dist.create.value),
scriptitScalaVersions := Seq("2.9.3", "2.10.3", "2.11.0-RC3"),
scriptitProperties := scalaProperties(appConfiguration.value, scriptitScalaVersions.value),
scriptitProperties ++= javaProperties,
Expand Down Expand Up @@ -56,7 +56,7 @@ object Scriptit {
Defaults.distinctParser(scriptitTests(scriptitBase, script).toSet, raw = false)

def scriptitTests(scriptitBase: File, script: String): Seq[String] =
(scriptitBase ** script).get map (_.getParentFile) x relativeTo(scriptitBase) map (_._2)
(scriptitBase ** script).get map (_.getParentFile) pair relativeTo(scriptitBase) map (_._2)

def runTest(name: String, path: String, script: Seq[Line], commands: Seq[Command], properties: Map[String, String], log: Logger)(dir: File): Result = {
val runner = new ScriptRunner(name, path, script)
Expand Down
26 changes: 0 additions & 26 deletions project/Util.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,6 @@ import sbt._
import sbt.inc.Analysis

object Util {
def copyDirectory(source: File, target: File, overwrite: Boolean = false, preserveLastModified: Boolean = false, setExecutable: Boolean = false): Set[File] = {
val sources = (source ***) x rebase(source, target)
copyMapped(sources, overwrite, preserveLastModified, setExecutable)
}

def copyFlat(files: Traversable[File], target: File, overwrite: Boolean = false, preserveLastModified: Boolean = false, setExecutable: Boolean = false): Set[File] = {
IO.createDirectory(target)
val sources = files map { f => (f, target / f.name) }
copyMapped(sources, overwrite, preserveLastModified, setExecutable)
}

def copyMapped(sources: Traversable[(File, File)], overwrite: Boolean = false, preserveLastModified: Boolean = false, setExecutable: Boolean = false): Set[File] = {
sources map { Function.tupled(copy(overwrite, preserveLastModified, setExecutable)) } toSet
}

def copy(overwrite: Boolean, preserveLastModified: Boolean, setExecutable: Boolean)(source: File, target: File): File = {
if (overwrite || !target.exists || source.lastModified > target.lastModified) {
if (source.isDirectory) IO.createDirectory(target)
else {
IO.createDirectory(target.getParentFile)
IO.copyFile(source, target, preserveLastModified)
if (setExecutable) target.setExecutable(source.canExecute, false)
}
}
target
}

def environment(property: String, env: String): Option[String] =
Option(System.getProperty(property)) orElse Option(System.getenv(env))
Expand Down
8 changes: 1 addition & 7 deletions project/ZincBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object ZincBuild extends Build {
lazy val zinc = Project(
"zinc",
file("."),
settings = buildSettings ++ Version.settings ++ Publish.settings ++ Scriptit.settings ++ Seq(
settings = buildSettings ++ Version.settings ++ Publish.settings ++ Dist.settings ++ Scriptit.settings ++ Seq(
resolveSbtLocally := false,
resolvers += (if (resolveSbtLocally.value) Resolver.mavenLocal else Opts.resolver.sonatypeSnapshots),
libraryDependencies ++= Seq(
Expand All @@ -31,10 +31,4 @@ object ZincBuild extends Build {
scalacOptions ++= Seq("-feature", "-deprecation", "-Xlint")
)
)

lazy val dist = Project(
id = "dist",
base = file("dist"),
settings = buildSettings ++ Dist.settings
)
}
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-pgp" % "0.8.3")

addSbtPlugin("com.typesafe.sbt" % "sbt-s3" % "0.5")

addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "0.8.0-M1")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 7af98ba

Please sign in to comment.