如何更新版本以反映git的索引更改?

时间:2015-05-08 16:21:23

标签: git sbt

我一直试图找到一种方法来确保版本反映当前git索引的版本,并且尚未找到方法这样做,例如:

lazy val root = (project in file(".")).enablePlugins(GitPlugin)

root.settings(
  isSnapshot in ThisBuild <<= git.gitUncommittedChanges in ThisBuild,
  version in ThisBuild <<= (git.gitDescribedVersion in ThisBuild, isSnapshot in ThisBuild) { (described, isSnapshot) =>
    described.map(tag => if (isSnapshot) s"$tag-SNAPSHOT" else tag).get
  },
  shellPrompt := { state => {
    val (describe, snapshot) = GitKeys.gitReader.value.withGit(git => (git.describedVersion.get, git.hasUncommittedChanges))
    val newVersion = s"$describe${if (snapshot) "-SNAPSHOT" else ""}"
    if (newVersion != version.value) {
      s"${scala.Console.RED}*** Version out of date ($newVersion vs ${version.value}), reload.\n${scala.Console.RESET}> "
    } else "> "
  }}
)

虽然这会警告用户该版本是否不再反映Git中的内容并请求重新加载,如果该版本会在此更改时自动更新,那将会很好...

这可能吗?

2 个答案:

答案 0 :(得分:2)

这不是我所要求的。经过大量工作后,这似乎可以解决问题(即使它需要用户输入命令):

val checkVersion = taskKey[Unit]("check-version")

def createVersion(git: GitReadonlyInterface) : (String, Boolean) = {
  val (tag, snapshot) = (git.describedVersion.get, git.hasUncommittedChanges)
  (s"$tag${if (snapshot) "-SNAPSHOT" else ""}", snapshot)
}

def updateVersion = Command.command("update-version") { state =>
  val extracted = Project.extract(state)
  val (newVersion, snapshot) = extracted.get(GitKeys.gitReader).withGit(createVersion)
  extracted.append(Seq(version := newVersion, isSnapshot := snapshot), state)
}

commands += updateVersion
version <<= (GitKeys.gitReader) { _.withGit(createVersion)._1 }
isSnapshot <<= (GitKeys.gitReader) { _.withGit(createVersion)._2 }
checkVersion <<= (GitKeys.gitReader, version, isSnapshot).map { (git, version, isSnapshot) =>
   val (newVersion, snapshot) = git.withGit(createVersion)
   if (version != newVersion) {
      sys.error("Version out of date, please run 'update-version'")
   }
}
update <<= update.dependsOn(checkVersion)
compile <<= compile.dependsOn(checkVersion)

此解决方案可确保项目的版本和快照是最新的,而仅提供非常快的版本+快照&# 39;重装。

如果你有大型项目,重新加载可能需要一段时间,这是非常有效的。

如果&#34;版本在ThisBuild&#34;中会更好。在整个SBT会议期间没有被记忆,而是作为范围的一部分重新评估。现在,如果某人在Global&#34;中有&#34;版本,则情况并非如此。

具体来说,手头的问题是&#34;版本&#34;需要在用户的顶级任务请求的范围内计算。

答案 1 :(得分:1)

version是一个设置(在sbt加载时修复),因此您必须reload来刷新设置的值。

> inspect version
[info] Setting: java.lang.String = 1.0.1-SNAPSHOT
[info] Description:
[info]  The version/revision of the current module.
...
> help reload
reload

    (Re)loads the project in the current directory.

reload plugins

    (Re)loads the plugins project (under project directory).

reload return

    (Re)loads the root project (and leaves the plugins project).