1

I'm using the plugin sbt-assembly (version 0.13.0), and I would like to call assemblyPackageDependency with or without appendContentHash depending on some InputKey

Basically, I would like to do something like this:

lazy val isGlobalCached = InputKey[Boolean]("")

lazy val myTask = taskKey[sbt.File]("myTask")

myTask := {
    if (isGlobalCached.value)
        //run with the modified assemblyOption
        //assemblyOption in assemblyPackageDependency ~= { x => x.copy(appendContentHash = true) }
        assemblyPackageDependency.value
    else
        assemblyPackageDependency.value
}

but I can't figure out how to set the assemblyOption only if the condition is true, and not globally


Here are a couple of things I tried that didn't work:

lazy val isGlobalCached = InputKey[Boolean]("")

lazy val myTask = taskKey[sbt.File]("myTask")

lazy val assemblyPackageDependencyWithHash = taskKey[sbt.File]("assemblyPackageDependencyWithHash")


assemblyPackageDependencyWithHash <<= assemblyPackageDependency
assemblyOption in assemblyPackageDependencyWithHash ~= { x => x.copy(appendContentHash = true) }

myTask := {
    //run with the modified assemblyOption
    if (isGlobalCached.value)
        assemblyPackageDependencyWithHash.value
    else
        assemblyPackageDependency.value
}

and:

lazy val isGlobalCached = InputKey[Boolean]("")

lazy val myTask = taskKey[sbt.File]("myTask")

lazy val globalCacheConf = config("globalCacheConf")

assemblyOption  in globalCacheConf := (assemblyOption in assemblyPackageDependency).value.copy(appendContentHash = true)

myTask := {
    //run with the modified assemblyOption
    if (isGlobalCached.value)
        assemblyPackageDependency.in(globalCacheConf).value
    else
        assemblyPackageDependency.value
}
lev
  • 3,986
  • 4
  • 33
  • 46

1 Answers1

1
import complete.DefaultParsers._

val myCmd : Command = Command("myCmd")(_ => Space ~> Bool) { (state, globalCached) =>
  val extracted = Project extract state
  import extracted._
  Project.runTask(
    assemblyPackageDependency,
    append(Seq(assemblyOption in assemblyPackageDependency := globalCached), state)
  )
  state
}

commands += myCmd
thirstycrow
  • 2,806
  • 15
  • 18
  • thanks, command might be the way to do it. I'm using this logic as part of another task. Can I execute the command, to get the result of `assemblyPackageDependency` ? – lev Sep 10 '15 at 07:26
  • The runTask function returns an Option[(State, Result[T])], from which you can get the result of the task. – thirstycrow Sep 10 '15 at 07:35
  • Command, unlike task, do not return a value as result. So, if you want to do something with the result of a task, (print the result, for example) you should: 1) do it in the command, after the runTask function call. or 2) define a new task, do it in the new task, and run the new task in the command. – thirstycrow Sep 10 '15 at 08:33