Skip to content

Commit

Permalink
Merge pull request #652 from armanbilge/pr/composable-aliases
Browse files Browse the repository at this point in the history
Introduce `tlCommandAliases` setting for defining command aliases
  • Loading branch information
armanbilge committed Oct 24, 2023
2 parents 5f2998e + 7c97f7d commit d2c50f7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 56 deletions.
55 changes: 14 additions & 41 deletions core/src/main/scala/org/typelevel/sbt/TypelevelPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.typelevel.sbt.gha.GitHubActionsPlugin
import sbt._

import Keys._
import TypelevelKernelPlugin.autoImport._

/**
* The [[TypelevelPlugin]] brings together the [[TypelevelCiReleasePlugin]] and the
Expand All @@ -44,7 +45,6 @@ object TypelevelPlugin extends AutoPlugin {
"Convert compiler warnings into errors under CI builds (default: true)")
}

import TypelevelKernelPlugin.mkCommand
import TypelevelCiPlugin.autoImport._
import TypelevelSettingsPlugin.autoImport._
import TypelevelSonatypeCiReleasePlugin.autoImport._
Expand Down Expand Up @@ -76,52 +76,25 @@ object TypelevelPlugin extends AutoPlugin {
scala <- githubWorkflowScalaVersions.value.filterNot(defaultScala.startsWith(_))
java <- githubWorkflowJavaVersions.value.tail // default java is head
} yield MatrixExclude(Map("scala" -> scala, "java" -> java.render))
}
) ++ addPrePRCommandAlias ++ addTlPrePRBotHookCommandAlias

// partially re-implemnents addCommandAlias
// this is so we can use the value of other settings to generate command
private def addPrePRCommandAlias: Seq[Setting[_]] = Seq(
GlobalScope / onLoad := {
},
GlobalScope / tlCommandAliases ++= {
val header = tlCiHeaderCheck.value
val scalafmt = tlCiScalafmtCheck.value
val scalafix = tlCiScalafixCheck.value

(GlobalScope / Keys.onLoad).value.compose { (state: State) =>
val command = mkCommand(
List("project /", "githubWorkflowGenerate") ++
List("+headerCreateAll").filter(_ => header) ++
List("+scalafmtAll", "scalafmtSbt").filter(_ => scalafmt) ++
List("+scalafixAll").filter(_ => scalafix)
)
BasicCommands.addAlias(state, "prePR", command)
}
},
GlobalScope / Keys.onUnload := {
(GlobalScope / Keys.onUnload)
.value
.compose((state: State) => BasicCommands.removeAlias(state, "prePR"))
}
)
val prePR = List("project /", "githubWorkflowGenerate") ++
List("+headerCreateAll").filter(_ => header) ++
List("+scalafmtAll", "scalafmtSbt").filter(_ => scalafmt) ++
List("+scalafixAll").filter(_ => scalafix)

private def addTlPrePRBotHookCommandAlias: Seq[Setting[_]] = Seq(
GlobalScope / onLoad := {
val header = tlCiHeaderCheck.value
val scalafmt = tlCiScalafmtCheck.value
val botHook = List("githubWorkflowGenerate") ++
List("+headerCreateAll").filter(_ => header) ++
List("+scalafmtAll", "scalafmtSbt").filter(_ => scalafmt)

(GlobalScope / Keys.onLoad).value.compose { (state: State) =>
val command = mkCommand(
List("githubWorkflowGenerate") ++
List("+headerCreateAll").filter(_ => header) ++
List("+scalafmtAll", "scalafmtSbt").filter(_ => scalafmt)
)
BasicCommands.addAlias(state, "tlPrePrBotHook", command)
}
},
GlobalScope / Keys.onUnload := {
(GlobalScope / Keys.onUnload)
.value
.compose((state: State) => BasicCommands.removeAlias(state, "tlPrePrBotHook"))
Map(
"prePR" -> prePR,
"tlPrePrBotHook" -> botHook
)
}
)

Expand Down
2 changes: 1 addition & 1 deletion docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Instead of using the super-plugins, for finer-grained control you can always add
`TypelevelKernelPlugin`

- `tlIsScala3` (setting): `true`, if `scalaVersion` is 3.x.
- `tlReplaceCommandAlias` (method): Replace a `addCommandAlias` definition.
- `tlCommandAliases` (setting): Command aliases defined for this build.
- `tlReleaseLocal` (command): Alias for `+publishLocal`.

### sbt-typelevel-versioning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ object TypelevelKernelPlugin extends AutoPlugin {
lazy val tlSkipIrrelevantScalas = settingKey[Boolean](
"Sets skip := true for compile/test/publish/etc. tasks on a project if the current scalaVersion is not in that project's crossScalaVersions (default: false)")

lazy val tlCommandAliases = settingKey[Map[String, List[String]]](
"Command aliases defined for this build"
)

@deprecated(
"Use `tlCommandAliases` for a more composable command definition experience",
"0.6.1")
def tlReplaceCommandAlias(name: String, contents: String): Seq[Setting[State => State]] =
Seq(GlobalScope / onLoad ~= { (f: State => State) =>
f andThen { s: State =>
Expand All @@ -51,12 +58,27 @@ object TypelevelKernelPlugin extends AutoPlugin {
import autoImport._

override def globalSettings = Seq(
Def.derive(tlIsScala3 := scalaVersion.value.startsWith("3."))
Def.derive(tlIsScala3 := scalaVersion.value.startsWith("3.")),
tlCommandAliases := Map(
"tlReleaseLocal" -> List("reload", "project /", "+publishLocal")
),
onLoad := {
val aliases = tlCommandAliases.value
onLoad.value.compose { (state: State) =>
aliases.foldLeft(state) {
case (state, (alias, command)) =>
BasicCommands.addAlias(state, alias, mkCommand(command))
}
}
},
onUnload := {
val aliases = tlCommandAliases.value.keys
onUnload.value.compose { (state: State) =>
aliases.foldLeft(state) { (state, alias) => BasicCommands.removeAlias(state, alias) }
}
}
)

override def buildSettings =
addCommandAlias("tlReleaseLocal", mkCommand(List("reload", "project /", "+publishLocal")))

override def projectSettings = Seq(
ivyConfigurations += CompileTime,
Compile / unmanagedClasspath ++= update.value.select(configurationFilter(CompileTime.name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import xerial.sbt.Sonatype

import Keys._
import Sonatype.autoImport._
import TypelevelKernelPlugin.mkCommand
import TypelevelKernelPlugin.autoImport._

object TypelevelSonatypePlugin extends AutoPlugin {

Expand All @@ -37,19 +37,21 @@ object TypelevelSonatypePlugin extends AutoPlugin {

import autoImport._

override def globalSettings = Seq(
tlCommandAliases += {
"tlRelease" -> List(
"reload",
"project /",
"+mimaReportBinaryIssues",
"+publish",
"tlSonatypeBundleReleaseIfRelevant")
}
)

override def buildSettings =
Seq(
tlSonatypeUseLegacyHost := false,
autoAPIMappings := true
) ++ addCommandAlias(
"tlRelease",
mkCommand(
List(
"reload",
"project /",
"+mimaReportBinaryIssues",
"+publish",
"tlSonatypeBundleReleaseIfRelevant"))
)

override def projectSettings = Seq(
Expand Down

0 comments on commit d2c50f7

Please sign in to comment.