From 2fe1e89d78e17e04c07a01139454e6cb5292107a Mon Sep 17 00:00:00 2001 From: Nikolay Vasilchuk Date: Thu, 12 Feb 2015 11:17:44 +0300 Subject: [PATCH] feat: support custom regexp Closes #126 --- README.md | 8 +++++++- tasks/bump.js | 12 ++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 980b4b7..1be9881 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,8 @@ grunt.initConfig({ pushTo: 'upstream', gitDescribeOptions: '--tags --always --abbrev=1 --dirty=-d', globalReplace: false, - prereleaseName: null + prereleaseName: null, + regExp: null } }, }) @@ -137,6 +138,11 @@ When left as the default `null` version bump:prereleae will behave as follows: * from a previous bump:git * 1.0.0-7-g10b5 to 1.0.0-8 +#### options.regExp +Type: `RegExp` +Default value: `null` + +Regex to find and replace version string in files described in `options.files`. If no value is specified, it will use the plugin's default. ### Usage Examples diff --git a/tasks/bump.js b/tasks/bump.js index 2d3eb43..2002bca 100644 --- a/tasks/bump.js +++ b/tasks/bump.js @@ -39,7 +39,8 @@ module.exports = function(grunt) { pushTo: 'upstream', gitDescribeOptions: '--tags --always --abbrev=1 --dirty=-d', globalReplace: false, - prereleaseName: null + prereleaseName: null, + regExp: null }); var dryRun = grunt.option('dry-run'); @@ -82,7 +83,11 @@ module.exports = function(grunt) { var globalVersion; // when bumping multiple files var gitVersion; // when bumping using `git describe` - var VERSION_REGEXP = new RegExp('([\\\'|\\\"]?version[\\\'|\\\"]?[ ]*:[ ]*[\\\'|\\\"]?)(\\\d+\\\.\\\d+\\\.\\\d+(-'+opts.prereleaseName+'\\\.\\\d+)?(-\\\d+)?)[\\\d||A-a|.|-]*([\\\'|\\\"]?)', 'i'); + var VERSION_REGEXP = opts.regExp || new RegExp( + '([\'|\"]?version[\'|\"]?[ ]*:[ ]*[\'|\"]?)(\\d+\\.\\d+\\.\\d+(-' + + opts.prereleaseName + + '\\.\\d+)?(-\\d+)?)[\\d||A-a|.|-]*([\'|\"]?)', 'i' + ); if (opts.globalReplace) { VERSION_REGEXP = new RegExp(VERSION_REGEXP.source, 'gi'); @@ -103,9 +108,8 @@ module.exports = function(grunt) { runIf(opts.bumpVersion, function() { grunt.file.expand(opts.files).forEach(function(file, idx) { var version = null; - var bumpFrom; var content = grunt.file.read(file).replace(VERSION_REGEXP, function(match, prefix, parsedVersion, namedPre, noNamePre, suffix) { - version = exactVersionToSet || gitVersion || semver.inc(parsedVersion, versionType || 'patch', opts.prereleaseName); + version = exactVersionToSet || gitVersion || semver.inc(parsedVersion, versionType || 'patch', opts.prereleaseName); return prefix + version + suffix; });