3
import java.util.Properties
4
import scala.util.control.Exception.catching
5
import java.lang.{NumberFormatException => NFE}
6
import java.io.FileInputStream
7
import com.jsuereth.git.GitRunner
8
import com.jsuereth.git.GitKeys.gitRunner
10
case class VersionInfo(canonical: String,
14
/** this file is responsible for setting up Scala versioning schemes and updating all the necessary bits. */
16
val buildNumberFile = SettingKey[File]("scala-build-number-file")
17
// TODO - Make this a setting?
18
val buildNumberProps = SettingKey[BaseBuildNumber]("scala-build-number-props")
19
val buildRelease = SettingKey[Boolean]("scala-build-release", "This is set to true if we're building a release.")
20
val mavenSuffix = SettingKey[String]("scala-maven-suffix", "This is set to whatever maven suffix is required.")
22
val gitSha = TaskKey[String]("scala-git-sha", "The sha of the current git commit.")
23
val gitDate = TaskKey[String]("scala-git-date", "The date of the current git commit.")
25
val mavenVersion = SettingKey[String]("scala-maven-version", "The maven version number.")
26
val osgiVersion = TaskKey[String]("scala-osgi-version", "The OSGi version number.")
27
val canonicalVersion = TaskKey[String]("scala-canonical-version", "The canonical version number.")
29
val scalaVersions = TaskKey[VersionInfo]("scala-version-info", "The scala versions used for this build.")
33
def settings: Seq[Setting[_]] = Seq(
34
buildNumberFile <<= baseDirectory apply (_ / "build.number"),
35
buildNumberProps <<= buildNumberFile apply loadBuildNumberProps,
36
buildRelease := Option(System.getProperty("build.release")) map (!_.isEmpty) getOrElse false,
37
mavenSuffix <<= buildRelease apply pickMavenSuffix,
38
mavenVersion <<= (buildNumberProps, mavenSuffix) apply makeMavenVersion,
39
gitSha <<= (gitRunner, baseDirectory, streams) map getGitSha,
40
gitDate <<= (gitRunner, baseDirectory, streams) map getGitDate,
41
osgiVersion <<= (buildNumberProps, gitDate, gitSha) map makeOsgiVersion,
42
canonicalVersion <<= (buildRelease, mavenVersion, buildNumberProps, gitDate, gitSha) map makeCanonicalVersion,
43
scalaVersions <<= (canonicalVersion, mavenVersion, osgiVersion) map VersionInfo.apply
47
/** This generates a properties file, if it does not already exist, with the maximum lastmodified timestamp
48
* of any source file. */
49
def generateVersionPropertiesFile(name: String)(dir: File, versions: VersionInfo, skip: Boolean, s: TaskStreams): Seq[File] = {
50
// TODO - We can probably clean this up by moving caching bits elsewhere perhaps....
51
val target = dir / name
52
// TODO - Regenerate on triggers, like recompilation or something...
53
def hasSameVersion: Boolean = {
54
val props = new java.util.Properties
55
val in = new java.io.FileInputStream(target)
56
try props.load(in) finally in.close()
57
versions.canonical == (props getProperty "version.number")
59
if (!target.exists || !(skip || hasSameVersion)) {
60
makeVersionPropertiesFile(target, versions)
65
// This creates the *.properties file used to determine the current version of scala at runtime. TODO - move these somewhere utility like.
66
def makeVersionPropertiesFile(f: File, versions: VersionInfo): Unit =
67
IO.write(f, "version.number = "+versions.canonical+"\n"+
68
"osgi.number = "+versions.osgi+"\n"+
69
"maven.number = "+versions.maven+"\n"+
70
"copyright.string = Copyright 2002-2013, LAMP/EPFL")
72
def makeCanonicalVersion(isRelease: Boolean, mvnVersion: String, base: BaseBuildNumber, gitDate: String, gitSha: String): String =
73
if(isRelease) mvnVersion
75
val suffix = if(base.bnum > 0) "-%d".format(base.bnum) else ""
76
"%s.%s.%s%s-%s-%s" format (base.major, base.minor, base.patch, suffix, gitDate, gitSha)
79
def makeMavenVersion(base: BaseBuildNumber, suffix: String): String = {
80
val firstSuffix = if(base.bnum > 0) "-%d".format(base.bnum) else ""
81
"%d.%d.%d%s%s" format (base.major, base.minor, base.patch, firstSuffix, suffix)
84
def makeOsgiVersion(base: BaseBuildNumber, gitDate: String, gitSha: String): String = {
85
val suffix = if(base.bnum > 0) "-%d".format(base.bnum) else ""
86
"%s.%s.%s.v%s%s-%s" format (base.major, base.minor, base.patch, gitDate, suffix, gitSha)
89
/** Determines what the maven sufffix should be for this build. */
90
def pickMavenSuffix(isRelease: Boolean): String = {
91
def default = if(isRelease) "" else "-SNAPSHOT"
92
Option(System.getProperty("maven.version.suffix")) getOrElse default
95
/** Loads the build.number properties file into SBT. */
96
def loadBuildNumberProps(file: File): BaseBuildNumber = {
97
val fin = new FileInputStream(file)
99
val props = new Properties()
101
def getProp(name: String): Int =
103
v <- Option(props.getProperty(name))
104
v2 <- catching(classOf[NFE]) opt v.toInt
105
} yield v2) getOrElse sys.error("Could not convert %s to integer!" format (name))
108
major=getProp("version.major"),
109
minor=getProp("version.minor"),
110
patch=getProp("version.patch"),
111
bnum =getProp("version.bnum")
113
} finally fin.close()
117
def getGitDate(git: GitRunner, baseDirectory: File, s: TaskStreams): String = {
118
val lines = getGitLines("log","-1","--format=\"%ci\"")(git,baseDirectory, s)
119
val line = if(lines.isEmpty) sys.error("Could not retreive git commit sha!") else lines.head
120
// Lines *always* start with " for some reason...
121
line drop 1 split "\\s+" match {
122
case Array(date, time, _*) => "%s-%s" format (date.replaceAll("\\-", ""), time.replaceAll(":",""))
123
case _ => sys.error("Could not parse git date: " + line)
127
def getGitSha(git: GitRunner, baseDirectory: File, s: TaskStreams): String = {
128
val lines = getGitLines("log","-1","--format=\"%H\"", "HEAD")(git,baseDirectory, s)
129
val line = if(lines.isEmpty) sys.error("Could not retreive git commit sha!") else lines.head
130
val noquote = if(line startsWith "\"") line drop 1 else line
131
val nog = if(noquote startsWith "g") noquote drop 1 else noquote
135
def getGitLines(args: String*)(git: GitRunner, baseDirectory: File, s: TaskStreams): Seq[String] =
136
git(args: _*)(baseDirectory, s.log) split "[\r\n]+"
140
case class BaseBuildNumber(major: Int, minor: Int, patch: Int, bnum: Int) {
141
override def toString = "BaseBuildNumber(%d.%d.%d-%d)" format (major, minor, patch, bnum)