From eda738d4faeffbda732bd648350b600c389ab7f1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=BCrg=20Lehni?= <juerg@scratchdisk.com>
Date: Sat, 23 Jan 2016 18:26:56 +0100
Subject: [PATCH] Split gulpfile.js into separate task files.

---
 gulp/tasks/build.js              |  66 +++++++++
 gulp/tasks/clean.js              |  34 +++++
 gulp/tasks/dist.js               |  31 +++++
 gulp/tasks/docs.js               |  31 +++++
 gulp/tasks/load.js               |  21 +++
 gulp/tasks/minify.js             |  51 +++++++
 gulp/tasks/test.js               |  19 +++
 gulp/utils/error.js              |  25 ++++
 gulp/utils/options.js            |  34 +++++
 gulpfile.js                      | 224 +------------------------------
 package.json                     |   6 +-
 projects/paperjs.sublime-project |   4 +-
 12 files changed, 320 insertions(+), 226 deletions(-)
 create mode 100644 gulp/tasks/build.js
 create mode 100644 gulp/tasks/clean.js
 create mode 100644 gulp/tasks/dist.js
 create mode 100644 gulp/tasks/docs.js
 create mode 100644 gulp/tasks/load.js
 create mode 100644 gulp/tasks/minify.js
 create mode 100644 gulp/tasks/test.js
 create mode 100644 gulp/utils/error.js
 create mode 100644 gulp/utils/options.js

