Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import sbt._
import sbt.plugins.JvmPlugin

import Keys._
import org.typelevel.sbt.kernel.{GitHelper, V}

object TypelevelKernelPlugin extends AutoPlugin {

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

private[sbt] lazy val currentReleaseImpl = 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 = previousReleasesImpl.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 currentPreReleaseImpl = Def.setting {
previousReleasesImpl.value.headOption.map(_.toString)
}

private[sbt] lazy val previousReleasesImpl = 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) <= _)
}
}
}

import autoImport._
Expand Down
40 changes: 4 additions & 36 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 Down Expand Up @@ -77,7 +75,7 @@ object TypelevelSitePlugin extends AutoPlugin {
import TypelevelGitHubPlugin._

override def requires =
MdocPlugin && LaikaPlugin && TypelevelGitHubPlugin && GenerativePlugin && NoPublishPlugin
MdocPlugin && LaikaPlugin && TypelevelGitHubPlugin && TypelevelVersioningPlugin && GenerativePlugin && NoPublishPlugin

override def globalSettings = Seq(
tlSiteApiModule := None
Expand Down Expand Up @@ -111,8 +109,8 @@ object TypelevelSitePlugin extends AutoPlugin {
mdocVariables := {
mdocVariables.value ++
Map(
"VERSION" -> currentRelease.value.getOrElse(version.value),
"PRERELEASE_VERSION" -> currentPreRelease.value.getOrElse(version.value),
"VERSION" -> currentReleaseImpl.value.getOrElse(version.value),
"PRERELEASE_VERSION" -> currentPreReleaseImpl.value.getOrElse(version.value),
"SNAPSHOT_VERSION" -> version.value
) ++
tlSiteApiUrl.value.map("API_URL" -> _.toString).toMap
Expand All @@ -130,7 +128,7 @@ object TypelevelSitePlugin extends AutoPlugin {
scalaVersion.value,
scalaBinaryVersion.value
)
version <- currentRelease.value
version <- currentReleaseImpl.value
} yield {
val o = moduleId.organization
val n = cross(moduleId.name)
Expand Down Expand Up @@ -250,36 +248,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 @@ -25,6 +25,7 @@ import sbt._
import scala.util.Try

import Keys._
import org.typelevel.sbt.TypelevelKernelPlugin.autoImport._

object TypelevelVersioningPlugin extends AutoPlugin {

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 currentRelease = currentReleaseImpl

lazy val currentPreRelease = currentPreReleaseImpl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should define these as proper settingKeys with descriptions, that are populated below based on the implementations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we should be mindful about naming. tl-prefixed to avoid collisions with other plugins.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


lazy val previousReleases = previousReleasesImpl
}

import autoImport._
Expand Down