Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Ahmed committed Jun 30, 2012
0 parents commit 6aa9f9d
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions .npmignore
@@ -0,0 +1 @@
/node_modules/
22 changes: 22 additions & 0 deletions LICENSE-MIT
@@ -0,0 +1,22 @@
Copyright (c) 2012 Adam Ahmed

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
58 changes: 58 additions & 0 deletions README.md
@@ -0,0 +1,58 @@
# grunt-heroku-deploy

Task to switch to a deploy branch and push to heroku

## Getting Started
Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-heroku-deploy`

Then add this line to your project's `grunt.js` gruntfile:

```javascript
grunt.loadNpmTasks('grunt-heroku-deploy');
```

[grunt]: https://github.com/cowboy/grunt
[getting_started]: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md

## Documentation

Here's how I use this:

I do my dev on master which is set up to push to wherever I'm storing code.

git branch --set-upstream master origin/master

I set up a branch locally that will be my deploy branch - it might have some different configuration's committed. It pushes to heroku by default.

git branch --set-upstream deploy heroku/master

Now I can run `grunt heroku-deploy` while on the master branch, which will do:

git checkout deploy # switch to the deploy branch
git merge master # merge in the changes I was making
git push # push it to heroku
git checkout master # switch back to where I was so I can continue developing

If you want to specify the deploy branch name, use the 'deployBranch' property on each target like so:
```javascript
grunt.initConfig({
'heroku-deploy' : {
production : {
deployBranch : 'prod'
},
staging : {
deployBranch : 'staging'
}
}
});
```

## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt][grunt].

## Release History
0.1.0 - "works for me". Needs testing and feedback.

## License
Copyright (c) 2012 Adam Ahmed
Licensed under the MIT license.
2 changes: 2 additions & 0 deletions bin/grunt-heroku-deploy
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('grunt').npmTasks('grunt-heroku-deploy').cli();
40 changes: 40 additions & 0 deletions grunt.js
@@ -0,0 +1,40 @@
module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
test: {
files: ['test/**/*.js']
},
lint: {
files: ['grunt.js', 'tasks/**/*.js', 'test/**/*.js']
},
watch: {
files: '<config:lint.files>',
tasks: 'default'
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
node: true,
es5: true
},
globals: {}
}
});

// Load local tasks.
grunt.loadTasks('tasks');

// Default task.
grunt.registerTask('default', 'lint test');

};
41 changes: 41 additions & 0 deletions package.json
@@ -0,0 +1,41 @@
{
"name": "grunt-heroku-deploy",
"description": "Task to switch to a deploy branch and push to heroku",
"version": "0.1.0",
"homepage": "https://github.com/hitsthings/grunt-heroku-deploy",
"author": {
"name": "Adam Ahmed",
"email": "hitsthings@gmail.com",
"url": "http://www.noiregrets.com"
},
"repository": {
"type": "git",
"url": "git://github.com/hitsthings/grunt-heroku-deploy.git"
},
"bugs": {
"url": "https://github.com/hitsthings/grunt-heroku-deploy/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/hitsthings/grunt-heroku-deploy/blob/master/LICENSE-MIT"
}
],
"main": "grunt.js",
"bin": "bin/grunt-heroku-deploy",
"engines": {
"node": "*"
},
"scripts": {
"test": "grunt test"
},
"dependencies": {
"grunt": "~0.3.9"
},
"devDependencies": {
"grunt": "~0.3.9"
},
"keywords": [
"gruntplugin"
]
}
117 changes: 117 additions & 0 deletions tasks/heroku-deploy.js
@@ -0,0 +1,117 @@
/*
* grunt-heroku-deploy
* https://github.com/hitsthings/grunt-heroku-deploy
*
* Copyright (c) 2012 Adam Ahmed
* Licensed under the MIT license.
*/

var spawn = require('child_process').spawn;

function pipeAll(proc) {
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
return proc;
}

function allOutput(proc, next) {
var out = '';
proc.stdout.on('data', function(data) { out += data; });
proc.stdout.on('end', function() {
next(null, out);
});
}

function doDeploy(originRef, deployRef, next) {
pipeAll(spawn('git', ['checkout', deployRef])).on('exit', function() {
pipeAll(spawn('git', ['merge', originRef])).on('exit', function() {
pipeAll(spawn('git', ['push'])).on('exit', function() {
pipeAll(spawn('git', ['checkout', originRef])).on('exit', function() {
next();
});
});
});
});
}

function getCurrentBranch(next) {
allOutput(spawn('git', ['branch']), function(err, out) {
if (err) {
return next(err);
}

var newline, current, branch;
if (out[0] === '*') {
current = -1;
} else {
current = out.indexOf('\n*');
}
if (!~current) {
return next(new Error("Current branch could not be determined."));
}

current++;

newline = out.indexOf('\n', current);
branch = out.substring(current + 2, ~newline ? newline : undefined);

next(null, branch === '(no branch)' ? null : branch);
});
}

function getCurrentCommitHash(next) {
allOutput(spawn('git', ['log', '-1', '--format=format:"%H"']), function(err, out) {
next(err, out && out.replace(/\n|\r/g, ''));
});
}

module.exports = function(grunt) {

// Please see the grunt documentation for more information regarding task and
// helper creation: https://github.com/cowboy/grunt/blob/master/docs/toc.md

// ==========================================================================
// TASKS
// ==========================================================================

grunt.registerMultiTask('heroku-deploy', 'Switch to the deploy branch, merge your starting location, push, and switch back', function() {
grunt.log.write(
grunt.helper(
'heroku-deploy',
grunt.config(['heroku-deploy', this.target, 'deployBranch']),
this.async()
)
);
});

// ==========================================================================
// HELPERS
// ==========================================================================

grunt.registerHelper('heroku-deploy', function(deployBranch, next) {
deployBranch = deployBranch || 'deploy';

getCurrentBranch(function(err, branch) {
if (err) {
return next(err);
}

if (!branch) {
console.log('No current branch');

getCurrentCommitHash(function(err, csid) {
if (err) {
return next(err);
}

console.log('Using ' + csid + ' as ref to merge.');
doDeploy(csid, deployBranch, next);
});
} else {
console.log('Current branch is ' + branch);
doDeploy(branch, deployBranch, next);
}
});
});

};
34 changes: 34 additions & 0 deletions test/heroku-deploy_test.js
@@ -0,0 +1,34 @@
var grunt = require('grunt');

/*
======== A Handy Little Nodeunit Reference ========
https://github.com/caolan/nodeunit
Test methods:
test.expect(numAssertions)
test.done()
Test assertions:
test.ok(value, [message])
test.equal(actual, expected, [message])
test.notEqual(actual, expected, [message])
test.deepEqual(actual, expected, [message])
test.notDeepEqual(actual, expected, [message])
test.strictEqual(actual, expected, [message])
test.notStrictEqual(actual, expected, [message])
test.throws(block, [error], [message])
test.doesNotThrow(block, [error], [message])
test.ifError(value)
*/

exports['heroku-deploy'] = {
setUp: function(done) {
// setup here
done();
},
'helper': function(test) {
test.expect(1);
// tests here
test.equal(1, 1, 'should return the correct value.');
test.done();
}
};

0 comments on commit 6aa9f9d

Please sign in to comment.