diff --git a/gulp/tasks/build.js b/gulp/tasks/build.js
new file mode 100644
index 00000000..1dd9f34e
--- /dev/null
+++ b/gulp/tasks/build.js
@@ -0,0 +1,66 @@
+/*
+ * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
+ * http://paperjs.org/
+ *
+ * Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
+ * http://scratchdisk.com/ & http://jonathanpuckey.com/
+ *
+ * Distributed under the MIT license. See LICENSE file for details.
+ *
+ * All rights reserved.
+ */
+
+var gulp = require('gulp'),
+    prepro = require('gulp-prepro'),
+    rename = require('gulp-rename'),
+    uncomment = require('gulp-uncomment'),
+    whitespace = require('gulp-whitespace'),
+    extend = require('extend'),
+    options = require('../utils/options.js');
+
+// Options to be used in Prepro.js preprocessing through the global __options
+// object, merged in with the options required above.
+var buildOptions = {
+    full: { paperScript: true },
+    core: { paperScript: false },
+    node: { environment: 'node', paperScript: true }
+};
+
+var buildNames = Object.keys(buildOptions);
+
+gulp.task('build',
+    buildNames.map(function(name) {
+        return 'build:' + name;
+    })
+);
+
+buildNames.forEach(function(name) {
+    gulp.task('build:' + name, ['clean:build', 'minify:acorn'], function() {
+        return gulp.src('src/paper.js')
+            .pipe(prepro({
+                // Evaluate constants.js inside the precompilation scope before
+                // the actual precompilation, so all the constants substitution
+                // statements in the code can work (look for: /*#=*/):
+                evaluate: ['src/constants.js'],
+                setup: function() {
+                    // Return objects to be defined in the preprocess-scope.
+                    // Note that this would be merge in with already existing
+                    // objects.
+                    return {
+                        __options: extend({}, options, buildOptions[name])
+                    };
+                }
+            }))
+            .pipe(uncomment({
+                mergeEmptyLines: true
+            }))
+            .pipe(whitespace({
+                spacesToTabs: 4,
+                removeTrailing: true
+            }))
+            .pipe(rename({
+                suffix: '-' + name
+            }))
+            .pipe(gulp.dest('dist'));
+    });
+});
diff --git a/gulp/tasks/clean.js b/gulp/tasks/clean.js
new file mode 100644
index 00000000..0b0c4a01
--- /dev/null
+++ b/gulp/tasks/clean.js
@@ -0,0 +1,34 @@
+/*
+ * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
+ * http://paperjs.org/
+ *
+ * Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
+ * http://scratchdisk.com/ & http://jonathanpuckey.com/
+ *
+ * Distributed under the MIT license. See LICENSE file for details.
+ *
+ * All rights reserved.
+ */
+
+var gulp = require('gulp'),
+    del = require('del');
+
+gulp.task('clean:build', function() {
+    return del([
+        'dist/paper-*.js'
+    ]);
+});
+
+gulp.task('clean:docs', function(callback) {
+    return del([
+        'dist/docs/**',
+        'dist/serverdocs/**'
+    ]);
+});
+
+gulp.task('clean:load', function() {
+    return del([
+        'dist/paper-full.js',
+        'dist/paper-node.js'
+    ]);
+});
diff --git a/gulp/tasks/dist.js b/gulp/tasks/dist.js
new file mode 100644
index 00000000..5376ca8a
--- /dev/null
+++ b/gulp/tasks/dist.js
@@ -0,0 +1,31 @@
+/*
+ * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
+ * http://paperjs.org/
+ *
+ * Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
+ * http://scratchdisk.com/ & http://jonathanpuckey.com/
+ *
+ * Distributed under the MIT license. See LICENSE file for details.
+ *
+ * All rights reserved.
+ */
+
+var gulp = require('gulp'),
+    merge = require('merge-stream'),
+    zip = require('gulp-zip');
+
+gulp.task('dist', ['minify', 'docs'], function() {
+    return merge(
+            gulp.src([
+                'dist/paper-full*.js',
+                'dist/paper-core*.js',
+                'LICENSE.txt',
+                'examples/**/*',
+            ], { base: '.' }),
+            gulp.src([
+                'dist/docs/**/*'
+            ], { base: 'dist' })
+        )
+        .pipe(zip('/paperjs.zip'))
+        .pipe(gulp.dest('dist'));
+});
diff --git a/gulp/tasks/docs.js b/gulp/tasks/docs.js
new file mode 100644
index 00000000..c7101630
--- /dev/null
+++ b/gulp/tasks/docs.js
@@ -0,0 +1,31 @@
+/*
+ * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
+ * http://paperjs.org/
+ *
+ * Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
+ * http://scratchdisk.com/ & http://jonathanpuckey.com/
+ *
+ * Distributed under the MIT license. See LICENSE file for details.
+ *
+ * All rights reserved.
+ */
+
+var gulp = require('gulp'),
+    del = require('del'),
+    shell = require('gulp-shell');
+
+var docOptions = {
+    local: 'docs', // Generates the offline docs
+    server: 'serverdocs' // Generates the website templates for the online docs
+};
+
+Object.keys(docOptions).forEach(function(name) {
+    gulp.task('docs:' + name, ['clean:docs'], shell.task([
+        'java -cp jsrun.jar:lib/* JsRun app/run.js -c=conf/' + name + '.conf ' +
+            '-D="renderMode:' + docOptions[name] + '"',
+    ], {
+        cwd: 'jsdoc-toolkit'
+    }));
+});
+
+gulp.task('docs', ['docs:local']);
diff --git a/gulp/tasks/load.js b/gulp/tasks/load.js
new file mode 100644
index 00000000..c33e4165
--- /dev/null
+++ b/gulp/tasks/load.js
@@ -0,0 +1,21 @@
+/*
+ * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
+ * http://paperjs.org/
+ *
+ * Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
+ * http://scratchdisk.com/ & http://jonathanpuckey.com/
+ *
+ * Distributed under the MIT license. See LICENSE file for details.
+ *
+ * All rights reserved.
+ */
+
+var gulp = require('gulp'),
+    del = require('del'),
+    symlink = require('gulp-symlink');
+
+gulp.task('load', ['clean:load'], function() {
+    return gulp.src('src/load.js')
+        .pipe(symlink('dist/paper-full.js'))
+        .pipe(symlink('dist/paper-node.js'));
+});
diff --git a/gulp/tasks/minify.js b/gulp/tasks/minify.js
new file mode 100644
index 00000000..e690104d
--- /dev/null
+++ b/gulp/tasks/minify.js
@@ -0,0 +1,51 @@
+/*
+ * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
+ * http://paperjs.org/
+ *
+ * Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
+ * http://scratchdisk.com/ & http://jonathanpuckey.com/
+ *
+ * Distributed under the MIT license. See LICENSE file for details.
+ *
+ * All rights reserved.
+ */
+
+var gulp = require('gulp'),
+    rename = require('gulp-rename'),
+    fs = require('fs'),
+    uglify = require('gulp-uglify');
+
+var acornPath = 'bower_components/acorn/';
+
+var uglifyOptions = {
+    output: {
+        ascii_only: true,
+        comments: /^!/
+    }
+};
+
+gulp.task('minify', ['build'], function() {
+    return gulp.src([
+            'dist/paper-full.js',
+            'dist/paper-core.js'
+        ])
+        .pipe(uglify(uglifyOptions))
+        .pipe(rename({
+            suffix: '.min'
+        }))
+        .pipe(gulp.dest('dist'));
+});
+
+gulp.task('minify:acorn', function() {
+    // Only compress acorn if the compressed file doesn't exist yet.
+    try {
+        fs.accessSync(acornPath + 'acorn.min.js');
+    } catch(e) {
+        return gulp.src(acornPath + 'acorn.js')
+            .pipe(uglify(uglifyOptions))
+            .pipe(rename({
+                suffix: '.min'
+            }))
+            .pipe(gulp.dest(acornPath));
+    }
+});
diff --git a/gulp/tasks/test.js b/gulp/tasks/test.js
new file mode 100644
index 00000000..a2ecec31
--- /dev/null
+++ b/gulp/tasks/test.js
@@ -0,0 +1,19 @@
+/*
+ * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
+ * http://paperjs.org/
+ *
+ * Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
+ * http://scratchdisk.com/ & http://jonathanpuckey.com/
+ *
+ * Distributed under the MIT license. See LICENSE file for details.
+ *
+ * All rights reserved.
+ */
+
+var gulp = require('gulp'),
+    qunit = require('gulp-qunit');
+
+gulp.task('test', function() {
+    return gulp.src('test/index.html')
+        .pipe(qunit({ timeout: 20, noGlobals: true }));
+});
diff --git a/gulp/utils/error.js b/gulp/utils/error.js
new file mode 100644
index 00000000..3a404256
--- /dev/null
+++ b/gulp/utils/error.js
@@ -0,0 +1,25 @@
+/*
+ * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
+ * http://paperjs.org/
+ *
+ * Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
+ * http://scratchdisk.com/ & http://jonathanpuckey.com/
+ *
+ * Distributed under the MIT license. See LICENSE file for details.
+ *
+ * All rights reserved.
+ */
+
+var gulp = require('gulp'),
+    gutil = require('gulp-util'),
+    ERROR = gutil.colors.red('[ERROR]');
+
+gulp.on('error', function(err) {
+    var msg = err.toString();
+    if (msg === '[object Object]')
+        msg = err;
+    gutil.log(ERROR, err);
+    if (err.stack)
+        gutil.log(ERROR, err.stack);
+    this.emit('end');
+});
diff --git a/gulp/utils/options.js b/gulp/utils/options.js
new file mode 100644
index 00000000..ae88229d
--- /dev/null
+++ b/gulp/utils/options.js
@@ -0,0 +1,34 @@
+/*
+ * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
+ * http://paperjs.org/
+ *
+ * Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
+ * http://scratchdisk.com/ & http://jonathanpuckey.com/
+ *
+ * Distributed under the MIT license. See LICENSE file for details.
+ *
+ * All rights reserved.
+ */
+
+var gitty = require('gitty');
+
+// Require the __options object, so we have access to the version number and
+// make amendments, e.g. the release date.
+var options = require('../../src/options.js'),
+    repo = gitty('.');
+
+function git(param) {
+    var args = arguments.length === 1 ? param.split(' ')
+            : [].slice.apply(arguments),
+        operation = args.shift();
+    return new gitty.Command(repo, operation, args).execSync().trim();
+}
+
+// Get the date of the last commit from this branch for release date:
+options.date = git('log -1 --pretty=format:%ad');
+// If we're not on the master branch, append the branch name to the version:
+var branch = git('rev-parse --abbrev-ref HEAD');
+if (branch !== 'master')
+    options.version += '-' + branch;
+
+module.exports = options;
diff --git a/gulpfile.js b/gulpfile.js
index 5868a806..b4646124 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -11,227 +11,9 @@
  */
 
 var gulp = require('gulp'),
