Skip to content

Commit e96f58e

Browse files
author
Alex Benassi
committed
feat(updater): add Gradle support
1 parent 7b7ed56 commit e96f58e

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ By default, `commit-and-tag-version` assumes you're working in a NodeJS based pr
4343

4444
That said, if you find your self asking [How can I use commit-and-tag-version for additional metadata files, languages or version files?](#can-i-use-commit-and-tag-version-for-additional-metadata-files-languages-or-version-files) – these configuration options will help!
4545

46+
### Gradle Support (Java/Kotlin)
47+
48+
If you are using Gradle, then just point to your `build.gradle` file (or `build.gradle.kts` if using Kotlin DSL).
49+
50+
```sh
51+
commit-and-tag-version --packageFiles build.gradle --bumpFiles build.gradle
52+
```
53+
54+
4655
## Installing `commit-and-tag-version`
4756

4857
### As a local `npm run` script

lib/updaters/gradle.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const versionRegex = /^version\s+=\s+['"]([\d.]+)['"]/m
2+
3+
module.exports.readVersion = function (contents) {
4+
const matches = versionRegex.exec(contents)
5+
if (matches === null) {
6+
throw new Error('Failed to read the version field in your gradle file - is it present?')
7+
}
8+
9+
return matches[1]
10+
}
11+
12+
module.exports.writeVersion = function (contents, version) {
13+
return contents.replace(versionRegex, () => {
14+
return `version = "${version}"`
15+
})
16+
}
17+

lib/updaters/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const path = require('path')
22
const JSON_BUMP_FILES = require('../../defaults').bumpFiles
33
const updatersByType = {
44
json: require('./types/json'),
5-
'plain-text': require('./types/plain-text')
5+
'plain-text': require('./types/plain-text'),
6+
gradle: require('./types/gradle')
67
}
78
const PLAIN_TEXT_BUMP_FILES = ['VERSION.txt', 'version.txt']
89

@@ -21,6 +22,9 @@ function getUpdaterByFilename (filename) {
2122
if (PLAIN_TEXT_BUMP_FILES.includes(filename)) {
2223
return getUpdaterByType('plain-text')
2324
}
25+
if (/build.gradle/.test(filename)) {
26+
return getUpdaterByType('gradle')
27+
}
2428
throw Error(
2529
`Unsupported file (${filename}) provided for bumping.\n Please specify the updater \`type\` or use a custom \`updater\`.`
2630
)

test/core.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,21 @@ describe('commit-and-tag-version', function () {
678678
).version.should.equal('6.4.0')
679679
})
680680

681+
it('bumps version in Gradle `build.gradle.kts` file', async function () {
682+
const expected = fs.readFileSync('./test/mocks/build-6.4.0.gradle.kts', 'utf-8')
683+
mock({
684+
bump: 'minor',
685+
fs: {
686+
'build.gradle.kts': fs.readFileSync('./test/mocks/build-6.3.1.gradle.kts')
687+
}
688+
})
689+
await exec({
690+
packageFiles: [{ filename: 'build.gradle.kts', type: 'gradle' }],
691+
bumpFiles: [{ filename: 'build.gradle.kts', type: 'gradle' }]
692+
})
693+
fs.readFileSync('build.gradle.kts', 'utf-8').should.equal(expected)
694+
})
695+
681696
it('bumps version # in npm-shrinkwrap.json', async function () {
682697
mock({
683698
bump: 'minor',

test/mocks/build-6.3.1.gradle.kts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plugins {
2+
id("org.springframework.boot") version "2.4.6"
3+
kotlin("jvm") version "1.4.31"
4+
kotlin("plugin.spring") version "1.4.31"
5+
}
6+
7+
version = "6.3.1"
8+
java.sourceCompatibility = JavaVersion.VERSION_1_8
9+
10+
repositories {
11+
mavenLocal()
12+
mavenCentral()
13+
}
14+

test/mocks/build-6.4.0.gradle.kts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plugins {
2+
id("org.springframework.boot") version "2.4.6"
3+
kotlin("jvm") version "1.4.31"
4+
kotlin("plugin.spring") version "1.4.31"
5+
}
6+
7+
version = "6.4.0"
8+
java.sourceCompatibility = JavaVersion.VERSION_1_8
9+
10+
repositories {
11+
mavenLocal()
12+
mavenCentral()
13+
}
14+

0 commit comments

Comments
 (0)