Skip to content

Commit ae4206b

Browse files
committed
chore(build): Simplify grunt tasks, update dependencies
* Move custom html2js task to grunt-html2js npmTask * Move site task to simple grunt-contrib-copy and grunt.template.process step * Centralize module information into one config object * Centralize demo assets into one folder
1 parent 3a5785c commit ae4206b

18 files changed

+144
-158
lines changed

Gruntfile.js

+127-143
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ module.exports = function(grunt) {
44

55
grunt.loadNpmTasks('grunt-contrib-watch');
66
grunt.loadNpmTasks('grunt-contrib-concat');
7+
grunt.loadNpmTasks('grunt-contrib-copy');
78
grunt.loadNpmTasks('grunt-contrib-jshint');
89
grunt.loadNpmTasks('grunt-contrib-uglify');
10+
grunt.loadNpmTasks('grunt-html2js');
911

1012
// Project configuration.
1113
grunt.initConfig({
1214
ngversion: '1.0.5',
1315
bsversion: '2.3.1',
14-
srcModules: [], //to be filled in by find-modules task
15-
tplModules: [],
16+
modules: [],//to be filled in by build task
1617
pkg: grunt.file.readJSON('package.json'),
1718
dist: 'dist',
1819
filename: 'ui-bootstrap',
20+
filenamecustom: '<%= filename %>-custom',
1921
meta: {
2022
modules: 'angular.module("ui.bootstrap", [<%= srcModules %>]);',
2123
tplmodules: 'angular.module("ui.bootstrap.tpls", [<%= tplModules %>]);',
@@ -39,17 +41,40 @@ module.exports = function(grunt) {
3941
options: {
4042
banner: '<%= meta.modules %>\n'
4143
},
42-
src: [],
44+
src: [], //src filled in by build task
4345
dest: '<%= dist %>/<%= filename %>-<%= pkg.version %>.js'
4446
},
4547
dist_tpls: {
4648
options: {
4749
banner: '<%= meta.all %>\n<%= meta.tplmodules %>\n'
4850
},
49-
src: [],
51+
src: [], //src filled in by build task
5052
dest: '<%= dist %>/<%= filename %>-tpls-<%= pkg.version %>.js'
5153
}
5254
},
55+
copy: {
56+
demohtml: {
57+
options: {
58+
//process html files with gruntfile config
59+
processContent: grunt.template.process
60+
},
61+
files: [{
62+
expand: true,
63+
src: ["**/*.html"],
64+
cwd: "misc/demo/",
65+
dest: "dist/"
66+
}]
67+
},
68+
demoassets: {
69+
files: [{
70+
expand: true,
71+
//Don't re-copy html files, we process those
72+
src: ["**/**/*", "!**/*.html"],
73+
cwd: "misc/demo",
74+
dest: "dist/"
75+
}]
76+
}
77+
},
5378
uglify: {
5479
dist:{
5580
src:['<%= dist %>/<%= filename %>-<%= pkg.version %>.js'],
@@ -61,7 +86,17 @@ module.exports = function(grunt) {
6186
}
6287
},
6388
html2js: {
64-
src: ['template/**/*.html']
89+
dist: {
90+
options: {
91+
module: null, // no bundle module for all the html2js templates
92+
base: '.'
93+
},
94+
files: [{
95+
expand: true,
96+
src: ['template/**/*.html'],
97+
ext: '.html.js'
98+
}]
99+
}
65100
},
66101
jshint: {
67102
files: ['Gruntfile.js','src/**/*.js'],
@@ -82,36 +117,50 @@ module.exports = function(grunt) {
82117

83118
//register before and after test tasks so we've don't have to change cli options on the goole's CI server
84119
grunt.registerTask('before-test', ['jshint', 'html2js']);
85-
grunt.registerTask('after-test', ['build', 'site']);
120+
grunt.registerTask('after-test', ['build', 'copy']);
86121

87122
// Default task.
88123
grunt.registerTask('default', ['before-test', 'test', 'after-test']);
89124

90125
//Common ui.bootstrap module containing all modules for src and templates
91126
//findModule: Adds a given module to config
92127
function findModule(name) {
128+
function breakup(text, separator) {
129+
return text.replace(/[A-Z]/g, function (match) {
130+
return separator + match;
131+
});
132+
}
133+
function ucwords(text) {
134+
return text.replace(/^([a-z])|\s+([a-z])/g, function ($1) {
135+
return $1.toUpperCase();
136+
});
137+
}
93138
function enquote(str) {
94139
return '"' + str + '"';
95140
}
96-
var tplModules = grunt.config('tplModules');
97-
var srcModules = grunt.config('srcModules');
98141

99-
grunt.file.expand('template/' + name + '/*.html').map(function(file) {
100-
tplModules.push(enquote(file));
101-
});
102-
grunt.file.expand('src/' + name + '/*.js').forEach(function(file) {
103-
srcModules.push(enquote('ui.bootstrap.' + name));
104-
});
105-
106-
grunt.config('tplModules', tplModules);
107-
grunt.config('srcModules', srcModules);
142+
var module = {
143+
name: name,
144+
moduleName: enquote('ui.bootstrap.' + name),
145+
displayName: ucwords(breakup(name, ' ')),
146+
srcFiles: grunt.file.expand("src/"+name+"/*.js"),
147+
tplFiles: grunt.file.expand("template/"+name+"/*.html"),
148+
tpljsFiles: grunt.file.expand("template/"+name+"/*.html.js"),
149+
tplModules: grunt.file.expand("template/"+name+"/*.html").map(enquote),
150+
dependencies: dependenciesForModule(name),
151+
docs: {
152+
md: grunt.file.expand("src/"+name+"/docs/*.md")
153+
.map(grunt.file.read).join("\n"),
154+
js: grunt.file.expand("src/"+name+"/docs/*.js")
155+
.map(grunt.file.read).join("\n"),
156+
html: grunt.file.expand("src/"+name+"/docs/*.html")
157+
.map(grunt.file.read).join("\n")
158+
}
159+
};
160+
module.dependencies.forEach(findModule);
161+
grunt.config('modules', grunt.config('modules').concat(module));
108162
}
109163

110-
grunt.registerTask('dist', 'Override dist directory', function() {
111-
var dir = this.args[0];
112-
if (dir) { grunt.config('dist', dir); }
113-
});
114-
115164
function dependenciesForModule(name) {
116165
var deps = [];
117166
grunt.file.expand('src/' + name + '/*.js')
@@ -136,140 +185,56 @@ module.exports = function(grunt) {
136185
});
137186
return deps;
138187
}
139-
grunt.registerTask('build', 'Create bootstrap build files', function() {
140188

141-
var srcFiles = [], tplFiles = [];
142-
if (this.args.length) {
143-
var modules = [].concat(this.args);
144-
//Find dependencies
145-
this.args.forEach(function(moduleName) {
146-
modules = modules.concat(dependenciesForModule(moduleName));
147-
findModule(moduleName);
148-
});
149-
srcFiles = modules.map(function(name) {
150-
return 'src/' + name + '/*.js';
151-
});
152-
tplFiles = modules.map(function(name) {
153-
grunt.file.expand('template/' + name + '/*.html').forEach(html2js);
154-
return 'template/' + name + '/*.html.js';
155-
});
156-
grunt.config('filename', grunt.config('filename')+'-custom');
189+
grunt.registerTask('dist', 'Override dist directory', function() {
190+
var dir = this.args[0];
191+
if (dir) { grunt.config('dist', dir); }
192+
});
157193

194+
grunt.registerTask('build', 'Create bootstrap build files', function() {
195+
//If arguments define what modules to build, build those. Else, everything
196+
if (this.args.length) {
197+
this.args.forEach(findModule);
198+
grunt.config('filename', grunt.config('filenamecustom'));
158199
} else {
159-
srcFiles = ['src/*/*.js'];
160-
tplFiles = ['template/*/*.html.js'];
161-
162-
var folders = grunt.file.expand({filter: 'isDirectory', cwd: '.'}, 'src/*');
163-
164-
folders.forEach(function(dir) {
200+
grunt.file.expand({
201+
filter: 'isDirectory', cwd: '.'
202+
}, 'src/*').forEach(function(dir) {
165203
findModule(dir.split('/')[1]);
166204
});
167205
}
168-
grunt.config('concat.dist.src', grunt.config('concat.dist.src').concat(srcFiles));
169-
grunt.config('concat.dist_tpls.src', grunt.config('concat.dist_tpls.src').concat(srcFiles).concat(tplFiles));
170-
171-
grunt.task.run(['concat', 'uglify']);
172-
});
173-
174-
grunt.registerTask('site', 'Create grunt demo site from every module\'s files', function() {
175-
176-
function breakup(text, separator) {
177-
return text.replace(/[A-Z]/g, function (match) {
178-
return separator + match;
179-
});
180-
}
181-
182-
function ucwords(text) {
183-
return text.replace(/^([a-z])|\s+([a-z])/g, function ($1) {
184-
return $1.toUpperCase();
206+
207+
//Pluck will take an array of objects, and map the given key to a new array
208+
//@example: expect( pluck([{a:1},{a:2}], 'a') ).toBe([1,2])
209+
function pluck(array, key) {
210+
return array.map(function(obj) {
211+
return obj[key];
185212
});
186213
}
187214

188-
var modules = grunt.file.expand({filter: 'isDirectory'}, 'src/*').map(function(dir) {
189-
var moduleName = dir.split("/")[1];
190-
if (grunt.file.isDir(dir + "/docs")) {
191-
return {
192-
name: moduleName,
193-
displayName: ucwords(breakup(moduleName, ' ')),
194-
js: grunt.file.expand(dir + "/docs/*.js").map(grunt.file.read).join(''),
195-
html: grunt.file.expand(dir + "/docs/*.html").map(grunt.file.read).join(''),
196-
description: grunt.file.expand(dir + "/docs/*.md").map(grunt.file.read).map(markdown).join('')
197-
};
198-
}
199-
}).filter(function(module){
200-
return module !== undefined;
201-
});
202-
203-
var templateFiles = grunt.file.expand("template/**/*.html.js");
204-
205-
grunt.file.write(
206-
'dist/index.html',
207-
grunt.template.process(grunt.file.read('misc/demo-template.html'), {data: {
208-
modules: modules,
209-
templateModules: templateFiles.map(function(fileName) {
210-
return "'"+fileName.substr(0, fileName.length - 3)+"'";
211-
}),
212-
templates: templateFiles.map(grunt.file.read).join(''),
213-
version : grunt.config('pkg.version'),
214-
ngversion: grunt.config('ngversion'),
215-
bsversion: grunt.config('bsversion')
216-
}})
217-
);
218-
219-
grunt.file.expand('misc/demo-assets/*.*').forEach(function(path) {
220-
grunt.file.copy(path, 'dist/assets/' + path.replace('misc/demo-assets/',''));
221-
});
222-
223-
grunt.file.expand('misc/demo-assets/img/*.*').forEach(function(path) {
224-
grunt.file.copy(path, 'dist/' + path.replace('misc/demo-assets/',''));
225-
});
226-
});
215+
var modules = grunt.config('modules');
216+
grunt.config('srcModules', pluck(modules, 'moduleName'));
217+
grunt.config('tplModules', pluck(modules, 'tplModules'));
218+
grunt.config('demoModules', modules.filter(function(module) {
219+
return module.docs.md && module.docs.js && module.docs.html;
220+
}));
221+
222+
var srcFiles = pluck(modules, 'srcFiles');
223+
var tpljsFiles = pluck(modules, 'tpljsFiles');
224+
//Set the concat task to concatenate the given src modules
225+
grunt.config('concat.dist.src', grunt.config('concat.dist.src')
226+
.concat(srcFiles));
227+
//Set the concat-with-templates task to concat the given src & tpl modules
228+
grunt.config('concat.dist_tpls.src', grunt.config('concat.dist_tpls.src')
229+
.concat(srcFiles).concat(tpljsFiles));
227230

228-
//Html templates to $templateCache for tests
229-
var TPL='angular.module("<%= file %>", []).run(["$templateCache", function($templateCache){\n' +
230-
' $templateCache.put("<%= file %>",\n "<%= content %>");\n' +
231-
'}]);\n';
232-
function escapeContent(content) {
233-
return content.replace(/"/g, '\\"').replace(/\n/g, '" +\n "').replace(/\r/g, '');
234-
}
235-
function html2js(template) {
236-
grunt.file.write(template + ".js", grunt.template.process(TPL, {data: {
237-
file: template,
238-
content: escapeContent(grunt.file.read(template))
239-
}}));
240-
}
241-
grunt.registerMultiTask('html2js', 'Generate js versions of html template', function() {
242-
var files = grunt._watch_changed_files || grunt.file.expand(this.data);
243-
files.forEach(html2js);
231+
grunt.task.run(['concat', 'uglify']);
244232
});
245233

246-
// Karma configuration
247-
function runKarma(command, options) {
248-
var karmaCmd = process.platform === 'win32' ? 'karma.cmd' : 'karma';
249-
var args = [command].concat(options);
250-
var done = grunt.task.current.async();
251-
var child = grunt.util.spawn({
252-
cmd: karmaCmd,
253-
args: args
254-
}, function(err, result, code) {
255-
if (code) {
256-
done(false);
257-
} else {
258-
done();
259-
}
260-
});
261-
child.stdout.pipe(process.stdout);
262-
child.stderr.pipe(process.stderr);
263-
}
264-
265234
grunt.registerTask('test', 'run tests on single-run server', function() {
266-
var options = ['--single-run', '--no-auto-watch', '--log-level=warn'];
267-
if (process.env.TRAVIS) {
268-
options = options.concat(['--browsers=Firefox']);
269-
} else {
270-
//Can augment options with command line arguments
271-
options = options.concat(this.args);
272-
}
235+
var options = ['--single-run', '--no-auto-watch', '--log-level=warn']
236+
.concat(this.args) //Let user augment test args with command line args
237+
.concat(process.env.TRAVIS ? '--browsers=Firefox' : '');
273238
runKarma('start', options);
274239
});
275240

@@ -352,6 +317,25 @@ module.exports = function(grunt) {
352317
}
353318
});
354319
});
320+
321+
// Karma configuration
322+
function runKarma(command, options) {
323+
var karmaCmd = process.platform === 'win32' ? 'karma.cmd' : 'karma';
324+
var args = [command].concat(options);
325+
var done = grunt.task.current.async();
326+
var child = grunt.util.spawn({
327+
cmd: karmaCmd,
328+
args: args
329+
}, function(err, result, code) {
330+
if (code) {
331+
done(false);
332+
} else {
333+
done();
334+
}
335+
});
336+
child.stdout.pipe(process.stdout);
337+
child.stderr.pipe(process.stderr);
338+
}
355339

356340
return grunt;
357341
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)