forked from aclap-dev/jocly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
375 lines (321 loc) · 9.82 KB
/
gulpfile.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
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
/* jshint esversion:6 */
const path = require('path');
const fs = require('fs');
const gulp = require('gulp');
const debug = require('gulp-debug');
const del = require("del");
const through = require('through2');
const Vinyl = require("vinyl");
const merge = require('merge-stream');
const rename = require("gulp-rename");
const concat = require('gulp-concat');
const add = require('gulp-add');
const sourcemaps = require('gulp-sourcemaps');
const runSequence = require('run-sequence');
const uglify = require('gulp-uglify');
const babel = require('gulp-babel');
const browserify = require('browserify');
const buffer = require("vinyl-buffer");
const source = require('vinyl-source-stream');
const argv = require('yargs').argv;
const gulpif = require('gulp-if');
const gutil = require('gulp-util');
const modulifyHeaders = {
model:
`exports.model = Model = {
Game: {},
Board: {},
Move: {}
};
`,
view:
`exports.view = View = {
Game: {},
Board: {},
Move: {}
};
`
};
const allGames = {};
var moduleDirs = [];
var modulesMap = {};
var exclusiveGames = null;
if (typeof argv.defaultGames == "undefined" || argv.defaultGames)
moduleDirs = fs.readdirSync("src/games").map((dir) => {
return path.join("src/games", dir);
});
if (argv.modules)
moduleDirs = moduleDirs.concat(argv.modules.split(":"));
moduleDirs.forEach((dir) => {
modulesMap[path.basename(dir)] = dir;
});
if (argv.games) {
exclusiveGames = {};
argv.games.split(":").forEach((game) => {
exclusiveGames[game] = true;
});
}
function HandleModuleGames(modelOnly) {
return through.obj(function (file, enc, next) {
// this is executed for every game module
var push = this.push.bind(this);
var moduleName = path.basename(file.path);
var moduleManifest = require(file.path);
var streams = [];
moduleManifest.games.forEach((game) => {
// this is executed for every game in the game module
if (exclusiveGames && !exclusiveGames[game.name])
return;
if (typeof argv.obsolete != "undefined" && !argv.obsolete && game.config.model.obsolete)
return;
// same some game data so we can list all games later
allGames[game.name] = {
title: game.config.model["title-en"],
summary: game.config.model.summary,
thumbnail: game.config.model.thumbnail,
module: moduleName,
obsolete: game.config.model.obsolete
};
// create the game config file
push(new Vinyl({
path: moduleName + "/" + game.name + "-config.js",
contents: new Buffer('exports.config = ' + JSON.stringify(game.config))
}));
// create some specified resources
if (!modelOnly) {
var resources = {
model: ["thumbnail", "rules", "description", "credits"],
view: ["css"]
};
["model", "view"].forEach((modelView) => {
resources[modelView].forEach((field) => {
var files = [];
switch (typeof game.config[modelView][field]) {
case "string":
files.push(game.config[modelView][field]);
break;
case "object":
for (var f in game.config[modelView][field])
files.push(game.config[modelView][field][f]);
break;
}
files = files.map((file) => {
return path.join(modulesMap[moduleName], file);
});
var stream = gulp.src(files)
.pipe(rename(function (path) {
path.dirname = moduleName;
}))
.pipe(through.obj(function (file, enc, next) {
push(file);
next();
}))
;
streams.push(stream);
});
});
}
// create model and view script files
function Scripts(which) {
var scripts = game[which + "Scripts"].map((script) => {
return path.join(modulesMap[moduleName], script);
});
var fileName = moduleName + "/" + game.name + "-" + which + ".js";
var stream = gulp.src(scripts)
.pipe(gulpif(!argv.prod, sourcemaps.init()))
.pipe(add('_', modulifyHeaders[which], true))
.pipe(concat(fileName))
.pipe(gulpif(argv.prod, uglify()))
.on('error', function (err) { gutil.log(gutil.colors.red('[Error]'), err.toString()); })
.pipe(gulpif(!argv.prod, sourcemaps.write('.')))
.pipe(through.obj(function (file, enc, next) {
push(file);
next();
}));
streams.push(stream);
}
if (modelOnly)
Scripts("model");
else
["model", "view"].forEach(Scripts);
});
// create module common resources
if (!modelOnly) {
var stream = gulp.src(modulesMap[moduleName] + "/res/**/*")
.pipe(rename(function (path) {
path.dirname = moduleName + "/res/" + path.dirname;
}))
.pipe(through.obj(function (file, enc, next) {
push(file);
next();
}))
;
streams.push(stream);
}
merge(streams)
.on("finish", function () {
next();
});
});
}
gulp.task("build-node-games", function () {
return gulp.src(moduleDirs)
.pipe(HandleModuleGames(true))
.pipe(gulp.dest("dist/node/games"));
});
function ProcessJS(stream, concatName, skipBabel) {
if (!argv.prod)
stream = stream.pipe(sourcemaps.init());
if (!skipBabel)
stream = stream.pipe(babel({
presets: ['es2015'],
compact: !!argv.prod
}));
if (argv.prod)
stream = stream.pipe(uglify())
.on('error', function (err) {
gutil.log(gutil.colors.red('[Error]'), err.toString());
this.emit('end');
});
if (concatName)
stream = stream.pipe(concat(concatName));
if (!argv.prod)
stream = stream.pipe(sourcemaps.write("."));
return stream;
}
gulp.task("build-node-core", function () {
var joclyCoreStream =
ProcessJS(gulp.src([
"src/core/jocly.core.js",
]));
var joclyBaseStream =
ProcessJS(gulp.src([
"src/core/jocly.util.js",
"src/core/jocly.uct.js",
"src/core/jocly.game.js"
]));
var allGamesStream = source('jocly-allgames.js');
allGamesStream.end('exports.games = ' + JSON.stringify(allGames));
allGamesStream = ProcessJS(allGamesStream.pipe(buffer()));
return merge(joclyCoreStream, allGamesStream, joclyBaseStream)
.pipe(gulp.dest("dist/node"));
});
function CopyLicense(target) {
return gulp.src(["COPYING.md", "CONTRIBUTING.md", "AGPL-3.0.txt"])
.pipe(gulp.dest(target));
}
gulp.task("copy-browser-license", function () {
return CopyLicense("dist/browser");
});
gulp.task("copy-node-license", function () {
return CopyLicense("dist/node");
});
gulp.task("build-node", function (callback) {
runSequence("build-node-games", ["build-node-core", "copy-node-license"], callback);
});
gulp.task("build-browser-games", function () {
return gulp.src(moduleDirs)
.pipe(HandleModuleGames(false))
.pipe(gulp.dest("dist/browser/games"));
});
gulp.task("build-browser-core", function () {
var _ProcessJS = function (s) { return s; };
var b = browserify({
entries: "src/browser/jocly.js",
debug: true,
standalone: "Jocly"
});
var joclyBrowserStream = ProcessJS(b.bundle()
.pipe(source('jocly.js'))
.pipe(buffer()));
var joclyCoreStream = ProcessJS(gulp.src([
"src/core/jocly.core.js",
]));
var joclyBaseStream = ProcessJS(gulp.src([
"src/core/jocly.util.js",
"src/core/jocly.uct.js",
"src/core/jocly.game.js"
]), "jocly.game.js", true);
var joclyExtraScriptsStream = ProcessJS(gulp.src([
"src/browser/jocly.aiworker.js",
"src/browser/jocly.embed.js"
]));
var joclyExtraStream = gulp.src([
"src/browser/jocly.embed.html"
]);
var joclyResStream = gulp.src("src/browser/res/**/*")
.pipe(rename(function (path) {
path.dirname = "res/" + path.dirname;
}));
var allGamesStream = source('jocly-allgames.js');
allGamesStream.end('exports.games = ' + JSON.stringify(allGames));
allGamesStream = ProcessJS(allGamesStream.pipe(buffer()));
return merge(joclyBrowserStream, joclyCoreStream, allGamesStream, joclyBaseStream,
joclyExtraStream, joclyExtraScriptsStream, joclyResStream)
.pipe(gulp.dest("dist/browser"));
});
gulp.task("build-browser-xdview", function () {
const lib = "third-party/";
const src = "src/";
const srcLib = "src/lib/";
const nmLib = "node_modules/";
var libs = ProcessJS(gulp.src([
lib + "three.js",
nmLib + "jquery/dist/jquery.js"
]));
var packedLibs = ProcessJS(gulp.src([
lib + "SubdivisionModifier.js",
lib + "tween.js",
lib + "tween.fix.js",
srcLib + "JoclyOrbitControls.js",
lib + "DeviceOrientationControls.js",
lib + "Projector.js",
lib + "threex.domevent.js",
lib + "threex.domevent.object3d.js",
lib + "StereoEffect.js",
lib + "AnaglyphEffect.js",
srcLib + "VRGamepad.js",
lib + "VRControls.js",
lib + "VREffect.js",
lib + "OBJLoader.js",
lib + "MTLLoader.js",
lib + "kalman.js",
src + "browser/jocly.ar.js",
src + "browser/jocly.state-machine.js",
src + "browser/jocly.xd-view.js"
]), "jocly-xdview.js", true);
return merge(libs, packedLibs)
.pipe(gulp.dest("dist/browser"))
;
});
gulp.task("build-browser", function (callback) {
runSequence("build-browser-games", ["build-browser-core",
"build-browser-xdview", "copy-browser-license"], callback);
});
gulp.task("build", function (callback) {
runSequence("clean", ["build-browser", "build-node"], callback);
});
gulp.task("clean", function () {
return del(["dist/*"], { force: true });
});
gulp.task("watch", function () {
gulp.watch(moduleDirs.map((dir) => { return dir + "/**/*"; }), ["build-node-games", "build-browser-games"]);
gulp.watch("src/{browser,core,lib}/**/*", ["build-browser-core", "build-browser-xdview"]);
gulp.watch("src/{node,core}/**/*", ["build-node-core"]);
});
gulp.task("help", function () {
var help = `
usage: gulp [<commands>] [<options>]
commands:
build: generate clean project build
watch: watch project and build dynamically on changes
options:
--prod: generate for production
--no-default-games: do not process game module from default src/games directory
--modules <modules>: process additional game modules from specified directories (colon separated)
--games <games>: process exclusively the specified games (colon separated)
--no-obsolete: do not include games marked as obsolete
`;
console.log(help);
process.exit(0);
});