Tone.js/gulp/gulpfile.js

209 lines
5.6 KiB
JavaScript
Raw Normal View History

2015-05-23 22:14:03 +00:00
var gulp = require("gulp");
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");
var argv = require("yargs")
.alias("f", "file")
.alias("s", "signal")
.alias("i", "instrument")
.alias("o", "source")
.alias("t", "structure")
.alias("e", "effect")
.alias("c", "core")
.alias("m", "component")
.argv;
var webserver = require("gulp-webserver");
2015-05-23 22:14:03 +00:00
/**
* BUILDING
*/
//collect all of the files into one file prefixed with 'require'
gulp.task("makerequire", function(done) {
var allFiles = [];
var task = gulp.src(["../Tone/*/*.js"])
.pipe(tap(function(file){
var fileName = path.relative("./", file.path);
allFiles.push(fileName.substring(0, fileName.length - 3));
}));
task.on("end", function(){
//build a require string
var reqString = "require("+JSON.stringify(allFiles)+", function(){});";
fs.writeFile("toneMain.js", reqString, function(err){
if (err){
console.log(err);
}
done();
});
});
});
//build the package from the requirefile
//replace all of the define strings
gulp.task("buildrequire", ["makerequire"], function(done){
var stream = gulp.src("./toneMain.js")
// 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
.pipe(replace("define('Tone/core/Tone', [], ", "Main("))
//replace the ToneModules
.pipe(replace(/define\(\s*'([^']*)'\s*\,\s*\[\s*'([^']*'\s*\,*\s*)+?\]\s*\,\s*/g, "Module("))
.pipe(tap(function(file){
var fileAsString = file.contents.toString();
file.contents = new Buffer(fileAsString.substr(0, fileAsString.indexOf("require([") - 1));
2015-08-18 16:53:06 +00:00
fs.writeFile("toneMain.js", file.contents, function(err){
if (err){
console.log(err);
}
});
2015-05-23 22:14:03 +00:00
}))
//surround the file with the header/footer
.pipe(insert.prepend(fs.readFileSync("./fragments/before.frag").toString()))
.pipe(insert.append(fs.readFileSync("./fragments/after.frag").toString()))
//clean up the files
.pipe(gulp.dest("../build/"));
2015-08-18 16:53:06 +00:00
stream.on("end", done);
});
gulp.task("buildp5Tone", ["buildrequire"], function(done){
var stream = gulp.src("./toneMain.js")
.pipe(rename("p5.Tone.js"))
//surround the file with the header/footer
.pipe(insert.prepend(fs.readFileSync("./fragments/before.frag").toString()))
.pipe(insert.append(fs.readFileSync("./fragments/p5-after.frag").toString()))
//clean up the files
.pipe(gulp.dest("../build/"));
2015-05-23 22:14:03 +00:00
stream.on("end", function(){
2015-06-07 16:11:26 +00:00
del(["./toneMain.js"], done);
2015-05-23 22:14:03 +00:00
});
});
2015-08-18 16:53:06 +00:00
gulp.task("build", ["buildp5Tone"], function(){
gulp.src("../build/{p5.Tone,Tone}.js")
2015-05-23 22:14:03 +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
.pipe(rename(function(path) {
path.basename += ".min";
}))
2015-05-23 22:14:03 +00:00
.pipe(gulp.dest("../build/"));
});
//default build
gulp.task("default", ["build"], function(){});
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() {
2015-06-13 23:27:51 +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
/**
2015-08-31 15:44:49 +00:00
* TEST RUNNER
2015-08-16 18:58:25 +00:00
*/
2015-08-31 15:44:49 +00:00
gulp.task("test", ["server", "collectTests"], function(){
gulp.src("../test/index.html")
.pipe(openFile({uri: "http://localhost:3000/test"}));
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 ||
argv.source || argv.effect || argv.structure){
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");
}
if (argv.structure){
tests.push("../test/structure/*.js");
}
}
// 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);
});
});