Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
173 lines (151 sloc) 5.84 KB
var path = require('path'),
fs = require('fs'),
es = require('event-stream'),
_ = require('underscore'),
runSequence = require('run-sequence');
var gulp = require('gulp'),
less = require('gulp-less'),
minifyCSS = require('gulp-minify-css'),
clean = require('gulp-clean'),
coffee = require('gulp-coffee'),
changed = require('gulp-changed'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
notify = require('gulp-notify'),
plumber = require('gulp-plumber'),
include = require('gulp-include'),
rimraf = require('gulp-rimraf');
// - Paths
var paths = {root : 'src/webroot/'};
var themesPath = './src/View/Themed/';
var themesPaths = fs.readdirSync(themesPath)
.filter(function(file) {
return fs.statSync(path.join(themesPath, file)).isDirectory();
});
themesPaths.map(function(__path) {
paths[__path.toLowerCase()] = path.join(themesPath, __path, 'webroot');
});
// - Partitioned task
var __partsTask = function(name, dependencies, task) {
if (task === undefined) {
if (typeof(dependencies) !== 'function') {
return gulp.task(name, dependencies);
}
task = dependencies;
dependencies = [];
}
// - Task per theme (gulp [task-[theme])
_.each(paths, function(__path, __ident) {
gulp.task(name + '-' + __ident, dependencies, (function(__path, __ident) {
return task(__path, __ident);
}.bind(this, __path, __ident)));
});
// - Global task (gulp [task])
return gulp.task(name, dependencies, function() {
return es.concat.apply(null, _.map(paths, function(__path, __ident) {
return task(__path, __ident);
}));
});
}
// ------------------------------
// - Styles tasks
// ------------------------------
gulp.task('style', function() {
gulp.start('less', 'css');
});
// -- Less
__partsTask('less', function(__path, __ident) {
var __extPath = path.join(__path, 'css');
var __minPath = path.join(__path, 'css', 'min/');
return gulp.src(['style.less', 'style-*.less', '!min/**'], {base: __extPath, cwd: __extPath})
.pipe(notify({
title : "LESS Processor",
message : "Processed file: (<%= options.ident %>) <%= file.relative %>",
templateOptions : {ident: __ident},
notifier : function(options, callback) {callback();}
}))
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
.pipe(less({paths: [__extPath]}))
.pipe(minifyCSS({'keepSpecialComments': 0}))
.pipe(gulp.dest(__minPath));
});
// -- Css
__partsTask('css', function(__path, __ident) {
var __extPath = path.join(__path, 'css');
var __minPath = path.join(__path, 'css', 'min/');
return gulp.src(['**/*.css', '!min/**'], {base: __extPath, cwd: __extPath})
.pipe(changed(__minPath))
.pipe(notify({
title : "CSS Processor",
message : "Processed file: (<%= options.ident %>) <%= file.relative %>",
templateOptions : {ident: __ident},
notifier : function(options, callback) {callback();}
}))
.pipe(minifyCSS({'keepSpecialComments': 0}))
.pipe(gulp.dest(__minPath));
});
// ------------------------------
// - Scripts tasks
// ------------------------------
gulp.task('script', function() {
gulp.start('coffee', 'js');
});
// -- Coffee
__partsTask('coffee', function(__path, __ident) {
var __extPath = path.join(__path, 'js');
var __minPath = path.join(__path, 'js', 'min/');
return gulp.src(['**/*.coffee', '!min/**'], {base: __extPath, cwd: __extPath})
.pipe(changed(__minPath, {extension: '.js'}))
.pipe(notify({
title : "CoffeeScript Processor",
message : "Processed file: (<%= options.ident %>) <%= file.relative %>",
templateOptions : {ident: __ident},
notifier : function(options, callback) {callback();}
}))
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
.pipe(include())
.pipe(coffee({bare: true}))
.pipe(uglify({mangle: false}))
.pipe(gulp.dest(__minPath));
});
// -- Js
__partsTask('js', function(__path, __ident) {
var __extPath = path.join(__path, 'js');
var __minPath = path.join(__path, 'js', 'min/');
return gulp.src(['**/*.js', '!min/**'], {base: __extPath, cwd: __extPath})
.pipe(changed(__minPath, {extension: '.js'}))
.pipe(notify({
title : "JavaScript Processor",
message : "Processed file: (<%= options.ident %>) <%= file.relative %>",
templateOptions : {ident: __ident},
notifier : function(options, callback) {callback();}
}))
.pipe(include())
.pipe(uglify({mangle: false}))
.pipe(gulp.dest(__minPath));
});
// ------------------------------
// - Utility tasks
// ------------------------------
gulp.task('clean', function() {
return gulp.src(['**/min/**/*'], {read: false})
.pipe(clean());
});
// - Main task
gulp.task('default', ['clean'], function() {
gulp.start('style', 'script');
});
// - Watch task
gulp.task('watch', function() {
_.each(paths, function(__path, __ident) {
var __extPath;
// - Styles
__extPath = path.join(__path, 'css');
gulp.watch(path.join(__extPath, '**/*.less'), ['less-' + __ident]);
gulp.watch(path.join(__extPath, '**/*.css'), ['css-' + __ident]);
// - Scripts
__extPath = path.join(__path, 'js');
gulp.watch(path.join(__extPath, '**/*.coffee'), ['coffee-' + __ident]);
gulp.watch(path.join(__extPath, '**/*.js'), ['js-' + __ident]);
});
});