如何通过Gulp任务增加版本号?

时间:2016-03-31 17:15:30

标签: javascript angularjs gulp build-process

我想用另一个字符串替换javascript文件(myConstantsFile.js)中指示版本号的字符串。因此,例如,我的版本号看起来像这样:“01.11.15”,在myConstantsFile.js中用其他常量写成:

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

现在,我的任务看起来像这样:

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

正如您所看到的,我使用的是常量,而不是incrementation code,它看起来像这样:

    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; 

我需要:

  1. 通过文件通过正则表达式选择当前版本号的方法。
  2. 知道在哪里可以将逻辑放在最后一个代码块中以增加数字并构建新字符串。

4 个答案:

答案 0 :(得分:23)

安装gulp-bump

npm install gulp-bump --save-dev

安装yargs

npm install yargs --save-dev

需要gulp-bump

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

需要yargs

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

你的碰撞任务

gulp.task('bump', function () {
    /// <summary>
    /// 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
    /// </summary>

    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注意:我相信很多人来到这个主题将会完全找到上面的答案。下面的代码是编辑存储在某处的版本号BESIDES npm / bower包文件,例如角度常量:

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/'));
});

我省略了一些mumbo-jumbo,它将我的常量写在一个漂亮的字符串中,但这就是要点并且它有效。

答案 1 :(得分:3)

从过去的5个小时开始研究吞咽,因为我有一个任务来修复要求。所以,作为一个明确的gob,我出来了下面的代码,没有正则表达式。感谢 @VSO @Wilmer Saint 快速启动。可能是一个微小的变化,但这对我有所帮助。

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
    });

或返回代码可以如下覆盖version.js文件中的数字

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

我的version.js只有以下代码

versionBuild = '1.0.8'

我在我的main函数中使用了以下内容(加载应用程序时加载)

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;
    }
}

答案 2 :(得分:2)

您可以使用gulp-bump,它非常简单和甜美:)

npm install --save gulp-bump
const bump = require('gulp-bump');

gulp.task('bump', async () => {
    gulp.src('./package.json')
    .pipe(bump({key: "version"}))
    .pipe(gulp.dest('./'));
  });
  

注意:在函数前使用async。这是一个要求。

答案 3 :(得分:0)

gulp.task('bump', function() {
    var vers = JSON.parse(fs.readFileSync(__dirname + '/package.json')).version.split('.');
    var second = parseInt(vers[1]);
    var third  = parseInt(vers[2]);
    var options = { key: 'version' };

    if(third == 9 && second != 9) {
        third = 0;
        options.type = 'minor';
    } else if (second == 9 && third == 9) {
        second = 0;
        options.type = 'major';
    }

    gulp.src(__dirname + '/package.json')
    .pipe(bump(options))
    .pipe(gulp.dest('./'));
});