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

feat: support prerelease version increments #393

Merged
merged 7 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ target
project/target
.idea
.idea_modules
.bsp
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ As of version 0.8, *sbt-release* comes with four strategies for computing the ne
* `Minor`: always bumps the *minor* part of the version
* `Bugfix`: always bumps the *bugfix* part of the version
* `Nano`: always bumps the *nano* part of the version
* `Next`: bumps the last version part (e.g. `0.17` -> `0.18`, `0.11.7` -> `0.11.8`, `3.22.3.4.91` -> `3.22.3.4.92`)
* `Next`: bumps the last version part (e.g. `0.17` -> `0.18`, `0.11.7` -> `0.11.8`, `3.22.3.4.91` -> `3.22.3.4.92`, `2.0.0-RC1` -> `2.0.0-RC2`)

Example:

Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name := "sbt-release"

homepage := Some(url("https://github.com/sbt/sbt-release"))
licenses := Seq("Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0"))

scalaVersion := "2.12.18"
Andrapyre marked this conversation as resolved.
Show resolved Hide resolved
publishMavenStyle := true
scalacOptions ++= Seq("-deprecation", "-feature", "-language:implicitConversions")

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/ReleasePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ object ReleasePlugin extends AutoPlugin {
withStreams(extracted.structure, st) { str =>
val nv = nodeView(st, str, key :: Nil)
val (newS, result) = runTask(task, st, str, extracted.structure.index.triggers, config)(nv)
(newS, processResult(result, newS.log))
(newS, processResult2(result))
}._1
}

Expand Down Expand Up @@ -223,7 +223,7 @@ object ReleasePlugin extends AutoPlugin {
snapshots
},