-    qunit = require('gulp-qunit'),
-    prepro = require('gulp-prepro'),
-    rename = require('gulp-rename'),
-    shell = require('gulp-shell'),
-    symlink = require('gulp-symlink'),
-    uglify = require('gulp-uglify'),
-    uncomment = require('gulp-uncomment'),
-    whitespace = require('gulp-whitespace'),
-    merge = require('merge-stream'),
-    del = require('del'),
-    extend = require('extend'),
-    fs = require('fs'),
-    gitty = require('gitty'),
-    zip = require('gulp-zip');
+    requireDir = require('require-dir');
 
-/**
- * Options
- */
-
-// Require the __options object before preprocessing, so we have access to the
-// version number and can make amendments, e.g. the release date.
-var options = require('./src/options.js');
-
-// Options to be used in Prepro.js preprocessing through the global __options
-// object.
-var buildOptions = {
-    full: { paperScript: true },
-    core: { paperScript: false },
-    node: { environment: 'node', paperScript: true }
-};
-
-var docOptions = {
-    local: 'docs', // Generates the offline docs
-    server: 'serverdocs' // Generates the website templates for the online docs
-};
-
-var uglifyOptions = {
-    output: {
-        ascii_only: true,
-        comments: /^!/
-    }
-};
-
-var acornPath = 'bower_components/acorn/';
-
-var buildNames = Object.keys(buildOptions);
-var docNames = Object.keys(docOptions);
-
-/**
- * Git
- */
-
-var gitRepo = gitty('.');
-
-function git(param) {
-    var args = arguments.length === 1 ? param.split(' ') : [].slice.apply(arguments);
-    var operation = args.shift();
-    return new gitty.Command(gitRepo, operation, args).execSync().trim();
-}
-
-// Get the date of the last commit from this branch for release date:
-options.date = git('log -1 --pretty=format:%ad');
-// If we're not on the master branch, append the branch name to the version:
-var branch = git('rev-parse --abbrev-ref HEAD');
-if (branch !== 'master')
-    options.version += '-' + branch;
-
-/**
- * Task: default
- */
-
-gulp.on('error', function(err) {
-    console.error(err.toString());
-    gulp.emit('end');
-});
+// Require all tasks in gulp, including the task sub-folder.
+requireDir('./gulp', { recurse: true });
 
 gulp.task('default', ['dist']);
