Skip to content

Commit

Permalink
Document & test simple post-process solution
Browse files Browse the repository at this point in the history
Fixes #5 (alternative fix)
  • Loading branch information
dwijnand committed Feb 22, 2017
1 parent c6a6d7f commit 5273b81
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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

Expand Down
32 changes: 32 additions & 0 deletions src/sbt-test/dynver/custom-version-string-0/build.sbt
Original file line number Diff line number Diff line change
@@ -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 [email protected]".!!(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)
}
Original file line number Diff line number Diff line change
@@ -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)
}
32 changes: 32 additions & 0 deletions src/sbt-test/dynver/custom-version-string-0/test
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5273b81

Please sign in to comment.