Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change setting inside SBT command?

Tags:

scala

sbt

I want to have a command publish-snapshot that would run the publish task with modified version setting (that setting is to be computed at the time of execution of the command).

I figured out how to get the current value of the version inside command, and Project.runTask("task", "scope", ...) seems to be a right call for invoking the publish task.

The only thing that I'm confused with is how to modify the State instance with a new version value. All my attempts seem to do nothing to the original version setting.

My last attempt:

val printVers = TaskKey[Unit]("printvers") val printVersTask = TaskKey[Unit]("printvers") <<= {version map println}  def publishSnapshot = Command.command("publish-snapshot") { state =>   val newState = SessionSettings.reapply(state.get(sessionSettings).get.appendRaw(version := "???"), state)   Project.runTask(printVers in Compile, newState, true)    state }  lazy val root = Project("main", file("."),                         settings =                           Defaults.defaultSettings ++                           Seq(printVersTask)).settings(commands += publishSnapshot) 

Is there some way to fix that behavior?

like image 337
Rogach Avatar asked Jan 10 '13 16:01

Rogach


People also ask

What are sbt settings?

A setting is something which can take the values stored at other Keys in the build state, and generates a new value for a particular build key. sbt converts all registered Setting[_] objects into a giant linear sequence and compiles them into a task graph. This task graph is then used to execute your build.

How do I update sbt dependencies?

I've used the sbt-updates plugin for this purpose locally for many years—simply add it to your local sbt user configuration and then run sbt dependencyUpdates in your project directory, and you'll get a list of dependencies that have updates in Maven Central (or whatever other repositories you have configured for that ...


2 Answers

With the help from sbt mailing list, I was able to create a solution as follows:

def publishSnapshot = Command.command("publish-snapshot") { state =>   val extracted = Project extract state   import extracted._   val eVersion = getOpt(version).get // getting current version   runTask(publish in Compile,     append(Seq(version := "newVersion"), state),     true   )   state } 
like image 109
Rogach Avatar answered Oct 06 '22 08:10

Rogach


This actually did not work for me. I'm using SBT 0.13.7

Adapting what I had to do to the above example, I had to do something like:

def publishSnapshot = Command.command("publish-snapshot") { state =>   val extracted = Project extract state   val newState = extracted.append(Seq(version := "newVersion"), state)   val (s, _) = Project.extract(newState).runTask(publish in Compile, newState)   s } 

Or alternatively do:

def publishSnapshot = Command.command("publish-snapshot") { state =>   val newState =     Command.process("""set version := "newVersion" """, state)   val (s, _) = Project.extract(newState).runTask(publish in Compile, newState)   s } 
like image 27
Samuel Heaney Avatar answered Oct 06 '22 10:10

Samuel Heaney