-
-/**
- * Task: test
- */
-
-gulp.task('test', function() {
-    return gulp.src('test/index.html')
-        .pipe(qunit({ timeout: 20, noGlobals: true }));
-});
-
-/**
- * Task: docs
- */
-
-docNames.forEach(function(name) {
-    gulp.task('docs:' + name, ['clean:docs'], shell.task([
-        'java -cp jsrun.jar:lib/* JsRun app/run.js -c=conf/' + name + '.conf ' +
-            '-D="renderMode:' + docOptions[name] + '"',
-    ], {
-        cwd: 'jsdoc-toolkit'
-    }));
-});
-
-gulp.task('docs', ['docs:local']);
-
-gulp.task('clean:docs', function(callback) {
-    return del([
-        'dist/docs/**',
-        'dist/serverdocs/**'
-    ]);
-});
-
-/**
- * Task: load
- */
-
-gulp.task('load', ['clean:load'], function() {
-    return gulp.src('src/load.js')
-        .pipe(symlink('dist/paper-full.js'))
-        .pipe(symlink('dist/paper-node.js'));
-});
-
-gulp.task('clean:load', function() {
-    return del([
-        'dist/paper-full.js',
-        'dist/paper-node.js'
-    ]);
-});
-
-/**
- * Task: build
- */
-
-gulp.task('build',
-    buildNames.map(function(name) {
-        return 'build:' + name;
-    })
-);
-
-buildNames.forEach(function(name) {
-    gulp.task('build:' + name, ['build:start'], function() {
-        return gulp.src('src/paper.js')
-            .pipe(prepro({
-                // Evaluate constants.js inside the precompilation scope before
-                // the actual precompilation, so all the constants substitution
-                // statements in the code can work (look for: /*#=*/):
-                evaluate: ['src/constants.js'],
-                setup: function() {
-                    // Return objects to be defined in the preprocess-scope.
-                    // Note that this would be merge in with already existing
-                    // objects.
-                    return {
-                        __options: extend({}, options, buildOptions[name])
-                    };
-                }
-            }))
-            .pipe(uncomment({
-                mergeEmptyLines: true
-            }))
-            .pipe(whitespace({
-                spacesToTabs: 4,
-                removeTrailing: true
-            }))
-            .pipe(rename({
-                suffix: '-' + name
-            }))
-            .pipe(gulp.dest('dist'));
-    });
-});
-
-gulp.task('build:start', ['clean:build', 'minify:acorn']);
-
-gulp.task('clean:build', function() {
-    return del([
-        'dist/paper-*.js'
-    ]);
-});
-
-gulp.task('minify:acorn', function() {
-    // Only compress acorn if the compressed file doesn't exist yet.
-    try {
-        fs.accessSync(acornPath + 'acorn.min.js');
-    } catch(e) {
-        return gulp.src(acornPath + 'acorn.js')
-            .pipe(uglify(uglifyOptions))
-            .pipe(rename({
-                suffix: '.min'
-            }))
-            .pipe(gulp.dest(acornPath));
-    }
-});
-
-/**
- * Task: minify
- */
-
-gulp.task('minify', ['build'], function() {
-    return gulp.src([
-            'dist/paper-full.js',
-            'dist/paper-core.js'
-        ])
-        .pipe(uglify(uglifyOptions))
-        .pipe(rename({
-            suffix: '.min'
-        }))
-        .pipe(gulp.dest('dist'));
-});
-
-/**
- * Task: dist
- */
-
-gulp.task('dist', ['minify', 'docs'], function() {
-    return merge(
-            gulp.src([
-                'dist/paper-full*.js',
-                'dist/paper-core*.js',
-                'LICENSE.txt',
-                'examples/**/*',
-            ], { base: '.' }),
-            gulp.src([
-                'dist/docs/**/*'
-            ], { base: 'dist' })
-        )
-        .pipe(zip('/paperjs.zip'))
-        .pipe(gulp.dest('dist'));
-});
diff --git a/package.json b/package.json
index 194893eb..d11e9867 100644
--- a/package.json
+++ b/package.json
@@ -48,12 +48,14 @@
     "gulp-shell": "^0.5.1",
     "gulp-symlink": "^2.1.3",
     "gulp-uglify": "^1.5.1",
-    "gulp-uncomment": "^0.2.0",
+    "gulp-uncomment": "^0.3.0",
+    "gulp-util": "^3.0.0",
     "gulp-whitespace": "^0.1.0",
     "gulp-zip": "^3.0.2",
     "jshint": "2.8.x",
     "merge-stream": "^1.0.0",
-    "prepro": "^2.0.0"
+    "prepro": "^2.0.0",
+    "require-dir": "^0.3.0"
   },
   "keywords": [
     "vector",
diff --git a/projects/paperjs.sublime-project b/projects/paperjs.sublime-project
index fb25a84b..ec04dbbb 100644
--- a/projects/paperjs.sublime-project
+++ b/projects/paperjs.sublime-project
@@ -11,9 +11,7 @@
       "path": "../examples"
     },
     {
-      "path": "../build",
-      // jsdoc-toolkit will have its own project file
-      "folder_exclude_patterns": ["jsdoc-toolkit"]
+      "path": "../gulp",
     },
     {
       "path": "..",