2017-05-11 11:13:03 +02:00
|
|
|
/* jshint esversion:6 */
|
2017-03-29 17:30:37 +02:00
|
|
|
const path = require('path');
|
2017-05-11 11:13:03 +02:00
|
|
|
const fs = require('fs');
|
2017-03-29 17:30:37 +02:00
|
|
|
|
|
|
|
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 = {
|
2017-05-11 11:13:03 +02:00
|
|
|
model:
|
|
|
|
`exports.model = Model = {
|
2017-03-29 17:30:37 +02:00
|
|
|
Game: {},
|
|
|
|
Board: {},
|
|
|
|
Move: {}
|
|
|
|
};
|
2017-05-11 11:13:03 +02:00
|
|
|
`,
|
|
|
|
view:
|
|
|
|
`exports.view = View = {
|
2017-03-29 17:30:37 +02:00
|
|
|
Game: {},
|
|
|
|
Board: {},
|
|
|
|
Move: {}
|
|
|
|
};
|
|
|
|
`
|
|
|
|
};
|
|
|
|
|
|
|
|
const allGames = {};
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
var moduleDirs = [];
|
|
|
|
var modulesMap = {};
|
2017-05-11 11:37:22 +02:00
|
|
|
var exclusiveGames = null;
|
2017-05-11 11:13:03 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2017-05-11 11:37:22 +02:00
|
|
|
if (argv.games) {
|
|
|
|
exclusiveGames = {};
|
|
|
|
argv.games.split(":").forEach((game) => {
|
|
|
|
exclusiveGames[game] = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-29 17:30:37 +02:00
|
|
|
function HandleModuleGames(modelOnly) {
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
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
|
|
|
|
|
2017-05-11 11:37:22 +02:00
|
|
|
if (exclusiveGames && !exclusiveGames[game.name])
|
|
|
|
return;
|
2017-05-11 11:42:52 +02:00
|
|
|
if (typeof argv.obsolete != "undefined" && !argv.obsolete && game.config.model.obsolete)
|
|
|
|
return;
|
2017-05-11 11:37:22 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
// 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,
|
2017-05-03 10:38:38 +02:00
|
|
|
obsolete: game.config.model.obsolete
|
2017-05-11 11:13:03 +02:00
|
|
|
};
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
// create the game config file
|
|
|
|
push(new Vinyl({
|
|
|
|
path: moduleName + "/" + game.name + "-config.js",
|
|
|
|
contents: new Buffer('exports.config = ' + JSON.stringify(game.config))
|
|
|
|
}));
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
// create some specified resources
|
|
|
|
if (!modelOnly) {
|
2017-04-15 12:14:55 +02:00
|
|
|
var resources = {
|
2017-05-11 11:13:03 +02:00
|
|
|
model: ["thumbnail", "rules", "description", "credits"],
|
2017-04-15 12:14:55 +02:00
|
|
|
view: ["css"]
|
|
|
|
};
|
2017-05-11 11:13:03 +02:00
|
|
|
["model", "view"].forEach((modelView) => {
|
|
|
|
resources[modelView].forEach((field) => {
|
2017-04-15 12:14:55 +02:00
|
|
|
var files = [];
|
2017-05-11 11:13:03 +02:00
|
|
|
switch (typeof game.config[modelView][field]) {
|
2017-04-15 12:14:55 +02:00
|
|
|
case "string":
|
|
|
|
files.push(game.config[modelView][field]);
|
|
|
|
break;
|
|
|
|
case "object":
|
2017-05-11 11:13:03 +02:00
|
|
|
for (var f in game.config[modelView][field])
|
2017-04-15 12:14:55 +02:00
|
|
|
files.push(game.config[modelView][field][f]);
|
|
|
|
break;
|
|
|
|
}
|
2017-05-11 11:13:03 +02:00
|
|
|
files = files.map((file) => {
|
|
|
|
return path.join(modulesMap[moduleName], file);
|
2017-04-15 12:14:55 +02:00
|
|
|
});
|
|
|
|
var stream = gulp.src(files)
|
2017-05-11 11:13:03 +02:00
|
|
|
.pipe(rename(function (path) {
|
2017-04-15 12:14:55 +02:00
|
|
|
path.dirname = moduleName;
|
|
|
|
}))
|
2017-05-11 11:13:03 +02:00
|
|
|
.pipe(through.obj(function (file, enc, next) {
|
2017-04-15 12:14:55 +02:00
|
|
|
push(file);
|
|
|
|
next();
|
|
|
|
}))
|
|
|
|
;
|
|
|
|
streams.push(stream);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
// 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))
|
2017-04-27 07:14:02 +02:00
|
|
|
.pipe(concat(fileName))
|
2017-05-11 11:13:03 +02:00
|
|
|
.pipe(gulpif(argv.prod, uglify()))
|
2017-04-27 07:14:02 +02:00
|
|
|
.on('error', function (err) { gutil.log(gutil.colors.red('[Error]'), err.toString()); })
|
2017-05-11 11:13:03 +02:00
|
|
|
.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();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2017-03-29 17:30:37 +02:00
|
|
|
}
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
gulp.task("build-node-games", function () {
|
|
|
|
return gulp.src(moduleDirs)
|
|
|
|
.pipe(HandleModuleGames(true))
|
|
|
|
.pipe(gulp.dest("dist/node/games"));
|
2017-03-29 17:30:37 +02:00
|
|
|
});
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
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;
|
2017-03-29 17:30:37 +02:00
|
|
|
}
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
gulp.task("build-node-core", function () {
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
var joclyCoreStream =
|
|
|
|
ProcessJS(gulp.src([
|
|
|
|
"src/core/jocly.core.js",
|
|
|
|
]));
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
var joclyBaseStream =
|
|
|
|
ProcessJS(gulp.src([
|
|
|
|
"src/core/jocly.util.js",
|
|
|
|
"src/core/jocly.uct.js",
|
|
|
|
"src/core/jocly.game.js"
|
|
|
|
]));
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
var allGamesStream = source('jocly-allgames.js');
|
|
|
|
allGamesStream.end('exports.games = ' + JSON.stringify(allGames));
|
|
|
|
allGamesStream = ProcessJS(allGamesStream.pipe(buffer()));
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
return merge(joclyCoreStream, allGamesStream, joclyBaseStream)
|
|
|
|
.pipe(gulp.dest("dist/node"));
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
});
|
2017-03-29 17:30:37 +02:00
|
|
|
|
|
|
|
function CopyLicense(target) {
|
2017-05-11 11:13:03 +02:00
|
|
|
return gulp.src(["COPYING.md", "CONTRIBUTING.md", "AGPL-3.0.txt"])
|
|
|
|
.pipe(gulp.dest(target));
|
2017-03-29 17:30:37 +02:00
|
|
|
}
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
gulp.task("copy-browser-license", function () {
|
|
|
|
return CopyLicense("dist/browser");
|
2017-03-29 17:30:37 +02:00
|
|
|
});
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
gulp.task("copy-node-license", function () {
|
|
|
|
return CopyLicense("dist/node");
|
2017-03-29 17:30:37 +02:00
|
|
|
});
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
gulp.task("build-node", function (callback) {
|
|
|
|
runSequence("build-node-games", ["build-node-core", "copy-node-license"], callback);
|
2017-03-29 17:30:37 +02:00
|
|
|
});
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
gulp.task("build-browser-games", function () {
|
|
|
|
return gulp.src(moduleDirs)
|
|
|
|
.pipe(HandleModuleGames(false))
|
|
|
|
.pipe(gulp.dest("dist/browser/games"));
|
2017-03-29 17:30:37 +02:00
|
|
|
});
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
gulp.task("build-browser-core", function () {
|
|
|
|
|
|
|
|
var _ProcessJS = function (s) { return s; };
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
var b = browserify({
|
|
|
|
entries: "src/browser/jocly.js",
|
|
|
|
debug: true,
|
|
|
|
standalone: "Jocly"
|
|
|
|
});
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
var joclyBrowserStream = ProcessJS(b.bundle()
|
|
|
|
.pipe(source('jocly.js'))
|
|
|
|
.pipe(buffer()));
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
var joclyCoreStream = ProcessJS(gulp.src([
|
|
|
|
"src/core/jocly.core.js",
|
|
|
|
]));
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
var joclyBaseStream = ProcessJS(gulp.src([
|
|
|
|
"src/core/jocly.util.js",
|
|
|
|
"src/core/jocly.uct.js",
|
|
|
|
"src/core/jocly.game.js"
|
|
|
|
]), "jocly.game.js", true);
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
var joclyExtraScriptsStream = ProcessJS(gulp.src([
|
|
|
|
"src/browser/jocly.aiworker.js",
|
|
|
|
"src/browser/jocly.embed.js"
|
|
|
|
]));
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
var joclyExtraStream = gulp.src([
|
|
|
|
"src/browser/jocly.embed.html"
|
|
|
|
]);
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
var joclyResStream = gulp.src("src/browser/res/**/*")
|
|
|
|
.pipe(rename(function (path) {
|
|
|
|
path.dirname = "res/" + path.dirname;
|
|
|
|
}));
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
var allGamesStream = source('jocly-allgames.js');
|
|
|
|
allGamesStream.end('exports.games = ' + JSON.stringify(allGames));
|
|
|
|
allGamesStream = ProcessJS(allGamesStream.pipe(buffer()));
|
2017-03-29 17:30:37 +02:00
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
return merge(joclyBrowserStream, joclyCoreStream, allGamesStream, joclyBaseStream,
|
|
|
|
joclyExtraStream, joclyExtraScriptsStream, joclyResStream)
|
|
|
|
.pipe(gulp.dest("dist/browser"));
|
2017-03-29 17:30:37 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
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",
|
2017-05-17 09:43:27 +02:00
|
|
|
lib + "kalman.js",
|
2017-05-11 11:13:03 +02:00
|
|
|
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"))
|
|
|
|
;
|
2017-03-29 17:30:37 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
gulp.task("build-browser", function (callback) {
|
|
|
|
runSequence("build-browser-games", ["build-browser-core",
|
|
|
|
"build-browser-xdview", "copy-browser-license"], callback);
|
2017-03-29 17:30:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
gulp.task("build", function (callback) {
|
|
|
|
runSequence("clean", ["build-browser", "build-node"], callback);
|
2017-03-29 17:30:37 +02:00
|
|
|
});
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
gulp.task("clean", function () {
|
|
|
|
return del(["dist/*"], { force: true });
|
2017-03-29 17:30:37 +02:00
|
|
|
});
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
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"]);
|
2017-03-29 17:30:37 +02:00
|
|
|
});
|
|
|
|
|
2017-05-11 11:13:03 +02:00
|
|
|
gulp.task("help", function () {
|
|
|
|
var help = `
|
2017-03-29 17:30:37 +02:00
|
|
|
usage: gulp [<commands>] [<options>]
|
|
|
|
|
|
|
|
commands:
|
|
|
|
build: generate clean project build
|
|
|
|
watch: watch project and build dynamically on changes
|
|
|
|
|
|
|
|
options:
|
|
|
|
--prod: generate for production
|
2017-05-11 11:13:03 +02:00
|
|
|
--no-default-games: do not process game module from default src/games directory
|
2017-05-11 11:37:22 +02:00
|
|
|
--modules <modules>: process additional game modules from specified directories (colon separated)
|
|
|
|
--games <games>: process exclusively the specified games (colon separated)
|
2017-05-11 11:42:52 +02:00
|
|
|
--no-obsolete: do not include games marked as obsolete
|
2017-03-29 17:30:37 +02:00
|
|
|
`;
|
2017-05-11 11:13:03 +02:00
|
|
|
console.log(help);
|
|
|
|
process.exit(0);
|
2017-03-29 17:30:37 +02:00
|
|
|
});
|