2

我正在将 SBT 插件从0.12over 移动到0.13. 在我的插件的不同点,我将一组动态任务安排到 SBT 构建图上。

下面是我的旧代码。这仍然是表达这一点的惯用方式,还是可以利用宏使一切更漂亮?

import sbt._
import Keys._

object Toplevel extends Build
{
    lazy val ordinals = taskKey[Seq[String]]("A list of things")
    lazy val times = taskKey[Int]("Number of times to list things")
    lazy val inParallel = taskKey[Seq[String]]("Strings to log in parallel")

    lazy val Foo = Project( id="Foo", base=file("foo"),
        settings = Defaults.defaultSettings ++ Seq(
            scalaVersion    := "2.10.2",

            ordinals := Seq( "First", "Second", "Third", "Four", "Five" ),

            times := 3,

            inParallel <<= (times, ordinals, streams) flatMap
            { case (t, os, s) =>
                os.map( o => toTask( () =>
                {
                    (0 until t).map( _ => o ).mkString(",")
                } ) ).join
            }

        )
    )
}

为完全人为的例子道歉!

编辑

所以,考虑到马克的建议,我有以下更整洁的代码:

import sbt._
import Keys._

object Toplevel extends Build
{
    lazy val ordinals = taskKey[Seq[String]]("A list of things")
    lazy val times = taskKey[Int]("Number of times to list things")
    lazy val inParallel = taskKey[Seq[String]]("Strings to log in parallel")

    def parTask = Def.taskDyn
    {
        val t = times.value
        ordinals.value.map(o => ordinalTask(o, t)).join
    }

    def ordinalTask(o: String, t: Int) = Def.task
    {
        (0 until t).map(_ => o).mkString(",")
    }

    lazy val Foo = Project( id="Foo", base=file("foo"),
        settings = Defaults.defaultSettings ++ Seq(
            scalaVersion    := "2.10.2",

            ordinals := Seq( "First", "Second", "Third", "Four", "Five" ),

            times := 3,

            inParallel := parTask.value
        )
    )
}

这似乎几乎就在那里,但构建失败:

[error] /home/alex.wilson/tmp/sbt0.13/project/build.scala:13: type mismatch;
[error]  found   : sbt.Def.Initialize[Seq[sbt.Task[String]]]
[error]  required: sbt.Def.Initialize[sbt.Task[?]]
[error]         ordinals.value.map(o => ordinalTask(o, t)).join
4

1 回答 1

3

您可以使用Def.taskDyn,它为flatMap. 不同的Def.task是,预期的返回类型是一个任务Initialize[Task[T]],而不是一个T。翻译你的例子,

inParallel := parTask.value

def parTask = Def.taskDyn {
   val t = times.value
   ordinals.value.map(o => ordinalTask(o, t)).joinWith(_.join)
}

def ordinalTask(o: String, t: Int) = Def.task {
   (0 until t).map(_ => o).mkString(",")
}
于 2013-10-07T19:07:03.853 回答