Tone.js/gulp/gulpfile.js

350 lines
9.5 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");
2016-11-07 00:40:32 +00:00
var jshint = require("gulp-jshint");
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
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
});
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-08-08 17:29:58 +00:00
* COMMIT BUILD
*/
gulp.task("cloneBuild", function(done) {
var gitUser = "";
if (process.env.TRAVIS && process.env.GH_TOKEN){
gitUser = process.env.GH_TOKEN+"@";
}
2017-08-08 17:29:58 +00:00
git.clone("https://"+gitUser+"github.com/Tonejs/build", {args: `${TMP_FOLDER}/build`}, done);
});
2017-04-28 03:34:18 +00:00
gulp.task("moveToDev", ["build", "cloneBuild"], function(){
// move files to 'dev' folder
return gulp.src("../build/*.js")
2017-08-08 17:29:58 +00:00
.pipe(gulp.dest(`${TMP_FOLDER}/build/dev/`));
});
gulp.task("commitDev", ["moveToDev"], function(){
2017-08-08 17:29:58 +00:00
process.chdir(`${TMP_FOLDER}/build`);
return gulp.src("./dev/*")
.pipe(git.add())
2017-08-08 17:29:58 +00:00
.pipe(git.commit(`${VERSION} build #${process.env.TRAVIS_BUILD_NUMBER}: ${process.env.TRAVIS_COMMIT_MESSAGE}`));
});
gulp.task("pushBuild", ["commitDev"], function(done){
if (process.env.TRAVIS && process.env.GH_TOKEN){
2017-04-28 02:01:00 +00:00
git.push("origin", "gh-pages", {args: " -f"}, function (err) {
if (err) throw err;
done();
});
} else {
done();
}
});
gulp.task("commitDevBuild", ["pushBuild"], function(){
2017-08-08 17:29:58 +00:00
return del([`${TMP_FOLDER}/build`], { force : true});
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
});
/**
* JS DOC ATTRIBUTES
*/
gulp.task("cloneSite", function(done){
var gitUser = "";
if (process.env.TRAVIS && process.env.GH_TOKEN){
gitUser = process.env.GH_TOKEN+"@";
}
git.clone("https://"+gitUser+"github.com/Tonejs/tonejs.github.io", {args: `${TMP_FOLDER}/Site`}, done);
});
gulp.task("commitSite", ["buildJsdocs"], function(){
process.chdir(`${TMP_FOLDER}/Site`);
return gulp.src("*")
.pipe(git.add())
.pipe(git.commit(`${VERSION} build #${process.env.TRAVIS_BUILD_NUMBER}: ${process.env.TRAVIS_COMMIT_MESSAGE}`));
});
gulp.task("pushJSDocs", ["commitSite"], function(done){
if (process.env.TRAVIS && process.env.GH_TOKEN){
git.push("origin", "master", {args: " -f"}, function (err) {
if (err) throw err;
done();
});
} else {
done();
}
});
gulp.task("empty.md", ["cloneSite"], function(){
return gulp.src("../Tone/*/*.js")
.pipe(tap(function(file){
var className = path.basename(file.path, ".js");
var pathSplit = file.path.split("/");
var category = pathSplit[pathSplit.length-2];
file.contents = Buffer.from(`---\ntitle: ${className}\nlayout: ${className === "Type" ? "type" : "doc"}\nversion: ${VERSION}\n---`);
2017-08-08 17:29:58 +00:00
}))
.pipe(rename({extname: ".md"}))
.pipe(flatten())
.pipe(gulp.dest(`${TMP_FOLDER}/Site/_documentation/${VERSION.includes("dev") ? "dev" : VERSION}`))
2017-08-08 17:29:58 +00:00
.pipe(tap(function(file){
// and another one which just forwards
var className = path.basename(file.path, ".md");
file.contents = Buffer.from(`---\ntitle: ${className}\nlayout: forward\n---`);
}))
.pipe(gulp.dest(`${TMP_FOLDER}/Site/_documentation/`));
});
2017-08-08 23:23:04 +00:00
gulp.task("buildJsdocs", ["empty.md"], function(done){
2017-08-08 17:29:58 +00:00
glob("../Tone/*/*.js", function(err, files){
2017-08-08 23:23:04 +00:00
var docs = child_process.execSync(`./node_modules/.bin/jsdoc -X -a public ${files.join(" ")}`);
docs = JSON.parse(docs)
//filter out some stuff
docs = docs.filter(function(datum){
//is public
return datum.access !== "private" &&
//doesnt inherit
(!datum.hasOwnProperty('inherits') || !datum.inherits.startsWith('Tone#')) &&
//isnt undocumented (or a default value)
(!datum.undocumented || datum.longname.includes('defaults'))
});
2017-08-08 23:23:04 +00:00
var dest = `${TMP_FOLDER}/Site/_data/jsdocs-${VERSION}.json`;
fs.writeFile(dest, JSON.stringify(docs, undefined, '\t'), done);
2017-08-08 17:29:58 +00:00
});
});
gulp.task("commitJSDocs", ["pushJSDocs"], function(){
return del([`${TMP_FOLDER}/Site`], { force : true});
});