Commit d111008a authored by Florian Schäfer's avatar Florian Schäfer

JOSM/wikipedia: Automatically determine the version number in the Gradle build

Uses different methods depending on what VCS is available, in this order:
* if git is available and the current commit has a `git-svn-id`: Use SVN revision number prepended with the letter "r" and if the working tree is not clean, appended with "-dirty"
* if git is available, but the current commit has no `git-svn-id`: Use the exact output of git-describe to get the version
* if SVN is available use the revision number from `svn info` prepended with "r"
* if all that fails, use "UNKNOWN" as version

git-svn-id: https://svn.openstreetmap.org/applications/editors/josm/plugins/wikipedia@34176 b9d5c4c9-76e1-0310-9c85-f3177eceb1e4
parent 95e84d71
import java.util.regex.Pattern
plugins {
id "java"
id "eclipse"
......@@ -83,7 +85,7 @@ dependencies {
testImplementation("org.openstreetmap.josm:josm-unittest:"){changing=true}
}
version = "34161"
version = getVersion()
josm {
josmCompileVersion = "13600"
manifest {
......@@ -93,3 +95,56 @@ josm {
oldVersionDownloadLink 12878, "33635", new URL("https://svn.openstreetmap.org/applications/editors/josm/dist/wikipedia.jar?p=33636")
}
}
/**
* @return the current version of the repo as determined by the first of these commands that returns a valid result:
* <ul>
* <li>`git log` Search for a line with a git-svn-id in the current commit (append "-dirty" if working tree differs)</li>
* <li>`git describe` Let git describe the current commit, should only fail, if this is not a git repo</li>
* <li>`svn info` take the revision number from the SVN info command</li>
* </ul>
*/
def getVersion() {
// First attempt: Check if the commit has a git-svn-id, return SVN revision
def result = getVersion("git-svn-id: .*@([1-9][0-9]*) .*", "git", "log", "-1", "--format=%b")
if (result == null) {
// Second attempt: Check if the commit can be git-described, return the description by git
result = getVersion("(.+)", "git", "describe", "--always", "--long", "--dirty")
if (result == null) {
// Third attempt: Check if we are in an SVN repo, return revision number
result = getVersion("Revision: ([1-9][0-9]*)", "svn", "info")
if (result == null) {
result = "UNKNOWN"
} else {
result = "r$result"
}
}
} else {
result = "r$result"
def dirtyProcess = new ProcessBuilder("git", "diff-index", "--quiet", "HEAD").start()
if (dirtyProcess.waitFor() != 0) {
result += "-dirty"
}
}
return result
}
/**
* Runs the specified command, matches the lines of the output with the given linePattern.
* @param linePattern the linePattern to match the lines against
* @param command the command to execute
* @return if a line matches, return the first RegEx group, else return null
*/
def getVersion(String linePattern, String... command) {
def process = new ProcessBuilder(command).directory(project.projectDir).start()
if (process.waitFor() != 0) {
return null
}
def pattern = Pattern.compile(linePattern)
return Arrays.stream(process.inputStream.text.split("\n"))
.map { pattern.matcher(it)}
.filter { it.matches() }
.map { it.group(1).trim() }
.findFirst()
.orElse(null)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment