From 5273b81edaf75b0d9d3c27b447f16b07915ea026 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 22 Feb 2017 15:06:09 +0000 Subject: [PATCH] Document & test simple post-process solution Fixes #5 (alternative fix) --- README.md | 15 +++++++-- .../dynver/custom-version-string-0/build.sbt | 32 +++++++++++++++++++ .../project/plugins.sbt | 5 +++ .../dynver/custom-version-string-0/test | 32 +++++++++++++++++++ 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/sbt-test/dynver/custom-version-string-0/build.sbt create mode 100644 src/sbt-test/dynver/custom-version-string-0/project/plugins.sbt create mode 100644 src/sbt-test/dynver/custom-version-string-0/test diff --git a/README.md b/README.md index 0528b49..4442e18 100644 --- a/README.md +++ b/README.md @@ -58,9 +58,18 @@ If you're not seeing what you expect, then start with this: ## Custom version string -To define a custom version string you can use `dynverGitDescribeOutput`, `dynverCurrentDate` and `sbtdynver.DynVer` object to redefine `version in ThisBuild` (and optionally also `dynver in ThisBuild`). +Sometimes you want to customise the version string. It might be for personal preference, or for compatibility with another tool or spec. -For example if you want a custom version string that doesn't use `+`'s (because Docker rejects them - [#5](https://github.com/dwijnand/sbt-dynver/issues/5)) you can customise like so: +As an example, Docker rejects tags which include `+`'s ([#5](https://github.com/dwijnand/sbt-dynver/issues/5). + +A simply way to solve this is to simply post-process the value of `version in ThisBuild` (and optionally `dynver in ThisBuild`), for example by replacing '+' with '-': + +```scala +version in ThisBuild ~= (_.replace('+', '-')) + dynver in ThisBuild ~= (_.replace('+', '-')) +``` + +If instead you want to completely customise the string format you can use `dynverGitDescribeOutput`, `dynverCurrentDate` and `sbtdynver.DynVer`, like so: ```scala def versionFmt(out: sbtdynver.GitDescribeOutput): String = @@ -77,7 +86,7 @@ inThisBuild(List( )) ``` -Essentially this defines how to transform `git describe`'s output into a string, what the fallback version string is, and then wires everything back together. +Essentially this defines how to transform the structured output of `git describe`'s into a string, defines the fallback version string, and then wires everything back together. ## Dependencies diff --git a/src/sbt-test/dynver/custom-version-string-0/build.sbt b/src/sbt-test/dynver/custom-version-string-0/build.sbt new file mode 100644 index 0000000..814cd76 --- /dev/null +++ b/src/sbt-test/dynver/custom-version-string-0/build.sbt @@ -0,0 +1,32 @@ +version in ThisBuild ~= (_.replace('+', '-')) + dynver in ThisBuild ~= (_.replace('+', '-')) + +def tstamp = Def.setting(sbtdynver.DynVer timestamp dynverCurrentDate.value) +def headSha = Def.task("git rev-parse --short=8 HEAD".!!(streams.value.log).trim) + +def check(a: String, e: String) = assert(a == e, s"Version mismatch: Expected $e, Incoming $a") + +TaskKey[Unit]("checkNotAGitRepo") := check(version.value, s"HEAD-${tstamp.value}") +TaskKey[Unit]("checkNoCommits") := check(version.value, s"HEAD-${tstamp.value}") +TaskKey[Unit]("checkOnCommit") := check(version.value, s"${headSha.value}") +TaskKey[Unit]("checkOnCommitDirty") := check(version.value, s"${headSha.value}-${tstamp.value}") +TaskKey[Unit]("checkOnTag") := check(version.value, s"1.0.0") +TaskKey[Unit]("checkOnTagDirty") := check(version.value, s"1.0.0-${tstamp.value}") +TaskKey[Unit]("checkOnTagAndCommit") := check(version.value, s"1.0.0-1-${headSha.value}") +TaskKey[Unit]("checkOnTagAndCommitDirty") := check(version.value, s"1.0.0-1-${headSha.value}-${tstamp.value}") + +TaskKey[Unit]("gitInitSetup") := { + "git init".!!(streams.value.log) + "git config user.email dynver@mailinator.com".!!(streams.value.log) + "git config user.name dynver".!!(streams.value.log) +} + +TaskKey[Unit]("gitAdd") := "git add .".!!(streams.value.log) +TaskKey[Unit]("gitCommit") := "git commit -am1".!!(streams.value.log) +TaskKey[Unit]("gitTag") := "git tag -a v1.0.0 -m1.0.0".!!(streams.value.log) + +TaskKey[Unit]("dirty") := { + import java.nio.file._, StandardOpenOption._ + import scala.collection.JavaConverters._ + Files.write(baseDirectory.value.toPath.resolve("f.txt"), Seq("1").asJava, CREATE, APPEND) +} diff --git a/src/sbt-test/dynver/custom-version-string-0/project/plugins.sbt b/src/sbt-test/dynver/custom-version-string-0/project/plugins.sbt new file mode 100644 index 0000000..66ad543 --- /dev/null +++ b/src/sbt-test/dynver/custom-version-string-0/project/plugins.sbt @@ -0,0 +1,5 @@ +sys.props.get("plugin.version") match { + case Some(x) => addSbtPlugin("com.dwijnand" % "sbt-dynver" % x) + case _ => sys.error("""|The system property 'plugin.version' is not defined. + |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) +} diff --git a/src/sbt-test/dynver/custom-version-string-0/test b/src/sbt-test/dynver/custom-version-string-0/test new file mode 100644 index 0000000..9f22a4b --- /dev/null +++ b/src/sbt-test/dynver/custom-version-string-0/test @@ -0,0 +1,32 @@ +> checkNotAGitRepo + +> gitInitSetup +> reload +> checkNoCommits + +> dirty +> gitAdd +> gitCommit +> reload +> checkOnCommit + +> dirty +> reload +> checkOnCommitDirty + +> gitCommit +> gitTag +> reload +> checkOnTag + +> dirty +> reload +> checkOnTagDirty + +> gitCommit +> reload +> checkOnTagAndCommit + +> dirty +> reload +> checkOnTagAndCommitDirty