Skip to content
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

Improvement to support setting the regex per file group. #168

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ Default value: `['package.json']`

Maybe you wanna bump 'component.json' instead? Or maybe both: `['package.json', 'component.json']`? Can be either a list of files to bump (an array of files) or a grunt glob (e.g., `['*.json']`).

Alternatively, you can use different regexes to your target files:

```js
files: [
{
path: 'index.html',
regexp: /(dist\/\w+\/\w+[-])(([\d-]+\.)+[\d]+)(([.\w]+['"]))/g
},
{
path: ['package.json', 'bower.json']
}
]
```
The example above replaces urls like `dist/js/app-1.0.1.js` or `dist/css/style-1.0.1.css`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a good example. Grunt-bump shouldn't be used for this since its not renaming those files.

This can be useful for replacing versions in non-JSON files. Although that is part of bigger change I want to make where you specify the key in a JSON file to replace the version with and then the regex is only for nonJSON files


#### options.updateConfigs
Type: `Array`
Default value: `[]`
Expand Down
115 changes: 67 additions & 48 deletions tasks/bump.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,62 +101,81 @@ module.exports = function(grunt) {

// BUMP ALL FILES
runIf(opts.bumpVersion, function() {
grunt.file.expand(opts.files).forEach(function(file, idx) {
var version = null;
var content = grunt.file.read(file).replace(
VERSION_REGEXP,
function(match, prefix, parsedVersion, namedPre, noNamePre, suffix) {
var type = versionType === 'git' ? 'prerelease' : versionType;
version = setVersion || semver.inc(
parsedVersion, type || 'patch', gitVersion || opts.prereleaseName
);
console.log('md', opts.metadata);
if (opts.metadata) {
if (!/^([0-9a-zA-Z-]+\.{0,1})*$/.test(opts.metadata)) {
grunt.fatal(
'Metadata can only contain letters, numbers, dashes ' +
'(-) and dots (.)'
);
}
version += '+' + opts.metadata;
}
return prefix + version + (suffix || '');
}
);
opts.files.forEach(function(fileconf,idx) {

if (!version) {
grunt.fatal('Can not find a version to bump in ' + file);
}
var regexp = VERSION_REGEXP;
var files = "";

var logMsg = 'Version bumped to ' + version + ' (in ' + file + ')';
if (!dryRun) {
grunt.file.write(file, content);
grunt.log.ok(logMsg);
if (!fileconf.path) {
//old notation
files = fileconf;
regexp = VERSION_REGEXP;
} else {
grunt.log.ok('bump-dry: ' + logMsg);
//new notation
files = fileconf.path;
regexp = fileconf.regexp || VERSION_REGEXP;
}

if (!globalVersion) {
globalVersion = version;
} else if (globalVersion !== version) {
grunt.warn('Bumping multiple files with different versions!');
}
grunt.file.expand(files).forEach(function (file, idx) {
var version = null;
var content = grunt.file.read(file).replace(regexp,
function (match, prefix, parsedVersion, namedPre, noNamePre, suffix) {
//console.log("param debug", match,prefix, parsedVersion, namedPre, noNamePre, suffix);
var type = versionType === 'git' ? 'prerelease' : versionType;

version = setVersion || semver.inc(
parsedVersion, type || 'patch', gitVersion || opts.prereleaseName
);
if (opts.metadata) {
console.log('md', opts.metadata);
}
if (opts.metadata) {
if (!/^([0-9a-zA-Z-]+\.{0,1})*$/.test(opts.metadata)) {
grunt.fatal(
'Metadata can only contain letters, numbers, dashes ' +
'(-) and dots (.)'
);
}
version += '+' + opts.metadata;
}
return prefix + version + (suffix || '');
}
);

var configProperty = opts.updateConfigs[idx];
if (!configProperty) {
return;
}
if (!version) {
grunt.fatal('Can not find a version to bump in ' + file);
}

var cfg = grunt.config(configProperty);
if (!cfg) {
return grunt.warn(
'Can not update "' + configProperty + '" config, it does not exist!'
);
}
var logMsg = 'Version bumped to ' + version + ' (in ' + file + ')';
if (!dryRun) {
grunt.file.write(file, content);
grunt.log.ok(logMsg);
} else {
grunt.log.ok('bump-dry: ' + logMsg);
}

cfg.version = version;
grunt.config(configProperty, cfg);
grunt.log.ok(configProperty + '\'s version updated');
if (!globalVersion) {
globalVersion = version;
} else if (globalVersion !== version) {
grunt.warn('Bumping multiple files with different versions!');
}

var configProperty = opts.updateConfigs[idx];
if (!configProperty) {
return;
}

var cfg = grunt.config(configProperty);
if (!cfg) {
return grunt.warn(
'Can not update "' + configProperty + '" config, it does not exist!'
);
}

cfg.version = version;
grunt.config(configProperty, cfg);
grunt.log.ok(configProperty + '\'s version updated');
});
});
next();
});
Expand Down