Skip to content

Instantly share code, notes, and snippets.

@JorgenEvens
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save JorgenEvens/04f9766cf0acde91ca07 to your computer and use it in GitHub Desktop.

Select an option

Save JorgenEvens/04f9766cf0acde91ca07 to your computer and use it in GitHub Desktop.
Boilerplate Gruntfile
module.exports = function(grunt) {
var pkg = grunt.file.readJSON('package.json');
grunt.initConfig({
pkg: pkg,
/* Deployment */
'ftp-deploy': {
build: {
auth: {
host: 'ftp.host',
port: 21,
authKey: 'main'
},
src: 'src',
dest: '/target/dir',
exclusions: ['**/.DS_Store','**/.git','**/*.styl']
}
},
/* External libraries */
copy: {
WebLab: {
files: [
{ expand: true,
cwd: process.cwd() + '/../../php/PEAR/',
src: ['WebLab/**', '!.git/'],
dest: process.cwd() + '/src/'
}
]
}
},
/* CSS */
stylus: {
development: {
options: {
urlfunc: 'embed',
compress: false,
define: {
version: pkg.version
},
use: [
require('nib')
],
import: []
},
files: {
'src/www/css/main.css': 'src/www/css/main.styl'
}
},
production: {
options: {
urlfunc: 'embed',
compress: true,
define: {
version: pkg.version
},
use: [
require('nib')
],
import: []
},
files: {
'src/www/css/main.css': 'src/www/css/main.styl'
}
}
},
/* Development */
watch: {
stylus: {
files: ['src/www/css/**/*.styl'],
tasks: ['stylus:development'],
options: {
spawn: false
}
},
reload: {
files: ['src/www/css/main.css', 'src/**/*.php'],
options: {
livereload: true,
spawn: false
}
}
},
/* Git operations */
gitcommit: {
version: {
options: {
message: 'Bump version to v<%= pkg.version %>'
},
files: {
src: ['package.json', 'src/Application/*.json']
}
}
},
gitpush: {
version: {}
},
'git-describe': {
options: {
template: ''
},
status: {}
}
});
/* Versioning */
(function() {
var updateFragment = function( i ) {
var version = grunt.config.get('pkg.version').split('.');
version[i]++;
while( ++i < 3 )
version[i] = 0;
version = version.join('.');
grunt.config.set('pkg.version', version);
grunt.file.write('package.json', JSON.stringify(pkg, null, 4));
grunt.file.expand('src/Application/*.json').forEach(function(i){
var json = grunt.file.readJSON(i);
json.Application.Version = version;
grunt.file.write(i, JSON.stringify(json,null,4));
})
},
update = function(i) {
return function() { return updateFragment(i); }
};
// Register tasks for different parts of the version number
grunt.registerTask('version-major', update(0));
grunt.registerTask('version-minor', update(1));
grunt.registerTask('version-patch', update(2));
}());
// Check if local repository is clean
grunt.registerTask('is-clean', function() {
grunt.event.once('git-describe', function(v){
if( v.dirty == '-dirty' )
grunt.fail.warn('Repository is still dirty! Commit your changes or stash them.');
})
grunt.task.run('git-describe');
});
grunt.loadNpmTasks('grunt-ftp-deploy');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-stylus');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-git');
grunt.loadNpmTasks('grunt-git-tag');
grunt.loadNpmTasks('grunt-git-describe');
grunt.registerTask('default',['build']);
// Copy external resources and compile a production main.css file
grunt.registerTask('build', ['copy', 'stylus:production']);
// Create a new version, tag it in git and push it to the default remote
grunt.registerTask('version', ['is-clean', 'version-patch', 'gitcommit:version', 'git-tag', 'gitpush:version']);
// When a deploy fails, try it again without versioning the project
grunt.registerTask('retry', ['build', 'ftp-deploy'] );
// Attempt to deploy a production build, versioning it for rollback
grunt.registerTask('deploy',['build', 'version', 'ftp-deploy']);
};
{
"author": "Jorgen Evens",
"version": "0.0.0",
"repository": {
"type": "git",
"url": ""
},
"devDependencies": {
"grunt-contrib-copy": "~0.4.1",
"grunt-contrib-stylus": "~0.9.0",
"grunt-contrib-watch": "~0.5.3",
"grunt-ftp-deploy": "~0.1.2",
"grunt-git": "^0.2.14",
"grunt-git-describe": "^2.3.2",
"grunt-git-tag": "0.0.5",
"nib": "~1.0.1",
"stylus": "~0.38.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment