Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SBT, how do you execute a task with a different Setting[T] value at runtime?

Tags:

scala

sbt

In my project build definition the SettingKey useProguard in the Android scope is set to true. This is what I want by default. When I execute one particular task, however, I want useProguard to be false. Everything in the Android scope comes from the sbt-android-plugin.

I'm not sure how best to solve this problem. From what I read it seems like a command can get the job done, since it can execute a task with a different state than what your current session sees. I tried to create such a command like so:

def buildWithoutProguard = Command.command("build-without-proguard") { state =>
  val extracted = Project.extract(state)
  import extracted._

  val transformed = session.mergeSettings :+ (useProguard in Android := false)
  val newStructure = Load.reapply(transformed, structure)
  val newState = Project.setProject(session, newStructure, state)
  Project.evaluateTask(buildAndRun, newState)
  state
}

I'm appending the command to my project settings and running the 'build-without-proguard' command executes the buildAndRun task as desired. However, useProguard is still true instead of false as I would expect.

First, this whole approach feels heavy handed to me. Assuming changing sbt-android-plugin isn't an option here then how else would I solve this problem?

Second, why doesn't this approach work as is?

like image 583
James Avatar asked Nov 04 '22 07:11

James


1 Answers

From what I understand from your question, you want the setting to be different for a dependency depending on what is depending on it. This doesn't make sense -- a dependency either is satisfied or it isn't, and what depends on it doesn't come into the equation.

Your solution seems satisfactory to me. An alternative would be making two projects, pointing to the same source, but with different proguard settings and different target, so one would build with and the other without proguard, and both would keep their state. You'd then do whatever you want just switching the projects.

like image 95
Daniel C. Sobral Avatar answered Nov 15 '22 06:11

Daniel C. Sobral