gpt4 book ai didi

scala - 根据设置重新定义 sbt 任务

转载 作者:行者123 更新时间:2023-12-02 17:50:37 27 4
gpt4 key购买 nike

我想根据设置值重新定义 sbt 任务。在默认情况下,它应该执行原始任务,在其他情况下,我想运行自定义任务。

给定任务和设置:

val someSetting = Def.settingKey[Boolean]("some setting")
val fooTask = Def.taskKey[String]("foo task")

someSetting := true

fooTask := {
println("** foo effect **")
"foo value"
}

尝试重新定义 fooTask:

fooTask := {
if( someSetting.value ) fooTask
else Def.task("whatever")
}.value

给出编译错误:

build.sbt:39: error: Illegal dynamic dependency
if( someSetting.value ) fooTask
^

使用Def.task任务编译...

fooTask := Def.task {
if( someSetting.value ) fooTask.value
else Def.task("whatever").value
}.value

...但即使 someSetting 为 false 也会执行!

> set someSetting := false
[info] Defining *:someSetting
[info] The new value will be used by *:fooTask
[info] Reapplying settings...
> fooTask
** foo effect **
[success] Total time: 1 s, completed Mar 20, 2017 11:40:13 PM

尝试使用 Def.taskDyn 代替...

fooTask := Def.taskDyn {
if( someSetting.value ) fooTask
else Def.task("whatever")
}.value

...导致“运行时引用未定义的设置”:

> set someSetting := true
[info] Defining *:someSetting
[info] The new value will be used by *:fooTask
[info] Reapplying settings...
[info] Set current project to sbt-task-games (in build file:/Users/jast/playspace/untitled8/)
> fooTask
[trace] Stack trace suppressed: run last *:fooTask for the full output.
[error] (*:fooTask) sbt.Init$RuntimeUndefined: References to undefined settings at runtime.
[error] setting(ScopedKey(Scope(Select(ProjectRef(file:/Users/jast/playspace/untitled8/,untitled8)),Global,Global,Global),fooTask)) at RangePosition(/Users/jast/playspace/untitled8/build.sbt,LineRange(43,47)) referenced from ScopedKey(Scope(Select(ProjectRef(file:/Users/jast/playspace/untitled8/,untitled8)),Global,Global,Global),fooTask)
[error] Total time: 0 s, completed Mar 20, 2017 11:49:59 PM

最佳答案

在返回执行该任务的任务之前,您需要“保存”对实际任务的引用。这就是 taskValue 的用途:

fooTask := Def.taskDyn {
val default = fooTask.taskValue
if (someSetting.value) Def.task(default.value)
else Def.task("whatever")
}.value

关于scala - 根据设置重新定义 sbt 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42915843/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com