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

时间:2012-04-18 13:30:33

标签: sbt

我正在尝试从package命令(在我正在编写的sbt插件中定义)中运行quick-install任务时暂时跳过编译任务。我可以通过在skip任务上设置compile设置来跳过所有编译,但这会导致跳过所有compile个任务:

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

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

我尝试过设置转换(基于https://github.com/harrah/xsbt/wiki/Advanced-Command-Example),应该用skip := false替换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)

    ...
}

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

编辑:

跳过设置是一个任务,所以很容易解决:

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
  }
}

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

1 个答案:

答案 0 :(得分: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
  }
}

很好,当然你可以选择那个!