@@ -4,18 +4,20 @@ module.exports = function(grunt) {
4
4
5
5
grunt . loadNpmTasks ( 'grunt-contrib-watch' ) ;
6
6
grunt . loadNpmTasks ( 'grunt-contrib-concat' ) ;
7
+ grunt . loadNpmTasks ( 'grunt-contrib-copy' ) ;
7
8
grunt . loadNpmTasks ( 'grunt-contrib-jshint' ) ;
8
9
grunt . loadNpmTasks ( 'grunt-contrib-uglify' ) ;
10
+ grunt . loadNpmTasks ( 'grunt-html2js' ) ;
9
11
10
12
// Project configuration.
11
13
grunt . initConfig ( {
12
14
ngversion : '1.0.5' ,
13
15
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
16
17
pkg : grunt . file . readJSON ( 'package.json' ) ,
17
18
dist : 'dist' ,
18
19
filename : 'ui-bootstrap' ,
20
+ filenamecustom : '<%= filename %>-custom' ,
19
21
meta : {
20
22
modules : 'angular.module("ui.bootstrap", [<%= srcModules %>]);' ,
21
23
tplmodules : 'angular.module("ui.bootstrap.tpls", [<%= tplModules %>]);' ,
@@ -39,17 +41,40 @@ module.exports = function(grunt) {
39
41
options : {
40
42
banner : '<%= meta.modules %>\n'
41
43
} ,
42
- src : [ ] ,
44
+ src : [ ] , //src filled in by build task
43
45
dest : '<%= dist %>/<%= filename %>-<%= pkg.version %>.js'
44
46
} ,
45
47
dist_tpls : {
46
48
options : {
47
49
banner : '<%= meta.all %>\n<%= meta.tplmodules %>\n'
48
50
} ,
49
- src : [ ] ,
51
+ src : [ ] , //src filled in by build task
50
52
dest : '<%= dist %>/<%= filename %>-tpls-<%= pkg.version %>.js'
51
53
}
52
54
} ,
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
+ } ,
53
78
uglify : {
54
79
dist :{
55
80
src :[ '<%= dist %>/<%= filename %>-<%= pkg.version %>.js' ] ,
@@ -61,7 +86,17 @@ module.exports = function(grunt) {
61
86
}
62
87
} ,
63
88
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
+ }
65
100
} ,
66
101
jshint : {
67
102
files : [ 'Gruntfile.js' , 'src/**/*.js' ] ,
@@ -82,36 +117,50 @@ module.exports = function(grunt) {
82
117
83
118
//register before and after test tasks so we've don't have to change cli options on the goole's CI server
84
119
grunt . registerTask ( 'before-test' , [ 'jshint' , 'html2js' ] ) ;
85
- grunt . registerTask ( 'after-test' , [ 'build' , 'site ' ] ) ;
120
+ grunt . registerTask ( 'after-test' , [ 'build' , 'copy ' ] ) ;
86
121
87
122
// Default task.
88
123
grunt . registerTask ( 'default' , [ 'before-test' , 'test' , 'after-test' ] ) ;
89
124
90
125
//Common ui.bootstrap module containing all modules for src and templates
91
126
//findModule: Adds a given module to config
92
127
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
+ }
93
138
function enquote ( str ) {
94
139
return '"' + str + '"' ;
95
140
}
96
- var tplModules = grunt . config ( 'tplModules' ) ;
97
- var srcModules = grunt . config ( 'srcModules' ) ;
98
141
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 ) ) ;
108
162
}
109
163
110
- grunt . registerTask ( 'dist' , 'Override dist directory' , function ( ) {
111
- var dir = this . args [ 0 ] ;
112
- if ( dir ) { grunt . config ( 'dist' , dir ) ; }
113
- } ) ;
114
-
115
164
function dependenciesForModule ( name ) {
116
165
var deps = [ ] ;
117
166
grunt . file . expand ( 'src/' + name + '/*.js' )
@@ -136,140 +185,56 @@ module.exports = function(grunt) {
136
185
} ) ;
137
186
return deps ;
138
187
}
139
- grunt . registerTask ( 'build' , 'Create bootstrap build files' , function ( ) {
140
188
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
+ } ) ;
157
193
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' ) ) ;
158
199
} 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 ) {
165
203
findModule ( dir . split ( '/' ) [ 1 ] ) ;
166
204
} ) ;
167
205
}
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 ] ;
185
212
} ) ;
186
213
}
187
214
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 ) ) ;
227
230
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' ] ) ;
244
232
} ) ;
245
233
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
-
265
234
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' : '' ) ;
273
238
runKarma ( 'start' , options ) ;
274
239
} ) ;
275
240
@@ -352,6 +317,25 @@ module.exports = function(grunt) {
352
317
}
353
318
} ) ;
354
319
} ) ;
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
+ }
355
339
356
340
return grunt ;
357
341
} ;
0 commit comments