将输入任务与 sbt 中的动态任务相结合 [英] combining input task with dynamic task in sbt

查看:30
本文介绍了将输入任务与 sbt 中的动态任务相结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个输入任务来处理用户输入并生成一堆子任务来运行.例子如下:

I'd like to make an input task that proceed user input and generate bunch of subtasks to run. Here is the example:

import sbt._
import Keys._
import Def.Initialize
import complete.DefaultParsers._

object TestBuild extends Build {
  val sampleInput = inputKey[Seq[String]]("sample dynamic input task")
  val sampleDynamic = taskKey[Seq[String]]("sample dynamic task")

  override lazy val settings = super.settings ++ Seq(
    sampleDynamic := Def.taskDyn {
      val sources = Seq("ab", "csd", "efda")
      sources.map(sampleTaskFor _).joinWith(_.join)
    }.value,
    sampleInput := Def.inputTaskDyn {
      val sources = spaceDelimited("<arg>").parsed
      sources.map(sampleTaskFor _).joinWith(_.join)
    }.value
  )

  private def sampleTaskFor(source : String) : Initialize[Task[String]] = Def.task {
    source + " : " + source
  }
}

有两个样本.第一个工作并显示带有预定义输入的简单 taskDyn.第二个是带有用户输入的预期动态任务,拒绝编译并出现我无法解释的错误

There are two samples. The first works and shows simple taskDyn with predefined input. The second is intended dynamic task with user input that refuses to compile with error that I can not interpret

[error] home/project/build.scala:15: Illegal dynamic reference: Def
[error]     sampleInput := Def.inputTaskDyn {
[error]                                     ^
[error] one error found
[error] (compile:compile) Compilation failed

如何避免?

我会在我的问题中附加不同的建议更改,但仍然无法解决问题

There I would append my question with different proposed changes that still can not solve the issue

      sources.map(sampleTaskFor _).joinWith(_.join)
-   }.value
+   }.evaluated
  )

如果我能够定义正确的 InputTask,它应该通过 evaluated 方法访问,正如我在尝试使用编译的不同 InputTasks 后在文档和实践中发现的那样.

If I would able to define correct InputTask it should be accessed via evaluated method, as I've found both in documentation and in practice after trying to play with different InputTasks that compiles.

仍然无法解决 sbt 宏引擎拒绝提供 inputTaskDyn 的问题.

Still that would not fix the issue that sbt macro engine refuses provided inputTaskDyn.

等待其他建议

推荐答案

试错法给了我答案,但没有一点理解.这是:

Trial and error method gave me the answer but non a single bit of understanding. Here it is:

import sbt._
import Keys._
import Def.Initialize
import complete.DefaultParsers._

object TestBuild extends Build {
  val sampleInput = inputKey[Seq[String]]("sample dynamic input task")
  val sampleDynamic = taskKey[Seq[String]]("sample dynamic task")

  override lazy val settings = super.settings ++ Seq(
    sampleDynamic := Def.taskDyn {
      val sources = Seq("ab", "csd", "efda")
      sources.map(sampleTaskFor _).joinWith(_.join)
    }.value,
    sampleInput := Def.inputTaskDyn {
      val sources = spaceDelimited("<arg>").parsed
      sampleTaskAll(sources)
    }.evaluated
  )

  private def sampleTaskFor(source : String) : Initialize[Task[String]] = Def.task {
    source + " : " + source
  }

  private def sampleTaskAll(sources : Seq[String]) : Initialize[Task[Seq[String]]] = Def.taskDyn {
    sources.map(sampleTaskFor _).joinWith(_.join)
  }
}

出于某种原因,我无法理解您应该在单独的方法中按照单个任务的顺序隔离创建多任务.

For a reason I can not comprehend you should isolate creating multitask out of sequence of single tasks in a separate method.

这篇关于将输入任务与 sbt 中的动态任务相结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