Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call sourceGenerators manually in sbt

Tags:

scala

sbt

I am using sourceGenerators in Compile to generate some Scala source files to target\scala-2.10\src_managed. When I run sbt compilethe sources are generated and compiled along with the regular code under src\main\scala.

But what if I want to generate the sources separately without compiling? What I am looking for is this flow:

  1. call a task to generate source code
  2. use the generated sources for IDE assistance in my regular sources
  3. compile everything

How can this be accomplished?

like image 412
reikje Avatar asked Apr 04 '14 10:04

reikje


1 Answers

Update

If I got you correct now, you want to be able to call the source generators seperately. For that you can simply define a custom task like this somewhere in your /build.sbt or /project/Project.scala file:

val generateSources = taskKey[List[File]]("generate sources")

generateSources <<= 
  (sourceGenerators in Compile) { _.join.map(_.flatten.toList) }

you can then call the generator manually from the sbt console like this:

> generateSources
[success] Total time: 0 s, completed 07.04.2014 13:42:41

Side Note: I would however reccomend to setup your IDE to generate the sources if the only thing you need them for is to get IDE support.


Old answer for future reference

You don't need to do anything special to use a generated class or object from a non-generated class or object.

In your /build.sbt or /project/Project.scala file you define the source generator:

sourceGenerators in Compile <+= sourceManaged in Compile map { dir =>
  val file = dir / "A.scala"
  IO.write(file, "class A(val name: String)")
  Seq(file)
}

Then you write some code which creates an instance of class A in/src/main/scala/B.scala:

object B extends App {
  val a = new A("It works")
  println(a.name)
}

If you compile this code from sbt, it will consider both generated and non-generated code at compilation:

> run
[info] Compiling 2 scala sources to <...>
[info] Running B
It works
[success] Total time: 0 s, completed 07.04.2014 13:15:47
like image 79
Martin Ring Avatar answered Sep 19 '22 13:09

Martin Ring