I'm using http://gruntjs.com/ to create a build script that iterates over a themes.json
file, the JSON include looks like this:
{
"vintage" : {
"_name" : "vintage",
"_long_name" : "Vintage",
"_js_includes" : [
"assets/js/plugins/bootstrap/bootstrap-transition.js",
[...]
"assets/js/plugins/*.js",
"assets/js/_*.js"
]
},
"pure" : {
"_name" : "pure",
"_long_name" : "Pure",
"_js_includes" : [
"assets/js/plugins/bootstrap/bootstrap-transition.js",
[...]
"assets/js/plugins/bootstrap/bootstrap-popover.js",
"assets/js/plugins/*.js",
"assets/js/_*.js"
]
}, "mystique" : {
"_name" : "mystique",
"_long_name" : "Mystique",
"_js_includes" : [
[...]
"assets/js/plugins/*.js",
"assets/js/_*.js"
]
}
}
I'm importing this file into grunt.initConfig
via the
grunt.initConfig({
themes: '<json:themes.json>',
[...]
method, problem is that the final theme in the array mystique
in my above example is somehow overriding the theme-specific tasks I'm trying to write. I want to be able to do grunt themes:pure
and have it grab the action which I'm registering dynamically inside a for
loop at this line for ( var new_key in themesObject ) {
of the code linked here:
The relevant bits, where things aren't working anyway, are the following:
for ( var key in themesObject ) {
initCommand = 'themes:'+themesObject[thisThemeName]._name; // --> themes:pure
taskString = themesObject[thisThemeName].taskString // --> lint pure:recess pure:concat pure:min pure:mincss pure:enqueue_ver
grunt.task.registerMultiTask( initCommand, taskString, function() {
grunt.log.writeln('Now executing task ['+this.target+'] for '+this.name+' theme...');
grunt.log.writeln( grunt.log.writeflags( this.data, this.target ) );
grunt.log.writeln( 'this.file = '+this.file);
})
The above code logs the initConfig
template in such a way that it seems correct and that running themes:[my-theme]
should be executing, however, no actual command execution happens, only the logging aspect.
Do I need to pass the taskString along to the registerMultiTask
function and repeat the task string inside the function itself, e.g.:
registerMultiTask( initCommand, taskString, function( taskString ) { taskString })
function and then do grunt.task.run(taskString)
inside each registered function? I understand that this method of passing variables is intended to receive command line input, but is there a way to pass a string to the registered function?
It seems that it's executing everything "flat" in the sense that it's executing the top-level of the JSON config object but not recursing to the deeper nested JSON where the execution should be happening... am I right here?
For anyone who's curious, this is Twitter Bootstrap with some modifications, basically, the goal here is to create child themes dynamically from themes.json
and export them to a sibling directory to the source where the grunt tasks and everything live... should be like this:
|source-directory
|--assets
|--|--css
|--|--|--bootstrap
|--|--|--|--less
|--|--scripts
|--|--|--plugins
|pure
|--assets
|--|--css
|--|--|--main.js
|--|--|--main.min.js
|--|--js
|--|--|--scripts.js
|--|--|--scripts.min.js
| vintage
|--assets
|--|--css
|--|--|--main.js
|--|--|--main.min.js
|--|--js
|--|--|--scripts.js
|--|--|--scripts.min.js
... etc.
I have a sneaking suspicion that somehow grunt won't let me dynamically define initConfig
to fill in the blanks with the for
loop in the way I'm intending... any light to shed on this would be much appreciated!
Update: since originally posting the question I've gotten things to the point where the dynamic assignment of themes
inside initConfig
is working as expected, but as of right now the template is echoing what I want but the tasks themselves are not running. I'm logging the results in each multiTask
(see code here - http://jsfiddle.net/6XKrp/2/) and getting the expected result for this.data
(the command I want specific to the theme->template), but the actions defined are not executing.
Update 2: things are logging as expected, and the tasks are being registered now, the problem is that they are either not executing, or just not executing recursively to the deeper levels of the JSON object, not sure why... does grunt.js
not execute recursively when called in this way?
Essentially, I was going about this incorrectly, not realizing that the JSON structure was non-negotiable at the top-level. You can have optional and/or multiple files, outputs, etc. nested beneath the top-level functions, but those top-level JSON data items are reserved for native functions and extensions.
Here's a gist of how I was able to accomplish what I outlined above: https://gist.github.com/4294776