Skip to content

Instantly share code, notes, and snippets.

@jmendiara
Created April 14, 2014 07:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmendiara/10625082 to your computer and use it in GitHub Desktop.
Save jmendiara/10625082 to your computer and use it in GitHub Desktop.
Execute Grunt tasks iteratively based on external config. It will allow you to modify the input parameters for the same tasks without modifying the Gruntfile.
{
"description": "Distribution for Spain",
"distros": [
{
"id": "dev",
"title": "Development Version",
"endpoint": "http://localhost:3000/"
},
{
"id": "qa",
"title": "QA Version",
"endpoint": "http://qa.hi.inet/"
},
{
"id": "prod",
"title": "Production Version",
"endpoint": "http://movistar.es/"
}
]
}
grunt.initConfig(
//The build task is in charge of creating a compilation enforcing
//one parameter in the HTML code: the url
build: {
//The default options for the build task
options: {
//The source file
src: 'app/index.html'
}
/*
* We will create here configurations dinamycally,
* one for each distribution in the external config distro file
* we will generate:
dev: {
url: 'http://localhost:3000/'
dest: 'dist/dev/index.html'
},
qa: {
url: 'http://qa.hi.inet/',
dest: 'dist/qa/index.html'
},
prod: {
url: 'http://movistar.es/',
dest: 'dist/prod/index.html'
}
*/
}
);
/**
* Schedules a Grunt task to be run for a distro determined by
* the distro.js configuration file
* @param {String} task the task name to execute
* @cb {Function} cb Callback that returns a grunt
* configuration for that task
*/
function schedule(task, cb) {
//Get the config for task ('build' in the example)
var config = grunt.config.get(task),
//Get the stored config for the distro (the distro.js file)
distros = grunt.config('distro').distro;
distros
.map(cb)
.forEach(function(configItem, index) {
var distroId = distros[index].id;
//Set the config for build:[dev, qa, prod]
config[distroId] = configItem;
//Schedule task to be run. build:[dev, qa, prod]
grunt.task.run([task + ':' + distroId]);
});
//Store the config for 'build';
//It has now the config for build:[dev, qa, prod]
grunt.config.set(task, config);
}
grunt.registerTask('distros:build','Generate the distributions', function() {
//DO the MAGIC here: We schedule a task, (build)
//But we set the config for the specific distro in the
//iterator function
schedule('build', function iterator(distro) {
//We generate the configuration for the build task and this distro
return {
url: distro.endpoint,
dest: 'dist/' + distro.id + '/index.html'
}
});
//Here we have configured the build task with three new subtasks (dev, qa, prod)
//And we have scheduled its running.
});
grunt.registerTask('readDistributionFile', 'reads and configures Grunt for several distributions', function() {
var distroFile = grunt.option('distro') ?
'build/config/samples/distro.' + grunt.option('distro') + '.json' :
'build/config/distro.json';
var distro = grunt.file.readJSON(distroFile);
grunt.config.set('distro', distro);
});
grunt.registerTask('build','Generate the distributions', [
'readDistributionFile',
'distros:build'
]);
# Generate the software for dev, qa & production for Spain
grunt build --distro=es
# Generate the software dev, qa & production for Brazil
grunt build --distro=br
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment