/** * Web Frontend for Botleagues * Copyright (C) 2015 Camil Staps * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ // see https://github.com/tugberkugurlu/gulp-bootswatch-sample/ var gulp = require('gulp'), changed = require('gulp-changed'), coffee = require('gulp-coffee'), concat = require('gulp-concat'), debug = require('gulp-debug'), foreach = require('gulp-foreach'), gulpif = require('gulp-if'), gutil = require('gulp-util'), imagemin = require('gulp-imagemin'), jade = require('gulp-jade'), jshint = require('gulp-jshint'), less = require('gulp-less'), minifycss = require('gulp-minify-css'), minifyhtml = require('gulp-minify-html'), order = require('gulp-order'), rename = require('gulp-rename'), stripdebug = require('gulp-strip-debug'), uglify = require('gulp-uglify'), path = require('path'), merge = require('merge-stream'), del = require('del'); var bootswatch_theme = 'lumen'; var production = false; gulp.task('default', [], function() { gulp.start('styles', 'scripts', 'images', 'jade'); }); gulp.task('all', [], function() { gulp.start('fonts', 'styles', 'scripts', 'images', 'jade', '3rd-party-styles', '3rd-party-scripts'); }); gulp.task('rebuild', ['clean'], function() { gulp.start('all'); }); gulp.task('production', ['clean'], function(){ production = true; gulp.start('all'); }); gulp.task('clean', function(cb) { del(['./build/assets/css', './build/assets/js', './build/assets/less', './build/assets/img', './build/assets/fonts', './build/*.html'], cb) }); gulp.task('fonts', function() { var fileList = [ './bower_components/bootstrap/dist/fonts/*', './bower_components/fontawesome/fonts/*' ]; var dst = './build/assets/fonts'; return gulp.src(fileList) .pipe(changed(dst)) .pipe(gulp.dest(dst)); }); gulp.task('3rd-party-styles', function() { var baseContent = '@import "./bower_components/bootstrap/less/bootstrap.less";' + '@import "./bower_components/bootswatch/' + bootswatch_theme + '/variables.less";' + '@import "./bower_components/bootswatch/' + bootswatch_theme + '/bootswatch.less";' + '@import "./bower_components/bootstrap/less/utilities.less";'; var isBootswatchFile = function(file) { var suffix = 'bootswatch.less'; return file.path.indexOf(suffix, file.path.length - suffix.length) !== -1; } var isBootstrapFile = function(file) { var suffix = 'bootstrap-', fileName = path.basename(file.path); return fileName.indexOf(suffix) == 0; } var fileList = [ './bower_components/bootswatch/' + bootswatch_theme + '/bootswatch.less', './bower_components/fontawesome/css/font-awesome.css' ]; var dst = './build/assets/css'; var bootswatch_dst = 'bootswatch.css'; return gulp.src(fileList) .pipe(changed(dst, {extension: '.css', destination: function(file){ if (isBootswatchFile(file)) { return bootswatch_dst; } else { return file; } }})) .pipe(gulpif(isBootswatchFile, foreach(function(stream, file) { var themeName = path.basename(path.dirname(file.path)), content = replaceAll(baseContent, bootswatch_theme, themeName), file = string_src('bootstrap-' + themeName + '.less', content); return file; }))) .pipe(less()) .pipe(gulpif(isBootstrapFile, foreach(function(stream, file) { var fileName = path.basename(file.path), themeName = fileName.substring(fileName.indexOf('-') + 1, fileName.indexOf('.')); // http://stackoverflow.com/questions/21719833/gulp-how-to-add-src-files-in-the-middle-of-a-pipe // https://github.com/gulpjs/gulp/blob/master/docs/recipes/using-multiple-sources-in-one-task.md return merge(stream, gulp.src(['./build/assets/css/font-awesome.css'])) .pipe(concat(bootswatch_dst)) }))) .pipe(minifycss({processImport: false})) .pipe(gulp.dest(dst)); }); gulp.task('styles', function() { var sources = [ './src/css/license.less', './src/css/forms.less', './src/css/general.less', './src/css/profile.less', './src/css/bootswatch-' + bootswatch_theme + '.less' ]; var dst = './build/assets/css'; var dst_filename = 'style.css'; return gulp.src(sources) .pipe(changed(dst, {destination: dst_filename})) .pipe(less()) .pipe(concat(dst_filename)) .pipe(gulpif(production, minifycss())) .pipe(gulp.dest(dst)); }); gulp.task('3rd-party-scripts', function(){ var fileList = [ './bower_components/jquery/dist/jquery.min.js', './bower_components/bootstrap/dist/js/bootstrap.min.js', './bower_components/jquery-cookie/jquery.cookie.js' ]; var dst = './build/assets/js'; return gulp.src(fileList) .pipe(changed(dst, {destination: 'script.js'})) .pipe(concat('script.js')) .pipe(stripdebug()) .pipe(uglify()) .pipe(gulp.dest(dst)); }); gulp.task('scripts', function(){ var src = [ './src/js/*.js', './src/js/*.coffee' ] ; var dst = './build/assets/js'; return gulp.src(src) .pipe(changed(dst, { extension: 'js' })) .pipe(gulpif(/\.coffee$/, coffee())) .pipe(gulpif(production, stripdebug())) .pipe(gulpif(production, uglify())) .pipe(gulp.dest(dst)); }); gulp.task('images', function(){ var src = './src/img/**/*', dst = './build/img'; return gulp.src(src) .pipe(changed(dst)) .pipe(imagemin()) .pipe(gulp.dest(dst)); }); gulp.task('jade', function(){ var src = './src/*.jade', dst = './build'; return gulp.src(src) .pipe(changed(dst, {extension: 'html'})) .pipe(jade()) .pipe(gulpif(production, minifyhtml())) .pipe(gulp.dest(dst)); }); // http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript function escapeRegExp(string) { return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); } function replaceAll(string, find, replace) { return string.replace(new RegExp(escapeRegExp(find), 'g'), replace); } function string_src(filename, string) { var src = require('stream').Readable({ objectMode: true }) src._read = function () { this.push(new gutil.File({ cwd: "", base: "", path: filename, contents: new Buffer(string) })) this.push(null) } return src }