Skip to content

Commit

Permalink
Merge pull request #2 from deepu105/lordlothar99-3594
Browse files Browse the repository at this point in the history
Use custom git wrapper
  • Loading branch information
lordlothar99 committed May 18, 2016
2 parents 2d98f29 + e34bcb0 commit 7c4f1b3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 55 deletions.
8 changes: 3 additions & 5 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,10 @@ module.exports = JhipsterGenerator.extend({
checkGit: function () {
if (!this.checkInstall || this.skipClient) return;
var done = this.async();
this.isGitInstalled(function () {
this.gitInstalled = true;
this.isGitInstalled(function (code) {
this.gitInstalled = code === 0;
done();
}.bind(this), function () {
done();
});
}.bind(this));
},

checkGitConnection: function () {
Expand Down
33 changes: 26 additions & 7 deletions generators/generator-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1423,17 +1423,36 @@ Generator.prototype.warning = function(msg) {
this.log(chalk.yellow.bold('WARNING! ') + msg);
};

Generator.prototype.isGitInstalled = function (successCb, errorCb) {
exec('git --version', function (err) {
if (err) {
Generator.prototype.isGitInstalled = function (callback) {
this.gitExec('--version', function (code) {
if (code !== 0) {
this.warning('git is not found on your computer.\n',
' Install git: ' + chalk.yellow('http://git-scm.com/')
);
errorCb && errorCb();
} else {
successCb && successCb();
}
callback && callback(code);
}.bind(this));
}
};

/*
* options is optional and takes any of child process options
* gitExec(args [, options ], callback)
* args can be an array of arguments
* The call back will receive code, stdout and stderr
*/
Generator.prototype.gitExec = function (args, options, callback) {
callback = arguments[arguments.length - 1];
if (arguments.length < 3) {
options = {};
}
options.async = true;
options.silent = true;

if (!Array.isArray(args)) {
args = [args];
}
var command = 'git ' + args.join(' ');
shelljs.exec(command, options, callback);
};

Generator.prototype.contains = _.includes;
1 change: 0 additions & 1 deletion generators/generator-constants.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 32 additions & 40 deletions generators/upgrade/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@ var util = require('util'),
generators = require('yeoman-generator'),
chalk = require('chalk'),
scriptBase = require('../generator-base'),
packagejs = require('../../package.json'),
fs = require('fs'),
shelljs = require('shelljs'),
semver = require('semver'),
Git = require('git-wrapper');
semver = require('semver');

var UpgradeGenerator = generators.Base.extend({});

util.inherits(UpgradeGenerator, scriptBase);

var configOptions = {};

/* Constants used throughout */
const GENERATOR_JHIPSTER = 'generator-jhipster',
UPGRADE_BRANCH = 'jhipster_upgrade';

module.exports = UpgradeGenerator.extend({
constructor: function () {
generators.Base.apply(this, arguments);

this.git = new Git();
this.force = this.options['force'];
},

Expand All @@ -40,44 +34,41 @@ module.exports = UpgradeGenerator.extend({

_gitCheckout: function(branch) {
var done = this.async();
this.git.exec('checkout', {q: true}, [branch], function(err, msg) {
if (err != null) this.error('Unable to checkout branch ' + branch + ':\n' + err);
this.gitExec(['checkout', '-q', branch], function(code, msg, err) {
if (code !== 0) this.error('Unable to checkout branch ' + branch + ':\n' + err);
this.log('Checked out branch \'' + branch + '\'');
done();
}.bind(this));
},

_gitCommitAll: function(commitMsg, callback) {
var commit = function() {
this.git.exec('commit', {q: true}, ['-m \"' + commitMsg + '\"', '-a', '--allow-empty'], function(err, msg) {
if (err != null) this.error('Unable to commit in git:\n' + err);
this.gitExec(['commit', '-q', '-m', '\"' + commitMsg + '\"', '-a', '--allow-empty'], function(code, msg, err) {
if (code !== 0) this.error('Unable to commit in git:\n' + err);
this.log('Committed: ' + commitMsg);
callback();
}.bind(this));
}.bind(this);
/* TODO change: temporary workaround due to https://github.com/pvorb/node-git-wrapper/pull/7 */
shelljs.exec('git add -A', {maxBuffer: 1024 * 500}, function(code, stdout, stderr) {
if (code !== 0) this.error('Unable to add resources in git:\n' + stderr);
this.gitExec(['add', '-A'], {maxBuffer: 1024 * 500}, function(code, msg, err) {
if (code !== 0) this.error('Unable to add resources in git:\n' + err);
commit();
}.bind(this));
},

configuring: {
assertGitPresent: function() {
var done = this.async();
this.isGitInstalled(function () {
done();
}, function () {
this.error('Exiting the process.');
this.isGitInstalled(function (code) {
if (code !== 0) this.error('Exiting the process.');
done();
});
}.bind(this));
},

checkLatestVersion: function() {
this.log('Looking for latest ' + GENERATOR_JHIPSTER + ' version...');
var done = this.async();
shelljs.exec('npm show ' + GENERATOR_JHIPSTER + ' version', {silent:true}, function (code, stdout, stderr) {
this.latestVersion = stdout.replace('\n','');
shelljs.exec('npm show ' + GENERATOR_JHIPSTER + ' version', {silent:true}, function (code, msg, err) {
this.latestVersion = msg.replace('\n','');
if (semver.lt(this.currentVersion, this.latestVersion)) {
this.log(chalk.green('New ' + GENERATOR_JHIPSTER + ' version found: ' + this.latestVersion));
} else if (this.force) {
Expand All @@ -92,8 +83,8 @@ module.exports = UpgradeGenerator.extend({
assertGitRepository: function() {
if (! fs.existsSync('.git')) {
var done = this.async();
this.git.exec('init', {}, [], function(err, msg) {
if (err != null) this.error('Unable to initialize a new git repository:\n' + err);
this.gitExec('init', function(code, msg, err) {
if (code !== 0) this.error('Unable to initialize a new git repository:\n' + err);
this.log('Initialized a new git repository');
this._gitCommitAll('Initial', function() {
done();
Expand All @@ -104,8 +95,8 @@ module.exports = UpgradeGenerator.extend({

assertNoLocalChanges: function() {
var done = this.async();
this.git.exec('status', {}, ['--porcelain'], function(err, msg) {
if (err != null) this.error('Unable to check for local changes:\n' + err);
this.gitExec(['status', '--porcelain'], function(code, msg, err) {
if (code !== 0) this.error('Unable to check for local changes:\n' + err);
if (msg != null && msg !== '') {
this.warning(' local changes found.\n' +
'\tPlease commit/stash them before upgrading');
Expand All @@ -117,8 +108,8 @@ module.exports = UpgradeGenerator.extend({

detectCurrentBranch: function() {
var done = this.async();
this.git.exec('rev-parse', {q: true}, ['--abbrev-ref', 'HEAD'], function(err, msg) {
if (err != null) this.error('Unable to detect current git branch:\n' + err);
this.gitExec(['rev-parse', '-q', '--abbrev-ref', 'HEAD'], function(code, msg, err) {
if (code !== 0) this.error('Unable to detect current git branch:\n' + err);
this.sourceBranch = msg.replace('\n','');
done();
}.bind(this));
Expand All @@ -127,15 +118,15 @@ module.exports = UpgradeGenerator.extend({
prepareUpgradeBranch: function() {
var done = this.async();
var createUpgradeBranch = function(callback) {
this.git.exec('branch', {q: true}, [UPGRADE_BRANCH], function(err, msg) {
if (err != null) this.error('Unable to create ' + UPGRADE_BRANCH + ':\n' + err);
this.gitExec(['branch', '-q', UPGRADE_BRANCH], function(code, msg, err) {
if (code !== 0) this.error('Unable to create ' + UPGRADE_BRANCH + ':\n' + err);
this.log('Created branch ' + UPGRADE_BRANCH);
this._gitCheckout(UPGRADE_BRANCH);
callback();
}.bind(this));
}.bind(this);
this.git.exec('rev-parse', {q: true}, ['--verify', UPGRADE_BRANCH], function(err, msg) {
if (err != null) createUpgradeBranch(done);
this.gitExec(['rev-parse', '-q', '--verify', UPGRADE_BRANCH], function(code, msg, err) {
if (code !== 0) createUpgradeBranch(done);
else done();
}.bind(this));
}
Expand All @@ -150,33 +141,34 @@ module.exports = UpgradeGenerator.extend({
updateJhipster: function() {
this.log('Updating ' + GENERATOR_JHIPSTER + '. This might take some time...');
var done = this.async();
shelljs.exec('npm install -g ' + GENERATOR_JHIPSTER, {silent:true}, function (code, stdout, stderr) {
shelljs.exec('npm install -g ' + GENERATOR_JHIPSTER, {silent:true}, function (code, msg, err) {
if (code === 0) this.log(chalk.green('Updated ' + GENERATOR_JHIPSTER + ' to version ' + this.latestVersion));
else this.error('Something went wrong while updating generator! ' + stderr);
else this.error('Something went wrong while updating generator! ' + err);
done();
}.bind(this));
},

cleanUp: function() {
var done = this.async();
shelljs.rm('-rf', '!(.yo-rc.json|.git)');
this.log('Cleaned up directory');
if (shelljs.rm('-rf', '!(.yo-rc.json|.git)').code === 0 ) {
this.log('Cleaned up directory');
}
done();
},

generate: function() {
this.log('Regenerating app with jhipster ' + this.latestVersion + '...');
var done = this.async();
shelljs.exec('yo jhipster --force --with-entities', {silent:false}, function (code, stdout, stderr) {
shelljs.exec('yo jhipster --force --with-entities', {silent:false}, function (code, msg, err) {
if (code === 0) this.log(chalk.green('Successfully regenerated app with jhipster ' + this.latestVersion));
else this.error('Something went wrong while generating project! '+ stderr);
else this.error('Something went wrong while generating project! '+ err);
done();
}.bind(this));
},

commitChanges: function() {
var done = this.async();
this._gitCommitAll('Upgrade to ' + this.latestVersion, function() {
this._gitCommitAll('Upgrade to JHipster ' + this.latestVersion, function() {
done();
});
},
Expand All @@ -188,8 +180,8 @@ module.exports = UpgradeGenerator.extend({
mergeChangesBack: function() {
this.log('Merging changes back to ' + this.sourceBranch + '...');
var done = this.async();
this.git.exec('merge', {q: true}, [UPGRADE_BRANCH], function(err, msg) {
if (err != null) this.error('Unable to merge changes back to ' + this.sourceBranch + ':\n' + err);
this.gitExec(['merge', '-q', UPGRADE_BRANCH], function(code, msg, err) {
if (code !== 0) this.error('Unable to merge changes back to ' + this.sourceBranch + ':\n' + err);
this.log(chalk.green('Merge done !') + '\n\tPlease now fix conflicts if any, and commit !');
done();
}.bind(this));
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
"js-yaml": "3.6.0",
"yo": "1.7.0",
"semver": "5.1.0",
"jhipster-uml": "1.6.1",
"git-wrapper": "0.1.1"
"jhipster-uml": "1.6.1"
},
"devDependencies": {
"eslint": "2.8.0",
Expand Down

0 comments on commit 7c4f1b3

Please sign in to comment.