Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sonatype mode #64

Merged
merged 2 commits into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/main/scala/sbtdynver/DynVerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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),
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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())
Expand All @@ -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

Expand Down
6 changes: 6 additions & 0 deletions src/test/scala/sbtdynver/DynVerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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-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"
}

object isVersionStableSpec extends Properties("IsVersionStableSpec") {
property("not a git repo") = notAGitRepo().isVersionStable() ?= false
property("no commits") = noCommits().isVersionStable() ?= false
Expand Down
1 change: 1 addition & 0 deletions src/test/scala/sbtdynver/RepoStates.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down