Skip to content

Instantly share code, notes, and snippets.

@jeffreyolchovy
Created December 15, 2014 06:00
Show Gist options
  • Save jeffreyolchovy/7255734a45872ecc7434 to your computer and use it in GitHub Desktop.
Save jeffreyolchovy/7255734a45872ecc7434 to your computer and use it in GitHub Desktop.
sbt-build-info
sbtPlugin := true
name := "sbt-build-info"
organization := "com.tapad"
version := "0.1.3"
credentials += Credentials(Path.userHome / ".ivy2" / ".credentials")
val tapadNexusSnapshots = "Tapad Nexus Snapshots" at "http://nexus.tapad.com:8080/nexus/content/repositories/snapshots"
val tapadNexusReleases = "Tapad Nexus Releases" at "http://nexus.tapad.com:8080/nexus/content/repositories/releases"
publishTo <<= (version) { version: String =>
if (version.endsWith("SNAPSHOT") || version.endsWith("TAPAD"))
Some(tapadNexusSnapshots)
else
Some(tapadNexusReleases)
}
publishMavenStyle := true
scalaVersion := "2.10.4"
package sbtbuildinfo
import java.io.File
import sbt._
import Keys._
object BuildInfoPlugin extends Plugin {
lazy val buildInfo = TaskKey[Seq[File]]("build-info", "Generate a resource which contains build information.")
object BuildInfoKeys {
lazy val buildInfoFileName = TaskKey[String]("build-info-filename")
lazy val buildInfoDefaultFileName = TaskKey[String]("build-info-default-filename")
lazy val buildInfoProps = TaskKey[Map[String, String]]("build-info-props")
}
import BuildInfoKeys._
lazy val buildInfoSettings: Seq[sbt.Def.Setting[_]] = Seq(
buildInfo := buildInfoGeneratorTask(buildInfo).value,
buildInfoDefaultFileName in buildInfo := "build-info.properties",
buildInfoFileName in buildInfo <<= (buildInfoFileName in buildInfo) or (buildInfoDefaultFileName in buildInfo),
buildInfoProps in buildInfo := generateGitProps,
resourceGenerators in Compile += buildInfoGeneratorTask(buildInfo).taskValue
)
private def buildInfoGeneratorTask(key: TaskKey[Seq[File]]): Def.Initialize[Task[Seq[File]]] = Def.task {
val log = (streams in key).value.log
log.info(s"Generating build-info (triggered by resource generator)")
val name = (buildInfoFileName in key).value
val file = (resourceManaged in Compile).value / name
val props = (buildInfoProps in key).value
val contents = props.map { case (k, v) => s"$k=$v" }.mkString("\n")
log.debug(s"$contents")
IO.write(file, contents)
Seq(file)
}
private def generateGitProps(): Map[String, String] = Map(
"git.commit.sha" -> Process("git rev-parse HEAD").lines.head
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment