diff options
author | Paul Phillips <paulp@improving.org> | 2011-01-27 23:26:19 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-01-27 23:26:19 +0000 |
commit | 4bc1fae32f8e1ae640e85ed15a577c557e18e385 (patch) | |
tree | db254d3d71020d9b999d33360974a2259b58867d /project/build/ForkSBT.scala | |
parent | 1c537ba1b3b68007e613a6c90d27cdae3f262c77 (diff) | |
download | scala-4bc1fae32f8e1ae640e85ed15a577c557e18e385.tar.gz scala-4bc1fae32f8e1ae640e85ed15a577c557e18e385.tar.bz2 scala-4bc1fae32f8e1ae640e85ed15a577c557e18e385.zip |
A bunch of work on the sbt build. No review.
Diffstat (limited to 'project/build/ForkSBT.scala')
-rw-r--r-- | project/build/ForkSBT.scala | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/project/build/ForkSBT.scala b/project/build/ForkSBT.scala new file mode 100644 index 0000000000..986c2166dc --- /dev/null +++ b/project/build/ForkSBT.scala @@ -0,0 +1,49 @@ +/** Scala SBT build + * Copyright 2005-2010 LAMP/EPFL + * @author Paul Phillips + */ + +import sbt._ + +/** Worked out a way to fork sbt tasks, preserving all sbt command line + * options and without hardcoding anything. + */ +trait ForkSBT { + self: BasicLayer => + + def jvmArguments: List[String] = { + import scala.collection.jcl.Conversions._ + import java.lang.management.ManagementFactory + ManagementFactory.getRuntimeMXBean().getInputArguments().toList + } + + private var extraJVMArgs: List[String] = Nil + def withJVMArgs[T](args: String*)(body: => T): T = { + val saved = extraJVMArgs + extraJVMArgs = args.toList + try { body } + finally extraJVMArgs = saved + } + + // Set a property in forked sbts to inhibit possible forking cycles. + def markForked = "-D" + forkProperty + "=true" + + /** Forks a new process to run "sbt task task ...": + */ + def forkTasks(tasks: String*): Boolean = { + require (!isForked, "Tried to fork but sbt is already forked: " + tasks.mkString(" ")) + + val sbtJar = System.getProperty("java.class.path") + val sbtMain = "xsbt.boot.Boot" // ok, much of anything. + val args = jvmArguments ++ Seq(markForked, "-classpath", sbtJar, sbtMain) ++ tasks + + log.info("Forking: " + args.mkString("java ", " ", "")) + Fork.java(None, args, StdoutOutput) == 0 + } + def maybeFork(task: TaskManager#Task): Option[String] = maybeFork(task, "Error during external compilation.") + def maybeFork(task: TaskManager#Task, errorMsg: String): Option[String] = { + if (isForked) task.run + else if (forkTasks("project " + this.name, task.name)) None + else Some(errorMsg) + } +} |