Skip to content

Commit

Permalink
feat(app): Upgrade release and changelog gulp task
Browse files Browse the repository at this point in the history
  • Loading branch information
thaiat committed Jan 26, 2015
1 parent b6a9dcc commit ff8a219
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 59 deletions.
19 changes: 19 additions & 0 deletions gulp_tasks/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
var fs = require('fs');
var gutil = require('gulp-util');
var chalk = require('chalk');
var stripJsonComments = require('strip-json-comments');
var _ = require('lodash');
var path = require('path');

/**
* Returns true if the target application is mobile
Expand Down Expand Up @@ -31,3 +34,19 @@ var execHandler = exports.execHandler = function(err, stdout, stderr) {
gutil.log(chalk.red('Error: ') + stderr);
}
};

var readTextFile = exports.readTextFile = function(filename) {
var body = fs.readFileSync(filename, 'utf8');
return body;
};

var readJsonFile = exports.readJsonFile = function(filename) {
var body = readTextFile(filename);
return JSON.parse(stripJsonComments(body));
};

var filterFiles = exports.filterFiles = function(files, extension) {
return _.filter(files, function(file) {
return path.extname(file) === extension;
});
};
22 changes: 10 additions & 12 deletions gulp_tasks/tasks/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var changelog = require('conventional-changelog');
var argv = require('yargs').argv;
var marked = require('marked');
var fs = require('fs');
var q = require('q');
var pkg = require('../../package.json');
var path = require('path');
var gutil = require('gulp-util');
var exec = $.exec;
var concat = $.concat;
var helper = require('../common/helper');

var constants = require('../common/constants')();

var repository = constants.repository;
Expand All @@ -20,6 +20,7 @@ if(repository.length <= 0) {
}

var makeChangelog = function(options) {
var pkg = helper.readJsonFile('./package.json');
var codename = pkg.codename;
var file = options.standalone ? '' : path.join(__dirname, 'CHANGELOG.md');
var subtitle = options.subtitle || '"' + codename + '"';
Expand All @@ -43,20 +44,16 @@ var makeChangelog = function(options) {
return deferred.promise;
};

gulp.task('changelog:conventional', false, function() {
gulp.task('changelog:conventional', false, function(cb) {
var dest = argv.dest || 'CHANGELOG.md';
var toHtml = argv.html || false;
return makeChangelog(argv).then(function(log) {
if(toHtml) {
log = marked(log, {
gfm: true
});
}
fs.writeFileSync(dest, log);
cb();
});
});

gulp.task('changelog:script', false, function() {
gulp.task('changelog:script', false, function(cb) {
var pkg = helper.readJsonFile('./package.json');
var options = argv;
var version = options.version || pkg.version;
var from = options.from || '';
Expand All @@ -70,9 +67,10 @@ gulp.task('changelog:script', false, function() {
})));
stream.queue(gulp.src('CHANGELOG.md'));

return stream.done()
stream.done()
.pipe(concat('CHANGELOG.md'))
.pipe(gulp.dest('./'));
.pipe(gulp.dest('./'))
.on('end', cb);
});

gulp.task('changelog', 'Generates a CHANGELOG.md file.', ['changelog:script']);
196 changes: 150 additions & 46 deletions gulp_tasks/tasks/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,29 @@ var stripJsonComments = require('strip-json-comments');
var bump = $.bump;
var tap = $.tap;
var XML = require('node-jsxml').XML;
var streamqueue = require('streamqueue');
var git = $.git;
var gulpif = $.
if;
var gulpif = $.if;
var gutil = require('gulp-util');
var GitHubApi = require('github');
var runSequence = require('run-sequence').use(gulp);
var del = require('del');
var inquirer = require('inquirer');
var Q = require('q');
var githubUsername = require('github-username');
var helper = require('../common/helper');
var constants = require('../common/constants')();

var readTextFile = function(filename) {
var body = fs.readFileSync(filename, 'utf8');
return body;
};

var readJsonFile = function(filename) {
var body = readTextFile(filename);
return JSON.parse(stripJsonComments(body));
};

var filterFiles = function(files, extension) {
return _.filter(files, function(file) {
return path.extname(file) === extension;
});
};

/**
* Bumps any version in the constants.versionFiles
*
* USAGE:
* gulp bump --minor (or --major or --prerelease or --patch which is the default)
* - or -
* gulp bump --ver=1.2.3
* @param {function} cb - The gulp callback
* @returns {void}
*/
gulp.task('bump', false, function() {
gulp.task('bump', false, function(cb) {
var bumpType = 'patch';
// major.minor.patch
if(args.patch) {
Expand All @@ -60,16 +51,11 @@ gulp.task('bump', false, function() {
bumpType = process.env.BUMP || bumpType;

var version;
var srcjson = filterFiles(constants.versionFiles, '.json');
var srcxml = filterFiles(constants.versionFiles, '.xml');

// preparing the queue for bumping json and then xml files
var stream = streamqueue({
objectMode: true
});
var srcjson = helper.filterFiles(constants.versionFiles, '.json');
var srcxml = helper.filterFiles(constants.versionFiles, '.xml');

// first we bump the json files
stream.queue(gulp.src(srcjson)
gulp.src(srcjson)
.pipe(gulpif(args.ver !== undefined, bump({
version: args.ver
}), bump({
Expand All @@ -81,24 +67,30 @@ gulp.task('bump', false, function() {
version = json.version;
}
}))
.pipe(gulp.dest('./')));

// then after we have the correct value for version, we take care of the xml files
if(srcxml.length > 0) {
stream.queue(gulp.src(srcxml)
.pipe(tap(function(file) {
var xml = new XML(String(file.contents));
xml.attribute('version').setValue(version);
file.contents = Buffer.concat([new Buffer(xml.toXMLString())]);
}))
.pipe(gulp.dest('./' + constants.clientFolder)));
}
return stream.done();
.pipe(gulp.dest('./'))
.on('end', function() {
// then after we have the correct value for version, we take care of the xml files
if(srcxml.length > 0) {
gulp.src(srcxml)
.pipe(tap(function(file) {
var xml = new XML(String(file.contents));
xml.attribute('version').setValue(version);
file.contents = Buffer.concat([new Buffer(xml.toXMLString())]);
}))
.pipe(gulp.dest('./' + constants.clientFolder))
.on('end', function() {
cb();
});
} else {
cb();
}

});

});

gulp.task('commit', false, ['bump'], function() {
var pkg = readJsonFile('./package.json');
var pkg = helper.readJsonFile('./package.json');
var message = 'docs(changelog): version ' + pkg.version;
return gulp.src(constants.versionFiles)
.pipe(git.add({
Expand All @@ -107,22 +99,24 @@ gulp.task('commit', false, ['bump'], function() {
.pipe(git.commit(message));
});

gulp.task('tag', false, ['commit'], function() {
var pkg = readJsonFile('./package.json');
gulp.task('tag', false, ['commit'], function(cb) {
var pkg = helper.readJsonFile('./package.json');
var v = 'v' + pkg.version;
var message = pkg.version;
git.tag(v, message, function(err) {
if(err) {
throw new Error(err);
}
cb();
});
});

gulp.task('push', false, ['tag'], function() {
gulp.task('push', false, ['tag'], function(cb) {
exec('git push origin master && git push origin master --tags', function(err) {
if(err) {
throw new Error(err);
}
cb();
});
});

Expand All @@ -132,4 +126,114 @@ gulp.task('push', false, ['tag'], function() {
// }).on('close', done);
// });

gulp.task('release', 'Publish a new release version.', ['push']);
gulp.task('release', 'Publish a new release version.', ['push']);

// ############GITHUB SECTION#########

var github = new GitHubApi({
version: '3.0.0',
protocol: 'https',
timeout: 5000
});

var gitGetEmailAsync = Q.nbind(git.exec, git, {
args: 'config --get user.email',
quiet: true
});

var githubUsernameAsync = Q.nfbind(githubUsername);

var inquireAsync = function(result) {
var deferred = Q.defer();
inquirer.prompt(result.questions, function(answers) {
// only resolving what we need
deferred.resolve({
username: answers.username || result.username,
password: answers.password
});
});
return deferred.promise;
};

var githubAuthSetupAndTestAsync = function(result) {
var deferred = Q.defer();
github.authenticate({
type: 'basic',
username: result.username,
password: result.password
});
github.misc.rateLimit({}, function(err, res) {
if(err) {
deferred.reject(gutil.colors.red('GitHub auth failed! ') + 'Response from server: ' + gutil.colors.yellow(JSON.parse(err).message));
}
deferred.resolve(gutil.colors.green('GitHub auth successful!'));
});
return deferred.promise;
};

gulp.task('githubAuth', false, function(cb) {
return gitGetEmailAsync()
.then(githubUsernameAsync)
.then(function(username) {
return {
username: username,
questions: [{
type: 'input',
message: 'Please enter your GitHub username',
name: 'username',
default: username,
validate: function(input) {
return input !== '';
},
when: function() {
return username === null;
}
}, {
type: 'password',
message: 'Please enter your GitHub password',
name: 'password',
validate: function(input) {
return input !== '';
}
}]
};
})
.then(inquireAsync)
.then(githubAuthSetupAndTestAsync)
.tap(gutil.log);
});

gulp.task('release:createRelease', false, function(cb) {
var pkg = helper.readJsonFile('./package.json');
var v = 'v' + pkg.version;
var message = pkg.version;
var ownerRepo = constants.repository.split('/').slice(-2);

return gulp.src('CHANGELOG.md')
.pipe(tap(function(file) {
var body = file.contents.toString();
body = body.slice(body.indexOf('###'));
var msg = {
owner: ownerRepo[0],
repo: ownerRepo[1],
tag_name: v,
name: v + ': version ' + message,
body: body
};
github.releases.createRelease(msg, function(err, res) {
if(err) {
gutil.log(gutil.colors.red('Error: ' + err));
} else {
del('CHANGELOG.md');
}
});
}));
});

gulp.task('delay', false, function(cb) {
setTimeout(cb, 1000);
});

gulp.task('release:full', 'Publish a new release version on GitHub and upload a changelog.', function() {
return runSequence('githubAuth', 'changelog', 'push', 'delay', 'release:createRelease');
});
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
},
"homepage": "https://github.com/Yoobic/install-mac",
"devDependencies": {
"chalk": "^0.5.1",
"conventional-changelog": "0.0.11",
"del": "1.1.1",
"github": "0.2.3",
"github-username": "1.1.1",
"gulp": "3.8.10",
"gulp-bump": "0.1.11",
"gulp-concat": "2.4.2",
Expand All @@ -36,9 +40,10 @@
"gulp-help": "1.3.1",
"gulp-if": "1.2.5",
"gulp-load-plugins": "0.7.1",
"gulp-mux": "0.1.2",
"gulp-mux": "0.1.8",
"gulp-tap": "0.1.3",
"gulp-util": "3.0.1",
"inquirer": "0.8.0",
"lodash": "2.4.1",
"marked": "0.3.2",
"node-jsxml": "0.6.0",
Expand Down

0 comments on commit ff8a219

Please sign in to comment.