Created
December 19, 2012 13:03
-
-
Save freekh/4336536 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sbt._ | |
import Keys._ | |
object VersionInfoPlugin extends Plugin { | |
val versionInfoPackage = SettingKey[String]("version-info-package") | |
val versionInfoClassTask = TaskKey[Seq[java.io.File]]("version-info-class", "writes a java class VersionInfo with information about the versionInfo to sourceManaged") | |
val versionInfo = TaskKey[String]("version-info", "the current versionInfo") | |
private val Classname = "VersionInfo" | |
val versionInfoSettings = Seq( | |
versionInfo <<= version map { version => | |
Option(System.getProperty("version")).getOrElse{ //use property version if found | |
version | |
} | |
}, | |
versionInfoClassTask <<= (sourceManaged in Compile, versionInfoPackage, versionInfo) map { (srcDir, versionInfoPackage, versionInfo) => | |
// depends on: ^ source directory, ^ the package ^ the version | |
val classString = """ | |
|package %s; | |
| | |
|public class %s { | |
| public static String CURRENT_VERSION = "%s"; | |
|}""" format (versionInfoPackage, Classname, versionInfo) stripMargin | |
val path = srcDir.getAbsolutePath+versionInfoPackage.replace(".", java.io.File.separator)+java.io.File.separator+Classname+".java" | |
IO.write(file(path), classString) | |
Seq(file(path)) | |
}, | |
sourceGenerators in Compile <+= versionInfoClassTask | |
//^ add our versionInfoClass to generators so it is picked up by sbt | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment