Getting Settings and Arguments with SBTs InputTask

I've written an SBT plugin that allows SBT users to define arbitrary tasks in their project that can be run (with full classpath and dependencies loaded) from SBT itself.

The body of the functionality looks like this

  def installTask(taskName: String, taskDescription: String = "") = {
    TaskKey[Unit](taskName, taskDescription) <<= (dependencyClasspath in Runtime)
      .map(loadTask(_, "tasks", taskName.capitalize))
      .map(_.run)
  }


  private def loadTask(dependencies: Keys.Classpath, taskPackage:String, className: String) = {
    val dependenciesUrls = dependencies.map(_.data.toURI.toURL).toArray
    val classLoader = new URLClassLoader(dependenciesUrls, null)
    val taskClass = s"$taskPackage.$className"
    classLoader.loadClass(taskClass).newInstance().asInstanceOf[Runnable]
  }

So people can, in their SBT file do this

import SbtTasks._

installTask("seedDB")

And when the call sbt seedDB the tasks.SeedDB will be executed in the application context.

Now the problem - I need to do this but accept arguments. I've got as far as understanding how to accept arguments

  def installTaskWithArgs(taskName: String, taskDescription: String = "") = {
    val input = InputKey[Unit](taskName, taskDescription)

    input := {
      val args: Seq[String] = Def.spaceDelimited("<arg>").parsed
      println(args)
    }
  }

But I can't work out how to, in this context access the values of my dependencyClasspath within the InputTask

  val dc = (dependencyClasspath in Runtime)

I also tried Commands but they seemed a bit more troublesome and all content appears to promote InputTasks over Commands anyway.

So - how do I accept arguments in a task while also getting access to the dependencyClasspath?

NB: Code has been elided for brevity please excuse any obvious exclusions

Answers


I think inputTaskDyn is your friend here:

input :=
  Def.inputTaskDyn {
    val args = Def.spaceDelimited("<arg>").parsed
    val dc = (dependencyClasspath in Runtime).value
    ...
  }.evaluated

for some background and links to further reading, see https://github.com/sbt/sbt/issues/999


Need Your Help

jQuery.ajax() with window.onbeforeunload not sending data

javascript php ajax jquery

Im trying to store data of how many clicks and the amount of time a visitor spends on the site

Perl Data::Dumper numeric sort on first level key of HoH

perl

I have a HoH data structure. The outer level hash's keys are numeric - so I'd like to Dump the HoH numerically sorted by the first hash key (don't care about the order on the inner hash). I've been