2015-05-08 98 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