Skip to content

Commit fd7c528

Browse files
committed
Adding support for format
1 parent 188be3a commit fd7c528

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A gradle plugin to calculate Android-friendly version names and codes from git t
88
Add the plugin the top of your `app/build.gradle` (or equivalent):
99
```groovy
1010
plugins {
11-
id 'com.gladed.androidgitversion' version '0.2.13'
11+
id 'com.gladed.androidgitversion' version '0.2.18'
1212
}
1313
```
1414

@@ -82,6 +82,7 @@ androidGitVersion {
8282
parts 2
8383
baseCode 2000
8484
hideBranches = [ 'develop' ]
85+
format = '%tag%%.count%%<commit>%%-branch%%...dirty%'
8586
}
8687
```
8788

@@ -128,7 +129,7 @@ Use caution when increasing this value, as the maximum version code is 214748364
128129
The default number of parts is 3.
129130

130131
### baseCode (int)
131-
`baseCode` set a floor for all generated version codes. Use this when you have already released a version with a code, and don't want to go backwards.
132+
`baseCode` sets a floor for all generated version codes. Use this when you have already released a version with a code, and don't want to go backwards.
132133

133134
The default baseCode is 0.
134135

@@ -137,6 +138,13 @@ The default baseCode is 0.
137138

138139
The default hideBranches are `[ 'master', 'release' ]`, meaning that intermediate builds will not show these branch names.
139140

141+
### format (string)
142+
`format` defines the form of the version name.
143+
144+
Parts include `tag` (the last tag), `count` (number of commits, if any, since last tag), `commit` (most recent commit prefix, if any, since the last tag), `branch` (branch name, if current branch is not in `hideBranches`), and `dirty` (inserting the word "dirty" if the build was made with uncommitted changes).
145+
146+
Parts are delimited as `%PARTNAME%`. Other characters appearing between % marks are preserved. Parts may be omitted in which case they will not appear in the version name.
147+
140148
## License
141149

142150
All code here is Copyright 2015 by Glade Diviney, and licensed under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0).

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
}
77
}
88
dependencies {
9-
classpath 'com.gradle.publish:plugin-publish-plugin:0.9.2'
9+
classpath 'com.gradle.publish:plugin-publish-plugin:0.9.5'
1010
}
1111
}
1212

@@ -31,7 +31,7 @@ task wrapper(type: Wrapper) {
3131
gradleVersion = '2.4'
3232
}
3333

34-
version = '0.2.13'
34+
version = '0.2.18'
3535
group = 'com.gladed.gradle.androidgitversion'
3636

3737
pluginBundle {

src/main/groovy/com/gladed/gradle/androidgitversion/AndroidGitVersion.groovy

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class AndroidGitVersionExtension {
6868
*/
6969
List<String> hideBranches = [ "master", "release" ]
7070

71+
/**
72+
* Format of version string.
73+
*/
74+
String format = '%tag%%-count%%-commit%%-branch%%-dirty%'
75+
7176
/** Project referenced by this plugin extension */
7277
private Project project
7378
private Results results
@@ -86,15 +91,24 @@ class AndroidGitVersionExtension {
8691
String name = results.lastVersion
8792

8893
if (name.equals("unknown")) return name
94+
name = this.format
8995

96+
def parts = [tag: results.lastVersion]
9097
if (results.revCount > 0) {
91-
name += "-${results.revCount}-${results.commitPrefix}"
98+
parts['count'] = results.revCount
99+
parts['commit'] = results.commitPrefix
92100
if (!hideBranches.contains(results.branchName)) {
93-
name += "-" + results.branchName.replaceAll("[^a-zA-Z0-9.-]", "_")
101+
parts['branch'] = results.branchName.replaceAll("[^a-zA-Z0-9.-]", "_")
94102
}
95103
}
104+
if (results.dirty) parts['dirty'] = 'dirty'
96105

97-
if (results.dirty) name += "-dirty"
106+
parts.each { part, value ->
107+
name = name.replaceAll(/%([^%]*)$part([^%]*)%/) { all, start, end ->
108+
start + value + end
109+
}
110+
}
111+
name = name.replaceAll(/%[^%]+%/,'')
98112

99113
return name
100114
}

src/test/groovy/com/gladed/gradle/androidgitversion/AndroidGitVersionTest.groovy

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,33 @@ class AndroidGitVersionTest extends GroovyTestCase {
237237
}
238238
}
239239

240+
void testFormat() {
241+
addCommit()
242+
addTag("1.0")
243+
addBranch("release/1.x")
244+
new File(projectFolder.root, "build.gradle").append("// addition 1")
245+
addCommit()
246+
new File(projectFolder.root, "build.gradle").append("// addition 2") // Dirty
247+
248+
plugin.format = '%tag%%!count%%_commit%%.branch%%+dirty%'
249+
// Should be something like '1.0!1_10a984.release_1.x+dirty'
250+
assert plugin.name().startsWith('1.0!1_')
251+
assert plugin.name().endsWith('.release_1.x+dirty')
252+
}
253+
254+
void testCleanFormat() {
255+
addCommit()
256+
addTag("1.0")
257+
addBranch("release/1.x")
258+
new File(projectFolder.root, "build.gradle").append("// addition 1")
259+
addCommit()
260+
261+
plugin.format = '%tag%%!count%%(commit)%%.branch%%+dirty%'
262+
// Should be something like '1.0!1_10a984.release_1.x'
263+
assert plugin.name().startsWith('1.0!1(')
264+
assert plugin.name().endsWith(').release_1.x')
265+
}
266+
240267
private void addCommit() {
241268
new File(projectFolder.root, "build.gradle").append("// addition")
242269
git.add().addFilepattern("build.gradle").call()

0 commit comments

Comments
 (0)