Tone.js/gulp/gulpfile.js

222 lines
5.6 KiB
JavaScript
Raw Normal View History

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");
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");
2016-11-07 00:40:32 +00:00
var jshint = require("gulp-jshint");
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")
2015-08-31 15:44:49 +00:00
.argv;
var webserver = require("gulp-webserver");
2016-03-04 21:25:44 +00:00
var KarmaServer = require("karma").Server;
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
});
});
2016-09-30 04:42:18 +00:00
gulp.task("compile", ["collectDependencies"], function(done){
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()))
2016-09-30 04:42:18 +00:00
.pipe(gulp.dest("../build/"))
.on("end", done);
});
gulp.task("footer", ["compile"], function(done){
gulp.src("../build/Tone.js")
2015-05-23 22:14:03 +00:00
.pipe(insert.append(fs.readFileSync("./fragments/after.frag").toString()))
2016-09-30 04:42:18 +00:00
.pipe(gulp.dest("../build/"))
.on("end", done);
2015-08-18 16:53:06 +00:00
});
gulp.task("build", ["footer"], function(){
2016-10-05 05:30:49 +00:00
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("cleanup", ["build"], function(){
del(["./toneMain.js"]);
2015-05-23 22:14:03 +00:00
});
//default build
gulp.task("default", ["cleanup"]);
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
2015-08-31 15:44:49 +00:00
/**
* THE WEBSERVER
*/
gulp.task("server", function(){
gulp.src("../")
.pipe(webserver({
// livereload: false,
directoryListing: true,
port : 3000,
open: false
}));
});
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")
.pipe(jshint())
2016-11-07 01:13:13 +00:00
.pipe(jshint.reporter("default"))
.pipe(jshint.reporter("fail"));
2016-11-07 00:40:32 +00:00
});
2015-08-16 18:58:25 +00:00
/**
2015-08-31 15:44:49 +00:00
* TEST RUNNER
2015-08-16 18:58:25 +00:00
*/
2016-03-04 21:25:44 +00:00
gulp.task("browser-test", ["server", "collectTests"], function(){
2015-08-31 15:44:49 +00:00
gulp.src("../test/index.html")
.pipe(openFile({uri: "http://localhost:3000/test"}));
2015-08-16 18:58:25 +00:00
});
2016-03-04 21:25:44 +00:00
gulp.task("karma-test", function (done) {
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 ||
2016-04-18 06:06:12 +00:00
argv.source || argv.effect || argv.event || argv.type){
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");
}
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"]);