2015-05-08 59 views
2

我一直试图找到一种方法,以确保版本反映了当前的git指数并没有找到一种方法,这样做呢,比如:如何更新版本以反映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中的内容并请求重新加载,如果版本会在此更改中自动更新,则会很不错...

这可能吗?

+0

你使用什么版本的sbt?你可能想在0.13.8中使用'enablePlugins(GitPlugin)'和其他好东西。 –

回答

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). 
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) 

该解决方案确保了项目的版本和快照均达到最新提供非常快的'版本+快照'重新加载。

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

如果“ThisBuild中的版本”在SBT会话中未被记忆,而是作为范围的一部分被重新评估,那么会更好。现在,如果有人拥有“全球版”,情况就不会如此。

具体而言,手头的问题是需要在用户的顶级任务请求的范围内计算“版本”。

+1

好的答案!你可以添加这样一个命令(和钩子)到SbtGit本身! – jsuereth