forked from angular-ui/bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
345 lines (313 loc) · 11 KB
/
Gruntfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
var markdown = require('node-markdown').Markdown;
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-html2js');
// Project configuration.
grunt.initConfig({
ngversion: '1.0.5',
bsversion: '2.3.1',
modules: [],//to be filled in by build task
pkg: grunt.file.readJSON('package.json'),
dist: 'dist',
filename: 'ui-bootstrap',
filenamecustom: '<%= filename %>-custom',
meta: {
modules: 'angular.module("ui.bootstrap", [<%= srcModules %>]);',
tplmodules: 'angular.module("ui.bootstrap.tpls", [<%= tplModules %>]);',
all: 'angular.module("ui.bootstrap", ["ui.bootstrap.tpls", <%= srcModules %>]);'
},
watch: {
html: {
files: ['template/**/*.html'],
tasks: ['html2js']
},
js: {
//nospawn makes the tests start faster
nospawn: true,
files: ['src/**/*.js'],
//we don't need to jshint here, it slows down everything else
tasks: ['test-run']
}
},
concat: {
dist: {
options: {
banner: '<%= meta.modules %>\n'
},
src: [], //src filled in by build task
dest: '<%= dist %>/<%= filename %>-<%= pkg.version %>.js'
},
dist_tpls: {
options: {
banner: '<%= meta.all %>\n<%= meta.tplmodules %>\n'
},
src: [], //src filled in by build task
dest: '<%= dist %>/<%= filename %>-tpls-<%= pkg.version %>.js'
}
},
copy: {
demohtml: {
options: {
//process html files with gruntfile config
processContent: grunt.template.process
},
files: [{
expand: true,
src: ["**/*.html"],
cwd: "misc/demo/",
dest: "dist/"
}]
},
demoassets: {
files: [{
expand: true,
//Don't re-copy html files, we process those
src: ["**/**/*", "!**/*.html"],
cwd: "misc/demo",
dest: "dist/"
}]
}
},
uglify: {
dist:{
src:['<%= dist %>/<%= filename %>-<%= pkg.version %>.js'],
dest:'<%= dist %>/<%= filename %>-<%= pkg.version %>.min.js'
},
dist_tpls:{
src:['<%= dist %>/<%= filename %>-tpls-<%= pkg.version %>.js'],
dest:'<%= dist %>/<%= filename %>-tpls-<%= pkg.version %>.min.js'
}
},
html2js: {
dist: {
options: {
module: null, // no bundle module for all the html2js templates
base: '.'
},
files: [{
expand: true,
src: ['template/**/*.html'],
ext: '.html.js'
}]
}
},
jshint: {
files: ['Gruntfile.js','src/**/*.js'],
options: {
curly: true,
immed: true,
newcap: true,
noarg: true,
sub: true,
boss: true,
eqnull: true,
globals: {
angular: true
}
}
}
});
//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', ['jshint', 'html2js']);
grunt.registerTask('after-test', ['build', 'copy']);
// Default task.
grunt.registerTask('default', ['before-test', 'test', 'after-test']);
//Common ui.bootstrap module containing all modules for src and templates
//findModule: Adds a given module to config
var foundModules = {};
function findModule(name) {
if (foundModules[name]) { return; }
foundModules[name] = true;
function breakup(text, separator) {
return text.replace(/[A-Z]/g, function (match) {
return separator + match;
});
}
function ucwords(text) {
return text.replace(/^([a-z])|\s+([a-z])/g, function ($1) {
return $1.toUpperCase();
});
}
function enquote(str) {
return '"' + str + '"';
}
var module = {
name: name,
moduleName: enquote('ui.bootstrap.' + name),
displayName: ucwords(breakup(name, ' ')),
srcFiles: grunt.file.expand("src/"+name+"/*.js"),
tplFiles: grunt.file.expand("template/"+name+"/*.html"),
tpljsFiles: grunt.file.expand("template/"+name+"/*.html.js"),
tplModules: grunt.file.expand("template/"+name+"/*.html").map(enquote),
dependencies: dependenciesForModule(name),
docs: {
md: grunt.file.expand("src/"+name+"/docs/*.md")
.map(grunt.file.read).map(markdown).join("\n"),
js: grunt.file.expand("src/"+name+"/docs/*.js")
.map(grunt.file.read).join("\n"),
html: grunt.file.expand("src/"+name+"/docs/*.html")
.map(grunt.file.read).join("\n")
}
};
module.dependencies.forEach(findModule);
grunt.config('modules', grunt.config('modules').concat(module));
}
function dependenciesForModule(name) {
var deps = [];
grunt.file.expand('src/' + name + '/*.js')
.map(grunt.file.read)
.forEach(function(contents) {
//Strategy: find where module is declared,
//and from there get everything inside the [] and split them by comma
var moduleDeclIndex = contents.indexOf('angular.module(');
var depArrayStart = contents.indexOf('[', moduleDeclIndex);
var depArrayEnd = contents.indexOf(']', depArrayStart);
var dependencies = contents.substring(depArrayStart + 1, depArrayEnd);
dependencies.split(',').forEach(function(dep) {
if (dep.indexOf('ui.bootstrap.') > -1) {
var depName = dep.trim().replace('ui.bootstrap.','').replace(/['"]/g,'');
if (deps.indexOf(depName) < 0) {
deps.push(depName);
//Get dependencies for this new dependency
deps = deps.concat(dependenciesForModule(depName));
}
}
});
});
return deps;
}
grunt.registerTask('dist', 'Override dist directory', function() {
var dir = this.args[0];
if (dir) { grunt.config('dist', dir); }
});
grunt.registerTask('build', 'Create bootstrap build files', function() {
//If arguments define what modules to build, build those. Else, everything
if (this.args.length) {
this.args.forEach(findModule);
grunt.config('filename', grunt.config('filenamecustom'));
} else {
grunt.file.expand({
filter: 'isDirectory', cwd: '.'
}, 'src/*').forEach(function(dir) {
findModule(dir.split('/')[1]);
});
}
//Pluck will take an array of objects, and map the given key to a new array
//@example: expect( pluck([{a:1},{a:2}], 'a') ).toBe([1,2])
function pluck(array, key) {
return array.map(function(obj) {
return obj[key];
});
}
var modules = grunt.config('modules');
grunt.config('srcModules', pluck(modules, 'moduleName'));
grunt.config('tplModules', pluck(modules, 'tplModules').filter(function(tpls) { return tpls.length > 0;} ));
grunt.config('demoModules', modules.filter(function(module) {
return module.docs.md && module.docs.js && module.docs.html;
}));
var srcFiles = pluck(modules, 'srcFiles');
var tpljsFiles = pluck(modules, 'tpljsFiles');
//Set the concat task to concatenate the given src modules
grunt.config('concat.dist.src', grunt.config('concat.dist.src')
.concat(srcFiles));
//Set the concat-with-templates task to concat the given src & tpl modules
grunt.config('concat.dist_tpls.src', grunt.config('concat.dist_tpls.src')
.concat(srcFiles).concat(tpljsFiles));
grunt.task.run(['concat', 'uglify']);
});
grunt.registerTask('test', 'run tests on single-run server', function() {
var options = ['--single-run', '--no-auto-watch', '--log-level=warn']
.concat(this.args) //Let user augment test args with command line args
.concat(process.env.TRAVIS ? '--browsers=Firefox' : '');
runKarma('start', options);
});
grunt.registerTask('server', 'start karma server', function() {
var options = ['--no-single-run', '--no-auto-watch'].concat(this.args);
runKarma('start', options);
});
grunt.registerTask('test-run', 'run tests against continuous karma server', function() {
var options = ['--single-run', '--no-auto-watch'].concat(this.args);
runKarma('run', options);
});
grunt.registerTask('test-watch', 'start karma server, watch & execute tests', function() {
var options = ['--no-single-run', '--auto-watch'].concat(this.args);
runKarma('start', options);
});
//changelog generation
grunt.registerTask('changelog', 'generates changelog markdown from git commits', function () {
var changeFrom = this.args[0], changeTo = this.args[1] || 'HEAD';
var done = grunt.task.current.async();
var child = grunt.util.spawn({
cmd:process.platform === 'win32' ? 'git.cmd' : 'git',
args: [
'log',
changeFrom + '..' + changeTo,
'--format=%H%n%s%n%b%n==END=='
]
}, function (err, result, code) {
var changelog = {};
function addChange(changeType, component, change) {
if (!changelog[changeType]) {
changelog[changeType] = {};
}
if (!changelog[changeType][component]) {
changelog[changeType][component] = [];
}
changelog[changeType][component].push(change);
}
var COMMIT_MSG_REGEXP = /^(chore|demo|docs|feat|fix|refactor|style|test)\((.+)\):? (.+)$/;
var gitlog = result.toString().split('\n==END==\n').reverse();
if (code) {
grunt.log.error(err);
done(false);
} else {
gitlog.forEach(function (logItem) {
var lines = logItem.split('\n');
var sha1 = lines.shift().substr(0,8); //Only first 7 of sha1
var subject = lines.shift();
var msgMatches = subject.match(COMMIT_MSG_REGEXP);
var changeType = msgMatches[1];
var component = msgMatches[2];
var componentMsg = msgMatches[3];
var breaking = logItem.match(/BREAKING CHANGE:([\s\S]*)/);
if (breaking) {
addChange('breaking', component, {
sha1: sha1,
msg: breaking[1]
});
}
addChange(changeType, component, {sha1:sha1, msg:componentMsg});
});
console.log(grunt.template.process(grunt.file.read('misc/changelog.tpl.md'), {data: {
changelog: changelog,
today: grunt.template.today('yyyy-mm-dd'),
version : grunt.config('pkg.version')
}}));
done();
}
});
});
// Karma configuration
function runKarma(command, options) {
var karmaCmd = process.platform === 'win32' ? 'karma.cmd' : 'karma';
var args = [command].concat(options);
var done = grunt.task.current.async();
var child = grunt.util.spawn({
cmd: karmaCmd,
args: args
}, function(err, result, code) {
if (code) {
done(false);
} else {
done();
}
});
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
}
return grunt;
};