【问题标题】:Bump a specific version number on SVN using Grunt使用 Grunt 在 SVN 上添加特定版本号
【发布时间】:2016-01-11 09:04:18
【问题描述】:

我有一个使用 Grunt 构建的 AngularJS 模块。该模块的版本在 package.json 文件中管理。

我需要做什么

我需要创建一个 grunt 任务以在需要时释放模块。这是这个任务必须做的事情:

  1. 在 SVN 上执行当前模块文件的标记(必须是 SVN,而不是 GIT)。
  2. 将 package.json 中的版本升级到给定版本(例如,我会将 --version = X.Y.Z 作为选项传递给 grunt 任务)。我不想要仅基于“补丁”、“次要”或“主要”升级的解决方案。
  3. 提交对 package.json 文件的更改。

到目前为止我发现了什么

grunt-bump 允许我使用 --setversion 选项传递特定版本。但它不能在 SVN 上提交更改,它只适用于 GIT。

grunt-svn-bump 允许我在 SVN 上提交,但我找不到指定下一个版本的方法。而且它不能执行“标签”部分。

grunt-svn-tag 允许我标记 SVN 存储库中的文件。

你知道另一个适合的 grunt 插件吗?任何帮助将不胜感激。

【问题讨论】:

    标签: angularjs svn gruntjs


    【解决方案1】:

    我厌倦了寻找一个适合的现有任务,所以我最终创建了它,基于 grunt-bump 和 grunt-svn-bump 的代码:

    grunt.registerTask('custom_bump', 'Custom task for bumping version after a release', function(){
        var semver = require('semver');
        var shell = require('shelljs');
    
        function shellRun( cmd ){
            if (grunt.option('dryRun')) {
                grunt.log.writeln('Command (not running because of dryRun option): ' + cmd);
            } else {
                grunt.verbose.writeln('Running: ' + cmd);
                var result = shell.exec(cmd, {silent:true});
                if (result.code !== 0) {
                    grunt.log.error('Error (' + result.code + ') ' + result.output);
                }
            }
        }
    
        // Options
        var options = this.options({
            filepath: 'package.json',
            commit: true,
            commitMessage : 'New version following a release'
        });
    
        // Recover the next version of the component
        var nextVersion = grunt.option('nextVersion');
        if( !nextVersion ){
            grunt.fatal( 'Next version is not defined.', 3 );
        }
        else if( !semver.valid( nextVersion ) ){
            grunt.warn( 'Next version is invalid.', 3 );
        }
    
        // Upgrade version into package.json
        var filepath = options.filepath;
        var file = grunt.file.readJSON( filepath );
        var currentVersion = file.version;
        if( semver.lte( nextVersion, currentVersion ) ){
            grunt.warn( 'Next version is lesser or equal than current version.' );
        }
        file.version = nextVersion;
        grunt.log.write( 'Bumping version in ' + filepath + ' from ' + currentVersion + ' to ' + nextVersion + '... ' );
        grunt.file.write( filepath, JSON.stringify( file, null, 2 ) );
        grunt.log.ok();
    
        // Commit the changed package.json file
        if( options.commit ){
            var message =
            grunt.log.write( 'Committing ' +  filepath + '... ' );
            shellRun( 'svn commit "' + filepath + '" -m "' + options.commitMessage + '"' );
            grunt.log.ok();
        }
    
        // Update the config for next tasks
        var configProperty = 'pkg';
        grunt.log.write( 'Updating version in ' + configProperty + ' config... ' );
        var config = grunt.config( configProperty );
        if( config ){
            config.version = nextVersion;
            grunt.config( configProperty, config );
            grunt.log.ok();
        } else {
            grunt.log.warn( "Cannot update pkg config !" );
        }
    
        grunt.log.ok( 'Version updated from ' +  currentVersion + ' to ' + nextVersion + '.' );
    });
    

    我的“发布”任务使用grunt-svn-tag 和我的自定义碰撞任务。

    grunt.initConfig({
        // ...
        svn_tag: {
          current: {
            options: {
              tag: '<%= pkg.name %>-<%= pkg.version %>'
            }
          }
        }
    });
    
    grunt.registerTask('release', [
      'svn_tag:current',
      'custom_bump'
    ]);
    

    【讨论】:

      【解决方案2】:

      免责声明,我不使用 SVN 或 Grunt,但使用 GIT 和 Gulp,所以我真的不知道其中任何一个的语法。

      话虽如此,我不会把它放在 grunt/gulp 任务中,但我会创建一个 NPM 任务来运行一个小的 shell 脚本。 npm release X.Y.Z
      shellscript 可能包含如下内容:

      #!/usr/bin/env bash
      
      VERSION=$2
      echo "gulp bump $VERSION"
      gulp bump $VERSION
      
      echo "staging package.json"
      git add package.json
      
      echo "commit release"
      git commit -m "release $VERSION"
      git tag -a "$VERSION" -m "release $VERSION"
      git push origin --tags
      

      现在我还没有测试过这种语法,但是我会尝试这种方式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-03
        • 1970-01-01
        • 2017-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多