From 034176e477ad3d4a72e70256692c02267eb145df Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Sat, 17 Feb 2018 20:22:24 +0100 Subject: [PATCH 1/2] Add sonatype friendly SNAPSHOT suffix --- src/main/scala/sbtdynver/DynVerPlugin.scala | 23 ++++++++++++++++----- src/test/scala/sbtdynver/DynVerSpec.scala | 6 ++++++ src/test/scala/sbtdynver/RepoStates.scala | 1 + 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/scala/sbtdynver/DynVerPlugin.scala b/src/main/scala/sbtdynver/DynVerPlugin.scala index 1e7f594..e3eeddb 100644 --- a/src/main/scala/sbtdynver/DynVerPlugin.scala +++ b/src/main/scala/sbtdynver/DynVerPlugin.scala @@ -10,9 +10,10 @@ object DynVerPlugin extends AutoPlugin { object autoImport { val dynver = taskKey[String]("The version of your project, from git") - val dynverInstance = settingKey[DynVer]("The dynver instance for this build.") + val dynverInstance = settingKey[DynVer]("The dynver instance for this build") val dynverCurrentDate = settingKey[Date]("The current date, for dynver purposes") val dynverGitDescribeOutput = settingKey[Option[GitDescribeOutput]]("The output from git describe") + val dynverSonatypeSnapshots = settingKey[Boolean]("Whether to append -SNAPSHOT to snapshot versions") val dynverCheckVersion = taskKey[Boolean]("Checks if version and dynver match") val dynverAssertVersion = taskKey[Unit]("Asserts if version and dynver match") @@ -22,13 +23,19 @@ object DynVerPlugin extends AutoPlugin { import autoImport._ override def buildSettings = Seq( - version := dynverGitDescribeOutput.value.version(dynverCurrentDate.value), + version := { + val out = dynverGitDescribeOutput.value + val date = dynverCurrentDate.value + if(dynverSonatypeSnapshots.value) out.sonatypeVersion(date) + else out.version(date) + }, isSnapshot := dynverGitDescribeOutput.value.isSnapshot, isVersionStable := dynverGitDescribeOutput.value.isVersionStable, dynverCurrentDate := new Date, dynverInstance := DynVer(Some((Keys.baseDirectory in ThisBuild).value)), dynverGitDescribeOutput := dynverInstance.value.getGitDescribeOutput(dynverCurrentDate.value), + dynverSonatypeSnapshots := false, dynver := dynverInstance.value.version(new Date), dynverCheckVersion := (dynver.value == version.value), @@ -74,6 +81,10 @@ final case class GitDescribeOutput(ref: GitRef, commitSuffix: GitCommitSuffix, d else ref.dropV.value + commitSuffix.mkString("+", "-", "") + dirtySuffix.value } + def sonatypeVersion: String = + if(isSnapshot()) version + "-SNAPSHOT" + else version + def isDirtyAfterTag = commitSuffix.distance == 0 && ref.isTag && isDirty() def isSnapshot(): Boolean = isDirty() || hasNoTags() || commitSuffix.distance > 0 def isVersionStable(): Boolean = !isDirty() @@ -107,9 +118,10 @@ object GitDescribeOutput extends ((GitRef, GitCommitSuffix, GitDirtySuffix) => G implicit class OptGitDescribeOutputOps(val _x: Option[GitDescribeOutput]) extends AnyVal { def mkVersion(f: GitDescribeOutput => String, fallback: => String): String = _x.fold(fallback)(f) - def version(d: Date): String = mkVersion(_.version, DynVer fallback d) - def isSnapshot: Boolean = _x.map(_.isSnapshot).getOrElse(true) - def isVersionStable: Boolean = _x.map(_.isVersionStable).getOrElse(false) + def version(d: Date): String = mkVersion(_.version, DynVer fallback d) + def sonatypeVersion(d: Date): String = mkVersion(_.sonatypeVersion, DynVer fallback d) + def isSnapshot: Boolean = _x.map(_.isSnapshot).getOrElse(true) + def isVersionStable: Boolean = _x.map(_.isVersionStable).getOrElse(false) def isDirty: Boolean = _x.fold(true)(_.isDirty()) def hasNoTags: Boolean = _x.fold(true)(_.hasNoTags()) @@ -119,6 +131,7 @@ object GitDescribeOutput extends ((GitRef, GitCommitSuffix, GitDirtySuffix) => G // sealed just so the companion object can extend it. Shouldn't've been a case class. sealed case class DynVer(wd: Option[File]) { def version(d: Date): String = getGitDescribeOutput(d) version d + def sonatypeVersion(d: Date): String = getGitDescribeOutput(d) sonatypeVersion d def isSnapshot(): Boolean = getGitDescribeOutput(new Date).isSnapshot def isVersionStable(): Boolean = getGitDescribeOutput(new Date).isVersionStable diff --git a/src/test/scala/sbtdynver/DynVerSpec.scala b/src/test/scala/sbtdynver/DynVerSpec.scala index b6c4c88..1bea51a 100644 --- a/src/test/scala/sbtdynver/DynVerSpec.scala +++ b/src/test/scala/sbtdynver/DynVerSpec.scala @@ -26,6 +26,12 @@ object IsSnapshotSpec extends Properties("IsSnapshotSpec") { property("on tag v1.0.0 and 1 commit with local changes") = onTagAndCommitDirty().isSnapshot() ?= true } +object SonatypeSnapshotSpec extends Properties("SonatypeSnapshotSpec") { + property("on tag v1.0.0 with local changes") = onTagDirty().sonatypeVersion() ?= "1.0.0+0-20160917-0000-SNAPSHOT" + property("on tag v1.0.0 and 1 commit, w/o local changes S") = onTagAndCommit().sonatypeVersion() ?= "1.0.0+1-1234abcd-SNAPSHOT" + property("on tag v1.0.0, w/o local changes") = onTag().sonatypeVersion() ?= "1.0.0" +} + object isVersionStableSpec extends Properties("IsVersionStableSpec") { property("not a git repo") = notAGitRepo().isVersionStable() ?= false property("no commits") = noCommits().isVersionStable() ?= false diff --git a/src/test/scala/sbtdynver/RepoStates.scala b/src/test/scala/sbtdynver/RepoStates.scala index e8e4114..8f97e05 100644 --- a/src/test/scala/sbtdynver/RepoStates.scala +++ b/src/test/scala/sbtdynver/RepoStates.scala @@ -36,6 +36,7 @@ object RepoStates { } def version() = dynver.version(date).replaceAllLiterally(sha, "1234abcd") + def sonatypeVersion() = dynver.sonatypeVersion(date).replaceAllLiterally(sha, "1234abcd") def isSnapshot() = dynver.isSnapshot() def isVersionStable() = dynver.isVersionStable() From e65cbb0e3b1c3edbbaeab92cded549835fe9e05e Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Mon, 26 Feb 2018 13:56:34 +0100 Subject: [PATCH 2/2] Update spec --- src/test/scala/sbtdynver/DynVerSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/scala/sbtdynver/DynVerSpec.scala b/src/test/scala/sbtdynver/DynVerSpec.scala index 1bea51a..cac847d 100644 --- a/src/test/scala/sbtdynver/DynVerSpec.scala +++ b/src/test/scala/sbtdynver/DynVerSpec.scala @@ -27,7 +27,7 @@ object IsSnapshotSpec extends Properties("IsSnapshotSpec") { } object SonatypeSnapshotSpec extends Properties("SonatypeSnapshotSpec") { - property("on tag v1.0.0 with local changes") = onTagDirty().sonatypeVersion() ?= "1.0.0+0-20160917-0000-SNAPSHOT" + property("on tag v1.0.0 with local changes") = onTagDirty().sonatypeVersion() ?= "1.0.0+0-1234abcd+20160917-0000-SNAPSHOT" property("on tag v1.0.0 and 1 commit, w/o local changes S") = onTagAndCommit().sonatypeVersion() ?= "1.0.0+1-1234abcd-SNAPSHOT" property("on tag v1.0.0, w/o local changes") = onTag().sonatypeVersion() ?= "1.0.0" }