Grunt Bump Up

Advertisement

Introduction Introduction

What is the version bump? When we build any package release then we set up the

Top ↑

Install NPM Packages Install NPM Packages

Install npm package grunt-bumpup Install npm package grunt-bumpup

To install the NPM package use below command:

npm i grunt-bumpup

Output of the above command is looks like this:

D:\xampp\htdocs\dev.test\wp-content\plugins\astra-sites (dev) (Astra Sites@2.0.0)
 ? npm i grunt-bumpup
 npm notice created a lockfile as package-lock.json. You should commit this file.
 npm WARN Invalid name: "Astra Sites"
 npm WARN astra-sites No description
 npm WARN astra-sites No repository field.
 npm WARN astra-sites No README data
 npm WARN astra-sites No license field.
 grunt-bumpup@0.6.3
 added 15 packages from 28 contributors, updated 5 packages and audited 3994 packages in 96.899s 

Top ↑

Install NPM package grunt-text-replace Install NPM package grunt-text-replace

To install the npm package use below command:

npm i grunt-text-replace

Output of below commands like elow

D:\xampp\htdocs\dev.test\wp-content\plugins\astra-sites (dev) (Astra Sites@2.0.0)
 ? npm i grunt-text-replace
 npm WARN Invalid name: "Astra Sites"
 npm WARN astra-sites No description
 npm WARN astra-sites No repository field.
 npm WARN astra-sites No README data
 npm WARN astra-sites No license field.
 grunt-text-replace@0.4.0
 added 1 package from 1 contributor and audited 3997 packages in 8.643s 

After installing both NPM packages now see the package.json file.

You can see the code something like below:

   "dependencies": {
     "grunt-bumpup": "^0.6.3",
     "grunt-text-replace": "^0.4.0"
   }

Top ↑

Edit Grunt.js Edit Grunt.js

Setup bumpup command.

bumpup: {
    options: {
        updateProps: {
            pkg: 'package.json'
        }
    },
    file: 'package.json'
},

Setup replace command

replace: {
    plugin_main: {
        src: ['astra-sites.php'],
        overwrite: true,
        replacements: [
            {
                from: /Version: \bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-z-A-Z-]+(?:\.[\da-z-A-Z-]+)*)?(?:\+[\da-z-A-Z-]+(?:\.[\da-z-A-Z-]+)*)?\b/g,
                to: 'Version: <%= pkg.version %>'
            }
        ]
    },
    stable_tag: {
        src: ['readme.txt'],
        overwrite: true,
        replacements: [
            {
                from: /Stable tag:\ .*/g,
                to: 'Stable tag: <%= pkg.version %>'
            }
        ]
    }
    plugin_function_comment: {
        src: [
            '*.php',
            '**/*.php',
            '!node_modules/**',
            '!php-tests/**',
            '!bin/**',
        ],
        overwrite: true,
        replacements: [
            {
                from: 'x.x.x',
                to: '<%=pkg.version %>'
            }
        ]
    }
}

Load NPM package tasks

grunt.loadNpmTasks( 'grunt-bumpup' );
grunt.loadNpmTasks( 'grunt-text-replace' );

Setup version-bump command

// Bump Version - `grunt version-bump --version=<number>`
grunt.registerTask('version-bump', function ( version ) {
    var version_number = grunt.option('version');
    if (version_number) {
        version_number = version_number ? version_number : 'patch';
        grunt.task.run( 'bumpup:' + newVersion);
        grunt.task.run( 'replace' );
    }
});

Use command grunt version-bump --version={number} to set your next release version.

E.g.

grunt version-bump --version=1.2.0

Top ↑

Complete Grunt.js Complete Grunt.js

module.exports = function( grunt ) {
	'use strict';
	var banner = '/**\n * <%= pkg.homepage %>\n * Copyright (c) <%= grunt.template.today("yyyy") %>\n * This file is generated automatically. Do not edit.\n */\n';
    var pkg = grunt.file.readJSON('package.json');
	// Project configuration
	grunt.initConfig( {
		pkg: pkg,
        bumpup: {
            options: {
                updateProps: {
                    pkg: 'package.json'
                }
            },
            file: 'package.json'
        },
        replace: {
            plugin_main: {
                src: ['astra-sites.php'],
                overwrite: true,
                replacements: [
                    {
                        from: /Version: \bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-z-A-Z-]+(?:\.[\da-z-A-Z-]+)*)?(?:\+[\da-z-A-Z-]+(?:\.[\da-z-A-Z-]+)*)?\b/g,
                        to: 'Version: <%= pkg.version %>'
                    }
                ]
            },
            stable_tag: {
                src: ['readme.txt'],
                overwrite: true,
                replacements: [
                    {
                        from: /Stable tag:\ .*/g,
                        to: 'Stable tag: <%= pkg.version %>'
                    }
                ]
            }
            plugin_function_comment: {
                src: [
                    '*.php',
                    '**/*.php',
                    '!node_modules/**',
                    '!php-tests/**',
                    '!bin/**',
                ],
                overwrite: true,
                replacements: [
                    {
                        from: 'x.x.x',
                        to: '<%=pkg.version %>'
                    }
                ]
            }
        }
	} );
    // Load grunt tasks
    grunt.loadNpmTasks( 'grunt-bumpup' );
    grunt.loadNpmTasks( 'grunt-text-replace' );
    // Bump Version - `grunt version-bump --version=<number>`
    grunt.registerTask('version-bump', function ( version ) {
        var version_number = grunt.option('version');
        if (version_number) {
            version_number = version_number ? version_number : 'patch';
            grunt.task.run( 'bumpup:' + newVersion);
            grunt.task.run( 'replace' );
        }
    });
};

Leave a Reply