-
Notifications
You must be signed in to change notification settings - Fork 26
/
build.js
executable file
·205 lines (149 loc) · 4.19 KB
/
build.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
#!/usr/bin/env node
var fs = require('fs');
var UglifyJS = require("uglify-js");
var moment = require("moment");
var version = require("./version.json");
var baseUrl = "html/src/";
var outputUrl = "build/";
var addToChangeLog = function(m) {
var log = '\n';
log += '##### v' + version.version + ' b ' + version.build + '\n';
log += '*' + version.date + '*\n\n';
log += m + '\n\n';
fs.appendFileSync("./CHANGELOG.md", log);
}
var updateVersionFile = function() {
var jsHeader = "/**\n";
jsHeader += " * @property Version - autogenerated with build script, holds current verison info\n";
jsHeader += " */\n"
jsHeader += "// DO NOT EDIT. Updated from version.json\nSQR.Version = ";
fs.writeFileSync("./version.json", JSON.stringify(version));
fs.writeFileSync(baseUrl + "Version.js", jsHeader + JSON.stringify(version) +";");
}
var walk = function(dir, filelist) {
var files = fs.readdirSync(dir);
var filelist = filelist || [];
files.forEach(function(file) {
if (fs.statSync(dir + file).isDirectory()) {
filelist = walk(dir + file + '/', filelist);
} else {
if(file.indexOf('.') != 0) filelist.push(dir + file);
}
});
return filelist;
};
var minify = function(set) {
var includes = [];
for(var i = 0; i < set.length; i++) {
includes.push(set[i]);
}
var result = "";
try {
result = UglifyJS.minify(includes);
} catch(e) {
console.log(e);
}
return result.code;
}
var concat = function(set) {
var concatFile = "";
for(var i = 0; i < set.length; i++) {
var f = set[i];
var name = f.substring(baseUrl.length);
concatFile += "/* --- --- [" + name + "] --- --- */\n\n";
concatFile += fs.readFileSync(f);
concatFile += "\n\n";
}
return concatFile;
}
var createBucket = function(folder, files) {
files = files || [];
if(folder instanceof Array) {
folder.forEach(function(f) {
files = files.concat(walk(baseUrl + f + '/'));
});
} else {
files = files.concat(walk(baseUrl + folder + '/'));
}
var result = {};
result.concat = concat(files);
result.mini = minify(files);
return result;
}
var saveBucket = function(bucket, fileBase) {
var cf = outputUrl + fileBase + '.js';
var mf = outputUrl + fileBase + '.min.js';
fs.writeFileSync(cf, bucket.concat);
fs.writeFileSync(mf, bucket.mini);
var cs = fs.statSync(cf).size;
var ms = fs.statSync(mf).size;
var cks = (cs / 1024) | 0;
var mks = (ms / 1024) | 0;
console.log('[ ' + cf + '\t' + cs + ' bytes\t' + cks + ' kb ]');
console.log('[ ' + mf + '\t' + ms + ' bytes\t' + mks + ' kb ]');
}
var logMessage = process.argv[2];
if(!logMessage) {
console.warn('WARNING - Please provide a log message for this build or - for dev/test builds');
return;
}
version.build++;
version.date = moment().format('MMM Do YYYY');
if(logMessage != '-') {
console.log('[ Squareroot.js v' + version.version + ' build ' + version.build + ' ]');
addToChangeLog(logMessage);
} else {
console.log('[ Squareroot.js v' + version.version + ' DEV BUILD (' + version.build + ') ]');
}
updateVersionFile();
var core = createBucket(['common', 'math', 'two', 'primitives', 'scene'], [baseUrl + 'SQR.js', baseUrl + 'Version.js', baseUrl + 'GLSL.js']);
saveBucket(core, 'sqr');
var two = createBucket(['math', 'two'], [baseUrl + 'SQR.js', baseUrl + 'Version.js']);
saveBucket(two, 'sqr-two');
saveBucket(createBucket('vr'), 'sqr-vr');
// #### Custom build for Kuula ###
var k = function() {
var ffs = [];
Array.prototype.slice.call(arguments).forEach(function(f) {
var p = baseUrl + f + '.js';
ffs.push(p);
});
return ffs;
}
var kuula = createBucket([], k(
'SQR',
'Version',
'GLSL',
'common/Buffer',
'common/Context',
'common/Loader',
'common/Pointer3d',
'common/Ray',
'common/Renderer',
'common/Shader',
'common/Texture',
'common/Transform',
'common/FrameBuffer',
'common/Collider',
'math/Color',
'math/Math',
'math/Matrix2D',
'math/Matrix33',
'math/Matrix44',
'math/ProjectionMatrix',
'math/Quaternion',
'math/Vector2',
'math/Vector3',
'math/Intersection',
'primitives/Mesh',
'primitives/Poly',
'primitives/Plane',
'primitives/PostEffect',
'primitives/Sphere',
'primitives/Cube',
'primitives/Vertex',
'two/CanvasRenderer',
'two/Transform2d',
'vr/Gyro'
));
saveBucket(kuula, 'sqr-kuula')