Skip to content

Commit

Permalink
New format option untaggedSuffix (#66)
Browse files Browse the repository at this point in the history
When publishing artifacts to Maven Snapshot repositories, the version needs to end in `-SNAPSHOT`. 

With this addition, we can automatically add such a suffix, while keeping a unique version based on the last tag.

Pull request: #66
  • Loading branch information
lefou authored Sep 1, 2022
1 parent 9cad6ef commit 2d81789
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ The `format` method has the following options:
* `dirtySep: String = "-DIRTY"` - will be printed before the dirty hash if the local repository is in modified state
* `dirtyHashDigits: Int = 8` - the number of digits to be used for the dirty hash
* `tagModifier: String => String` - allows to modify the git tag (By default this strips a leading `v` if one exists. e.g. v1.2.3 -> 1.2.3)
* `untaggedSuffix: String = "" - will append the given string at the end of the version when the current revision is not tagged (e.g. use with `"-SNAPSHOT"` to publish to Maven Snapshot repositories)

When used with its defaults, the outcome is identical to the version scheme used by mill.
When used with its defaults, the outcome is identical to the version scheme used by Mill.

== Download

Expand Down
38 changes: 33 additions & 5 deletions core/src/de/tobiasroeser/mill/vcs/version/VcsState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@ case class VcsState(
revHashDigits: Int = 6,
dirtySep: String = "-DIRTY",
dirtyHashDigits: Int = 8,
tagModifier: String => String = stripV
tagModifier: String => String = stripV,
untaggedSuffix: String = ""
): String = {
val versionPart = tagModifier(lastTag.getOrElse(noTagFallback))

val commitCountPart = if (lastTag.isEmpty || commitsSinceLastTag > 0) {
val isUntagged = lastTag.isEmpty || commitsSinceLastTag > 0

val commitCountPart = if (isUntagged) {
s"$countSep${if (commitCountPad > 0) {
(10000000000000L + commitsSinceLastTag).toString().substring(14 - commitCountPad, 14)
} else if (commitCountPad == 0) commitsSinceLastTag else ""}"
} else if (commitCountPad == 0) commitsSinceLastTag
else ""}"
} else ""

val revisionPart = if (lastTag.isEmpty || commitsSinceLastTag > 0) {
val revisionPart = if (isUntagged) {
s"$revSep${currentRevision.take(revHashDigits)}"
} else ""

Expand All @@ -36,7 +40,9 @@ case class VcsState(
case Some(d) => dirtySep + d.take(dirtyHashDigits)
}

s"$versionPart$commitCountPart$revisionPart$dirtyPart"
val snapshotSuffix = if (isUntagged) untaggedSuffix else ""

s"$versionPart$commitCountPart$revisionPart$dirtyPart$snapshotSuffix"
}

/**
Expand All @@ -52,6 +58,28 @@ case class VcsState(
case t => t
}

@deprecated("Binary compatibility shim. Use other overload instead.", "mill-vcs-verison after 0.2.0")
private[version] def format(
noTagFallback: String,
countSep: String,
commitCountPad: Byte,
revSep: String,
revHashDigits: Int,
dirtySep: String,
dirtyHashDigits: Int,
tagModifier: String => String
): String = format(
noTagFallback = noTagFallback,
countSep = countSep,
commitCountPad = commitCountPad,
revSep = revSep,
revHashDigits = revHashDigits,
dirtySep = dirtySep,
dirtyHashDigits = dirtyHashDigits,
tagModifier = tagModifier,
untaggedSuffix = ""
)

}

object VcsState {
Expand Down
16 changes: 16 additions & 0 deletions core/test/src/de/tobiasroeser/mill/vcs/version/VcsStateSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ class VcsStateSpec extends AnyFreeSpec {
)
}

"should append a -SNAPSHOT suffix" in {
assert(
state("0.7.3", 0, null, "61568ec80f2465f3f01ea2c7e92273f4fbf94b01")
.format(untaggedSuffix = "-SNAPSHOT") === "0.7.3"
)
assert(
state("0.7.3", 4, "a6ea44d3726", "61568ec80f2465f3f01ea2c7e92273f4fbf94b01")
.format(untaggedSuffix = "-SNAPSHOT") === "0.7.3-4-61568e-DIRTYa6ea44d3-SNAPSHOT"
)
assert(
state("0.7.3", 4, null, "61568ec80f2465f3f01ea2c7e92273f4fbf94b01")
.format(untaggedSuffix = "-SNAPSHOT") === "0.7.3-4-61568e-SNAPSHOT"
)
}


"Example format configs" - {
"mill" in {
assert(
Expand Down

0 comments on commit 2d81789

Please sign in to comment.