Make sbt new extensible

Problem
-------
`sbt new` (`sbt init`) hardcodes the templates, which I think is ok,
but without changing much, we can make it extensible.

Solution
--------
This adds two new keys `templateDescriptions` and `templateRunLocal`,
which can customize the behavior for in-house usage etc.
This commit is contained in:
Eugene Yokota 2023-06-18 15:48:18 -04:00
parent fe4ff0f617
commit 5398f6e1aa
4 changed files with 29 additions and 6 deletions

View File

@ -361,6 +361,8 @@ object Defaults extends BuildCommon {
fork :== false,
initialize :== {},
templateResolverInfos :== Nil,
templateDescriptions :== TemplateCommandUtil.defaultTemplateDescriptions,
templateRunLocal := templateRunLocalInputTask(runLocalTemplate).evaluated,
forcegc :== sys.props
.get("sbt.task.forcegc")
.map(java.lang.Boolean.parseBoolean)
@ -2644,6 +2646,19 @@ object Defaults extends BuildCommon {
if (useCoursier.value) CoursierDependencyResolution(csrConfiguration.value)
else IvyDependencyResolution(ivyConfiguration.value)
}
def templateRunLocalInputTask(
runLocal: (Seq[String], Logger) => Unit
): Initialize[InputTask[Unit]] =
Def.inputTask {
import Def._
val s = streams.value
val args = spaceDelimited().parsed
runLocal(args, s.log)
}
def runLocalTemplate(arguments: Seq[String], log: Logger): Unit =
TemplateCommandUtil.defaultRunLocalTemplate(arguments.toList, log)
}
object Classpaths {

View File

@ -571,6 +571,8 @@ object Keys {
val sbtBinaryVersion = settingKey[String]("Defines the binary compatibility version substring.").withRank(BPlusSetting)
val skip = taskKey[Boolean]("For tasks that support it (currently only 'compile', 'update', 'publish' and 'publishLocal'), setting skip to true will force the task to not to do its work. The exact semantics may vary depending on the task.").withRank(BSetting)
val templateResolverInfos = settingKey[Seq[TemplateResolverInfo]]("Template resolvers used for 'new'.").withRank(BSetting)
val templateDescriptions = settingKey[Seq[(String, String)]]("List of templates with description used for 'new' / 'init'.")
val templateRunLocal = inputKey[Unit]("Runs a local template.").withRank(DTask)
val interactionService = taskKey[InteractionService]("Service used to ask for user input through the current user interface(s).").withRank(CTask)
val insideCI = SettingKey[Boolean]("insideCI", "Determines if the sbt is running in a Continuous Integration environment", AMinusSetting)

View File

@ -41,6 +41,7 @@ private[sbt] object TemplateCommandUtil {
val extracted = (Project extract s0)
val (s1, ivyConf) = extracted.runTask(Keys.ivyConfiguration, s0)
val scalaModuleInfo = extracted.get(Keys.updateSbtClassifiers / Keys.scalaModuleInfo)
val templateDescriptions = extracted.get(Keys.templateDescriptions)
val args0 = inputArg.toList ++
(s0.remainingCommands match {
case exec :: Nil if exec.commandLine == "shell" => Nil
@ -52,10 +53,10 @@ private[sbt] object TemplateCommandUtil {
run(infos, args0, s0.configuration, ivyConf, globalBase, scalaModuleInfo, log)
terminate
} else {
fortifyArgs() match {
fortifyArgs(templateDescriptions.toList) match {
case Nil => terminate
case arg :: Nil if arg.endsWith(".local") =>
localRun(arg :: Nil, log)
extracted.runInputTask(Keys.templateRunLocal, " " + arg, s0)
reload
case args =>
run(infos, args, s0.configuration, ivyConf, globalBase, scalaModuleInfo, log)
@ -166,7 +167,7 @@ private[sbt] object TemplateCommandUtil {
private lazy val term: ITerminal = ITerminal.get
private lazy val isAnsiSupported = term.isAnsiSupported
private lazy val nonMoveLetters = ('a' to 'z').toList diff List('h', 'j', 'k', 'l', 'q')
private lazy val templates = List(
private[sbt] lazy val defaultTemplateDescriptions = List(
ScalaToolkitSlug -> "Scala Toolkit (beta) by Scala Center and VirtusLab",
TypelevelToolkitSlug -> "Toolkit to start building Typelevel apps",
SbtCrossPlatformSlug -> "A cross-JVM/JS/Native project",
@ -179,10 +180,11 @@ private[sbt] object TemplateCommandUtil {
"spotify/scio.g8" -> "A Scio project",
"disneystreaming/smithy4s.g8" -> "A Smithy4s project",
)
private def fortifyArgs(): List[String] =
private def fortifyArgs(templates: List[(String, String)]): List[String] =
if (System.console eq null) Nil
else
ITerminal.withStreams(true, false) {
assert(templates.size <= 20, "template list cannot have more than 20 items")
val mappingList = templates.zipWithIndex.map {
case (v, idx) => toLetter(idx) -> v
}
@ -261,7 +263,9 @@ private[sbt] object TemplateCommandUtil {
else ans0
}
private def localRun(
// This is used by Defaults.runLocalTemplate, which implements
// templateRunLocal input task.
private[sbt] def defaultRunLocalTemplate(
arguments: List[String],
log: Logger
): Unit =

View File

@ -149,7 +149,9 @@ private[sbt] class TaskProgress(
"console",
"consoleProject",
"consoleQuick",
"state"
"state",
"streams",
"streams-manager",
)
private[this] val hiddenTasks = Set(
"compileEarly",