|
1 |
| -/** |
2 |
| - Applies a consistent versioning scheme to all projects using this script |
3 |
| -
|
4 |
| -Uses git tags to mint versions by default. |
5 |
| -git tags can be of a few forms: |
6 |
| -- short sha (typical for a PR or a commit) (e.g. 38960ae) |
7 |
| -- versioned tags (typical for a release) (e.g. v0.8.45, v0.8.45.1, v0.8.45rc1, v0.8.45.1rc4) |
8 |
| -
|
9 |
| -Produces the following variables and supports token replacement |
10 |
| -- version: server version amenable for creating jars |
11 |
| -- fullVersion: full version string |
12 |
| -- cliMajorVersion: cli version amenable for binding to server as a default |
13 |
| - 0.8.44 or 0.8.44-1 (for clean tags) or 0.8.45-SNAPSHOT (for unclean repositories) |
14 |
| -
|
15 |
| - All inference can be overridden by passing in the releaseVersion property |
16 |
| - e.g. -PreleaseVersion=0.2.3.4 will set the jar version to 0.2.3-4 |
17 |
| -
|
18 |
| - **/ |
19 |
| - |
20 |
| - |
| 1 | +import groovy.json.JsonSlurper |
21 | 2 | import org.apache.tools.ant.filters.ReplaceTokens
|
22 | 3 |
|
| 4 | + |
23 | 5 | def detailedVersionString = "0.0.0-unknown-SNAPSHOT"
|
24 | 6 | def cliMajorVersion = "0.15.0" // base default cli major version
|
25 |
| -def snapshotVersion = false |
26 |
| -if (project.hasProperty("releaseVersion")) { |
27 |
| - version = releaseVersion |
28 |
| - detailedVersionString = releaseVersion |
29 |
| -} else { |
30 |
| - try { |
31 |
| - // apply this plugin in a try-catch block so that we can handle cases without .git directory |
32 |
| - apply plugin: "com.palantir.git-version" |
33 |
| - def details = versionDetails() |
34 |
| - detailedVersionString = gitVersion() |
35 |
| - version = details.lastTag |
36 |
| - version = version.startsWith("v")? version.substring(1): version |
37 |
| - def suffix = details.isCleanTag? "": "-SNAPSHOT" |
38 |
| - snapshotVersion = ! details.isCleanTag |
39 |
| - } |
40 |
| - catch (Exception e) { |
41 |
| - e.printStackTrace() |
42 |
| - // last fall back |
43 |
| - version = detailedVersionString |
44 |
| - } |
45 |
| -} |
46 | 7 |
|
47 |
| -// trim version if it is of size 4 to size 3 |
48 |
| -def versionParts = version.tokenize(".") |
49 |
| -cliMajorVersion = version |
50 |
| -if (versionParts.size() > 3) { |
51 |
| - // at-least 4 part version |
52 |
| - // we check if the 4th part is a .0 in which case we want to create a release |
53 |
| - if ((versionParts.size() == 4) && (versionParts[3] == '0')) { |
54 |
| - versionParts = versionParts[0..2] |
55 |
| - } |
56 |
| - version = versionParts[0..2].join('.') |
57 |
| - if (versionParts.size() > 3) { |
58 |
| - version = version + "-" + versionParts[3..versionParts.size()-1].join('-') |
| 8 | +def inputFile = file("${rootProject.buildDir}/version.json") |
| 9 | + |
| 10 | +task readJsonData { |
| 11 | + if (inputFile.exists()) { |
| 12 | + def jsonSlurper = new JsonSlurper() |
| 13 | + def data = jsonSlurper.parse(inputFile) |
| 14 | + |
| 15 | + detailedVersionString = data.fullVersion |
| 16 | + cliMajorVersion = data.cliMajorVersion |
| 17 | + version = data.version |
| 18 | + } else { |
| 19 | + println "git.properties JSON file not found: ${inputFile.path}" |
59 | 20 | }
|
60 | 21 | }
|
61 | 22 |
|
62 |
| -if (snapshotVersion) { |
63 |
| - if (versionParts[versionParts.size()-1].isInteger()) { |
64 |
| - def base_version = versionParts[0..versionParts.size()-2].join('.') |
65 |
| - version = base_version + '.' + (versionParts[versionParts.size()-1].toInteger()+1).toString() + "-SNAPSHOT" |
66 |
| - cliMajorVersion = base_version + "." + versionParts[versionParts.size()-1] |
67 |
| - } else { |
68 |
| - // we are unable to part the last token as an integer, so we just append SNAPSHOT to this version |
69 |
| - version = versionParts[0..versionParts.size()-1].join('.') + '-SNAPSHOT' |
70 |
| - cliMajorVersion = versionParts[0..versionParts.size()-1].join('.') |
71 |
| - } |
| 23 | +task printVersionDetails() { |
| 24 | + println("fullVersion=" + detailedVersionString) |
| 25 | + println("cliMajorVersion=" + cliMajorVersion) |
| 26 | + println("version=" + version) |
72 | 27 | }
|
73 | 28 |
|
74 | 29 | processResources {
|
75 | 30 | filter(ReplaceTokens, tokens:[fullVersion: detailedVersionString])
|
76 | 31 | filter(ReplaceTokens, tokens:[cliMajorVersion: cliMajorVersion])
|
77 | 32 | }
|
78 | 33 |
|
79 |
| -task printVersionDetails() { |
80 |
| - println("fullVersion=" + detailedVersionString) |
81 |
| - println("cliMajorVersion=" + cliMajorVersion) |
82 |
| - println("version=" + version) |
83 |
| -} |
0 commit comments