Changeset 34176 in osm


Ignore:
Timestamp:
2018-04-28T22:27:20+02:00 (6 years ago)
Author:
floscher
Message:

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
File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wikipedia/build.gradle

    r34174 r34176  
     1import java.util.regex.Pattern
     2
    13plugins {
    24  id "java"
     
    8486}
    8587
    86 version = "34161"
     88version = getVersion()
    8789josm {
    8890  josmCompileVersion = "13600"
     
    9496  }
    9597}
     98
     99/**
     100 * @return the current version of the repo as determined by the first of these commands that returns a valid result:
     101 *   <ul>
     102 *     <li>`git log` Search for a line with a git-svn-id in the current commit (append "-dirty" if working tree differs)</li>
     103 *     <li>`git describe` Let git describe the current commit, should only fail, if this is not a git repo</li>
     104 *     <li>`svn info` take the revision number from the SVN info command</li>
     105 *   </ul>
     106 */
     107def getVersion() {
     108  // First attempt: Check if the commit has a git-svn-id, return SVN revision
     109  def result = getVersion("git-svn-id: .*@([1-9][0-9]*) .*", "git", "log", "-1", "--format=%b")
     110  if (result == null) {
     111    // Second attempt: Check if the commit can be git-described, return the description by git
     112    result = getVersion("(.+)", "git", "describe", "--always", "--long", "--dirty")
     113    if (result == null) {
     114      // Third attempt: Check if we are in an SVN repo, return revision number
     115      result = getVersion("Revision: ([1-9][0-9]*)", "svn", "info")
     116      if (result == null) {
     117        result = "UNKNOWN"
     118      } else {
     119        result = "r$result"
     120      }
     121    }
     122  } else {
     123    result = "r$result"
     124    def dirtyProcess = new ProcessBuilder("git", "diff-index", "--quiet", "HEAD").start()
     125    if (dirtyProcess.waitFor() != 0) {
     126      result += "-dirty"
     127    }
     128  }
     129  return result
     130}
     131
     132/**
     133 * Runs the specified command, matches the lines of the output with the given linePattern.
     134 * @param linePattern the linePattern to match the lines against
     135 * @param command the command to execute
     136 * @return if a line matches, return the first RegEx group, else return null
     137 */
     138def getVersion(String linePattern, String... command) {
     139  def process = new ProcessBuilder(command).directory(project.projectDir).start()
     140  if (process.waitFor() != 0) {
     141    return null
     142  }
     143  def pattern = Pattern.compile(linePattern)
     144  return Arrays.stream(process.inputStream.text.split("\n"))
     145    .map { pattern.matcher(it)}
     146    .filter { it.matches() }
     147    .map { it.group(1).trim() }
     148    .findFirst()
     149    .orElse(null)
     150}
Note: See TracChangeset for help on using the changeset viewer.