如何通过 Gulp 任务增加版本号? [英] How to Increment Version Number via Gulp Task?

查看:26
本文介绍了如何通过 Gulp 任务增加版本号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用另一个字符串替换 javascript 文件 (myConstantsFile.js) 中指示版本号的字符串.因此,例如,我的版本号如下所示:01.11.15",在 myConstantsFile.js 中与其他常量如下所示:

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; 

我需要:

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

推荐答案

安装 gulp-bump

npm install gulp-bump --save-dev

安装 yargs

npm install yargs --save-dev

需要gulp-bump

Require 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 注意:我相信很多来到这个帖子的人都会寻找上面的答案.下面的代码是编辑存储在 npm/bower 包文件之外的某个地方的版本号,例如角度常量:


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

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

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

这篇关于如何通过 Gulp 任务增加版本号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