Skip to content

feat(updater): add Gradle support #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
16 changes: 16 additions & 0 deletions lib/updaters/types/gradle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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}"`
})
}
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()
}