File size: 12,248 Bytes
5fae594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
module.exports = function (grunt) {

   var autoStartBrowsers = ['Chrome', 'Firefox', 'Safari'];

   var STREAM_SOURCE_PORT = 4567;

   // NB: source files are order sensitive
   var OBOE_BROWSER_SOURCE_FILES = [
      'build/version.js'                
   ,  'src/LICENCE.js'                
   ,  'src/functional.js'                
   ,  'src/util.js'                    
   ,  'src/lists.js'                    
   ,  'src/libs/clarinet.js'               
   ,  'src/clarinetListenerAdaptor.js'
   ,  'src/parseResponseHeaders.browser.js'
   ,  'src/streamingHttp.browser.js'
   ,  'src/jsonPathSyntax.js'
   ,  'src/ascent.js'            
   ,  'src/incrementalContentBuilder.js'            
   ,  'src/jsonPath.js'
   ,  'src/singleEventPubSub.js'
   ,  'src/pubSub.js'
   ,  'src/events.js'
   ,  'src/patternAdapter.js'   
   ,  'src/instanceApi.js'   
   ,  'src/instanceController.js'
   ,  'src/wire.js'
   ,  'src/publicApi.js'
   ];
   
   var OBOE_NODE_SOURCE_FILES = [
      'build/version.js'   
   ,  'src/functional.js'                
   ,  'src/util.js'                    
   ,  'src/lists.js'                                   
   ,  'src/clarinetListenerAdaptor.js'
   ,  'src/streamingHttp.node.js'
   ,  'src/jsonPathSyntax.js'
   ,  'src/ascent.js'   
   ,  'src/incrementalContentBuilder.js'            
   ,  'src/jsonPath.js'
   ,  'src/singleEventPubSub.js'   
   ,  'src/pubSub.js'
   ,  'src/events.js'
   ,  'src/patternAdapter.js'      
   ,  'src/instanceApi.js'   
   ,  'src/instanceController.js'
   ,  'src/wire.js'
   ,  'src/publicApi.js'
   ];   
   
   var FILES_TRIGGERING_KARMA = [
      'src/**/*.js', 
      'test/specs/*.spec.js', 
      'test/libs/*.js'
   ];
     
   grunt.initConfig({

      pkg:grunt.file.readJSON("package.json")
      
   ,  clean: ['dist/*.js', 'build/*.js']
      
   ,  concat: {
         browser:{         
            src: OBOE_BROWSER_SOURCE_FILES,
            dest: 'build/oboe-browser.concat.js'
         },
         node:{         
            src: OBOE_NODE_SOURCE_FILES,
            dest: 'build/oboe-node.concat.js'
         }         
      }
      
   ,  wrap: {
         browserPackage: {
            src: 'build/oboe-browser.concat.js',
            dest: '.',
            wrapper: [
               '// This file is the concatenation of many js files. \n' +
               '// See https://github.com/jimhigson/oboe.js for the raw source\n' +
               // having a local undefined, window, Object etc allows slightly better minification:                    
               '(function  (window, Object, Array, Error, JSON, undefined ) {\n'
               
                  // source code here
                
            ,     '\n\n;' +          
                                  
                  'if ( typeof define === "function" && define.amd ) {' +
                     'define( "oboe", [], function () { return oboe; } );' +
                  '} else {' +
                     'window.oboe = oboe;' +
                  '}' +
               '})(window, Object, Array, Error, JSON);'
            ]
         },
                  
         nodePackage: {
            src: 'build/oboe-node.concat.js',
            dest: '.',
            wrapper: [
               '// this file is the concatenation of several js files. See https://github.com/jimhigson/oboe-browser.js/tree/master/src ' +
                   'for the unconcatenated source\n' +
                    
               'module.exports = (function  () {\n' + 
                  'var clarinet = require("clarinet");\n'
                  
                  // source code here
                                        
            ,  '\n\n;return oboe;})();'
            ]
         }         
      }
      
                       
   ,  uglify: {
         build:{
            files:{
               'build/oboe-browser.min.js': 'build/oboe-browser.concat.js'
            }
         }
      }
      
   ,  karma: {
         options:{            
            singleRun: true,
            proxies: {
               '/testServer'   : 'http://localhost:' + STREAM_SOURCE_PORT   
            },         
            // test results reporter to use
            // possible values: 'dots', 'progress', 'junit'
            reporters : ['progress'],
                        
            // enable / disable colors in the output (reporters and logs)
            colors : true            
         }
         
      ,  
         'precaptured-dev': {
            // for doing a single test run with already captured browsers during development.
            // this is good for running tests in browsers karma can't easily start such as
            // IE running inside a Windows VM on a unix dev environment                
            browsers: [],     
            configFile: 'test/unit.conf.js',
            singleRun: 'true'            
         }
      ,
         'single-dev': {
            browsers: autoStartBrowsers,
            configFile: 'test/unit.conf.js'
         }
      ,
         'single-concat': {
            browsers: autoStartBrowsers,
            configFile: 'test/concat.conf.js'      
         }  
      ,  
         'single-minified': {
            browsers: autoStartBrowsers,
            configFile: 'test/min.conf.js'
         }
         
      ,  
         'single-amd': {
            browsers: autoStartBrowsers,
            configFile: 'test/amd.conf.js'
         }         
         
      ,  
         'single-browser-http': {
            browsers: autoStartBrowsers,
            configFile: 'test/http.conf.js'
         }         
         
      ,  
         'persist': {
            // for setting up a persistent karma server.
            // To start the server, the task is:
            //    karma:persist 
            // To run these, the task is: 
            //    karma:persist:run
            configFile: 'test/unit.conf.js',           
            browsers: [], 
            singleRun:false,
            background:true
         }   
      }
      
   ,  copy: {
         browserDist: {
            files: [
               {src: ['build/oboe-browser.min.js'],    dest: 'dist/oboe-browser.min.js'}
            ,  {src: ['build/oboe-browser.concat.js'], dest: 'dist/oboe-browser.js'    }
            ]
         },
         nodeDist: {
            files: [
               {src: ['build/oboe-node.concat.js'],    dest: 'dist/oboe-node.js'}
            ]
         }         
      }      
      
   ,  exec:{
         // these might not go too well on Windows :-) - get Cygwin.
         reportMinifiedSize:{
            command: "echo minified size is `wc -c < dist/oboe-browser.min.js` bytes" 
         },
         reportMinifiedAndGzippedSize:{
            command: "echo Size after gzip is `gzip --best --stdout dist/oboe-browser.min.js | wc -c` bytes - max 5120"
         },
         createGitVersionJs:{
            command: "echo \"// `git describe`\" > build/version.js"
         }
      }
      
   ,  watch:{
         karma:{
            files:FILES_TRIGGERING_KARMA,
            tasks:['karma:persist:run']
         },
                           
         // like above but reports the file size. This is good for 
         // watching while developing to make sure it doesn't get
         // too big. Doesn't run tests against minified.
         karmaAndSize:{
            files: FILES_TRIGGERING_KARMA,
            tasks:[
               'karma:persist:run',
               'browser-build',               
               'dist-sizes']
         },
         
         // like above but reports the file size. This is good for 
         // watching while developing to make sure it doesn't get
         // too big. Doesn't run tests against minified.
         testNode:{
            files: FILES_TRIGGERING_KARMA,
            tasks:[ 
               'node-build-run']
         },         
         
         restartStreamSourceAndRunTests:{
            // this fails at the moment because start-stream-source
            // fails if run more than once - the port is taken.
            files: ['test/streamsource.js'],
            tasks: ['start-stream-source', 'karma:persist:run']
         }         
      }
                 
   ,  concurrent:{
         watchDev: {
            tasks:[ 'watch:karmaAndSize', 'watch:restartStreamSourceAndRunTests' ],
            options:{
               logConcurrentOutput: true
            }
         }
      }
      
   });

   require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

   var streamSource;
   
   grunt.registerTask('start-stream-source', function () {
      grunt.log.ok('do we have a streaming source already?', !!streamSource);
   
      // if we previously loaded the streamsource, stop it to let the new one in:
      if( streamSource ) {
         grunt.log.ok('there seems to be a streaming server already, let\'s stop it');
         streamSource.stop();
      }
         
      streamSource = require('./test/streamsource.js');
      streamSource.start(STREAM_SOURCE_PORT, grunt);
   });

   grunt.registerTask("jasmine_node_oboe", "Runs jasmine-node.", function() {
   
      require('jasmine-node').executeSpecsInFolder({
            
         "specFolders":[process.cwd() + '/test/specs/'], 
         "isVerbose":true, 
         "showColors":true, 
         "teamcity":false, 
         "useRequireJs":false, 
         "regExpSpec":/oboe\.(?:integration|performance)\.spec\.(js)$/, 
         "junitreport":{"report":false, "savePath":"./reports/", "useDotNotation":true, "consolidate":true}, 
         "includeStackTrace":true, 
         "growl":false,
         onComplete: this.async()
         
      });
   });   

   // change the auto-starting browsers so that future tests will use
   // phantomjs instead of actual browsers. Can do: 
   //    grunt headless-mode default
   // to run without any actual browsers
   grunt.registerTask('headless-mode', function(){
      autoStartBrowsers.length = 0;
      autoStartBrowsers.push('PhantomJS');
   })
      
   grunt.registerTask('test-start-server',   [
      'karma:persist'
   ]);
   
   grunt.registerTask('test-run',   [
      'karma:persist:run'
   ]);
   
   grunt.registerTask('dist-sizes',   [
      'exec:reportMinifiedAndGzippedSize'
   ]);
            
   grunt.registerTask('node-build',      [
      'exec:createGitVersionJs',   
      'concat:node', 
      'wrap:nodePackage',
      'copy:nodeDist'
   ]);

   grunt.registerTask('node-build-test',      [
      'node-build',
      'jasmine_node_oboe'
   ]);
      
   grunt.registerTask('node',      [
      'start-stream-source',
      'node-build-test'
   ]);   

   grunt.registerTask('browser-build',      [
      'exec:createGitVersionJs',
      'concat:browser', 
      'concat:node', 
      'wrap:browserPackage', 
      'uglify',
      'copy:browserDist'   
   ]);
         
   grunt.registerTask('browser-build-test',      [
      'karma:single-dev', 
      'karma:single-browser-http',
      'browser-build',
      'karma:single-concat',                                         
      'karma:single-minified',     
      'karma:single-amd'   
   ]);
   
   grunt.registerTask('build',      [
      'browser-build',
      'node-build'      
   ]);      
   
   // build and run just the integration tests.
   grunt.registerTask('build-integration-test',      [
      'build',   
      'start-stream-source',
      'karma:single-concat',
      'jasmine_node_oboe',
      'dist-sizes'
   ]);
   
   grunt.registerTask('default',      [
   
      'clear',   
      'clean',
      'start-stream-source',
      
      'browser-build-test',     

      'node-build-test',
      
      'dist-sizes'                                                            
   ]);
   
   

   // browser-test-auto-run or node-test-auto-run
   //
   // The most useful for developing. Start this task, capture some browsers
   // (unless node) then edit the code. Tests will be run as the code is 
   // saved.
   grunt.registerTask('browser-test-auto-run',   [
      'start-stream-source',
      'karma:persist',
      'concurrent:watchDev'       
   ]);
   grunt.registerTask('node-test-auto-run',   [
      'start-stream-source',
      'watch:testNode'       
   ]);   
};