Skip to content

Commit c361892

Browse files
authored
feat: add version file support for java .properties file format (#283)
supersedes #275
2 parents 3e7bb68 + cad3f09 commit c361892

8 files changed

Lines changed: 231 additions & 10 deletions

File tree

.github/workflows/test.yml

Lines changed: 132 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,133 @@ jobs:
753753
EXPECTED_VERSION: "6.5.0"
754754
EXPECTED_VERSION_PATH: "package.version"
755755

756+
test-properties:
757+
runs-on: ubuntu-latest
758+
steps:
759+
- name: Checkout code
760+
uses: actions/checkout@v5
761+
with:
762+
path: "./"
763+
764+
- run: npm ci --prod
765+
- run: npm run build
766+
767+
- run: "git config --global user.email 'changelog@github.com'"
768+
- run: "git config --global user.name 'Awesome Github action'"
769+
- run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"
770+
771+
- name: Generate changelog
772+
uses: ./
773+
env:
774+
ENV: "dont-use-git"
775+
EXPECTED_TAG: "v2.1.0"
776+
with:
777+
github-token: ${{ secrets.github_token }}
778+
version-file: "test-file.properties"
779+
780+
- name: Generate changelog
781+
uses: ./
782+
env:
783+
ENV: "dont-use-git"
784+
EXPECTED_TAG: "v3.1.0"
785+
with:
786+
github-token: ${{ secrets.github_token }}
787+
version-file: "test-file.properties"
788+
version-path: "colon_version"
789+
790+
- name: Generate changelog
791+
uses: ./
792+
env:
793+
ENV: "dont-use-git"
794+
EXPECTED_TAG: "v0.1.0"
795+
with:
796+
github-token: ${{ secrets.github_token }}
797+
version-file: "test-file.properties"
798+
version-path: "foobar_version"
799+
800+
- name: Show file
801+
run: |
802+
echo "$(<test-file.properties)"
803+
804+
- name: Test output
805+
run: node ./test-output.js
806+
env:
807+
FILES: "test-file.properties"
808+
EXPECTED_VERSION: "2.1.0,3.1.0"
809+
810+
test-properties-new:
811+
runs-on: ubuntu-latest
812+
steps:
813+
- name: Checkout code
814+
uses: actions/checkout@v5
815+
with:
816+
path: "./"
817+
818+
- run: npm ci --prod
819+
- run: npm run build
820+
821+
- run: "git config --global user.email 'changelog@github.com'"
822+
- run: "git config --global user.name 'Awesome Github action'"
823+
- run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"
824+
825+
- name: Generate changelog
826+
id: changelog
827+
uses: ./
828+
env:
829+
ENV: "dont-use-git"
830+
EXPECTED_TAG: "v0.1.0"
831+
with:
832+
github-token: ${{ secrets.github_token }}
833+
version-file: "test-file-new.properties"
834+
835+
- name: Show file
836+
run: |
837+
echo "$(<test-file-new.properties)"
838+
839+
- name: Test output
840+
run: node ./test-output.js
841+
env:
842+
FILES: "test-file-new.properties"
843+
EXPECTED_VERSION: "0.1.0"
844+
845+
test-properties-empty:
846+
runs-on: ubuntu-latest
847+
steps:
848+
- name: Checkout code
849+
uses: actions/checkout@v5
850+
with:
851+
path: "./"
852+
853+
- run: npm ci --prod
854+
- run: npm run build
855+
856+
- run: "git config --global user.email 'changelog@github.com'"
857+
- run: "git config --global user.name 'Awesome Github action'"
858+
- run: "git add . && git commit --allow-empty -m 'feat: Added fake file so version will be bumped'"
859+
860+
- run: touch ./test-file-empty.properties
861+
862+
- name: Generate changelog
863+
id: changelog
864+
uses: ./
865+
env:
866+
ENV: "dont-use-git"
867+
EXPECTED_TAG: "v6.5.0"
868+
with:
869+
github-token: ${{ secrets.github_token }}
870+
version-file: "test-file-empty.properties"
871+
fallback-version: "6.5.0"
872+
873+
- name: Show file
874+
run: |
875+
echo "$(<test-file-empty.properties)"
876+
877+
- name: Test output
878+
run: node ./test-output.js
879+
env:
880+
FILES: "test-file-empty.properties"
881+
EXPECTED_VERSION: "6.5.0"
882+
756883
test-pre-commit:
757884
runs-on: ubuntu-latest
758885
steps:
@@ -844,7 +971,7 @@ jobs:
844971
EXPECTED_TAG: "v1.5.0"
845972
with:
846973
github-token: ${{ secrets.github_token }}
847-
version-file: "test-file.json, test-file-2.json, test-file.toml, test-file.yaml"
974+
version-file: "test-file.json, test-file-2.json, test-file.toml, test-file.yaml, test-file.properties"
848975

849976
- name: Show files
850977
run: |
@@ -856,12 +983,14 @@ jobs:
856983
echo "$(<test-file.toml)\n\n"
857984
echo "Test yaml file"
858985
echo "$(<test-file.yaml)\n\n"
986+
echo "Test properties file"
987+
echo "$(<test-file.properties)\n\n"
859988
860989
- name: Test output
861990
run: node ./test-output.js
862991
env:
863-
FILES: "test-file.json, test-file-2.json, test-file.toml, test-file.yaml"
864-
EXPECTED_VERSION: "1.5.0, 1.5.0, 1.5.0, 1.11.0"
992+
FILES: "test-file.json, test-file-2.json, test-file.toml, test-file.yaml, test-file.properties"
993+
EXPECTED_VERSION: "1.5.0, 1.5.0, 1.5.0, 1.11.0, 2.1.0"
865994

866995
test-config-file-path:
867996
runs-on: ubuntu-latest

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This action will bump version, tag commit and generate a changelog with conventi
1818
- **Optional** `input-file`: Read the changelog from this file. This will prepend the newly generated changelogs to the file's content.
1919
- **Optional** `output-file`: File to output the changelog to. Default `CHANGELOG.md`, when providing `'false'` no file will be generated / updated.
2020
- **Optional** `release-count`: Number of releases to preserve in changelog. Default `5`, use `0` to regenerate all. This input has no effect if `input-file` is used.
21-
- **Optional** `version-file`: The path to the file that contains the version to bump (supports comma-separated list of file paths). Default `./package.json`. Other supported formats are `mix.exs`, `.toml`, `.yml`, `.yaml`, and `.json`.
21+
- **Optional** `version-file`: The path to the file that contains the version to bump (supports comma-separated list of file paths). Default `./package.json`. Other supported formats are `mix.exs`, `.toml`, `.yml`, `.yaml`, `.json` and `.properties`.
2222
- **Optional** `version-path`: The place inside the version file to bump. Default `version`.
2323
- **Optional** `skip-git-pull`: Do not pull the repo before tagging. Ensure you full cloned the repo in the first place to get tags. Default `'false'`.
2424
- **Optional** `skip-on-empty`: Boolean to specify if you want to skip empty release (no-changelog generated). This case occurred when you push `chore` commit with `angular` for example. Default `'true'`.
@@ -146,7 +146,7 @@ Overwrite everything
146146
tag-prefix: 'v'
147147
output-file: 'MY_CUSTOM_CHANGELOG.md'
148148
release-count: '10'
149-
version-file: './my_custom_version_file.json' // or .yml, .yaml, .toml, mix.exs
149+
version-file: './my_custom_version_file.json' // or .yml, .yaml, .toml, mix.exs, .properties
150150
version-path: 'path.to.version'
151151
skip-on-empty: 'false'
152152
skip-version-file: 'false'

package-lock.json

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"conventional-recommended-bump": "^9.0.0",
4040
"git-semver-tags": "7.0.1",
4141
"object-path": "^0.11.8",
42+
"properties": "^1.2.1",
4243
"semver": "^7.6.3",
4344
"yaml": "^2.6.0"
4445
}

