1

I tried everything I could think of but running taskB still ends up with the error message that task is not defined either in */*:taskB or in */cmd:taskB if I put it in custom configuration.

Command.command("doStuff", Help.more("doStuff", "whatever")) {
      (state: State) =>
        val e = Project.extract(state)

        val taskA = taskKey[Seq[String]]("A")
        val taskB = taskKey[Seq[File]]("B")

        val cmdConfig = config("cmd")

        val newState = e.append(
          inConfig(cmdConfig)(Seq(
              taskA := {
                // do stuff
              },
              taskB := {
                // do stuff
              }
            )
          )
          , state
        )

        val result: (State, Seq[File]) = e.runTask(taskB in cmdConfig, newState)
)

When debugging it, it seems like the task are not present in structure.data where it is searched for.

lisak
  • 21,611
  • 40
  • 152
  • 243
  • Do you need to add your tasks specifically from the command, or the point is just to have command calling the tasks? – lpiepiora Aug 14 '14 at 19:13
  • The point is having the command call them. If it doesn't crash (I comment out the e.runTask part), the tasks are then accessible from SBT, standard `*/*:taskB`. I tried various axis combinations in e.runTask(), but it never finds the task, I tried to debug it, but it is really tough down there. – lisak Aug 14 '14 at 23:18
  • https://gist.github.com/l15k4/7e553b49606312fb133f – lisak Aug 14 '14 at 23:26

1 Answers1

2

First of all, I assume you just want to call the tasks from your command, and you don't really care if they are added by modifying state in that command.

If so I'd do it more standard way, as it is done by the AutoPlugins.

import sbt._
import Keys._

object MyPlugin extends AutoPlugin {

  object autoImport {
    val taskA = taskKey[Seq[String]]("Task A")
    val taskB = taskKey[Seq[File]]("Task B")
  }

  import autoImport._

  val cmdConfig = config("cmd")

  override def projectConfigurations = Seq(cmdConfig)

  // this is optional of course, you can also enable plugin manually
  override def trigger = allRequirements

  override def projectSettings = 
    Seq(commands += doStuffCommand) ++
    inConfig(cmdConfig)(Seq(
      taskA := {
        println("TASK A")
        Seq("A", "B")
      },
      taskB := {
        println("TASK B")
        Seq(file("."))
      }
    ))


  lazy val doStuffCommand =
   Command.command("doStuff", Help.more("doStuff", "whatever")) {
     (state: State) =>
     val e = Project.extract(state)
     val (newState, bResult) = e.runTask(taskB in cmdConfig, state)
     newState
   }

}

Besides maybe you don't need a command at all, and just having some tasks calling another tasks would be simpler.

lpiepiora
  • 13,659
  • 1
  • 35
  • 47
  • lpiepiora you saved my day, I should have focused on the Plugin documentation rather than Commands documentation. I'm still wondering why the command-centric solution wasn't working. I need to use command because I'm passing a reference on a server instance from one command to another via State, so that after booting it up you can control it. Btw would you please add a comment to the `projectConfigurations` and explain what it is good for? I guess that if you want to add a setting that uses this Configuration, you have to "register" it before? – lisak Aug 15 '14 at 09:30
  • Btw how many companies in Poland is using Scala? It is still just 2 in Czech Republic, I expected the interest to be more increasing than it is. – lisak Aug 15 '14 at 09:42
  • @Sloin regarding the 1st point. Yes you have to tell sbt about your configurations. Normally you could do it on `project`, like `project.configs(YourConfiguration)`. However with AutoPlugins you can use `projectConfigurations` to add them automatically once the plugin is activated. So basically I think you can say that your assumptions are correct. Regarding the second point - I'm sorry I cannot really elaborate on this fact, as I don't know. We're not using it at the moment at the company I'm working in, and it's more of a hobby of mine, as I like the language for its expressiveness. – lpiepiora Aug 15 '14 at 12:14
  • please would you know how to access build definition project classpath in this command? If somebody uses this plugin/command, then `runTask(fullClasspath in Compile, state)` or `runTask(fullClasspath in Runtime, state)` and any other `Keys.classpath` returns only classpath without build definition project dependencies such as plugins like this one. So that I can't access my own plugin classpath at the moment somebody runs the command. – lisak Aug 19 '14 at 09:11