How to Increment Version Number via Gulp Task?

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

I would like to replace a string indicating version number in a javascript file (myConstantsFile.js), with another string. So, for example, my version number looks like this: "01.11.15", written like this in myConstantsFile.js with other constants:

.constant('productVersion', '1.11.15'); 

Right now, my task looks like this:

gulp.task('increment-version', function(){     gulp.src(['./somedir/myConstantsFile.js'])         .pipe(replace(/'productVersion', '(.*)'/g, '99.99.99'))         .pipe(gulp.dest('./somedir/')); });

As you can see, I am using a constant, not running incrementation code, which would look like this:

    var numberString = '0.0.1';     var versionParts = numberString.split('.');     var vArray = {       vMajor : versionParts[0],       vMinor : versionParts[1],       vPatch : versionParts[2]     }       vArray.vPatch = parseFloat(vArray.vPatch) + 1;     var periodString = ".";      var newVersionNumberString = vArray.vMajor + periodString +                                  vArray.vMinor+ periodString +                                  vArray.vPatch; 

I need:

  1. A way to select the current version number via regex via the file.
  2. To know where I can put the logic in the last code block to increment the number and build the new string.

回答1:

Install gulp-bump

npm install gulp-bump --save-dev

Install yargs

npm install yargs --save-dev

Require gulp-bump

var bump = require('gulp-bump');

Require yargs

var args = require('yargs').argv;

Your bump task

gulp.task('bump', function () {     ///      /// It bumps revisions     /// Usage:     /// 1. gulp bump : bumps the package.json and bower.json to the next minor revision.     ///   i.e. from 0.1.1 to 0.1.2     /// 2. gulp bump --version 1.1.1 : bumps/sets the package.json and bower.json to the      ///    specified revision.     /// 3. gulp bump --type major       : bumps 1.0.0      ///    gulp bump --type minor       : bumps 0.1.0     ///    gulp bump --type patch       : bumps 0.0.2     ///    gulp bump --type prerelease  : bumps 0.0.1-2     ///       var type = args.type;     var version = args.version;     var options = {};     if (version) {         options.version = version;         msg += ' to ' + version;     } else {         options.type = type;         msg += ' for a ' + type;     }       return gulp         .src(['Path to your package.json', 'path to your bower.json'])         .pipe(bump(options))         .pipe(gulp.dest('path to your root directory')); });

VSO Note: I believe a lot of people coming to this thread will be looking exactly for the answer above. The code below is to edit a version number stored somewhere BESIDES the npm/bower package files, such as in angular constants:

gulp.task('increment-version', function(){     //docString is the file from which you will get your constant string     var docString = fs.readFileSync('./someFolder/constants.js', 'utf8');      //The code below gets your semantic v# from docString     var versionNumPattern=/'someTextPreceedingVNumber', '(.*)'/; //This is just a regEx with a capture group for version number     var vNumRexEx = new RegExp(versionNumPattern);     var oldVersionNumber = (vNumRexEx.exec(docString))[1]; //This gets the captured group      //...Split the version number string into elements so you can bump the one you want     var versionParts = oldVersionNumber.split('.');     var vArray = {         vMajor : versionParts[0],         vMinor : versionParts[1],         vPatch : versionParts[2]     };      vArray.vPatch = parseFloat(vArray.vPatch) + 1;     var periodString = ".";      var newVersionNumber = vArray.vMajor + periodString +                            vArray.vMinor+ periodString +                            vArray.vPatch;      gulp.src(['./someFolder/constants.js'])         .pipe(replace(/'someTextPreceedingVNumber', '(.*)'/g, newVersionNumber))         .pipe(gulp.dest('./someFolder/')); });

I ommitted some mumbo-jumbo that writes my constant in a pretty string, but that's the gist and it works.



回答2:

Started looking into gulp since past 5 hours,as I had a task to fix the requirement. So, being a definite noob to gulp I came out with the below code which is without the regex expression. Thanks to @VSO and @Wilmer Saint for a quick start. Might be a tiny change, but this helped me.

gulp.task('version', function(){   var fs = require('fs');     //docString is the file from which you will get your constant string     var docString = fs.readFileSync('app/scripts/version/version.js', 'utf8'); //type of docString i an object here.      var versionParts = docString.split('.');      var vArray = {         vMajor : versionParts[0],         vMinor : versionParts[1],         vPatch : versionParts[2]     };      vArray.vPatch = parseFloat(vArray.vPatch) + 1;     var periodString = ".";     var newVersionNumber = vArray.vMajor + periodString +                            vArray.vMinor+ periodString +                            vArray.vPatch;        require('fs').writeFileSync('app/scripts/version/version.js', newVersionNumber);         return gulp.src(['app/scripts/version/version.js'])             .pipe(gulp.dest('app/scripts/version/new_version'));//creates version.js file in the directory     });

or the return code could be as below to override the number in version.js file

return gulp.src(['app/scripts/version/version.js'],                 {base: './app/scripts/version/version.js'})         .pipe((gulp.dest('./app/scripts/version/version.js'))) 

My version.js has only below code

versionBuild = '1.0.8'

I used the below in my main function(loads on loading the app)

var versionBuild=parseInt(1000*Math.random()); var random = function(digs){     var rndn;     if(window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1") {         rndn = Math.random();         if(digs != undefined && !isNaN(digs)){               rndn =  parseInt(Math.pow(10, digs)*rndn)               }               return rndn;     }     else {         return versionBuild;     } }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!