src/version/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Git = require('./git')
33
const Yaml = require('./yaml')
44
const Toml = require('./toml')
55
const Mix = require('./mix')
6+
const Properties = require('./properties')
67

78
module.exports = (fileExtension, filePath) => {
89
switch (fileExtension.toLowerCase()) {
@@ -22,6 +23,9 @@ module.exports = (fileExtension, filePath) => {
2223
case 'exs':
2324
return new Mix()
2425

26+
case 'properties':
27+
return new Properties()
28+
2529
default:
2630
throw new Error(`File extension "${fileExtension}" from file "${filePath}" is not supported`)
2731
}

src/version/properties.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const BaseVersioning = require('./base')
2+
const bumpVersion = require('../helpers/bumpVersion')
3+
const core = require("@actions/core");
4+
const properties = require('properties')
5+
6+
module.exports = class Properties extends BaseVersioning {
7+
8+
/**
9+
* @type {string | null}
10+
*/
11+
fileContent = null
12+
13+
/**
14+
* Reads and parses .properties file
15+
*/
16+
parseFile = () => {
17+
this.fileContent = this.readFile()
18+
this.oldVersion = properties.parse(this.fileContent)[this.versionPath]
19+
}
20+
21+
/**
22+
* Bumps the version in .properties file
23+
*
24+
* @param {!string} releaseType - The type of release
25+
* @return {*}
26+
*/
27+
bump = async (releaseType) => {
28+
this.newVersion = await bumpVersion(
29+
releaseType,
30+
this.oldVersion,
31+
)
32+
33+
// regex to capture key, separator and value in separate groups with whitespace
34+
// see: https://regexr.com/8gtpa
35+
const regex = new RegExp(`^(\\s*${this.versionPath}\\s*)(\\s*[:=]\\s*)(.*)$`, 'm')
36+
37+
if (regex.test(this.fileContent)) {
38+
// replace existing version in file while preserving the separator and whitespaces
39+
this.update(
40+
this.fileContent.replace(
41+
regex,
42+
`$1$2${this.newVersion}`
43+
)
44+
)
45+
} else {
46+
// append new version to the end of the file and preserve previous newline character
47+
const eof = this.fileContent.endsWith('\n') ? '\n' : ''
48+
const newline = this.fileContent.length > 0 && !this.fileContent.endsWith('\n') ? '\n' : ''
49+
50+
this.update(
51+
this.fileContent + newline + `${this.versionPath}=${this.newVersion}` + eof
52+
)
53+
}
54+
55+
core.info(`Bumped file "${this.fileLocation}" from "${this.oldVersion}" to "${this.newVersion}"`)
56+
}
57+
58+
}

test-file.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=test file for version parsing/replacing
2+
3+
#version = 0.4.2
4+
!version = 0.4.3
5+
version = 2.0.0
6+
7+
# colon_version:0.4.4
8+
! colon_version:0.4.5
9+
colon_version:3.0.0

test-output.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const assert = require('assert')
33
const objectPath = require('object-path')
44
const yaml = require('yaml')
55
const toml = require('@iarna/toml')
6+
const properties = require('properties')
67

78
const actionConfig = yaml.parse(fs.readFileSync('./action.yml', 'utf8'))
89

@@ -45,6 +46,10 @@ FILES.split(',').map((file, index) => {
4546
parsedContent = fileContent
4647
break
4748

49+
case 'properties':
50+
parsedContent = properties.parse(fileContent)
51+
break
52+
4853
default:
4954
assert.fail(`File extension "${fileExtension}" is not supported!`)
5055
}
@@ -57,7 +62,7 @@ FILES.split(',').map((file, index) => {
5762

5863
const expectedVersions = EXPECTED_VERSION.split(',')
5964
const expectedVersion = expectedVersions.length > 0
60-
? expectedVersions[index]
65+
? expectedVersions[index].trim()
6166
: expectedVersions
6267

6368
if (fileExtension.toLowerCase() === 'exs') {
@@ -67,8 +72,8 @@ FILES.split(',').map((file, index) => {
6772

6873
const newVersionInFile = objectPath.get(parsedContent, EXPECTED_VERSION_PATH, null)
6974

70-
console.log(`"${file}" check if "${newVersionInFile}" matches what is expected "${expectedVersion.trim()}"`)
75+
console.log(`"${file}" check if "${newVersionInFile}" matches what is expected "${expectedVersion}"`)
7176

72-
assert.strictEqual(newVersionInFile, expectedVersion.trim(), 'Version does not match what is expected')
77+
assert.strictEqual(newVersionInFile, expectedVersion, 'Version does not match what is expected')
7378
})
7479

0 commit comments

Comments
 (0)