Skip to content

Commit

Permalink
build(*): Add build and dist task, allowing custom builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoslin committed Jan 16, 2013
1 parent b7fc0d2 commit cab4d1f
Showing 1 changed file with 78 additions and 32 deletions.
110 changes: 78 additions & 32 deletions grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
modules: '', //to be filled in by find-modules task
tplModules: '', //to be filled in by find-templates task
srcModules: [], //to be filled in by find-modules task
tplModules: [],
ngversion: '1.0.3',
pkg:'<json:package.json>',
dist: 'dist',
filename: 'ui-bootstrap',
meta: {
modules: 'angular.module("ui.bootstrap", [<%= modules %>]);',
modules: 'angular.module("ui.bootstrap", [<%= srcModules %>]);',
tplmodules: 'angular.module("ui.bootstrap.tpls", [<%= tplModules %>]);',
all: 'angular.module("ui.bootstrap", ["ui.bootstrap.tpls", <%= modules %>]);'
all: 'angular.module("ui.bootstrap", ["ui.bootstrap.tpls", <%= srcModules %>]);'
},
lint: {
files: ['grunt.js','src/**/*.js']
Expand All @@ -23,22 +25,22 @@ module.exports = function(grunt) {
},
concat: {
dist: {
src: ['<banner:meta.modules>', 'src/*/*.js'],
dest: 'dist/ui-bootstrap-<%= pkg.version %>.js'
src: ['<banner:meta.modules>'],
dest: '<%= dist %>/<%= filename %>-<%= pkg.version %>.js'
},
dist_tpls: {
src: ['<banner:meta.all>', 'src/*/*.js', '<banner:meta.tplmodules>', 'template/**/*.html.js'],
dest: 'dist/ui-bootstrap-tpls-<%= pkg.version %>.js'
src: ['<banner:meta.all>', '<banner:meta.tplmodules>'],
dest: '<%= dist %>/<%= filename %>-tpls-<%= pkg.version %>.js'
}
},
min: {
dist:{
src:['dist/ui-bootstrap-<%= pkg.version %>.js'],
dest:'dist/ui-bootstrap-<%= pkg.version %>.min.js'
src:['<%= dist %>/<%= filename %>-<%= pkg.version %>.js'],
dest:'<%= dist %>/<%= filename %>-<%= pkg.version %>.min.js'
},
dist_tpls:{
src:['dist/ui-bootstrap-tpls-<%= pkg.version %>.js'],
dest:'dist/ui-bootstrap-tpls-<%= pkg.version %>.min.js'
src:['<%= dist %>/<%= filename %>-tpls-<%= pkg.version %>.js'],
dest:'<%= dist %>/<%= filename %>-tpls-<%= pkg.version %>.min.js'
}
},
html2js: {
Expand All @@ -60,24 +62,64 @@ module.exports = function(grunt) {

//register before and after test tasks so we've don't have to change cli options on the goole's CI server
grunt.registerTask('before-test', 'lint html2js');
grunt.registerTask('after-test', 'find-modules find-templates concat min site');
grunt.registerTask('after-test', 'find-modules build min site');

// Default task.
grunt.registerTask('default', 'before-test test after-test');

//Common ui.bootstrap module containing all modules
grunt.registerTask('find-modules', 'Generate ui.bootstrap module depending on all existing directives', function() {
var modules = grunt.file.expandDirs('src/*').map(function(dir) {
return '"ui.bootstrap.' + dir.split("/")[1] + '"';
//Common ui.bootstrap module containing all modules for src and templates
grunt.registerTask('find-modules', 'Generate ui.bootstrap and template modules depending on all existing directives', function() {
grunt.file.expandDirs('src/*').forEach(function(dir) {
findModule(dir.split('/')[1]);
});
grunt.config('modules', modules);
});

grunt.registerTask('find-templates', 'Generate template modules depending on all existing directives', function() {
var tplModules = grunt.file.expand('template/**/*.html').map(function(file) {
return '"'+file+'"';
//Adds a given module to config
function findModule(name) {
function enquote(str) {
return '"' + str + '"';
}
var tplBase = 'template/' + name + '/*.html',
srcBase = 'src/' + name + '/*.js',
tplModules = grunt.config('tplModules'),
srcModules = grunt.config('srcModules');

grunt.file.expand(tplBase).map(function(file) {
tplModules.push(enquote(file));
});
grunt.file.expand(srcBase).forEach(function(file) {
srcModules.push(enquote('ui.bootstrap.' + name));
});

grunt.config('tplModules', tplModules);
grunt.config('srcModules', srcModules);
}

grunt.registerTask('dist', 'Override dist directory', function() {
var dir = this.args[0];
if (dir) { grunt.config('dist', dir); }
});

grunt.registerTask('build', 'Build custom bootstrap file', function() {
var srcFiles, tplFiles;
if (this.args.length) {
this.args.forEach(findModule);
srcFiles = this.args.map(function(name) {
return 'src/' + name + '/*.js';
});
tplFiles = this.args.map(function(name) {
return 'template/' + name + '/*.html.js';
});
grunt.config('filename', grunt.config('filename')+'-custom');
} else {
srcFiles = ['src/*/*.js'];
tplFiles = ['template/*/*.html.js'];
}
grunt.config('concat.dist.src',
grunt.config('concat.dist.src').concat(srcFiles));
grunt.config('concat.dist_tpls.src',
grunt.config('concat.dist_tpls.src').concat(srcFiles).concat(tplFiles));
grunt.task.run('concat');
});

grunt.registerTask('site', 'Create grunt demo site from every module\'s files', function() {
Expand Down Expand Up @@ -132,22 +174,24 @@ module.exports = function(grunt) {
});

//Html templates to $templateCache for tests
//@return filename of js file
function html2js(file) {
return htmljsName;
}
grunt.registerMultiTask('html2js', 'Generate js versions of html template', function() {
//Put templates on ng's run function so they are global
var TPL='angular.module("<%= file %>", []).run(["$templateCache", function($templateCache){\n' +
' $templateCache.put("<%= file %>",\n "<%= content %>");\n' +
'}]);\n';
var files = grunt._watch_changed_files || grunt.file.expand(this.data);

function escapeContent(content) {
return content.replace(/"/g, '\\"').replace(/\n/g, '" +\n "').replace(/\r/g, '');
}
files.forEach(function(file) {
var TPL='angular.module("<%= file %>", []).run(["$templateCache", function($templateCache){\n' +
' $templateCache.put("<%= file %>",\n "<%= content %>");\n' +
'}]);\n';
grunt.file.write(file + ".js", grunt.template.process(TPL, {
file: file,
content: escapeContent(grunt.file.read(file))
file: file,
content: escapeContent(grunt.file.read(file))
}));
});
function escapeContent(content) {
return content.replace(/"/g, '\\"').replace(/\n/g, '" +\n "').replace(/\r/g, '');
}
});

// Testacular configuration
Expand Down Expand Up @@ -194,4 +238,6 @@ module.exports = function(grunt) {
var options = ['--no-single-run', '--auto-watch'].concat(this.args);
runTestacular('start', options);
});
};

return grunt;
};

0 comments on commit cab4d1f

Please sign in to comment.