Skip to content

Instantly share code, notes, and snippets.

@jonschlinkert
Forked from brianfeister/attach_heads.js
Created December 19, 2012 03:49
Show Gist options
  • Select an option

  • Save jonschlinkert/4334244 to your computer and use it in GitHub Desktop.

Select an option

Save jonschlinkert/4334244 to your computer and use it in GitHub Desktop.
/**
* Task: attach_heads
* Description: Set the heads for all themes declared in themes.json
*/
module.exports = function(grunt) {
'use strict';
var fs = require('fs');
var path = require('path');
grunt.registerTask('attach_heads', 'Set the heads recursively for all JS/CSS in themes.json', function() {
var themeMetaObj = grunt.file.readJSON('themes.json');
var pkgObject = grunt.file.readJSON('package.json');
var banner =
' (v.'+pkgObject.version+')\n' +
' * by '+pkgObject.name+' \n' +
' * '+pkgObject.homepage+'\n' +
' * Copyright (c) '+grunt.template.today("yyyy")+' '+pkgObject.name+'\n' +
' * \n' +
' * ============================================= */ \n';
// iterate over the themeMetaObj for number of themes (default is declared here as well)
for ( var key in themeMetaObj ) {
var thisTheme = themeMetaObj[key];
var headObj = {
'devCss' : '../'+thisTheme._name+'/assets/css/main.css',
'prodCss' : '../'+thisTheme._name+'/assets/css/main.min.css',
'devJs' : '../'+thisTheme._name+'/assets/js/scripts.js',
'prodJs' : '../'+thisTheme._name+'/assets/js/scripts.min.js'
};
var thisBanner = '';
thisBanner = '/*! ================================================ \n' +
' * \n' +
' * '+thisTheme._long_name+' Theme ' +
banner;
for ( var fileKey in headObj ) {
var thisFile = headObj[fileKey];
var content = grunt.file.read( thisFile );
content = thisBanner + content;
grunt.file.write( thisFile, content );
grunt.log.writeln('"' + thisFile + '" updated with latest head.');
}
}
});
};
/*jshint laxcomma:true */
module.exports = function(grunt) {
grunt.initConfig({
pkg: '<json:package.json>',
themeMeta: '<json:themes.json>',
meta: {
// // set dynamically in generateThemeConfig()
},
lint: {
files: ['assets/js/_*.js', 'grunt.js']
},
recess: {
// set dynamically in generateThemeConfig()
},
concat: {
// set dynamically in generateThemeConfig()
},
min: {
// set dynamically in generateThemeConfig()
},
mincss: {
// set dynamically in generateThemeConfig()
},
watch: {
// set dynamically in generateThemeConfig()
}
});
grunt.loadTasks('build/tasks');
grunt.loadNpmTasks('grunt-contrib');
grunt.loadNpmTasks('grunt-recess');
function generateThemeConfig( registerThemeTask ) {
var i
, len
, o
, allTasksLen
, msg
, themeMetaObj = grunt.file.readJSON('themes.json')
, pkgObject = grunt.file.readJSON('package.json')
// tasks performed on all themes are declared in package.json
, themeSpecificTasks = pkgObject.themeSpecificTasks;
// iterate over the themeMetaObj for number of themes (default is declared here as well)
for ( var key in themeMetaObj ) {
var thisTheme = themeMetaObj[key]
, taskArray = [];
grunt.config( 'meta.'+thisTheme._name, {
banner: '/*! ================================================\n' +
' * '+thisTheme._long_name+' Theme by example.co \n' +
' * v.<%= pkg.version %>\n' +
' * Copyright (c) <%= grunt.template.today("yyyy") %> ' +
' example.Co */\n' +
' * ================================================ */'
});
grunt.config( 'recess.'+thisTheme._name+'Dev', {
// set dynamically in generateThemeBundles()
src: 'assets/css/less/'+thisTheme._name+'/master.less',
dest: '../'+thisTheme._name+'/assets/css/main.css',
options: {
compile: true
}
});
grunt.config( 'recess.'+thisTheme._name+'Prod', {
// set dynamically in generateThemeBundles()
src: 'assets/css/less/'+thisTheme._name+'/master.less',
dest: '../'+thisTheme._name+'/assets/css/main.min.css',
options: {
compile: true,
compress: true
}
});
grunt.config( 'concat.'+thisTheme._name, {
src: thisTheme._js_includes,
dest: '../'+thisTheme._name+'/assets/js/scripts.js'
});
grunt.config( 'min.'+thisTheme._name, {
src: ['<'+thisTheme._name+'Banner>', 'assets/js/scripts.js'],
dest: '../'+thisTheme._name+'/assets/js/scripts.min.js'
});
}
registerThemeTask();
}
// grunt.log.writeln( grunt.log.writeflags( grunt.config.get(), prefix) );
// register themes as a callback to ensure config is generated first
generateThemeConfig( function() {
var themeMetaObj = grunt.file.readJSON('themes.json')
, pkgObject = grunt.file.readJSON('package.json')
// tasks performed on all themes are declared in package.json
// in attribute name `themeSpecificTasks`
, themeSpecificTasks = pkgObject.themeSpecificTasks
, allTaskArray = [];
// iterate over the themeMetaObj for number of themes (default is declared here as well)
for ( var key in themeMetaObj ) {
var thisTheme = themeMetaObj[key]
, taskArray = [];
// now that our config is setup, let's register this as a MultiTask
taskArray = ['lint'];
for ( var tasksKey in themeSpecificTasks ) {
if ( themeSpecificTasks[tasksKey] === 'recess' ) {
taskArray.push( themeSpecificTasks[tasksKey]+':'+thisTheme._name+'Dev' );
taskArray.push( themeSpecificTasks[tasksKey]+':'+thisTheme._name+'Prod' );
} else {
taskArray.push( themeSpecificTasks[tasksKey]+':'+thisTheme._name );
}
}
taskArray.push( 'enqueue_ver' );
allTaskArray = allTaskArray.concat( taskArray );
grunt.registerTask( 'theme:'+thisTheme._name, taskArray);
}
grunt.registerTask( 'default', allTaskArray );
});
};
{
"name": "example.com",
"description": "Tagline here",
"version": "1.0",
"homepage": "http://example.com",
"author": "[email protected]",
"dependencies": {
"grunt": "~0.3.15",
"grunt-contrib": "~0.2.0",
"grunt-recess": "~0.1.1",
"uglify-js": "~1.3.3",
"jshint": "~0.5.9"
},
"devDependencies": {},
"themeSpecificTasks" : [
"recess",
"concat",
"min",
"mincss",
"enqueue_ver"
]
}
{
"default" : {
"_name" : "default",
"_long_name" : "Default",
"_js_includes" : [
"assets/js/plugins/bootstrap/bootstrap-transition.js",
"assets/js/plugins/bootstrap/bootstrap-alert.js",
"assets/js/plugins/bootstrap/bootstrap-button.js",
"assets/js/plugins/bootstrap/bootstrap-carousel.js",
"assets/js/plugins/bootstrap/bootstrap-collapse.js",
"assets/js/plugins/bootstrap/bootstrap-dropdown.js",
"assets/js/plugins/bootstrap/bootstrap-modal.js",
"assets/js/plugins/bootstrap/bootstrap-tooltip.js",
"assets/js/plugins/bootstrap/bootstrap-popover.js",
"assets/js/plugins/bootstrap/bootstrap-scrollspy.js",
"assets/js/plugins/bootstrap/bootstrap-tab.js",
"assets/js/plugins/bootstrap/bootstrap-typeahead.js",
"assets/js/plugins/*.js",
"assets/js/_*.js"
]
},
"vintage" : {
"_name" : "vintage",
"_long_name" : "Vintage",
"_js_includes" : [
"assets/js/plugins/bootstrap/bootstrap-transition.js",
"assets/js/plugins/bootstrap/bootstrap-alert.js",
"assets/js/plugins/bootstrap/bootstrap-button.js",
"assets/js/plugins/bootstrap/bootstrap-collapse.js",
"assets/js/plugins/bootstrap/bootstrap-dropdown.js",
"assets/js/plugins/bootstrap/bootstrap-tooltip.js",
"assets/js/plugins/bootstrap/bootstrap-popover.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-alert.js",
"assets/js/plugins/bootstrap/bootstrap-button.js",
"assets/js/plugins/bootstrap/bootstrap-collapse.js",
"assets/js/plugins/bootstrap/bootstrap-dropdown.js",
"assets/js/plugins/bootstrap/bootstrap-tooltip.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/bootstrap/bootstrap-transition.js",
"assets/js/plugins/bootstrap/bootstrap-alert.js",
"assets/js/plugins/bootstrap/bootstrap-button.js",
"assets/js/plugins/bootstrap/bootstrap-collapse.js",
"assets/js/plugins/bootstrap/bootstrap-dropdown.js",
"assets/js/plugins/bootstrap/bootstrap-tooltip.js",
"assets/js/plugins/bootstrap/bootstrap-popover.js",
"assets/js/plugins/*.js",
"assets/js/_*.js"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment