Tone.js/gulp/gulpfile.js

238 lines
6.1 KiB
JavaScript
Raw Normal View History

/* globals process, __dirname */
2015-05-23 22:14:03 +00:00
var gulp = require("gulp");
2016-09-30 04:42:18 +00:00
var gutil = require("gulp-util");
var glob = require("glob");
2015-05-23 22:14:03 +00:00
var tap = require("gulp-tap");
var concat = require("gulp-concat");
var path = require("path");
var fs = require("fs");
var amdOptimize = require("amd-optimize");
var replace = require("gulp-replace");
var indent = require("gulp-indent");
2017-08-08 17:29:58 +00:00
var child_process = require("child_process");
var flatten = require("gulp-flatten");
2015-05-23 22:14:03 +00:00
var insert = require("gulp-insert");
var del = require("del");
var uglify = require("gulp-uglify");
var rename = require("gulp-rename");
2015-06-08 14:42:11 +00:00
var sass = require("gulp-ruby-sass");
var prefix = require("gulp-autoprefixer");
2015-08-31 15:44:49 +00:00
var openFile = require("gulp-open");
2017-10-26 19:10:52 +00:00
var eslint = require("gulp-eslint");
2017-04-30 17:02:35 +00:00
var coveralls = require("gulp-coveralls");
var git = require("gulp-git");
2015-08-31 15:44:49 +00:00
var argv = require("yargs")
.alias("f", "file")
.alias("s", "signal")
.alias("i", "instrument")
.alias("o", "source")
2015-11-04 20:59:09 +00:00
.alias("v", "event")
.alias("t", "control")
2015-08-31 15:44:49 +00:00
.alias("e", "effect")
.alias("c", "core")
.alias("m", "component")
2016-04-18 06:06:12 +00:00
.alias("y", "type")
2017-03-26 18:51:25 +00:00
.alias("x", "examples")
2015-08-31 15:44:49 +00:00
.argv;
2016-03-04 21:25:44 +00:00
var KarmaServer = require("karma").Server;
2015-05-23 22:14:03 +00:00
var BRANCH = process.env.TRAVIS && !process.env.TRAVIS_PULL_REQUEST ? process.env.TRAVIS_BRANCH : "dev";
var IS_DEV = BRANCH === "dev";
2017-08-08 17:29:58 +00:00
var VERSION = fs.readFileSync("../Tone/core/Tone.js", "utf-8")
.match(/(?:Tone\.version\s*=\s*)(?:'|")(.*)(?:'|");/m)[1];
//dev versions are just 'dev'
VERSION = IS_DEV ? "dev" : VERSION;
2017-08-08 17:29:58 +00:00
var TMP_FOLDER = "../tmp";
2015-05-23 22:14:03 +00:00
/**
* BUILDING
*/
//collect all of the files into one file prefixed with 'require'
2016-09-30 04:42:18 +00:00
gulp.task("collectDependencies", function(done) {
glob("../Tone/*/*.js", function(err, files){
var modules = [];
gutil.log(gutil.colors.magenta("files found:", files.length));
files.forEach(function(file){
//remove the precedding ../ and the trailing .js
var module = file.substring(3, file.length - 3);
modules.push(module);
2015-05-23 22:14:03 +00:00
});
2016-09-30 04:42:18 +00:00
//write it to disk
var reqString = "/* BEGIN REQUIRE */ require("+JSON.stringify(modules)+", function(){});";
fs.writeFile("toneMain.js", reqString, done);
2015-05-23 22:14:03 +00:00
});
});
gulp.task("compile", ["collectDependencies"], function(){
return gulp.src("./toneMain.js")
2015-05-23 22:14:03 +00:00
// Traces all modules and outputs them in the correct order.
.pipe(amdOptimize("gulp/toneMain", {
baseUrl : "../",
preserveComments : true
}))
.pipe(concat("Tone.js"))
.pipe(replace("'use strict';", ""))
//indent the contents
.pipe(indent({
tabs:true,
amount:1
}))
//replace the MainModule
2016-09-30 04:42:18 +00:00
.pipe(replace(/\/\* BEGIN REQUIRE \*\/(.|\n)*/gm, ""))
2015-05-23 22:14:03 +00:00
.pipe(replace("define('Tone/core/Tone', [], ", "Main("))
//replace the ToneModules
.pipe(replace(/define\(\s*'([^']*)'\s*\,\s*\[\s*'([^']*'\s*\,*\s*)+?\]\s*\,\s*/g, "Module("))
.pipe(insert.prepend(fs.readFileSync("./fragments/before.frag").toString()))
.pipe(gulp.dest("../build/"));
2016-09-30 04:42:18 +00:00
});
gulp.task("footer", ["compile"], function(){
return gulp.src("../build/Tone.js")
2015-05-23 22:14:03 +00:00
.pipe(insert.append(fs.readFileSync("./fragments/after.frag").toString()))
.pipe(gulp.dest("../build/"));
2015-08-18 16:53:06 +00:00
});
gulp.task("minify", ["footer"], function(){
return gulp.src("../build/Tone.js")
2016-09-30 04:42:18 +00:00
.pipe(uglify({
preserveComments : "some",
compress: {
dead_code : true,
evaluate : true,
loops : true,
if_return : true,
hoist_vars : true,
booleans : true,
conditionals : true,
sequences : true,
comparisons : true,
hoist_funs : true,
join_vars : true,
cascade : true,
},
2015-08-18 16:53:06 +00:00
}))
2016-09-30 04:42:18 +00:00
.pipe(rename({
suffix : ".min"
}))
// .pipe(del(["./toneMain.js"]))
.pipe(gulp.dest("../build/"));
});
gulp.task("build", ["minify"], function(){
return del(["./toneMain.js"]);
2015-05-23 22:14:03 +00:00
});
//default build
gulp.task("default", ["build"]);
2015-05-23 22:14:03 +00:00
2015-06-08 14:42:11 +00:00
/**
* Sass
*/
gulp.task("sass", function () {
sass("../examples/style/examples.scss", {sourcemap: false})
.pipe(prefix("last 2 version"))
.pipe(gulp.dest("../examples/style/"));
});
gulp.task("example", function() {
2016-11-07 00:40:32 +00:00
gulp.watch(["../examples/style/examples.scss"], ["sass"]);
2015-06-08 14:42:11 +00:00
});
2015-08-16 18:58:25 +00:00
2016-11-07 00:40:32 +00:00
/**
* LINTING
*/
gulp.task("lint", function() {
return gulp.src("../Tone/*/*.js")
2017-10-26 19:10:52 +00:00
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task("lint-fix", function() {
return gulp.src("../Tone/*/*.js")
.pipe(eslint({
fix : true
}))
.pipe(eslint.format())
.pipe(eslint.failAfterError())
.pipe(gulp.dest("../Tone"));
2016-11-07 00:40:32 +00:00
});
2017-03-26 18:51:25 +00:00
gulp.task("karma-test", ["default"], function (done) {
2016-03-04 21:25:44 +00:00
new KarmaServer({
2016-11-07 00:40:32 +00:00
configFile: __dirname + "/karma.conf.js",
2016-03-04 21:25:44 +00:00
singleRun: true
}, done).start();
});
2015-08-16 18:58:25 +00:00
gulp.task("collectTests", function(done){
2015-08-31 15:44:49 +00:00
var tests = ["../test/*/*.js", "!../test/helper/*.js", "!../test/tests/*.js"];
if (argv.file){
tests = ["../test/*/"+argv.file+".js"];
} else if (argv.signal || argv.core || argv.component || argv.instrument ||
2017-03-26 18:51:25 +00:00
argv.source || argv.effect || argv.event || argv.type || argv.examples){
2015-08-31 15:44:49 +00:00
tests = [];
if (argv.signal){
tests.push("../test/signal/*.js");
}
if (argv.core){
tests.push("../test/core/*.js");
}
if (argv.source){
tests.push("../test/source/*.js");
}
if (argv.instrument){
tests.push("../test/instrument/*.js");
}
if (argv.component){
tests.push("../test/component/*.js");
}
if (argv.effect){
tests.push("../test/effect/*.js");
}
2015-12-06 18:09:17 +00:00
if (argv.event){
tests.push("../test/event/*.js");
2015-08-31 15:44:49 +00:00
}
2016-04-18 06:06:12 +00:00
if (argv.type){
tests.push("../test/type/*.js");
}
2017-03-26 18:51:25 +00:00
if (argv.examples){
tests.push("../test/examples/*.js");
}
}
2015-08-31 15:44:49 +00:00
// console.log(argv.signal === undefined);
2015-08-16 18:58:25 +00:00
var allFiles = [];
2015-08-31 15:44:49 +00:00
var task = gulp.src(tests)
2015-08-16 18:58:25 +00:00
.pipe(tap(function(file){
var fileName = path.relative("../test/", file.path);
allFiles.push(fileName.substring(0, fileName.length - 3));
}));
task.on("end", function(){
//build a require string
allFiles.unshift("Test");
var innerTask = gulp.src("./fragments/test.frag")
.pipe(replace("{FILES}", JSON.stringify(allFiles)))
2015-08-31 15:44:49 +00:00
.pipe(rename("Main.js"))
2015-08-16 18:58:25 +00:00
.pipe(gulp.dest("../test/"));
innerTask.on("end", done);
});
2016-11-07 00:40:32 +00:00
});
/**
* TEST ALL
*/
gulp.task("travis-test", ["lint", "karma-test"]);
2017-04-30 17:02:35 +00:00
/**
* COVERALLS
*/
gulp.task("coveralls", function(){
return gulp.src("../test/coverage/**/lcov.info")
.pipe(coveralls());
2017-08-08 17:29:58 +00:00
});