35

我想要一个命令publish-snapshot来运行publish具有修改version设置的任务(该设置将在执行命令时计算)。

我想出了如何获取version内部命令的当前值,并且Project.runTask("task", "scope", ...)似乎是调用publish任务的正确调用。

我唯一感到困惑的是如何State使用新版本值修改实例。我所有的尝试似乎都对原始版本设置没有任何作用。

我的最后一次尝试:

val printVers = TaskKey[Unit]("printvers")
val printVersTask = TaskKey[Unit]("printvers") <<= {version map println}

def publishSnapshot = Command.command("publish-snapshot") { state =>
  val newState = SessionSettings.reapply(state.get(sessionSettings).get.appendRaw(version := "???"), state)
  Project.runTask(printVers in Compile, newState, true)

  state
}

lazy val root = Project("main", file("."),
                        settings =
                          Defaults.defaultSettings ++
                          Seq(printVersTask)).settings(commands += publishSnapshot)

有什么方法可以解决这种行为吗?

4

3 回答 3

21

sbt邮件列表的帮助下,我能够创建如下解决方案:

def publishSnapshot = Command.command("publish-snapshot") { state =>
  val extracted = Project extract state
  import extracted._
  val eVersion = getOpt(version).get // getting current version
  runTask(publish in Compile,
    append(Seq(version := "newVersion"), state),
    true
  )
  state
}
于 2013-01-11T09:02:49.293 回答
12

这实际上对我不起作用。我正在使用 SBT 0.13.7

将我必须做的事情适应上面的例子,我必须做类似的事情:

def publishSnapshot = Command.command("publish-snapshot") { state =>
  val extracted = Project extract state
  val newState = extracted.append(Seq(version := "newVersion"), state)
  val (s, _) = Project.extract(newState).runTask(publish in Compile, newState)
  s
}

或者做:

def publishSnapshot = Command.command("publish-snapshot") { state =>
  val newState =
    Command.process("""set version := "newVersion" """, state)
  val (s, _) = Project.extract(newState).runTask(publish in Compile, newState)
  s
}
于 2015-04-13T03:32:14.257 回答
5

要从命令更新任意设置,请执行以下操作:

def updateFoo = Command.command("updateFoo") { state =>
 val extracted = Project extract state
 import extracted._
 println("'foo' set to true")
 //append returns state with updated Foo
 append(Seq(foo := true), state)
}
于 2015-05-06T19:49:43.040 回答