blob: 4a042188d098345c89d3426674e96d5bba86a40e [file] [log] [blame]
Matteo Scandoloe53ee052015-11-03 14:32:00 +01001'use strict';
2
3// BUILD
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +01004//
Matteo Scandoloe53ee052015-11-03 14:32:00 +01005// The only purpose of this gulpfile is to build a XOS view and copy the correct files into
6// .html => dashboards
7// .js (minified and concat) => static/js
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +01008//
Matteo Scandoloe53ee052015-11-03 14:32:00 +01009// The template are parsed and added to js with angular $templateCache
10
11var gulp = require('gulp');
Matteo Scandolod838fac2015-11-04 16:10:09 +010012var ngAnnotate = require('gulp-ng-annotate');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010013var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010016var concat = require('gulp-concat');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010017var del = require('del');
18var wiredep = require('wiredep');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010019var angularFilesort = require('gulp-angular-filesort');
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +010020var _ = require('lodash');
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010021var eslint = require('gulp-eslint');
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010022var inject = require('gulp-inject');
23var rename = require('gulp-rename');
Matteo Scandolo61a714d2015-11-06 11:16:12 +010024var replace = require('gulp-replace');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010025
Matteo Scandolof1072ed2015-11-09 11:25:40 +010026var TEMPLATE_FOOTER = `}]);
27angular.module('xos.<%= name %>').run(function($location){$location.path('/')});
28angular.bootstrap(angular.element('#xos<%= fileName %>'), ['xos.<%= name %>']);`;
29
Matteo Scandoloe53ee052015-11-03 14:32:00 +010030module.exports = function(options){
31
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010032 // delete previous builded file
Matteo Scandoloe53ee052015-11-03 14:32:00 +010033 gulp.task('clean', function(){
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010034 return del(
35 [options.dashboards + 'xos<%= fileName %>.html'],
36 {force: true}
37 );
Matteo Scandoloe53ee052015-11-03 14:32:00 +010038 });
39
40 // compile and minify scripts
41 gulp.task('scripts', function() {
42 return gulp.src([
Matteo Scandolo8a1f2362015-11-06 09:52:01 +010043 options.tmp + '**/*.js'
44 ])
45 .pipe(ngAnnotate())
46 .pipe(angularFilesort())
47 .pipe(concat('xos<%= fileName %>.js'))
48 .pipe(uglify())
49 .pipe(gulp.dest(options.static + 'js/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +010050 });
51
52 // set templates in cache
53 gulp.task('templates', function(){
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010054 return gulp.src('./src/templates/*.html')
Matteo Scandoloe53ee052015-11-03 14:32:00 +010055 .pipe(templateCache({
Matteo Scandolo6be0cd22015-11-03 16:27:07 +010056 module: 'xos.<%= name %>',
Matteo Scandoloba5e19d2015-11-06 18:25:52 +010057 root: 'templates/',
58 templateFooter: TEMPLATE_FOOTER
Matteo Scandoloe53ee052015-11-03 14:32:00 +010059 }))
Matteo Scandoloce1dd312015-11-04 11:46:54 +010060 .pipe(gulp.dest(options.tmp));
Matteo Scandoloe53ee052015-11-03 14:32:00 +010061 });
62
Matteo Scandolo6be0cd22015-11-03 16:27:07 +010063 // copy html index to Django Folder
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010064 gulp.task('copyHtml', ['clean'], function(){
65 return gulp.src(options.src + 'index.html')
Matteo Scandolo61a714d2015-11-06 11:16:12 +010066 // remove dev dependencies from html
67 .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, ''))
68 .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, ''))
Matteo Scandoloba5e19d2015-11-06 18:25:52 +010069 .pipe(replace(/ng-app=".*"\s/, ''))
Matteo Scandolo61a714d2015-11-06 11:16:12 +010070 // injecting minified files
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010071 .pipe(
72 inject(
73 gulp.src([
74 options.static + 'js/vendor/xos<%= fileName %>Vendor.js',
75 options.static + 'js/xos<%= fileName %>.js'
76 ])
77 )
78 )
79 .pipe(rename('xos<%= fileName %>.html'))
80 .pipe(gulp.dest(options.dashboards));
81 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +010082
83 // minify vendor js files
84 gulp.task('wiredep', function(){
85 var bowerDeps = wiredep().js;
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +010086 if(!bowerDeps){
87 return;
88 }
89
90 // remove angular (it's already loaded)
91 _.remove(bowerDeps, function(dep){
92 return dep.indexOf('angular/angular.js') !== -1;
93 });
94
Matteo Scandoloe53ee052015-11-03 14:32:00 +010095 return gulp.src(bowerDeps)
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +010096 .pipe(concat('xos<%= fileName %>Vendor.js'))
Matteo Scandoloe53ee052015-11-03 14:32:00 +010097 .pipe(uglify())
Matteo Scandolo8a1f2362015-11-06 09:52:01 +010098 .pipe(gulp.dest(options.static + 'js/vendor/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +010099 });
100
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100101 gulp.task('lint', function () {
102 return gulp.src(['src/js/**/*.js'])
103 .pipe(eslint())
104 .pipe(eslint.format())
105 .pipe(eslint.failAfterError());
106 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100107
108 gulp.task('build', function() {
109 runSequence(
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100110 'templates',
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100111 'babel',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100112 'scripts',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100113 'wiredep',
Matteo Scandolo8a1f2362015-11-06 09:52:01 +0100114 'copyHtml',
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100115 'cleanTmp'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100116 );
117 });
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100118};