6

package从命令中运行任务时,我试图暂时跳过编译任务quick-install(在我正在编写的 sbt 插件中定义)。我可以通过将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 := 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)

    ...
}

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

编辑:

跳过设置是一个任务,因此一个简单的解决方法是:

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

我不相信这是最强大的解决方案,但它似乎可以满足我的需要。

4

1 回答 1

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

没关系,当然你可以去!

于 2013-02-07T11:42:35.507 回答