如何在自定义sbt命令中暂时跳过运行编译任务?

Ste*_*ler 6 sbt

我正试图packagequick-install命令中运行任务时暂时跳过编译任务(在我写的sbt插件中定义).我可以通过将skip设置放在compile任务上来跳过所有编译,但这会导致所有compile任务被跳过:

  object MyPlugin extends Plugin {
    override lazy val settings = Seq(
      (skip in compile) := true
    )
    ...
  }
Run Code Online (Sandbox Code Playgroud)

我需要的是只compile在运行我的quick-install命令时跳过.有没有办法临时修改设置,或仅将其范围限定为我的快速安装命令?

我尝试过设置转换(基于https://github.com/harrah/xsbt/wiki/Advanced-Command-Example),它应该替换所有的skip := falsewith 实例skip := true,但它没有任何效果(即编译)转换后仍然发生):

object SbtQuickInstallPlugin extends Plugin {
  private lazy val installCommand =  Command.args("quick-install", "quick install that skips compile step")(doCommand(Configurations.Compile))

  override lazy val settings = Seq(
    commands ++= Seq(installCommand),
    (Keys.skip in compile) := false    // by default, don't skip compiles
  )

  def doCommand(configs: Configuration*)(state: State, args: Seq[String]): State = {
    val extracted = Project.extract(state)
    import extracted._
    val oldStructure = structure

    val transformedSettings = session.mergeSettings.map(
      s => s.key.key match {
        case skip.key => { skip in s.key.scope := true } // skip compiles
        case _ => s
      }
    )

    // apply transformed settings (in theory)
    val newStructure = Load.reapply(transformedSettings, oldStructure)
    Project.setProject(session, newStructure, state)

    ...
}
Run Code Online (Sandbox Code Playgroud)

知道我缺少什么和/或更好的方法吗?

编辑:

跳过设置是一个任务,所以一个简单的解决方案是:

object SbtQuickInstallPlugin extends Plugin {
  private lazy val installCommand =  Command.args("quick-install", "quick install that skips compile step")(doCommand(Configurations.Compile))

  private var shouldSkipCompile = false  // by default, don't skip compiles

  override lazy val settings = Seq(
    commands ++= Seq(installCommand),
    (Keys.skip in compile) := shouldSkipCompile
  )

  def doCommand(configs: Configuration*)(state: State, args: Seq[String]): State = {
    shouldSkipCompile = true // start skipping compiles

    ... // do stuff that would normally trigger a compile such as running the packageBin task

    shouldSkipCompile = false // stop skipping compiles
  }
}
Run Code Online (Sandbox Code Playgroud)

我不相信这是最强大的解决方案,但它似乎适用于我需要的东西.

cte*_*ekk 0

object SbtQuickInstallPlugin extends Plugin {
  private lazy val installCommand =  Command.args("quick-install", "quick install that skips compile step")(doCommand(Configurations.Compile))

  private var shouldSkipCompile = false  // by default, don't skip compiles

  override lazy val settings = Seq(
    commands ++= Seq(installCommand),
    (Keys.skip in compile) := shouldSkipCompile
  )

  def doCommand(configs: Configuration*)(state: State, args: Seq[String]): State = {
    shouldSkipCompile = true // start skipping compiles

    ... // do stuff that would normally trigger a compile such as running the packageBin task

    shouldSkipCompile = false // stop skipping compiles
  }
}
Run Code Online (Sandbox Code Playgroud)

没问题,当然可以!