Not Sleeping
Not Sleeping

Reputation: 1167

SBT: How to refer to other project source code in build.sbt?

I have a Scala.js project and want to use the SBT sourceGenerators setting to generate some code at compile time. In the SBT file below, everything works if I hardcode the string in the sourceGenerators section or if I refer to Scala code defined in the "project" directory.

What I want to do however is have my build.sbt file refer to source code in a completely separate "generator" project. I have experimented with various methods of depending on a RootProject(file("path-to-generator-project")), but nothing seems to work. Can anyone help me figure out how to have build.sbt code refer to classes built from source code in another project?

EDIT: Editing to add an example of what I'm trying to do.

If my folder structure looks like this:

MyScalaJSProject
  build.sbt
  client
  server
  shared
  generator
    src/main/scala/Generator.scala
    (object with a generate: String method)
    build.sbt (generator is its own root level project)

With that structure, I want my top level build.sbt to be able to refer to Generator.generate to produce the Strings used for the sourceGenerators. But if I call Generator.generate in the build.sbt below, I get a build error as my top level project build.sbt does not know about the generator project.

import sbt.Project.projectToRef

lazy val clients = Seq(client)

lazy val server = (project in file("server")).settings(
  name := "ServerProject",
  scalaVersion := "2.11.8",
  scalaJSProjects := clients,
  pipelineStages := Seq(scalaJSProd, gzip),
  resolvers += ...some resolvers...,
  libraryDependencies ++= Seq(...some dependencies...),
  sourceGenerators in Compile += Def.task {
    val file = (sourceManaged in Compile).value / "demo" / "Test.scala"
    IO.write(file, Generator.generate)  <-- build error here
    Seq(file)
  }.taskValue
).enablePlugins(PlayScala).
  aggregate(clients.map(projectToRef): _*).
  dependsOn(sharedJvm)

lazy val client = (project in file("client")).settings(
  name := "ClientProject",
  scalaVersion := "2.11.8",
  persistLauncher := true,
  persistLauncher in Test := false,
  libraryDependencies ++= Seq(...client dependencies...),
  addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
).enablePlugins(ScalaJSPlugin, ScalaJSPlay).
  dependsOn(sharedJs)

lazy val shared = (crossProject.crossType(CrossType.Pure) in file("shared")).
  settings(
    name := "SharedProject",
    scalaVersion := scalaV,
    libraryDependencies ++= Seq(...shared dependencies...)
  ).
  jsConfigure(_ enablePlugins ScalaJSPlay)

lazy val sharedJvm = shared.jvm
lazy val sharedJs = shared.js

// loads the Play project at sbt startup
onLoad in Global := (Command.process("project server", _: State)) compose (onLoad in Global).value

// for Eclipse users
EclipseKeys.skipParents in ThisBuild := false
// Compile the project before generating Eclipse files, so that generated .scala or .class files for views and routes are present
EclipseKeys.preTasks := Seq(compile in (server, Compile))

Upvotes: 3

Views: 2645

Answers (1)

Olivier Samyn
Olivier Samyn

Reputation: 1115

As you want to use the generator directly in the sbt file, your Generator class is part of your build; and as such should be defined in the project folder. For more info, see http://www.scala-sbt.org/0.13/docs/Organizing-Build.html

I quickly wrote a simple example with a simple generator defined in the project folder.

build.sbt

name := "ServerProject"

scalaVersion := "2.11.8"

sourceGenerators in Compile += Def.task {
    val file = (sourceManaged in Compile).value / "demo" / "Test.scala"
    IO.write(file, Generator.generate)
    Seq(file)
}.taskValue

project/Generator.scala

object Generator {
  def generate = """
    package demo

    object Test{
      def main(args: Array[String]): Unit = {
        println("Hello, world!")
      }
    }
    """
}

Doing a sbt run will print "Hello, world!" as expected.

If your generator is more complex; you have two options:

First: extract it as an sbt AutoPlugin; and make your build depend on the plugin. (See here.) It's quite easy to do and you can reuse the code in other projects.

Second: add a build.sbt file in the project folder that will make your main build depend on the generator project. Using the same example; here is the folder structure to create:

build.sbt

Same as above

generator/src/main/scala/Generator.scala

Same as above project/Generator.scala (I just moved the file)

project/build.sbt

lazy val generator=RootProject(file("../generator"))

lazy val root=(project in file(".")).dependsOn(generator)

Upvotes: 6

Related Questions