1 | /* |
---|
2 | * grunt-contrib-bump |
---|
3 | * http://gruntjs.com/ |
---|
4 | * |
---|
5 | * Copyright (c) 2013 "Cowboy" Ben Alman, contributors |
---|
6 | * Licensed under the MIT license. |
---|
7 | */ |
---|
8 | |
---|
9 | 'use strict'; |
---|
10 | |
---|
11 | var semver = require('semver'); |
---|
12 | var shell = require('shelljs'); |
---|
13 | |
---|
14 | module.exports = function(grunt) { |
---|
15 | |
---|
16 | grunt.registerTask('bump', 'Bump the version property of a JSON file.', function() { |
---|
17 | // Validate specified semver increment modes. |
---|
18 | var valids = ['major', 'minor', 'patch', 'prerelease']; |
---|
19 | var modes = []; |
---|
20 | this.args.forEach(function(mode) { |
---|
21 | var matches = []; |
---|
22 | valids.forEach(function(valid) { |
---|
23 | if (valid.indexOf(mode) === 0) { matches.push(valid); } |
---|
24 | }); |
---|
25 | if (matches.length === 0) { |
---|
26 | grunt.log.error('Error: mode "' + mode + '" does not match any known modes.'); |
---|
27 | } else if (matches.length > 1) { |
---|
28 | grunt.log.error('Error: mode "' + mode + '" is ambiguous (possibly: ' + matches.join(', ') + ').'); |
---|
29 | } else { |
---|
30 | modes.push(matches[0]); |
---|
31 | } |
---|
32 | }); |
---|
33 | if (this.errorCount === 0 && modes.length === 0) { |
---|
34 | grunt.log.error('Error: no modes specified.'); |
---|
35 | } |
---|
36 | if (this.errorCount > 0) { |
---|
37 | grunt.log.error('Valid modes are: ' + valids.join(', ') + '.'); |
---|
38 | throw new Error('Use valid modes (or unambiguous mode abbreviations).'); |
---|
39 | } |
---|
40 | // Options. |
---|
41 | var options = this.options({ |
---|
42 | filepaths: ['package.json'], |
---|
43 | syncVersions: false, |
---|
44 | commit: true, |
---|
45 | commitMessage: 'Bumping version to {%= version %}.', |
---|
46 | tag: true, |
---|
47 | tagName: 'v{%= version %}', |
---|
48 | tagMessage: 'Version {%= version %}', |
---|
49 | tagPrerelease: false, |
---|
50 | }); |
---|
51 | // Normalize filepaths to array. |
---|
52 | var filepaths = Array.isArray(options.filepaths) ? options.filepaths : [options.filepaths]; |
---|
53 | // Process JSON files, in-order. |
---|
54 | var versions = {}; |
---|
55 | filepaths.forEach(function(filepath) { |
---|
56 | var o = grunt.file.readJSON(filepath); |
---|
57 | var origVersion = o.version; |
---|
58 | // If syncVersions is enabled, only grab version from the first file, |
---|
59 | // guaranteeing new versions will always be in sync. |
---|
60 | var firstVersion = Object.keys(versions)[0]; |
---|
61 | if (options.syncVersions && firstVersion) { |
---|
62 | o.version = firstVersion; |
---|
63 | } |
---|
64 | modes.forEach(function(mode) { |
---|
65 | var orig = o.version; |
---|
66 | var s = semver.parse(o.version); |
---|
67 | s.inc(mode); |
---|
68 | o.version = String(s); |
---|
69 | // Workaround for https://github.com/isaacs/node-semver/issues/50 |
---|
70 | if (/-/.test(orig) && mode === 'patch') { |
---|
71 | o.version = o.version.replace(/\d+$/, function(n) { return n - 1; }); |
---|
72 | } |
---|
73 | // If prerelease on an un-prerelease version, bump patch version first |
---|
74 | if (!/-/.test(orig) && mode === 'prerelease') { |
---|
75 | s.inc('patch'); |
---|
76 | s.inc('prerelease'); |
---|
77 | o.version = String(s); |
---|
78 | } |
---|
79 | }); |
---|
80 | if (versions[origVersion]) { |
---|
81 | versions[origVersion].filepaths.push(filepath); |
---|
82 | } else { |
---|
83 | versions[origVersion] = {version: o.version, filepaths: [filepath]}; |
---|
84 | } |
---|
85 | // Actually *do* something. |
---|
86 | grunt.log.write('Bumping version in ' + filepath + ' from ' + origVersion + ' to ' + o.version + '...'); |
---|
87 | grunt.file.write(filepath, JSON.stringify(o, null, 2)); |
---|
88 | grunt.log.ok(); |
---|
89 | }); |
---|
90 | // Commit changed files? |
---|
91 | if (options.commit) { |
---|
92 | Object.keys(versions).forEach(function(origVersion) { |
---|
93 | var o = versions[origVersion]; |
---|
94 | commit(o.filepaths, processTemplate(options.commitMessage, { |
---|
95 | version: o.version, |
---|
96 | origVersion: origVersion |
---|
97 | })); |
---|
98 | }); |
---|
99 | } |
---|
100 | // We're only going to create one tag. And it's going to be the new |
---|
101 | // version of the first bumped file. Because, sanity. |
---|
102 | var newVersion = versions[Object.keys(versions)[0]].version; |
---|
103 | if (options.tag) { |
---|
104 | if (options.tagPrerelease || modes.indexOf('prerelease') === -1) { |
---|
105 | tag( |
---|
106 | processTemplate(options.tagName, {version: newVersion}), |
---|
107 | processTemplate(options.tagMessage, {version: newVersion}) |
---|
108 | ); |
---|
109 | } else { |
---|
110 | grunt.log.writeln('Not tagging (prerelease version).'); |
---|
111 | } |
---|
112 | } |
---|
113 | if (this.errorCount > 0) { |
---|
114 | grunt.warn('There were errors.'); |
---|
115 | } |
---|
116 | }); |
---|
117 | |
---|
118 | // Using custom delimiters keeps templates from being auto-processed. |
---|
119 | grunt.template.addDelimiters('bump', '{%', '%}'); |
---|
120 | |
---|
121 | function processTemplate(message, data) { |
---|
122 | return grunt.template.process(message, { |
---|
123 | delimiters: 'bump', |
---|
124 | data: data, |
---|
125 | }); |
---|
126 | } |
---|
127 | |
---|
128 | // Kinda borrowed from https://github.com/geddski/grunt-release |
---|
129 | function commit(filepaths, message) { |
---|
130 | grunt.log.writeln('Committing ' + filepaths.join(', ') + ' with message: ' + message); |
---|
131 | run("git commit -m '" + message + "' '" + filepaths.join("' '") + "'"); |
---|
132 | } |
---|
133 | |
---|
134 | function tag(name, message) { |
---|
135 | grunt.log.writeln('Tagging ' + name + ' with message: ' + message); |
---|
136 | run("git tag '" + name + "' -m '" + message + "'"); |
---|
137 | } |
---|
138 | |
---|
139 | function run(cmd) { |
---|
140 | if (grunt.option('no-write')) { |
---|
141 | grunt.verbose.writeln('Not actually running: ' + cmd); |
---|
142 | } else { |
---|
143 | grunt.verbose.writeln('Running: ' + cmd); |
---|
144 | var result = shell.exec(cmd, {silent:true}); |
---|
145 | if (result.code !== 0) { |
---|
146 | grunt.log.error('Error (' + result.code + ') ' + result.output); |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | }; |
---|