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

查看:20
本文介绍了在 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
  }
}

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

How can I avoid it?


Trial and error log

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

replace InputTask.value with InputTask.evaluated

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

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.

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


waiting for other suggestions

解决方案

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天全站免登陆