releaseVersion := { ver => Version(ver).map(_.withoutQualifier.string).getOrElse(versionFormatError(ver)) },
releaseVersion := { ver => Version(ver).map(_.withoutSnapshot.string).getOrElse(versionFormatError(ver)) },
releaseVersionBump := Version.Bump.default,
releaseNextVersion := {
ver => Version(ver).map(_.bump(releaseVersionBump.value).asSnapshot.string).getOrElse(versionFormatError(ver))
Expand Down
91 changes: 70 additions & 21 deletions src/main/scala/Version.scala
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
package sbtrelease

import util.control.Exception._
import scala.util.matching.Regex
import util.control.Exception.*

object Version {
sealed trait Bump {
def bump: Version => Version
}

object Bump {
case object Major extends Bump { def bump = _.bumpMajor }
case object Minor extends Bump { def bump = _.bumpMinor }
case object Bugfix extends Bump { def bump = _.bumpBugfix }
case object Nano extends Bump { def bump = _.bumpNano }
case object Next extends Bump { def bump = _.bump }

val default = Next
/**
* Strategy to always bump the major version by default. Ex. 1.0.0 would be bumped to 2.0.0
*/
case object Major extends Bump { def bump: Version => Version = _.bumpMajor }
/**
* Strategy to always bump the minor version by default. Ex. 1.0.0 would be bumped to 1.1.0
*/
case object Minor extends Bump { def bump: Version => Version = _.bumpMinor }
/**
* Strategy to always bump the bugfix version by default. Ex. 1.0.0 would be bumped to 1.0.1
*/
case object Bugfix extends Bump { def bump: Version => Version = _.bumpBugfix }
/**
* Strategy to always bump the nano version by default. Ex. 1.0.0.0 would be bumped to 1.0.0.1
*/
case object Nano extends Bump { def bump: Version => Version = _.bumpNano }
/**
* Strategy to always increment to the next version from smallest to greatest. This strategy is the default.
* Ex:
* Major: 1 becomes 2
* Minor: 1.0 becomes 1.1
* Bugfix: 1.0.0 becomes 1.0.1
* Nano: 1.0.0.0 becomes 1.0.0.1
* Qualifier with version number: 1.0-RC1 becomes 1.0-RC2
* Qualifier without version number: 1.0-alpha becomes 1.0
*/
case object Next extends Bump { def bump: Version => Version = _.bump }

val default: Bump = Next
}

val VersionR = """([0-9]+)((?:\.[0-9]+)+)?([\.\-0-9a-zA-Z]*)?""".r
val PreReleaseQualifierR = """[\.-](?i:rc|m|alpha|beta)[\.-]?[0-9]*""".r
val VersionR: Regex = """([0-9]+)((?:\.[0-9]+)+)?([\.\-0-9a-zA-Z]*)?""".r
val PreReleaseQualifierR: Regex = """[\.-](?i:rc|m|alpha|beta)[\.-]?[0-9]*""".r

def apply(s: String): Option[Version] = {
allCatch opt {
Expand All @@ -34,9 +58,21 @@ object Version {
}

case class Version(major: Int, subversions: Seq[Int], qualifier: Option[String]) {
def bump = {
def bump: Version = {
val maybeBumpedPrerelease = qualifier.collect {
case Version.PreReleaseQualifierR() => withoutQualifier
case rawQualifier @ Version.PreReleaseQualifierR() =>
val qualifierEndsWithNumberRegex = """[0-9]*$""".r

val opt = for {
versionNumberQualifierStr <- qualifierEndsWithNumberRegex.findFirstIn(rawQualifier)
versionNumber <- Try(versionNumberQualifierStr.toInt)
.toRight(new Exception(s"Version number not parseable to a number. Version number received: $versionNumberQualifierStr"))
.toOption
newVersionNumber = versionNumber + 1
newQualifier = rawQualifier.replaceFirst(versionNumberQualifierStr, newVersionNumber.toString)
} yield Version(major, subversions, Some(newQualifier))

opt.getOrElse(copy(qualifier = None))
}
def maybeBumpedLastSubversion = bumpSubversionOpt(subversions.length-1)
def bumpedMajor = copy(major = major + 1)
Expand All @@ -46,14 +82,13 @@ case class Version(major: Int, subversions: Seq[Int], qualifier: Option[String])
.getOrElse(bumpedMajor)
}

def bumpMajor = copy(major = major + 1, subversions = Seq.fill(subversions.length)(0))
def bumpMinor = maybeBumpSubversion(0)
def bumpBugfix = maybeBumpSubversion(1)
def bumpNano = maybeBumpSubversion(2)

def maybeBumpSubversion(idx: Int) = bumpSubversionOpt(idx) getOrElse this
def bumpMajor: Version = copy(major = major + 1, subversions = Seq.fill(subversions.length)(0))
def bumpMinor: Version = maybeBumpSubversion(0)
def bumpBugfix: Version = maybeBumpSubversion(1)
def bumpNano: Version = maybeBumpSubversion(2)
def maybeBumpSubversion(idx: Int): Version = bumpSubversionOpt(idx) getOrElse this

private def bumpSubversionOpt(idx: Int) = {
private def bumpSubversionOpt(idx: Int): Option[Version] = {
val bumped = subversions.drop(idx)
val reset = bumped.drop(1).length
bumped.headOption map { head =>
Expand All @@ -64,10 +99,24 @@ case class Version(major: Int, subversions: Seq[Int], qualifier: Option[String])

def bump(bumpType: Version.Bump): Version = bumpType.bump(this)

def withoutQualifier = copy(qualifier = None)
def asSnapshot = copy(qualifier = Some("-SNAPSHOT"))
@deprecated("Use .withoutSnapshot to remove the snapshot part of the qualifier, the most common original use case for .withoutQualifier. If removing the full qualifier is really intended, simply construct a new Version class without a qualifier.")
def withoutQualifier: Version = copy(qualifier = None)

def asSnapshot: Version = copy(qualifier = qualifier.map { qualifierStr =>
s"$qualifierStr-SNAPSHOT"
}.orElse(Some("-SNAPSHOT")))

def withoutSnapshot: Version = copy(qualifier = qualifier.flatMap { qualifierStr =>
val snapshotRegex = """-SNAPSHOT""".r
val newQualifier = snapshotRegex.replaceFirstIn(qualifierStr, "")
if (newQualifier == qualifierStr) {
None
} else {
Some(newQualifier)
}
})

def string = "" + major + mkString(subversions) + qualifier.getOrElse("")
def string: String = "" + major + mkString(subversions) + qualifier.getOrElse("")

private def mkString(parts: Seq[Int]) = parts.map("."+_).mkString
}
34 changes: 22 additions & 12 deletions src/test/scala/VersionSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@ object VersionSpec extends Specification {
"bump the nano version if there's only a nano version" in {
bump("1.2.3.4") must_== "1.2.3.5"
}
"drop the qualifier if it's a pre release" in {
bump("1-rc1") must_== "1"
bump("1.2-rc1") must_== "1.2"
bump("1.2.3-rc1") must_== "1.2.3"

"drop the qualifier if it's a pre release and there is no version number at the end" in {
bump("1-rc") must_== "1"
bump("1-RC1") must_== "1"
bump("1-M1") must_== "1"
bump("1-rc-1") must_== "1"
bump("1-rc.1") must_== "1"
bump("1-beta") must_== "1"
bump("1-beta-1") must_== "1"
bump("1-beta.1") must_== "1"
bump("1-alpha") must_== "1"
}
"bump the qualifier if it's a pre release and there is a version number at the end" in {
bump("1-rc1") must_== "1-rc2"
bump("1.2-rc1") must_== "1.2-rc2"
bump("1.2.3-rc1") must_== "1.2.3-rc2"

bump("1-RC1") must_== "1-RC2"
bump("1-M1") must_== "1-M2"
bump("1-rc-1") must_== "1-rc-2"
bump("1-rc.1") must_== "1-rc.2"
bump("1-beta-1") must_== "1-beta-2"
bump("1-beta.1") must_== "1-beta.2"
}
"not drop the qualifier if it's not a pre release" in {
bump("1.2.3-Final") must_== "1.2.4-Final"
}
Expand Down Expand Up @@ -91,5 +93,13 @@ object VersionSpec extends Specification {
bumpSubversion("1.2.3.4.5-alpha")(2) must_== "1.2.3.5.0-alpha"
}
}

"Version as snapshot" should {
def snapshot(v: String) = version(v).asSnapshot.string
"include qualifier if it exists" in {
snapshot("1.0.0-RC1") must_== "1.0.0-RC1-SNAPSHOT"
}
"have no qualifier if none exists" in {
snapshot("1.0.0") must_== "1.0.0-SNAPSHOT"
}
}
}