Scala 如何动态调用SBT?

Scala 如何动态调用SBT?,scala,dynamic,sbt,Scala,Dynamic,Sbt,我想创建一个新的自定义inputAsk(testOnlyCustom) 调用testOnly,参数与testOnlyCustom和 根据SBT设置(condition),在调用testOnly之前调用另一个任务(我们称之为pre)。在这里,我必须强制“顺序”执行 因此: 虽然我能够通过testCustom引用pre和test(因此没有参数)实现一个解决方案,但我无法解决testOnlyCustom的问题,因为使用了inputAsk 这是我的密码: import sbt._ import sb

我想创建一个新的自定义
inputAsk
testOnlyCustom

  • 调用
    testOnly
    ,参数与
    testOnlyCustom
  • 根据SBT设置(
    condition
    ),在调用testOnly之前调用另一个任务(我们称之为
    pre
    。在这里,我必须强制“顺序”执行
因此:

虽然我能够通过
testCustom
引用
pre
test
(因此没有参数)实现一个解决方案,但我无法解决
testOnlyCustom
的问题,因为使用了
inputAsk

这是我的密码:

import sbt._
import sbt.Keys._
import sbt.Def._
import sbtsequential.Plugin._


object Simple extends sbt.Plugin {

  import SimpleKeys._

  object SimpleKeys {
    lazy val condition = SettingKey[Boolean]("mode", "The mode.")

    lazy val pre = TaskKey[Unit]("test-with-pre", "Do some pre step.")

    lazy val testWithPre = TaskKey[Unit]("test-with-pre", "Run pre task beforehand")
    lazy val testCustom = TaskKey[Unit]("test-custom", "Run pre (depending on condition) and then test.")

    lazy val testOnlyWithPre = InputKey[Unit]("test-only-with-pre", "Run selected tests (like test-only in SBT) with pre executed before.")
    lazy val testOnlyCustom = InputKey[Unit]("test-only-configured", "Run pre (depending on condition) and then call test-only.")
  }

  lazy val baseSettings: Seq[sbt.Def.Setting[_]] = Seq(

    // this is working
    testWithPre := test.value,
    testWithPre <<= testWithPre.dependsOn( pre ),

    testCustom := Def.taskDyn {
      val c = condition.value

      if (c) {
        testWithPre
      } else {
        test
      }
    }.value,


    //
    // this is the part, where my question focuses on
    //
    testOnlyWithPre := testOnly.evaluated,
    testOnlyWithPre <<= testOnlyWithPre.dependsOn( pre ),

    // is this the correct approach?
    testOnlyCustom := Def.inputTaskDyn {
      // ???????????????????????????????
      Def.task()
    }.evaluated
  )

  lazy val testSimpleSettings: Seq[sbt.Def.Setting[_]] = baseSettings
}
导入sbt_
导入sbt.Keys_
导入sbt.Def_
导入sbtsequential.Plugin_
简单对象扩展sbt.Plugin{
导入SimpleKeys_
对象简化{
lazy val condition=SettingKey[Boolean](“模式”、“模式”)
lazy val pre=TaskKey[Unit](“使用pre进行测试”,“执行一些pre步骤”)
lazy val testWithPre=TaskKey[Unit](“使用pre进行测试”,“预先运行pre任务”)
lazy val testCustom=TaskKey[Unit](“测试自定义”、“运行预测试(取决于条件)然后测试”)
lazy val testOnlyWithPre=InputKey[Unit](“仅使用pre进行测试”,“使用pre之前执行的命令运行选定的测试(类似于仅在SBT中进行的测试)”)
lazy val TestOnlyCustomom=InputKey[Unit](“仅配置测试”,“运行预测试(取决于条件),然后仅调用测试。”)
}
lazy val baseSettings:Seq[sbt.Def.Setting[]]=Seq(
//这是有效的
testWithPre:=test.value,

testWithPre我能想到的最好的解决方案是

    testOnlyCustom := Def.inputTaskDyn {
      val args: Seq[String] = spaceDelimited("").parsed
      val c = condition.value

      if (c) {
        testOnlyWithPre.toTask(" " + args.head)
      } else {
        testOnly.toTask(" " + args.head)
      }
    }.evaluated
但是,这仍然迫使我使用一个新的解析器(
spaceDelimited
),而我不能(重新)使用testOnly解析器

有没有关于如何重用解析器的想法?

其他注释 首先,OlegYch_uu在Freenode#sbt上指出,sbt 0.13.9将通过

def runInputTask[T](key: InputKey[T], input: String, state: State): (State, T)
sbt.Extracted.scala中


第二个,仅测试解析器可以通过
sbt.Defaults#inputTests
重复使用。仅就记录而言,SBT1等效

testOnlyWithPre:=test.dependsOn(pre).value

testOnlyWithPre:=testOnly.dependsOn(pre).evaluated

def runInputTask[T](key: InputKey[T], input: String, state: State): (State, T)