Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp - recompile a bumped version for tagging

Regarding an issue I am having with gulp-tag-version, the README there suggests:

function inc(importance) {
    // get all the files to bump version in
    return gulp.src(['./package.json', './bower.json']) 
        // bump the version number in those files
        .pipe(bump({type: importance}))
        // save it back to filesystem
        .pipe(gulp.dest('./'))

        /*  Recompile the Javascript here */

        // commit the changed version number
        .pipe(git.commit('bumps package version')) 
        // read only one file to get the version number
        .pipe(filter('package.json'))
        // **tag it in the repository**
        .pipe(tag_version()) 
}

gulp.task('patch', function() { return inc('patch'); })
gulp.task('feature', function() { return inc('minor'); })
gulp.task('release', function() { return inc('major'); })

I would like to recompile some Javascript between the version being bumped and git tagging it. I have a task for that, js, but it is not clear how one can call the js task (or otherwise re-order the bump/tag tasks) to accomplish this common and desirable outcome (i.e. having the version in the headers of your compiled code).

It is also worth noting that if one had three tasks bump, compile and tag, the bump of the package.json appears to be cached and not re-read by a separate tag task.

like image 752
Brian M. Hunt Avatar asked Mar 18 '23 05:03

Brian M. Hunt


1 Answers

One simple way to get this working is to get 3 new separate tasks that depend on each others:

  • tag depends on commit, since the tag will be applied to the last commit we have to do that before
  • commit depends on js, because we should re-build your files with the bumped version
  • js depends on bump, bump the version of your manifest files before doing anything
  • bump has no dependencies.

Problem with this is that you will have to change your js to add the bump dependency, and I'm sure that you don't want to bump each time you recompile or one of your watch is triggered.

So you could use something like run-sequence to bypass that.

Also one thing I like to reduce the number of gulp tasks is to use arguments, but you can stick with the inc function if you want, that's a detail (here --major, --minor, or --patch).

So you could do something like this (from top to down in the execution order):

gulp.task('uprev', function () {
  return gulp.src(['./package.json', './bower.json'])
    .pipe(bump({ type: process.argv[3] ? process.argv[3].substr(2) : 'patch' }))
    .pipe(gulp.dest('./'));
});

gulp.task('rebuild', function (cb) {
  runSequence('uprev', 'js', cb); //uprev will here be executed before js
});

gulp.task('commit', ['rebuild'], function () {
    return gulp.src(['./package.json', './bower.json', 'dist/**/*'])
      .pipe(git.add())
      .pipe(git.commit('bump version'));
});

gulp.task('bump', ['commit'], function () {
  return gulp.src('package.json')
    .pipe(tagVersion());
});

For your commit, you may want to add both of your manifest and the compiled files, I've added all the content of a random dist folder for demo.

I've intentionally cut the bump and commit tasks so you don't have to use gulp-filter, which seems to me pretty useless for this little kind of thing, but it's as you want of course.

One last thing (promise), you could avoid to use gulp-tag-version by using a simple node fs call combined with gulp-git you already have:

gulp.task('bump', ['commit'], function (cb) {
  fs.readFile('./package.json', function (err, data) {
    if (err) { return cb(err); }
    var version = JSON.parse(data.toString()).version;
    git.tag(version, 'Version message', function (err) {
      cb(err);
    });
});
like image 124
Preview Avatar answered Mar 24 '23 06:03

Preview