From 97fa45a3e8a8618e948a7f2fe971bcf108f6b383 Mon Sep 17 00:00:00 2001 From: Maurits Meester Date: Tue, 28 May 2024 17:12:44 +0200 Subject: [PATCH 1/3] feat(regex): accept regex for text files --- index.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index b440850..afc6c95 100644 --- a/index.js +++ b/index.js @@ -35,7 +35,8 @@ const parseFileOption = option => { const path = (typeof option !== 'string' && option.path) || 'version'; const consumeWholeFile = typeof option !== 'string' ? option.consumeWholeFile : false; const versionPrefix = typeof option !== 'string' ? option.versionPrefix : null; - return { file, mimeType, path, consumeWholeFile, versionPrefix }; + const pattern = typeof option !== 'string' ? option.pattern : null; + return { file, mimeType, path, consumeWholeFile, versionPrefix, pattern }; }; const getFileType = (file, mimeType) => { @@ -110,7 +111,7 @@ class Bumper extends Plugin { return Promise.all( options.map(async out => { - const { file, mimeType, path, consumeWholeFile, versionPrefix = '' } = parseFileOption(out); + const { file, mimeType, path, consumeWholeFile, versionPrefix = '', pattern } = parseFileOption(out); this.log.exec(`Writing version to ${file}`, isDryRun); if (isDryRun) return noop; @@ -141,9 +142,15 @@ class Bumper extends Plugin { case 'ini': return writeFileSync(file, ini.encode(parsed)); default: - const versionMatch = new RegExp(latestVersion || '', 'g'); - const write = (parsed && !consumeWholeFile) ? parsed.replace(versionMatch, version) : version + EOL; - return writeFileSync(file, write); + if (pattern) { + const regex = new RegExp(pattern); + const write = (parsed && !consumeWholeFile) ? parsed.replace(regex, `$1${version}`) : version + EOL; + return writeFileSync(file, write); + } else { + const versionMatch = new RegExp(latestVersion || '', 'g'); + const write = (parsed && !consumeWholeFile) ? parsed.replace(versionMatch, version) : version + EOL; + return writeFileSync(file, write); + } } }) ); From 6d3a5e5f4725cdb124b09cf7cc461d7d9a8bda6c Mon Sep 17 00:00:00 2001 From: Maurits Meester Date: Tue, 28 May 2024 20:19:17 +0200 Subject: [PATCH 2/3] test(regex): make sure regex does what it needs to do --- test.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/test.js b/test.js index 3b4cee6..ae7a9f0 100644 --- a/test.js +++ b/test.js @@ -21,7 +21,8 @@ mock({ './VERSION-OLD2': `v0.9.0${EOL}`, './README.md': `Release v1.0.0${EOL}`, './foo.yaml': `version: v1.0.0${EOL}`, - './invalid.toml': `/# -*- some invalid toml -*-${EOL}version = "1.0.0"${EOL}` + './invalid.toml': `/# -*- some invalid toml -*-${EOL}version = "1.0.0"${EOL}`, + './wp-plugin.php': `hello world

; ?>${EOL}` }); const namespace = 'bumper'; @@ -340,3 +341,21 @@ test('should update version in JSON file with prefix', async () => { await runTasks(plugin); assert.equal(readFile('./bower.json'), '{\n "version": "^1.0.1"\n}\n'); }); + +test('should update version php plugin by regex', async () => { + const options = { + [namespace]: { + out: { + file: './wp-plugin.php', + pattern: '(\\* Version: )(\\d+(\\.\\d+){1,})' + } + } + }; + + const plugin = factory(Bumper, { namespace, options }); + await runTasks(plugin); + assert.equal( + readFile('./wp-plugin.php'), + `hello world

; ?>\n` + ); +}); From eadafd14faef581fd5418d03f94dac3236b6c61f Mon Sep 17 00:00:00 2001 From: Maurits Meester Date: Tue, 28 May 2024 20:28:04 +0200 Subject: [PATCH 3/3] docs(regex): explainer on how regex works --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index ea81c33..9636473 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,20 @@ The `versionPrefix` option (default: `''`) can be used in cases where you'd like With the above configuration, if release-it determines the new version to be `1.0.0`, it'll be saved as `^1.0.0` in the targeted file. +For less structured files like text or php files, you can use `pattern` to replace the version number. The `pattern` is a regular expression that should match the version number. The first capturing group should match the version number. The `pattern` option only works for files with `type: text/*`: + +```json +"plugins": { + "@release-it/bumper": { + "out": { + "file": "file.php", + "type": "text/php", + "pattern": "(\\* Version: )(\\d+(\\.\\d+){1,})" + } + } +} +``` + ## Command-line Options for this plugin can be set from the command line. Some examples: