scala - Idiomatically defining dynamic tasks in SBT 0.13? -


i'm moving sbt plugin 0.12 on 0.13. @ various points in plugin schedule dynamic set of tasks onto sbt build graph.

below old code. still idiomatic way express this, or possible leverage macros make prettier?

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 list things")     lazy val inparallel = taskkey[seq[string]]("strings 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             }          )     ) } 

apologies entirely contrived example!

edit

so, taking mark's advice account have following tidier code:

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 list things")     lazy val inparallel = taskkey[seq[string]]("strings 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         )     ) } 

this seems there, fails build with:

[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 

you can use def.taskdyn, provides new syntax flatmap. difference def.task expected return type task initialize[task[t]] instead of t. translating example,

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(",") } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -