I would like to create an sbt plugin for my project before opening it.
The project binds the Java agent to the start of the application so that it can be used for various types of profiling. The agent writes text files for further processing.
I would like to be able to write an sbt plugin that can
- There is an alternative to
run , called runWithProfiling , which starts a new Java process by adding an agent to the argument list and passing all user commands. - on exit, I then want to call some arbitrary code after processing to create an HTML report
I know how to create a new command, but I donβt know how to best implement the run alternative ... I donβt want to reinvent the wheel by copying all the code that run . Is there a way that I can call run , but make sure my parameters are passed (once) and that this is definitely a new Java process?
Also, the ability to do the same for tests would be great.
UPDATE : this is the code that I have now, but it suffers from several problems marked as TODO s
import sbt._ import Keys._ import sbt.Attributed.data object LionPlugin extends Plugin { val lion = TaskKey[Unit]("lion", "Run a main class with lions-share profiling.") override val projectSettings = Seq( fork := true, javaOptions ++= Seq( "-Xloggc:gc.log", "-XX:+PrintGCDetails", "-XX:+PrintGCDateStamps", "-XX:+PrintTenuringDistribution", "-XX:+PrintHeapAtGC" // TODO: need to get hold of the local jar file for a particular artifact // IMPL: pass the jar as the agent ), lion <<= ( runner, fullClasspath in Runtime, mainClass in Runtime, streams in Runtime ) map runLion ) // TODO: update to a task that can take parameters (eg number of repeats, profiling settings) def runLion(runner: ScalaRun, cp: Classpath, main: Option[String], streams: TaskStreams): Unit = { assert(runner.isInstanceOf[ForkRun], "didn't get a forked runner... SBT is b0rk3d") println("RUNNING with " + runner.getClass) // TODO: ask user if main is None, like 'run' does val m = main.getOrElse("Scratch") // TODO: get the user arguments val args = Nil runner.run(m, data(cp), args, streams.log) // IMPL: post-process and produce the report println("FINISHED") } }
source share