From b1b54751cd4d96c102110bdbb512c729d1f618a5 Mon Sep 17 00:00:00 2001 From: Stefan Zeiger Date: Wed, 16 Dec 2015 10:28:03 -0800 Subject: Use sbt for PR validation builds. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Improve version handling in the sbt build: The new settings `baseVersion` and `baseVersionSuffix` make it easier to set version numbers for different kinds of builds in a consistent way without e.g. having to first get a git SHA outside of sbt. The new task `generateBuildCharacterPropertiesFile` writes the file `buildcharacter.properties` to the root dir. The format is compatible with the same file produced by the ANT build but it only contains a subset of the properties, in particular the Maven version, which is needed in publishing scripts and passed around between different Jenkins jobs as `jenkins.properties`. SHAs in version numbers are consistently shortened to 7 digits (as used by git and github). Previously we used 7 digits in Maven snapshot version numbers but 10 digits in canonical and OSGi version numbers. # Add Jenkins script support to the sbt build: The new command `setupPublishCore` takes the PR validation snapshot repository as an argument and changes the required settings for the `publish-core` build (use SHA-SNAPSHOT versioning, compile with optimization enabled, do not publish scaladoc sets) For example, the following command can be used to generate `buildcharacter.properties` with the version numbers needed for PR validation builds: sbt setupPublishCore dummy generateBuildCharacterPropertiesFile The sbt build will now automatically detect and use a “~/.credentials” file with the credentials for publishing to a remote repository. # Call sbt from `publish-core`: The correct`$SBT_CMD` is set directly in `bootstrap` and used by `publish-core` to first generate `buildcharacter.properties` and then build and publish. Parsing the git revision, computing a version number and getting binary dependencies are no longer required in the script. This is all done in the sbt build. --- project/ScriptCommands.scala | 19 +++++++++ project/VersionUtil.scala | 99 +++++++++++++++++++++++++++----------------- 2 files changed, 79 insertions(+), 39 deletions(-) create mode 100644 project/ScriptCommands.scala (limited to 'project') diff --git a/project/ScriptCommands.scala b/project/ScriptCommands.scala new file mode 100644 index 0000000000..537990d985 --- /dev/null +++ b/project/ScriptCommands.scala @@ -0,0 +1,19 @@ +import sbt._ +import Keys._ +import complete.DefaultParsers._ + +/** Custom commands for use by the Jenkins scripts. This keeps the surface area and call syntax small. */ +object ScriptCommands { + def all = Seq(setupPublishCore) + + /** Set up the environment for `validate/publish-core`. The argument is the Artifactory snapshot repository URL. */ + def setupPublishCore = Command.single("setupPublishCore") { case (state, url) => + Project.extract(state).append(Seq( + VersionUtil.baseVersionSuffix in Global := "SHA-SNAPSHOT", + // Append build.timestamp to Artifactory URL to get consistent build numbers (see https://github.com/sbt/sbt/issues/2088): + publishTo in Global := Some("scala-pr" at url.replaceAll("/$", "") + ";build.timestamp=" + System.currentTimeMillis), + publishArtifact in (Compile, packageDoc) in ThisBuild := false, + scalacOptions in Compile in ThisBuild += "-optimise" + ), state) + } +} diff --git a/project/VersionUtil.scala b/project/VersionUtil.scala index 71de772b08..f237c35c68 100644 --- a/project/VersionUtil.scala +++ b/project/VersionUtil.scala @@ -5,21 +5,29 @@ import java.io.FileInputStream import scala.collection.JavaConverters._ object VersionUtil { + lazy val baseVersion = settingKey[String]("The base version number from which all others are derived") + lazy val baseVersionSuffix = settingKey[String]("Identifies the kind of version to build") 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 generateBuildCharacterPropertiesFile = taskKey[File]("Generating buildcharacter.properties file.") - lazy val versionPropertiesSettings = Seq[Setting[_]]( - versionProperties := versionPropertiesImpl.value + lazy val globalVersionSettings = Seq[Setting[_]]( + // Set the version properties globally (they are the same for all projects) + versionProperties in Global := versionPropertiesImpl.value, + version in Global := versionProperties.value.mavenVersion ) 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 ) + lazy val generateBuildCharacterFileSettings = Seq[Setting[_]]( + generateBuildCharacterPropertiesFile := generateBuildCharacterPropertiesFileImpl.value + ) + case class Versions(canonicalVersion: String, mavenVersion: String, osgiVersion: String, commitSha: String, commitDate: String, isRelease: Boolean) { val githubTree = if(isRelease) "v" + mavenVersion @@ -28,30 +36,36 @@ object VersionUtil { 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 - } + def toMap: Map[String, String] = Map( + "version.number" -> canonicalVersion, + "maven.version.number" -> mavenVersion, + "osgi.version.number" -> osgiVersion + ) } - 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 + /** Compute the canonical, Maven and OSGi version number from `baseVersion` and `baseVersionSuffix`. + * Examples of the generated versions: + * + * ("2.11.8", "SNAPSHOT" ) -> ("2.11.8-20151215-133023-7559aed3c5", "2.11.8-SNAPSHOT", "2.11.8.v20151215-133023-7559aed3c5") + * ("2.11.8", "SHA-SNAPSHOT") -> ("2.11.8-20151215-133023-7559aed3c5", "2.11.8-7559aed3c5-SNAPSHOT", "2.11.8.v20151215-133023-7559aed3c5") + * ("2.11.8", "" ) -> ("2.11.8", "2.11.8", "2.11.8.v20151215-133023-VFINAL-7559aed3c5") + * ("2.11.8", "M3" ) -> ("2.11.8-M3", "2.11.8-M3", "2.11.8.v20151215-133023-M3-7559aed3c5") + * ("2.11.8", "RC4" ) -> ("2.11.8-RC4", "2.11.8-RC4", "2.11.8.v20151215-133023-RC4-7559aed3c5") + * ("2.11.8-RC4", "SPLIT" ) -> ("2.11.8-RC4", "2.11.8-RC4", "2.11.8.v20151215-133023-RC4-7559aed3c5") + * + * A `baseVersionSuffix` of "SNAPSHOT" is the default, which is used for local snapshot builds. The PR validation + * job uses "SHA-SNAPSHOT". An empty suffix is used for releases. All other suffix values are treated as RC / + * milestone builds. The special suffix value "SPLIT" is used to split the real suffix off from `baseVersion` + * instead and then apply the usual logic. */ + private lazy val versionPropertiesImpl: Def.Initialize[Versions] = Def.setting { + + val (base, suffix) = { + val (b, s) = (baseVersion.value, baseVersionSuffix.value) + if(s == "SPLIT") { + val split = """([\w+\.]+)(-[\w+\.]+)??""".r + val split(b2, sOrNull) = b + (b2, Option(sOrNull).map(_.drop(1)).getOrElse("")) + } else (b, s) } def executeTool(tool: String) = { @@ -62,24 +76,31 @@ object VersionUtil { Process(cmd).lines.head } - val commitDate = executeTool("get-scala-commit-date") - val commitSha = executeTool("get-scala-commit-sha") + val date = executeTool("get-scala-commit-date") + val sha = executeTool("get-scala-commit-sha").substring(0, 7) // The script produces 10 digits at the moment - Versions( - canonicalVersion = s"$ver-$commitDate-$commitSha", - mavenVersion = s"${version.value}", - osgiVersion = s"$ver.v$commitDate$osgiSuffix-$commitSha", - commitSha = commitSha, - commitDate = commitDate, - isRelease = !osgiSuffix.isEmpty - ) + val (canonicalV, mavenV, osgiV, release) = suffix match { + case "SNAPSHOT" => (s"$base-$date-$sha", s"$base-SNAPSHOT", s"$base.v$date-$sha", false) + case "SHA-SNAPSHOT" => (s"$base-$date-$sha", s"$base-$sha-SNAPSHOT", s"$base.v$date-$sha", false) + case "" => (s"$base", s"$base", s"$base.v$date-VFINAL-$sha", true) + case suffix => (s"$base-$suffix", s"$base-$suffix", s"$base.v$date-$suffix-$sha", true) + } + + Versions(canonicalV, mavenV, osgiV, sha, date, release) } - 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) + private lazy val generateVersionPropertiesFileImpl: Def.Initialize[Task[File]] = Def.task { + writeProps(versionProperties.value.toMap + ("copyright.string" -> copyrightString.value), + (resourceManaged in Compile).value / s"${thisProject.value.id}.properties") + } + + private lazy val generateBuildCharacterPropertiesFileImpl: Def.Initialize[Task[File]] = Def.task { + writeProps(versionProperties.value.toMap, (baseDirectory in ThisBuild).value / "buildcharacter.properties") + } + private def writeProps(m: Map[String, String], propFile: File): File = { + val props = new Properties + m.foreach { case (k, v) => props.put(k, v) } // 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 -- cgit v1.2.3