lemur/gulp/build.js

264 lines
8.7 KiB
JavaScript
Raw Normal View History

'use strict';
2015-06-22 22:47:27 +02:00
var gulp = require('gulp'),
minifycss = require('gulp-minify-css'),
concat = require('gulp-concat'),
less = require('gulp-less'),
gulpif = require('gulp-if'),
gutil = require('gulp-util'),
foreach = require('gulp-foreach'),
path =require('path'),
merge = require('merge-stream'),
del = require('del'),
size = require('gulp-size'),
plumber = require('gulp-plumber'),
autoprefixer = require('gulp-autoprefixer'),
jshint = require('gulp-jshint'),
inject = require('gulp-inject'),
cache = require('gulp-cache'),
ngAnnotate = require('gulp-ng-annotate'),
csso = require('gulp-csso'),
useref = require('gulp-useref'),
filter = require('gulp-filter'),
rev = require('gulp-rev'),
2015-06-22 22:47:27 +02:00
revReplace = require('gulp-rev-replace'),
imagemin = require('gulp-imagemin'),
minifyHtml = require('gulp-minify-html'),
bowerFiles = require('main-bower-files'),
2015-07-21 01:13:42 +02:00
karma = require('karma'),
replace = require('gulp-replace'),
argv = require('yargs').argv;
2015-06-22 22:47:27 +02:00
2020-12-09 04:20:28 +01:00
gulp.task('clean', async function (cb) {
2015-06-22 22:47:27 +02:00
del(['.tmp', 'lemur/static/dist'], cb);
});
2020-12-09 04:20:28 +01:00
gulp.task('default', gulp.series('clean', function () {
gulp.start('fonts', 'styles');
}));
gulp.task('test', gulp.series(function (done) {
2015-07-21 01:13:42 +02:00
new karma.Server({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, function() {
done();
}).start();
2020-12-09 04:20:28 +01:00
}));
2015-07-21 01:13:42 +02:00
2020-12-09 04:20:28 +01:00
gulp.task('dev:fonts', async function () {
2015-06-22 22:47:27 +02:00
var fileList = [
'bower_components/bootstrap/dist/fonts/*',
'bower_components/fontawesome/fonts/*'
2015-06-22 22:47:27 +02:00
];
return gulp.src(fileList)
.pipe(gulp.dest('.tmp/fonts'));
});
2020-12-09 04:20:28 +01:00
gulp.task('dev:styles', async function () {
var baseContent = '@import "bower_components/bootstrap/less/bootstrap.less";@import "bower_components/bootswatch/$theme$/variables.less";@import "bower_components/bootswatch/$theme$/bootswatch.less";@import "bower_components/bootstrap/less/utilities.less";';
2015-06-22 22:47:27 +02:00
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/sandstone/bootswatch.less',
'bower_components/fontawesome/css/font-awesome.css',
'bower_components/angular-chart.js/dist/angular-chart.css',
'bower_components/angular-loading-bar/src/loading-bar.css',
'bower_components/angular-ui-switch/angular-ui-switch.css',
'bower_components/angular-wizard/dist/angular-wizard.css',
2016-06-27 23:40:46 +02:00
'bower_components/ng-table/dist/ng-table.css',
2015-10-06 01:06:56 +02:00
'bower_components/angularjs-toaster/toaster.css',
'bower_components/angular-ui-select/dist/select.css',
2015-10-06 01:06:56 +02:00
'lemur/static/app/styles/lemur.css'
2015-06-22 22:47:27 +02:00
];
return gulp.src(fileList)
.pipe(gulpif(isBootswatchFile, foreach(function (stream, file) {
var themeName = path.basename(path.dirname(file.path)),
content = replaceAll(baseContent, '$theme$', themeName),
file2 = string_src('bootstrap-' + themeName + '.less', content);
2015-06-22 22:47:27 +02:00
return file2;
2015-06-22 22:47:27 +02:00
})))
.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
2020-12-09 04:20:28 +01:00
return merge(stream, gulp.src(['.tmp/styles/font-awesome.css', '.tmp/styles/lemur.css'], { allowEmpty: true }))
.pipe(concat('style-' + themeName + '.css'));
2015-06-22 22:47:27 +02:00
})))
.pipe(plumber())
.pipe(concat('styles.css'))
.pipe(minifycss())
.pipe(autoprefixer('last 1 version'))
.pipe(gulp.dest('.tmp/styles'))
.pipe(size());
});
// http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript
function escapeRegExp(string) {
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
2015-06-22 22:47:27 +02:00
}
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) }));
2015-06-22 22:47:27 +02:00
this.push(null);
};
return src;
}
2020-12-09 04:20:28 +01:00
gulp.task('dev:scripts', async function () {
2015-06-22 22:47:27 +02:00
return gulp.src(['lemur/static/app/angular/**/*.js'])
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(size());
});
2020-12-09 04:20:28 +01:00
gulp.task('build:extras', async function () {
2015-10-06 01:06:56 +02:00
return gulp.src(['lemur/static/app/*.*', '!lemur/static/app/*.html'])
2015-06-22 22:47:27 +02:00
.pipe(gulp.dest('lemur/static/dist'));
});
function injectHtml(isDev) {
return gulp.src('lemur/static/app/index.html')
.pipe(
inject(gulp.src(bowerFiles({ base: 'app' })), {
2015-06-22 22:47:27 +02:00
starttag: '<!-- inject:bower:{{ext}} -->',
addRootSlash: false,
ignorePath: isDev ? ['lemur/static/app/', '.tmp/'] : null
})
)
.pipe(inject(gulp.src(['lemur/static/app/angular/**/*.js']), {
2015-06-22 22:47:27 +02:00
starttag: '<!-- inject:{{ext}} -->',
addRootSlash: false,
ignorePath: isDev ? ['lemur/static/app/', '.tmp/'] : null
}))
.pipe(inject(gulp.src(['.tmp/styles/**/*.css']), {
2015-06-22 22:47:27 +02:00
starttag: '<!-- inject:{{ext}} -->',
addRootSlash: false,
ignorePath: isDev ? ['lemur/static/app/', '.tmp/'] : null
}))
.pipe(
gulpif(!isDev,
2020-12-09 04:20:28 +01:00
inject(gulp.src('lemur/static/dist/ngviews/ngviews.min.js', { allowEmpty: true }), {
2015-06-22 22:47:27 +02:00
starttag: '<!-- inject:ngviews -->',
addRootSlash: false
})
)
2016-12-13 20:13:44 +01:00
).pipe(gulp.dest('.tmp/'));
2015-06-22 22:47:27 +02:00
}
gulp.task('dev:inject', gulp.series(['dev:styles', 'dev:scripts'], function () {
2015-06-22 22:47:27 +02:00
return injectHtml(true);
2020-12-09 04:20:28 +01:00
}));
2015-06-22 22:47:27 +02:00
2020-12-09 04:20:28 +01:00
gulp.task('build:ngviews', async function () {
2015-06-22 22:47:27 +02:00
return gulp.src(['lemur/static/app/angular/**/*.html'])
.pipe(minifyHtml({
empty: true,
spare: true,
quotes: true
}))
.pipe(gulp.dest('lemur/static/dist/angular'))
.pipe(size());
});
gulp.task('build:inject', gulp.series(['dev:styles', 'dev:scripts', 'build:ngviews'], function () {
2020-12-09 04:20:28 +01:00
return injectHtml(false);
}));
gulp.task('build:html', gulp.series(['dev:styles', 'dev:scripts', 'build:ngviews', 'build:inject'], function () {
2016-12-13 20:13:44 +01:00
var jsFilter = filter(['**/*.js'], {'restore': true});
var cssFilter = filter(['**/*.css'], {'restore': true});
2015-06-22 22:47:27 +02:00
return gulp.src('.tmp/index.html')
.pipe(jsFilter)
.pipe(ngAnnotate())
2016-12-13 20:13:44 +01:00
.pipe(jsFilter.restore)
2015-06-22 22:47:27 +02:00
.pipe(cssFilter)
.pipe(csso())
2016-12-13 20:13:44 +01:00
.pipe(cssFilter.restore)
2015-06-22 22:47:27 +02:00
.pipe(useref())
.pipe(gulp.dest('lemur/static/dist'))
.pipe(size());
2020-12-09 04:20:28 +01:00
}));
2015-06-22 22:47:27 +02:00
2020-12-09 04:20:28 +01:00
gulp.task('build:fonts', gulp.series('dev:fonts', function () {
2015-06-22 22:47:27 +02:00
return gulp.src('.tmp/fonts/**/*')
.pipe(gulp.dest('lemur/static/dist/fonts'));
2020-12-09 04:20:28 +01:00
}));
2015-06-22 22:47:27 +02:00
2020-12-09 04:20:28 +01:00
gulp.task('build:images', async function () {
2015-06-22 22:47:27 +02:00
return gulp.src('lemur/static/app/images/**/*')
.pipe(cache(imagemin({
optimizationLevel: 3,
progressive: true,
interlaced: true
})))
.pipe(gulp.dest('lemur/static/dist/images'))
.pipe(size());
});
2020-12-09 04:20:28 +01:00
gulp.task('package:strip', async function () {
return gulp.src('lemur/static/dist/scripts/main*')
.pipe(replace('http:\/\/localhost:3000', ''))
2015-12-01 18:39:56 +01:00
.pipe(replace('http:\/\/localhost:8000', ''))
.pipe(useref())
.pipe(gulp.dest('lemur/static/dist/scripts'))
.pipe(size());
});
2015-06-22 22:47:27 +02:00
2020-12-09 04:20:28 +01:00
gulp.task('addUrlContextPath:revision', async function(){
return gulp.src(['lemur/static/dist/**/*.css','lemur/static/dist/**/*.js'])
.pipe(rev())
.pipe(gulp.dest('lemur/static/dist'))
.pipe(rev.manifest())
.pipe(gulp.dest('lemur/static/dist'))
});
gulp.task('addUrlContextPath:revreplace', gulp.series('addUrlContextPath:revision', function(){
// var manifest = gulp.src("lemur/static/dist/rev-manifest.json");
// var urlContextPathExists = argv.urlContextPath ? true : false;
return gulp.src( "lemur/static/dist/index.html")
.pipe(gulp.dest('lemur/static/dist'));
}));
gulp.task('addUrlContextPath', gulp.series('addUrlContextPath:revreplace', async function(){
var urlContextPathExists = argv.urlContextPath ? true : false;
['lemur/static/dist/scripts/main*.js',
'lemur/static/dist/angular/**/*.html']
.forEach(function(file){
return gulp.src(file)
.pipe(gulpif(urlContextPathExists, replace('api/', argv.urlContextPath + '/api/')))
.pipe(gulpif(urlContextPathExists, replace('/angular/', '/' + argv.urlContextPath + '/angular/')))
.pipe(gulp.dest(function(file){
return file.base;
2018-08-22 22:21:35 +02:00
}))
})
2020-12-09 04:20:28 +01:00
}));
gulp.task('build', gulp.series(['build:ngviews', 'build:inject', 'build:images', 'build:fonts', 'build:html', 'build:extras']));
gulp.task('package', gulp.series(['addUrlContextPath', 'package:strip']));