-
Notifications
You must be signed in to change notification settings - Fork 13
/
Jakefile
1064 lines (898 loc) · 35.9 KB
/
Jakefile
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2011-2015 by Animatron.
* All rights are reserved.
*
* Animatron player is licensed under the MIT License, see LICENSE.
*/
// Versions used:
// node: 0.8.9
// npm: 1.2.8
// jake: 0.5.14
// phantomjs: 1.7.0
// jasmine-node: 1.7.1
// docco: 0.6.2
// python markdown: ?
// orderly: 1.1.0
// jsonschema: 0.3.2
var fs = require('fs')/*,
path = require('path')*/;
// ALIASES
jake.cat = function(file) { return fs.readFileSync(file, 'utf8'); };
jake.echo = function(what, where) { fs.appendFileSync(where, what, 'utf8'); };
// CONSTANTS
var VERSION_FILE = 'VERSION',
VERSIONS_FILE = 'VERSIONS',
VERSION_LOG_FILE = 'VERSION_LOG',
CHANGES_FILE = 'CHANGES',
PACKAGE_FILE = 'package.json',
VERSION = (function(file) {
return jake.cat(_loc(file)).trim();
})(VERSION_FILE),
PACKAGE = (function(file) {
return JSON.parse(jake.cat(_loc(file)).trim());
})(PACKAGE_FILE);
var COPYRIGHT_YEAR = 2015;
var COPYRIGHT_COMMENT =
[ '/*',
' * Copyright © 2011-' + COPYRIGHT_YEAR + ' by Animatron.',
' * All rights are reserved.',
' * ',
' * Animatron Player is licensed under the MIT License.',
' * ',
' * ' + VERSION + ', built at @BUILD_TIME',
' */'].join('\n') + '\n';
var MINIFY_KEEP_COPYRIGHTS = '/WARRANTY|Free to use/';
var NODE_GLOBAL = false,
LOCAL_NODE_DIR = './node_modules';
JAVA_PATH = process.env.JAVA_BINARY || 'java';
var Binaries = {
JSHINT: NODE_GLOBAL ? 'jshint' : (LOCAL_NODE_DIR + '/jshint/bin/jshint'),
JASMINE_NODE: NODE_GLOBAL ? 'jasmine-node' : (LOCAL_NODE_DIR + '/jasmine-node/bin/jasmine-node'),
JSDUCK: 'jsduck',
JASMINE: 'jasmine',
KARMA: NODE_GLOBAL ? 'karma' : (LOCAL_NODE_DIR + '/karma/bin/karma'),
CAT: 'cat',
MV: 'mv',
MARKDOWN: 'python -m markdown',
GIT: 'git',
BROWSERIFY: 'browserify',
CLOSURECOMPILER: JAVA_PATH + ' -jar ' + LOCAL_NODE_DIR + '/google-closure-compiler/compiler.jar'
};
var Dirs = {
SRC: 'src',
DIST: 'dist',
TESTS: 'spec',
DOCS: 'doc'
};
var SubDirs = {
VENDOR: 'vendor',
ENGINES: 'engine',
MODULES: 'module',
IMPORTERS: 'import',
BUNDLES: 'bundle',
ANM: 'anm'
};
var Files = {
Main: { INIT: 'anm.js',
PLAYER: 'player.js' },
Ext: { VENDOR: [ 'matrix.js'/*, 'json2.js'*/, 'font-detector.js' ],
ENGINES: { _ALL_: [ 'dom-engine.js',
'node-engine.js' ],
DOM: 'dom-engine.js',
NODE: 'node-engine.js'/*,
JASMINE: 'jasmine-engine.js'*/ },
IMPORTERS: { _ALL_: [ 'animatron-importer.js',
'animatron-intact-importer.js' ],
ANM: 'animatron-importer.js',
ANM_INTACT: 'animatron-intact-importer.js' },
MODULES: { _ALL_: [ /* 'audio-export.js' */ ]
/* AUDIO_EXPORT: 'audio-export.js' */ },
ANALYTICS: 'analytics.js'},
Doc: { README: 'README.md',
EMBEDDING: 'embedding.md',
SCRIPTING: 'scripting.md' }
};
var _default_bundle_includes = _in_dir(Dirs.DIST, [Files.Main.PLAYER])
.concat(_in_dir(Dirs.SRC + '/' + SubDirs.IMPORTERS, [Files.Ext.IMPORTERS.ANM])) // animatron-importer.js
.concat(_in_dir(Dirs.SRC + '/' + SubDirs.MODULES, []));
var Bundles = [
{ name: 'Animatron Local',
file: 'animatron.local',
includes: _default_bundle_includes
},
{ name: 'Animatron',
file: 'animatron',
includes: _default_bundle_includes
.concat(_in_dir(Dirs.SRC + '/' + SubDirs.ANM, [Files.Ext.ANALYTICS]))
}
];
var Tests = {
Config: Dirs.TESTS + '/karma.conf.js'
};
var Docs = {
Config: Dirs.DOCS + '/jduck.json',
FromSRC: { INCLUDE: [ Dirs.SRC + '/*.js' ] },
FromMD: {
Files: {
README_SRC: Files.Doc.README,
README_DST: Dirs.DOCS + '/README.html',
SCRIPTING_SRC: Dirs.DOCS + '/' + Files.Doc.SCRIPTING,
SCRIPTING_DST: Dirs.DOCS + '/scripting.html',
EMBEDDING_SRC: Dirs.DOCS + '/' + Files.Doc.EMBEDDING,
EMBEDDING_DST: Dirs.DOCS + '/embedding.html'
},
Parts: {
_head: Dirs.DOCS + '/_head.html',
_foot: Dirs.DOCS + '/_foot.html'
}
}
};
var Bucket = {
Release: { ALIAS: 'rls', NAME: 'player.animatron.com' },
Development: { ALIAS: 'dev', NAME: 'player-dev.animatron.com' },
};
var Validation = {
Schema: { ANM_SCENE: Dirs.SRC + '/' + SubDirs.IMPORTERS + '/animatron-project-' + VERSION + '.orderly' }
};
var BUILD_FILE_NAME = 'BUILD',
BUILD_FILE = Dirs.DIST + '/' + BUILD_FILE_NAME,
BUILD_FORMAT = '%H%n%ci%n%cn <%ce>';
var DONE_MARKER = '<Done>.\n',
NONE_MARKER = '<None>.\n',
FAILED_MARKER = '<Failed>.\n';
var DESC_WIDTH = 80,
DESC_PAD = 27,
DESC_TAB = 4,
DESC_PFX = '# ',
DESC_1ST_PFX = DESC_PAD + DESC_PFX.length;
var JSON_INDENT = 2;
var EXEC_OPTS = { printStdout: !jake.program.opts.quiet,
printStderr: !jake.program.opts.quiet },
SILENT_EXEC_OPTS = { printStdout: false,
printStderr: !jake.program.opts.quiet };
var PRODUCTION_TAG = 'production',
DEVELOPMENT_TAG = 'development';
var MOCK_MINIFICATION = false; // it's for debugging purposes, when we need full version in minified files
var _print = !jake.program.opts.quiet ? console.log : function() { };
function _build_time() { var now = new Date();
return now.toString() + ' / ' + now.toISOString(); }
function _extended_build_time() { var now = new Date();
return now.toISOString() + ' ' +
now.getTime() + '\n' +
now.toString(); }
//there are some specifics when building from TC,
//namely absence of the git repo on hand
var isTeamCityBuild = !!process.env.TEAMCITY_BUILDCONF_NAME;
// TASKS =======================================================================
// default =====================================================================
desc(_dfit_nl(['Get full distribution in the /dist directory.',
'Exactly the same as calling {jake dist}.',
'Produces: /dist directory.']));
task('default', ['dist'], function() {});
// clean =======================================================================
desc(_dfit_nl(['Clean previous build artifacts.']));
task('clean', function() {
_print('Clean previous build artifacts..');
jake.rmRf(_loc(Dirs.DIST));
_print(DONE_MARKER);
});
// build =======================================================================
desc(_dfit_nl(['Build process (with no prior cleaning).',
'Called by <dist>.',
'Depends on: <_prepare>, <_build-file>.',
'Produces: /dist directory.']));
task('build', ['_prepare', '_bundles', '_build-file'], function() {});
// build-min ===================================================================
desc(_dfit_nl(['Build process (with no prior cleaning).',
'Called by <dist-min>.',
'Depends on: <_prepare>, <_bundles>, <_minify>, <_build-file>.',
'Produces: /dist directory.']));
task('build-min', ['_prepare', '_bundles', '_minify', '_build-file'], function() {});
// dist ========================================================================
desc(_dfit_nl(['Clean previous build and create distribution files, '+
'so `dist` directory will contain the full '+
'distribution for this version, including '+
'all required files — sources and bundles.',
'Coherently calls <clean> and <build>.',
'Produces: /dist directory.']));
task('dist', ['clean', 'build'], function() {});
// dist-min ====================================================================
desc(_dfit_nl(['Clean previous build and create distribution files, '+
'so `dist` directory will contain the full '+
'distribution for this version, including '+
'all required files — sources and bundles.',
'Coherently calls <clean> and <build>.',
'Produces: /dist directory.']));
task('dist-min', ['clean', 'build-min'], function() {});
// test ========================================================================
desc(_dfit_nl(['Run tests for the distribution.',
'Usage: Just call {jake test}.',
'Requires: `karma`, `karma-mocha-reporter`.']));
task('test', ['dist-min', 'test-dist']);
// test-dist ===================================================================
desc(_dfit_nl(['Test the distribution which already exists.',
'Usage: Just call {jake test-dist}.',
'Requires: `karma`, `karma-mocha-reporter`.']));
task('test-dist', { async: true }, function() {
_print('Running tests');
jake.exec([ Binaries.KARMA, 'start',
_loc(Tests.Config),
'--single-run'
].join(' '), EXEC_OPTS,
function() { _print('Tests finished successfully');
_print(DONE_MARKER);
complete(); });
});
// docs ========================================================================
desc(_dfit_nl(['Generate Docco docs and compile API documentation into '+
'HTML files inside of the /doc directory.',
'Requires: `jsduck`, Python installed, `markdown` module for Python'+
'(and Python is used only because of this module).',
'Produces: /doc/api/*, /doc/player.html, /doc/embedding.html, '+
'/doc/README.html, /doc/scripting.html.']));
task('docs', { async: true }, function() {
_print('Generating docs');
function _src_docs(next) {
_print('For sources');
_versionize(_loc(Docs.Config));
jake.exec([ Binaries.JSDUCK,
'--config=' + _loc(Docs.Config)
].join(' '), EXEC_OPTS,
function() { _print('Source docs were Generated successfully at ' + _loc(Dirs.DOCS + '/api'));
_print(DONE_MARKER);
if (next) next(); });
}
function _md_docs(_src, _dst, next) {
_print('For ' + _src);
jake.exec([ [ Binaries.MARKDOWN,
_loc(_src),
'>', _loc(_dst),
].join(' '),
[ Binaries.CAT,
_loc(Docs.FromMD.Parts._head),
_loc(_dst),
_loc(Docs.FromMD.Parts._foot),
'>', _loc(_dst + '.tmp'),
].join(' '),
[ Binaries.MV,
_loc(_dst + '.tmp'),
_loc(_dst)
].join(' ')
], EXEC_OPTS,
function() { _print(_dst + ' was Generated successfully');
_print(DONE_MARKER);
if (next) next(); });
}
_src_docs(function() {
_md_docs(Docs.FromMD.Files.README_SRC, Docs.FromMD.Files.README_DST, function() {
_md_docs(Docs.FromMD.Files.EMBEDDING_SRC, Docs.FromMD.Files.EMBEDDING_DST, function() {
_md_docs(Docs.FromMD.Files.SCRIPTING_SRC, Docs.FromMD.Files.SCRIPTING_DST, function() {
complete();
});
});
});
});
//sudo pip install markdown
//python -m markdown doc/API.md > doc/API.html
//cat doc/_head.html doc/API.html doc/_foot.html > doc/API.tmp.html
//mv doc/API.tmp.html doc/API.html
//python -m markdown doc/README.md > doc/README.html
//cat doc/_head.html doc/README.html doc/_foot.html > doc/README.tmp.html
//mv doc/README.tmp.html doc/README.html
//python -m markdown doc/scripting.md > doc/scripting.html
//cat doc/_head.html doc/scripting.html doc/_foot.html > doc/scripting.tmp.html
//mv doc/README.tmp.html doc/scripting.html
});
// version =====================================================================
desc(_dfit_nl(['Get current version or apply a new version to the '+
'current state of files. If applies a new version, '+
'modifies VERSION and VERSIONS files, then also adds '+
'a git tag, while pushes nothing. Uses VERSION_LOG file '+
'to provide annotation for a new tag.',
'Usage: {jake version} to get current version and '+
'{jake version[v0.8]} to set current version '+
'to a new one (do not forget to push tags). '+
'If this version exists, you will get detailed information about it. '+
'To remove a previous version, use <rm-version> task. '+
'Use {jake version[+v0.8]} to force creating a version even if it exists.',
'Affects: (if creates a new version) '+
'VERSION, VERSIONS files and a git tag.']));
task('version', { async: true }, function(param) {
if (!param) { _print(VERSION); return; }
// Read versions
var _forced = (param.indexOf('+') == 0);
var _v = _version(_forced ? param.substring(1) : param);
_print('Current version: ' + VERSION);
_print('Selected version: ' + _v + '\n');
// TODO: if (_v.match(/\d+\.\d+/))
var _vhash = _versions.read();
if (!_vhash) { _print(FAILED_MARKER); throw new Error('There is no version data stored in ' + VERSIONS_FILE + ' file.'); }
_print();
// Show or write a version data
if (_vhash[_v] && !_forced) { // TODO: add force-version
_print('Selected version exists, here\'s the detailed information about it:\n');
if (!jake.program.opts.quiet) {
jake.exec([
[ Binaries.GIT,
'show',
'-s',
//'--format=full',
_v ].join(' ')
], EXEC_OPTS,
function() { _print('\n' + DONE_MARKER); complete(); });
}
} else {
_print('Selected version does not exists, applying the new one (' + _v + ') to a current state.\n');
_print('Getting head revision sha and date.');
// TODO: ensure VERSION_LOG_FILE exists
var _getCommitData = jake.createExec([
[ Binaries.GIT,
'show',
'-s',
'--format="%H %ct"',
'HEAD' ].join(' ')
], EXEC_OPTS);
_getCommitData.on('stdout', function(commit_data) {
commit_data = commit_data.toString().split(/\s+/);
var _hash = commit_data[0],
_timestamp = commit_data[1],
_timestr = new Date(_timestamp * 1000).toString();
_print('HEAD sha is: ' + _hash);
_print('HEAD timestamp is: ' + _timestamp);
_print('HEAD human-written time is: ' + _timestr);
_vhash[_v] = [ _hash, _timestamp, _timestr ];
_vhash['latest'] = _v;
_print('Applying a git tag ' + _v + ' to HEAD');
jake.exec([
[ Binaries.GIT,
'tag',
'-a', // annotated
//'-s', // signed
'-f', // force creation
'--file', _loc(VERSION_LOG_FILE),
_v ].join(' ')
], EXEC_OPTS, function() {
VERSION = _v;
jake.rmRf(_loc(VERSION_FILE));
_print('Writing ' + _v + ' to ' + VERSION_FILE + ' file.\n');
jake.echo(_v, _loc(VERSION_FILE));
PACKAGE.version = _v.substr(1); // trim 'v'
jake.rmRf(_loc(PACKAGE_FILE));
_print('Writing ' + _v + ' to ' + PACKAGE_FILE + ' file.\n');
jake.echo(JSON.stringify(PACKAGE, null, JSON_INDENT), _loc(PACKAGE_FILE));
_print('Writing ' + _v + ' information to ' + VERSIONS_FILE + ' file.\n');
_versions.write(_vhash);
_print();
_print('Version ' + _v + ' was applied.');
_print(DONE_MARKER);
complete();
});
});
_getCommitData.on('error', function(msg) {
fail(msg, 1);
});
_getCommitData.run();
}
});
// rm-version ==================================================================
desc(_dfit_nl(['Remove given version information from versions '+
'data files among with the git tag. Pushes nothing.',
'Usage: {jake version[v0.9:v0.8]} to remove version 0.9 '+
'and then set current (and latest) version to 0.8, '+
'{jake rm-version[v0.9:]} to remove given version, '+
'but to stay at the current one. (Do not forget to push tags.) '+
'To add a new version, use <version> task.',
'Affects: (if removes a version) '+
'VERSION, VERSIONS files and removes a git tag.']));
task('rm-version', { async: true }, function(param) {
if (!param) { throw new Error('Both target version and fallback version should be specified, e.g.: {jake rm-version[v0.5:v0.4]}.'); }
var _s = param.split(':'),
src_v = _version(_s[0]),
dst_v = _version(_s[1]) || VERSION;
if (!src_v) { _print(FAILED_MARKER); throw new Error('Target version should be specified, e.g.: {jake rm-version[v0.5:v0.4]}.'); }
if (!dst_v) { _print(FAILED_MARKER); throw new Error('Fallback version should be specified, e.g.: {jake rm-version[v0.5:v0.4]}.'); }
if (dst_v == VERSION) { _print(FAILED_MARKER); throw new Error('Destination version is a current version (' + VERSION + '), should be some of the previous ones.'); }
var _vhash = _versions.read();
if (!_vhash) { _print(FAILED_MARKER); throw new Error('There is no any version data stored in ' + VERSIONS_FILE + ' file.'); }
if (!_vhash[src_v]) { _print(FAILED_MARKER); throw new Error('No version ' + src_v + ' registered in ' + VERSIONS_FILE + ' file.'); }
if (!_vhash[dst_v]) { _print(FAILED_MARKER); throw new Error('No version ' + dst_v + ' registered in ' + VERSIONS_FILE + ' file.'); }
_print();
_print(src_v + ' -> ' + dst_v);
_print();
_print('Removing a git tag ' + src_v);
jake.exec([
[ Binaries.GIT,
'tag',
'-d',
src_v ].join(' ')
], EXEC_OPTS, function() {
VERSION = dst_v;
jake.rmRf(_loc(VERSION_FILE));
_print(dst_v + ' -> ' + VERSION_FILE);
jake.echo(dst_v, _loc(VERSION_FILE));
jake.rmRf(_loc(PACKAGE_FILE));
_print(dst_v + ' -> ' + PACKAGE_FILE);
PACKAGE.version = dst_v.substr(1);
jake.echo(JSON.stringify(PACKAGE, null, JSON_INDENT), _loc(PACKAGE_FILE));
_vhash[src_v] = null;
delete _vhash[src_v];
_vhash['latest'] = dst_v;
_print('Removing ' + src_v + ' information from ' + VERSIONS_FILE + ' file.\n');
_versions.write(_vhash);
_print();
_print('Version ' + src_v + ' was removed and replaced back to ' + dst_v);
_print(DONE_MARKER);
complete();
});
});
// invalidate ==================================================================
desc('Creates a CloudFront invalidation. Usage: jake invalidate[1.3]');
task('invalidate', [], { async: true }, function(version) {
version = version || VERSION;
console.log('Creating invalidation for version', version);
var credentials = getCredentials();
var AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: credentials.key,
secretAccessKey: credentials.secret
});
var distributionId = credentials.distributionId;
if (!distributionId) {
fail('CloudFront Distribution ID not provided', 1);
}
var paths = [
'/%VERSION%/bundle/animatron.js',
'/%VERSION%/bundle/animatron.min.js',
'/%VERSION%/bundle/animatron.local.js',
'/%VERSION%/bundle/animatron.local.min.js',
'/%VERSION%/player.js',
'/%VERSION%/player.min.js',
'/%VERSION%/publish.js',
'/%VERSION%/BUILD'
];
var cloudFront = new AWS.CloudFront();
var items = paths.map(function(path) { return path.replace('%VERSION%', version);});
var params = {
DistributionId: distributionId,
InvalidationBatch: {
CallerReference: new Date().getTime().toString(),
Paths: {
Quantity: items.length,
Items: items
}
}
};
cloudFront.createInvalidation(params, function(err, res){
if(err) fail(err, 1);
_print('Invalidation '+res.Invalidation.Id + ' created successfully');
complete();
});
});
task('deploy-publishjs', { async: true }, function(version, bucket) {
version = version || VERSION;
var s3bucket = 'player-dev.animatron.com',
isProd = bucket === 'prod';
if (isProd) {
s3bucket = 'player.animatron.com';
}
console.log('Starting deployment of publish.js version', version, 'to', s3bucket);
var credentials = getCredentials();
var AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: credentials.key,
secretAccessKey: credentials.secret
});
var s3 = new AWS.S3();
var params = {
'Bucket': s3bucket,
'ACL': 'public-read',
'ContentType': 'text/javascript',
'Key': version + '/publish.js',
'Body': jake.cat('./publish.js')
};
if (!isProd) {
params.CacheControl = 'max-age=300';
}
s3.putObject(params, function(err) {
if (err) {
fail('Deployment failed: ' + err.message, 1);
} else {
console.log('Deployment of publish.js complete.');
complete();
}
});
});
task('deploy', ['dist-min'], function(version, bucket) {
version = version || VERSION;
var s3bucket = 'player-dev.animatron.com',
isProd = bucket === 'prod';
if (isProd) {
s3bucket = 'player.animatron.com';
}
var doDeployment = function() {
console.log('Starting deployment of version', version, 'to', s3bucket);
var credentials = getCredentials();
var localPrefix = './dist/',
remotePrefix = version + '/',
files = [
'BUILD',
'player.js',
'player.min.js',
'bundle/animatron.js',
'bundle/animatron.min.js',
'bundle/animatron.local.js',
'bundle/animatron.local.min.js'
];
var AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: credentials.key,
secretAccessKey: credentials.secret
});
var s3 = new AWS.S3();
var async = require('async'),
zlib = require('zlib'),
fs = require('fs');
async.each(files, function(file, done) {
var key = remotePrefix + file;
var isJs = file.substring(file.length-3) === '.js';
var params = {
'Bucket': s3bucket,
'ACL': 'public-read',
'ContentType': isJs ? 'text/javascript' : 'text/plain',
'Key': key
};
if (!isProd) {
params.CacheControl = 'max-age=300';
}
var body = jake.cat(localPrefix + file);
if (isJs) {
params.ContentEncoding = 'gzip';
body = zlib.gzipSync(body);
}
params.Body = body;
s3.putObject(params, function(err, data) {
if (err) {
done(err);
} else {
console.log(key, 'uploaded to S3 successfully');
done();
}
});
}, function(err) {
if (err) {
fail('Deployment failed: ' + err.message, 1);
} else {
console.log('Deployment complete.');
if (isProd) {
//invalidate files after production deployment
var invalidate = jake.Task.invalidate;
invalidate.addListener('complete', function(){
complete();
});
invalidate.invoke(version);
return;
} else {
complete();
}
}
});
};
if (!isProd || isTeamCityBuild) {
//we can't check which branch TC is on, so we'll have to trust it is
//configured correctly
doDeployment();
} else {
var git = jake.createExec('git symbolic-ref --short HEAD');
git.on('stdout', function(branch) {
if (branch.toString().trim() !== 'master') {
fail('You have to be on the master branch to deploy to production', 1);
} else {
doDeployment();
}
});
git.run();
}
});
// SUBTASKS ====================================================================
// _prepare ====================================================================
desc(_dfit(['Internal. Create '+Dirs.DIST+' folder']));
task('_prepare', function() {
_print('Create required destination folders..');
_print('mkdir -p ' + _loc(Dirs.DIST));
jake.mkdirP(_loc(Dirs.DIST));
_print(DONE_MARKER);
});
// _bundles ====================================================================
desc(_dfit(['Internal. Create bundles from existing sources and put them into '+Dirs.DIST+'/'+SubDirs.BUNDLES+' folder']));
task('_bundles', ['browserify'], function() {
_print('Create Bundles..');
var BUILD_TIME = _build_time();
var targetDir = Dirs.DIST + '/' + SubDirs.BUNDLES;
jake.mkdirP(_loc(targetDir));
Bundles.forEach(function(bundle) {
_print('Package bundle \'' + bundle.name + '\'');
var targetFile = targetDir + '/' + bundle.file + '.js';
jake.rmRf(_loc(targetFile));
_print('.. (c) > ' + targetFile);
jake.echo(COPYRIGHT_COMMENT.replace(/@BUILD_TIME/g, BUILD_TIME)
.concat('\n\n\n'),
_loc(targetFile));
bundle.includes.forEach(function(bundleFile) {
jake.echo(jake.cat(_loc(bundleFile)).trim() + '\n', _loc(targetFile));
_print('.. ' + bundleFile + ' > ' + targetFile);
});
});
_print(DONE_MARKER);
});
// _minify =====================================================================
desc(_dfit(['Internal. Create a minified copy of all the sources and bundles '+
'from '+Dirs.DIST+' folder and append a .min suffix to them']));
task('_minify', { async: true }, function() {
_print('Minify all the files and put them in ' + Dirs.DIST + ' folder');
var BUILD_TIME = _build_time();
// TODO: use Jake new Rules technique for that (http://jakejs.com/#rules)
function minify(src, cb) {
var dst = _minified(src);
if (MOCK_MINIFICATION) {
jake.cpR(src, dst);
cb(dst);
return;
}
jake.exec([
[ Binaries.CLOSURECOMPILER,
'--compilation_level SIMPLE_OPTIMIZATIONS',
'--js', src,
'--js_output_file', dst
].join(' ')
], EXEC_OPTS, function() { cb(dst); });
_print('min -> ' + src + ' -> ' + dst);
}
function copyrightize(src) {
var new_content = COPYRIGHT_COMMENT.replace(/@BUILD_TIME/g, BUILD_TIME)
.concat(jake.cat(src).trim() + '\n');
jake.rmRf(src);
jake.echo(new_content, src);
_print('(c) -> ' + src);
}
// since there is only one thread, it will [hopefully] work ok
var queue = {};
function minifyInQueue(src) {
var task_id = _guid();
queue[task_id] = {};
minify(src, function(dst) {
_print(DONE_MARKER);
delete queue[task_id];
if (!Object.keys(queue).length) complete();
});
}
function minifyInQueueWithCopyright(src) {
var task_id = _guid();
queue[task_id] = {};
minify(src, function(dst) {
copyrightize(dst);
_print(DONE_MARKER);
delete queue[task_id];
if (!Object.keys(queue).length) complete();
});
}
/*
_print('.. Vendor Files');
Files.Ext.VENDOR.forEach(function(vendorFile) {
minifyInQueue(_loc(Dirs.DIST + '/' + SubDirs.VENDOR + '/' + vendorFile));
});
*/
_print('.. Main files');
//minifyInQueueWithCopyright(_loc(Dirs.DIST + '/' + Files.Main.INIT));
minifyInQueueWithCopyright(_loc(Dirs.DIST + '/' + Files.Main.PLAYER));
_print('.. Bundles');
Bundles.forEach(function(bundle) {
minifyInQueueWithCopyright(_loc(Dirs.DIST + '/' + SubDirs.BUNDLES + '/' + bundle.file + '.js'));
});
});
// _build-file =================================================================
desc(_dfit(['Internal. Create a BUILD file informing about the time and commit of a build.']));
task('_build-file', { async: true }, function() {
_print('Fill ' + BUILD_FILE + ' file with information about current build');
_print();
var BUILD_TIME = _extended_build_time();
console.log('Build time:', BUILD_TIME);
var updateBuildFile = function(commitInfo) {
jake.rmRf(_loc(BUILD_FILE));
_print('Updating ' + BUILD_FILE + ' file.\n');
jake.echo(BUILD_TIME + '\n' +
VERSION + '\n' +
commitInfo, _loc(BUILD_FILE));
_print(DONE_MARKER);
complete();
};
if (isTeamCityBuild) {
var commitInfo = process.env.BUILD_VCS_NUMBER_Animatron_AnimatronPlayerDevelopment +
'\n' + 'Built by TeamCity. Build #' + process.env.BUILD_NUMBER;
console.log(commitInfo);
updateBuildFile(commitInfo);
} else {
var getCommit = jake.createExec([
[ Binaries.GIT,
'log',
'-n', '1',
'--format=format:"' + BUILD_FORMAT + '"'
].join(' ')
], EXEC_OPTS);
getCommit.on('stdout', function(commitInfo) {
commitInfo = commitInfo.toString();
console.log(commitInfo);
updateBuildFile(commitInfo);
});
getCommit.addListener('stderr', function(msg) {
fail(msg, 1);
});
getCommit.addListener('error', function(msg) {
fail(msg, 1);
});
getCommit.run();
}
});
task('browserify', { 'async': true }, function() {
console.log('Creating Browserify bundle.');
//check if the browserify binary exists
var browserifyPath = '/usr/local/bin/browserify';
if (!fs.existsSync(browserifyPath)) {
browserifyPath = './node_modules/browserify/bin/cmd.js';
}
jake.exec(browserifyPath + ' -t browserify-css src/main.js -o dist/player.js', function() {
console.log('dist/player.js created successfully');
complete();
});
});
// UTILS =======================================================================
var _credentials = null;
function getCredentials() {
if (_credentials) {
return _credentials;
}
if (process.env.S3_KEY) {
//get credentials from the environment
return (_credentials = {
key: process.env.S3_KEY,
secret: process.env.S3_SECRET,
distributionId: process.env.CF_DISTRIBUTION
});
} else {
//get credentials from .s3
if (!fs.existsSync('./.s3')) {
fail('No credentials for deployment provided', 1);
}
var creds = fs.readFileSync('./.s3').toString().split(/\s+/);
return (_credentials = {
key: creds[1],
secret: creds[2],
distributionId: creds[3]
});
}
}
function _in_dir(dir, files) {
var res = [];
files.forEach(function(file) {
res.push(dir + '/' + file);
});
return res;
}
function _loc(path) { return './' + path; }
function _minified(path) {
var len = path.length;
if (path.substr(len - 3) !== '.js') throw new Error('The path ' + path + ' points to file with no .js suffix; ' +
'Can\'t determine minified path.');
return path.substr(0, len - 3) + '.min.js';
}
function _src_map(path) { return path + '.map'; }
function _src_map_url(path) {
var path = path.replace(Dirs.DIST + '/', '');
return '.' + path.substr(path.lastIndexOf('/')) + '.map';
}
function _src_map_root(path) { return './'; }
function _src_map_prefix(path) {
return path.split('/').length - 1;
}
function _dfit(lines) {
return _fit(lines, DESC_PFX, DESC_PAD, DESC_TAB, DESC_WIDTH, DESC_1ST_PFX);
}
function _dfit_nl(lines) {
return _fit(lines, DESC_PFX, DESC_PAD, DESC_TAB, DESC_WIDTH, DESC_1ST_PFX) + '\n';
}
function _fit_nl(lines, prefix, spaces, tabs, width, def_prefix) {
return _fit(lines, prefix, spaces, width, def_prefix) + '\n';
}
function _fit(lines, prefix, spaces, tabs, width, def_prefix) {
if (!lines.length) return '';
function tabulate(line, indent_first) {
var pad = '',
tab = '';
for (var j = 0; j < spaces; j++) { pad += ' '; }
for (var j = 0; j < tabs; j++) { tab += ' '; }
if (spaces + prefix.length + line.length <= width) {
new_lines.push(pad + prefix + (indent_first ? tab : '') + line.trim());
} else {
var left = line.length,
pos = 0,
chunk = 0;
while (left > 0) {
var do_indent = indent_first || (chunk > 0);
var cut_size = width - spaces - prefix.length - ((do_indent ? tabs : 0));
new_lines.push(pad + prefix + (do_indent ? tab : '') +
line.substring(pos, pos + cut_size).trim());
pos += cut_size;
left -= cut_size;
chunk++;
}
}
}
var new_lines = [];
if (!def_prefix) {
tabulate(lines[0]);
} else {
if ((def_prefix + lines[0].length) <= width) {
new_lines.push(lines[0].trim());
} else {
var cut_size = width - def_prefix;
new_lines.push(lines[0].substring(0, cut_size).trim());
tabulate(lines[0].substring(cut_size), true);
}
}
for (var i = 1, il = lines.length; i < il; i++) {