diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2015-11-13 14:38:55 +1000 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2015-11-13 14:38:55 +1000 |
commit | 7a75c0ff03f7d8fa36f886b4de61696c20828672 (patch) | |
tree | 1bd62970b1dd748023e611843f43536f26074a6d /project/VersionUtil.scala | |
parent | 0c597f00390ff1ad5de113f9d33c6a88238732d9 (diff) | |
parent | 7248894154180291f18def808ed992fb495b257e (diff) | |
download | scala-7a75c0ff03f7d8fa36f886b4de61696c20828672.tar.gz scala-7a75c0ff03f7d8fa36f886b4de61696c20828672.tar.bz2 scala-7a75c0ff03f7d8fa36f886b4de61696c20828672.zip |
Merge pull request #4824 from szeiger/wip/sbt-mkBin
Improvements to the sbt build
Diffstat (limited to 'project/VersionUtil.scala')
-rw-r--r-- | project/VersionUtil.scala | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/project/VersionUtil.scala b/project/VersionUtil.scala new file mode 100644 index 0000000000..71de772b08 --- /dev/null +++ b/project/VersionUtil.scala @@ -0,0 +1,103 @@ +import sbt._ +import Keys._ +import java.util.Properties +import java.io.FileInputStream +import scala.collection.JavaConverters._ + +object VersionUtil { + lazy val copyrightString = settingKey[String]("Copyright string.") + lazy val versionProperties = settingKey[Versions]("Version properties.") + lazy val generateVersionPropertiesFile = taskKey[File]("Generating version properties file.") + + lazy val versionPropertiesSettings = Seq[Setting[_]]( + versionProperties := versionPropertiesImpl.value + ) + + lazy val generatePropertiesFileSettings = Seq[Setting[_]]( + copyrightString := "Copyright 2002-2015, LAMP/EPFL", + resourceGenerators in Compile += generateVersionPropertiesFile.map(file => Seq(file)).taskValue, + versionProperties := versionPropertiesImpl.value, + generateVersionPropertiesFile := generateVersionPropertiesFileImpl.value + ) + + case class Versions(canonicalVersion: String, mavenVersion: String, osgiVersion: String, commitSha: String, commitDate: String, isRelease: Boolean) { + val githubTree = + if(isRelease) "v" + mavenVersion + else if(commitSha != "unknown") commitSha + else "master" + + override def toString = s"Canonical: $canonicalVersion, Maven: $mavenVersion, OSGi: $osgiVersion, github: $githubTree" + + def toProperties: Properties = { + val props = new Properties + props.put("version.number", canonicalVersion) + props.put("maven.version.number", mavenVersion) + props.put("osgi.version.number", osgiVersion) + props + } + } + + lazy val versionPropertiesImpl: Def.Initialize[Versions] = Def.setting { + /** Regexp that splits version number split into two parts: version and suffix. + * Examples of how the split is performed: + * + * "2.11.5": ("2.11.5", null) + * "2.11.5-acda7a": ("2.11.5", "-acda7a") + * "2.11.5-SNAPSHOT": ("2.11.5", "-SNAPSHOT") */ + val versionSplitted = """([\w+\.]+)(-[\w+\.]+)??""".r + + val versionSplitted(ver, suffixOrNull) = version.value + + val osgiSuffix = suffixOrNull match { + case null => "-VFINAL" + case "-SNAPSHOT" => "" + case suffixStr => suffixStr + } + + def executeTool(tool: String) = { + val cmd = + if (System.getProperty("os.name").toLowerCase.contains("windows")) + s"cmd.exe /c tools\\$tool.bat -p" + else s"tools/$tool" + Process(cmd).lines.head + } + + val commitDate = executeTool("get-scala-commit-date") + val commitSha = executeTool("get-scala-commit-sha") + + Versions( + canonicalVersion = s"$ver-$commitDate-$commitSha", + mavenVersion = s"${version.value}", + osgiVersion = s"$ver.v$commitDate$osgiSuffix-$commitSha", + commitSha = commitSha, + commitDate = commitDate, + isRelease = !osgiSuffix.isEmpty + ) + } + + lazy val generateVersionPropertiesFileImpl: Def.Initialize[Task[File]] = Def.task { + val props = versionProperties.value.toProperties + val propFile = (resourceManaged in Compile).value / s"${thisProject.value.id}.properties" + props.put("copyright.string", copyrightString.value) + + // unfortunately, this will write properties in arbitrary order + // this makes it harder to test for stability of generated artifacts + // consider using https://github.com/etiennestuder/java-ordered-properties + // instead of java.util.Properties + IO.write(props, null, propFile) + propFile + } + + /** The global versions.properties data */ + lazy val versionProps: Map[String, String] = { + val props = new Properties() + val in = new FileInputStream(file("versions.properties")) + try props.load(in) + finally in.close() + props.asScala.toMap + } + + /** Get a subproject version number from `versionProps` */ + def versionNumber(name: String): String = + versionProps(s"$name.version.number") +} |