How to make a SBT task depend on a module defined in the same SBT project?

ぐ巨炮叔叔 提交于 2019-12-22 18:36:21

问题


I have module A and module B in a multi-module SBT project. I want to write a resource generator task for module B that invokes code from module A. One way to do this is to pull all the code from module A under project/ but that is unfeasible as module A is massive and I would like to keep it where it is (see https://stackoverflow.com/a/47323703/471136). How do I do this in SBT?

Secondly, is it possible to get rid of module B altogether i.e. I want the resource generator task for module A actually invoke code from module A but module A is a root module and does not live in SBT's project?

Note: This question is not a duplicate of this: Defining sbt task that invokes method from project code? since that one resolves to moving the code into SBT's project which is specifically something I am seeking to avoid here.


回答1:


I think I'm doing something like your first part in the following project: My module gen is the equivalent of your module A, and my module core is the equivalent of your module B. Without testing, the structure is roughly as follows:

// taking inspiration from
// http://stackoverflow.com/questions/11509843/
lazy val ugenGenerator = TaskKey[Seq[File]]("ugen-generate", "Generate UGen class files")

lazy val gen = Project(id = "gen", base = file("gen")) ...

lazy val core = Project(id = "core", base = file("core"))
  .settings(
    sourceGenerators in Compile <+= ugenGenerator in Compile,
    ugenGenerator in Compile := {
      val src   = (sourceManaged       in Compile        ).value
      val cp    = (dependencyClasspath in Runtime in gen ).value
      val st    = streams.value
      runUGenGenerator(description.value, outputDir = src, 
        cp = cp.files, log = st.log)
    }
  )

def runUGenGenerator(name: String, outputDir: File, cp: Seq[File],
                    log: Logger): Seq[File] = {
  val mainClass   = "my.class.from.Gen"
  val tmp         = java.io.File.createTempFile("sources", ".txt")
  val os          = new java.io.FileOutputStream(tmp)

  log.info(s"Generating UGen source code in $outputDir for $name")

  try {
    val outs  = CustomOutput(os)
    val fOpt  = ForkOptions(javaHome = None, outputStrategy = Some(outs), bootJars = cp,
        workingDirectory = None, connectInput = false)
    val res: Int = Fork.scala(config = fOpt,
      arguments = mainClass :: "-d" :: outputDir.getAbsolutePath :: Nil)

    if (res != 0) {
      sys.error(s"UGen class file generator failed with exit code $res")
    }
  } finally {
    os.close()
  }
  val sources = scala.io.Source.fromFile(tmp).getLines().map(file).toList
  tmp.delete()
  sources
}

This works in sbt 0.13, but I haven't had time to figure out why it doesn't work in 1.x.

By the way, how do I write sourceGenerators in Compile <+= ugenGenerator in Compile without deprecated syntax?


回答2:


Based on @0__ answer above, here is my simplified version:

  /**
    * Util to run the main of a sub-module from within SBT
    *
    * @param cmd The cmd to run with the main class with args (if any)
    * @param module The sub-module
    */
  def runModuleMain(cmd: String, module: Reference) = Def.task {
    val log = streams.value.log
    log.info(s"Running $cmd ...")
    val classPath = (fullClasspath in Runtime in module).value.files
    val opt = ForkOptions(bootJars = classPath, outputStrategy = Some(LoggedOutput(log)))
    val res = Fork.scala(config = opt, arguments = cmd.split(' '))
    require(res == 0, s"$cmd exited with code $res")
  }

You can then invoke it as:

resourceGenerators in Compile += Def.taskDyn {
  val dest = (resourceManaged in Compile).value
  IO.createDirectory(dest)
  runModuleMain(
    cmd = "com.mycompany.foo.ResourceGen $dest $arg2 $arg3 ...",
    module = $referenceToModule // submodule containing com.mycompany.foo.ResourceGen
  ).taskValue
  dest.listFiles()
}

There is one more way to do this:

resourceGenerators in Compile += Def.taskDyn {
  val dest = (resourceManaged in Compile).value
  IO.createDirectory(dest)      
  Def.task {
    val cmd = "com.mycompany.foo.ResourceGen $dest $arg2 $arg3 ..."        
    (runMain in Compile).toTask(" " + $cmd).value 
    dest.listFiles()
  }
}.taskValue


来源:https://stackoverflow.com/questions/47418859/how-to-make-a-sbt-task-depend-on-a-module-defined-in-the-same-sbt-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!