Skip to content

Commit

Permalink
feat(updater): add Gradle support
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Benassi committed Jul 25, 2022
1 parent 7b7ed56 commit e96f58e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ By default, `commit-and-tag-version` assumes you're working in a NodeJS based pr

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!

### Gradle Support (Java/Kotlin)

If you are using Gradle, then just point to your `build.gradle` file (or `build.gradle.kts` if using Kotlin DSL).

```sh
commit-and-tag-version --packageFiles build.gradle --bumpFiles build.gradle
```


## Installing `commit-and-tag-version`

### As a local `npm run` script
Expand Down
17 changes: 17 additions & 0 deletions lib/updaters/gradle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const versionRegex = /^version\s+=\s+['"]([\d.]+)['"]/m

module.exports.readVersion = function (contents) {
const matches = versionRegex.exec(contents)
if (matches === null) {
throw new Error('Failed to read the version field in your gradle file - is it present?')
}

return matches[1]
}

module.exports.writeVersion = function (contents, version) {
return contents.replace(versionRegex, () => {
return `version = "${version}"`
})
}

6 changes: 5 additions & 1 deletion lib/updaters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const path = require('path')
const JSON_BUMP_FILES = require('../../defaults').bumpFiles
const updatersByType = {
json: require('./types/json'),
'plain-text': require('./types/plain-text')
'plain-text': require('./types/plain-text'),
gradle: require('./types/gradle')
}
const PLAIN_TEXT_BUMP_FILES = ['VERSION.txt', 'version.txt']

Expand All @@ -21,6 +22,9 @@ function getUpdaterByFilename (filename) {
if (PLAIN_TEXT_BUMP_FILES.includes(filename)) {
return getUpdaterByType('plain-text')
}
if (/build.gradle/.test(filename)) {
return getUpdaterByType('gradle')
}
throw Error(
`Unsupported file (${filename}) provided for bumping.\n Please specify the updater \`type\` or use a custom \`updater\`.`
)
Expand Down
15 changes: 15 additions & 0 deletions test/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,21 @@ describe('commit-and-tag-version', function () {
).version.should.equal('6.4.0')
})

it('bumps version in Gradle `build.gradle.kts` file', async function () {
const expected = fs.readFileSync('./test/mocks/build-6.4.0.gradle.kts', 'utf-8')
mock({
bump: 'minor',
fs: {
'build.gradle.kts': fs.readFileSync('./test/mocks/build-6.3.1.gradle.kts')
}
})
await exec({
packageFiles: [{ filename: 'build.gradle.kts', type: 'gradle' }],
bumpFiles: [{ filename: 'build.gradle.kts', type: 'gradle' }]
})
fs.readFileSync('build.gradle.kts', 'utf-8').should.equal(expected)
})

it('bumps version # in npm-shrinkwrap.json', async function () {
mock({
bump: 'minor',
Expand Down
14 changes: 14 additions & 0 deletions test/mocks/build-6.3.1.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
id("org.springframework.boot") version "2.4.6"
kotlin("jvm") version "1.4.31"
kotlin("plugin.spring") version "1.4.31"
}

version = "6.3.1"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
mavenLocal()
mavenCentral()
}

14 changes: 14 additions & 0 deletions test/mocks/build-6.4.0.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
id("org.springframework.boot") version "2.4.6"
kotlin("jvm") version "1.4.31"
kotlin("plugin.spring") version "1.4.31"
}

version = "6.4.0"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
mavenLocal()
mavenCentral()
}

0 comments on commit e96f58e

Please sign in to comment.