Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic Change of the version #76

Open
e-orz opened this issue Aug 14, 2018 · 3 comments
Open

Automatic Change of the version #76

e-orz opened this issue Aug 14, 2018 · 3 comments

Comments

@e-orz
Copy link

e-orz commented Aug 14, 2018

Hi,
I like your plug in very much.
My normal workflow is to open sbt and leave it open even for days (unless changing something related to the build, there is no need to reload the project)
The version setting isn't changed unless I reload the project.
It is technically understood (version is a setting not a task) but in case of a dirty files I will have to reload the project each minute and it takes more than 30 seconds (or use an outdated version).

I ended up doing:

def refreshVersion = Command.command("refreshVersion") { state =>
  val extracted = Project extract state
  import extracted._
  /* - At first I thought to manually set the version. It seems unimportant - just refresh the session
    val result: Option[(State, Result[String])] = Project.runTask(dynver, state)
    result match {
      case None => state.fail // Key wasn't defined.
      case Some((newState, Inc(inc))) => state.fail // error detail, inc is of type Incomplete, use Incomplete.show(inc.tpe) to get an error message
      case Some((newState, Value(v))) =>
        println(s"Setting the version to $v")
        appendWithoutSession(Seq(version := v), state) // do something with v: inc.Analysis
    }
  */
  state.log.info("refreshing version")
  appendWithoutSession(Seq(), state) // do something with v: inc.Analysis
}

ThisBuild / commands += refreshVersion

val dirtyEnd = """.*(-\d\d\d\d)$""".r
def stripTime(version: String) = version match {
  case dirtyEnd(time) => version.stripSuffix(time)
  case _ => version
}
ThisBuild / version ~= stripTime
ThisBuild / dynver ~= stripTime

and make compile depend on:

versionCheck := {
  val dynamicVersion = dynver.value
  val staticVersion = version.value
  if (dynamicVersion != staticVersion)
    sys.error(s"The version setting ($staticVersion) is different from the dynamic (dynver) one ($dynamicVersion). Please use the refreshVersion command to refresh the setting.")
}

This way the dynamic version only contains the day - refresh is needed only once a day.
Also, reload in our project takes more than 30 seconds. The refresh command works 2 in seconds.
But, still, the refresh should be done manually.

Do you have a better idea?

Thanks!

@dwijnand
Copy link
Member

No that looks like a nice setup.

If you'd like we can look at adding some parts of that in the code and documenting the remainder in the README. What do you think?

@e-orz
Copy link
Author

e-orz commented Aug 14, 2018

It's a good idea (I spent quite an amount of time creating this setup).
I am not a sbt expect and don't fully understand why appendWithoutSession(Seq(), state) works and refreshes the version of the state, but it works.

Also, please note I didn't use dynverAssertVersion . Our project have multiple sub-projects and setting the version of only one project didn't trigger the assert.

@dwijnand
Copy link
Member

Commands can redefine State (that's why they have a State => State function), so that appendWithoutSession triggers sbt to recompute the setting values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants