Skip to content

Commit

Permalink
Merge pull request #333 from bplommer/expose-versioning-info
Browse files Browse the repository at this point in the history
Expose versioning info in version plugin
  • Loading branch information
armanbilge authored Jul 16, 2022
2 parents e2df0e2 + 91d747d commit 080f660
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.typelevel.sbt

import org.typelevel.sbt.kernel.GitHelper
import org.typelevel.sbt.kernel.V
import sbt._
import sbt.plugins.JvmPlugin

Expand All @@ -37,6 +39,7 @@ object TypelevelKernelPlugin extends AutoPlugin {
BasicCommands.addAlias(BasicCommands.removeAlias(s, name), name, contents)
}
})

}

import autoImport._
Expand Down Expand Up @@ -83,4 +86,34 @@ object TypelevelKernelPlugin extends AutoPlugin {
}
}

private[sbt] lazy val currentRelease: Def.Initialize[Option[String]] = Def.setting {
// some tricky logic here ...
// if the latest release is a pre-release (e.g., M or RC)
// and there are no stable releases it is bincompatible with,
// then for all effective purposes it is the current release

val release = previousReleases.value match {
case head :: tail if head.isPrerelease =>
tail
.filterNot(_.isPrerelease)
.find(head.copy(prerelease = None).mustBeBinCompatWith(_))
.orElse(Some(head))
case releases => releases.headOption
}

release.map(_.toString)
}

// latest tagged release, including pre-releases
private[sbt] lazy val currentPreRelease: Def.Initialize[Option[String]] = Def.setting {
previousReleases.value.headOption.map(_.toString)
}

private[this] lazy val previousReleases: Def.Initialize[List[V]] = Def.setting {
val currentVersion = V(version.value).map(_.copy(prerelease = None))
GitHelper.previousReleases(fromHead = true, strict = false).filter { v =>
currentVersion.forall(v.copy(prerelease = None) <= _)
}
}

}
33 changes: 1 addition & 32 deletions site/src/main/scala/org/typelevel/sbt/TypelevelSitePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import laika.helium.config.ImageLink
import laika.sbt.LaikaPlugin
import laika.theme.ThemeProvider
import mdoc.MdocPlugin
import org.typelevel.sbt.kernel.GitHelper
import org.typelevel.sbt.kernel.V
import org.typelevel.sbt.site._
import sbt._

Expand All @@ -38,6 +36,7 @@ import MdocPlugin.autoImport._
import LaikaPlugin.autoImport._
import gha.GenerativePlugin
import GenerativePlugin.autoImport._
import TypelevelKernelPlugin._
import TypelevelKernelPlugin.autoImport._

object TypelevelSitePlugin extends AutoPlugin {
Expand Down Expand Up @@ -251,36 +250,6 @@ object TypelevelSitePlugin extends AutoPlugin {
)
)

private lazy val currentRelease = Def.setting {
// some tricky logic here ...
// if the latest release is a pre-release (e.g., M or RC)
// and there are no stable releases it is bincompatible with,
// then for all effective purposes it is the current release

val release = previousReleases.value match {
case head :: tail if head.isPrerelease =>
tail
.filterNot(_.isPrerelease)
.find(head.copy(prerelease = None).mustBeBinCompatWith(_))
.orElse(Some(head))
case releases => releases.headOption
}

release.map(_.toString)
}

// latest tagged release, including pre-releases
private lazy val currentPreRelease = Def.setting {
previousReleases.value.headOption.map(_.toString)
}

private lazy val previousReleases = Def.setting {
val currentVersion = V(version.value).map(_.copy(prerelease = None))
GitHelper.previousReleases(fromHead = true, strict = false).filter { v =>
currentVersion.forall(v.copy(prerelease = None) <= _)
}
}

private def previewTask = Def
.taskDyn {
// inlined from https://github.com/planet42/Laika/blob/9022f6f37c9017f7612fa59398f246c8e8c42c3e/sbt/src/main/scala/laika/sbt/Tasks.scala#L192
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.typelevel.sbt

import com.typesafe.sbt.GitPlugin
import com.typesafe.sbt.SbtGit.git
import org.typelevel.sbt.TypelevelKernelPlugin._
import org.typelevel.sbt.kernel.GitHelper
import org.typelevel.sbt.kernel.V
import sbt._
Expand All @@ -37,6 +38,12 @@ object TypelevelVersioningPlugin extends AutoPlugin {
lazy val tlUntaggedAreSnapshots =
settingKey[Boolean](
"If true, an untagged commit is given a snapshot version, e.g. 0.4-00218f9-SNAPSHOT. If false, it is given a release version, e.g. 0.4-00218f9. (default: true)")

lazy val tlLatestVersion = settingKey[Option[String]](
"The latest tagged version on this branch. Priority is given to the latest stable version, but if you have tagged a binary-breaking prelease version (such as a milestone or release candidate), that will be selected instead. If applicable, this will be the current tagged version.")

lazy val tlLatestPreReleaseVersion = settingKey[Option[String]](
"The latest tagged version on this branch, including milestones and release candidates. If applicable, this will be the current tagged version.")
}

import autoImport._
Expand Down Expand Up @@ -123,7 +130,9 @@ object TypelevelVersioningPlugin extends AutoPlugin {
if (isSnapshot.value) version += "-SNAPSHOT"

version
}
},
tlLatestVersion := currentRelease.value,
tlLatestPreReleaseVersion := currentPreRelease.value
)

private val Description = """^.*-(\d+)-[a-zA-Z0-9]+$""".r
Expand Down

0 comments on commit 080f660

Please sign in to comment.