blob: b232ef926c5d5260383b9380bc1f200aa0ef9bff [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'
Matteo Scandoloc0c94092016-02-02 17:25:18 -080076 ]),
77 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010078 )
79 )
80 .pipe(rename('xos<%= fileName %>.html'))
81 .pipe(gulp.dest(options.dashboards));
82 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +010083
84 // minify vendor js files
85 gulp.task('wiredep', function(){
86 var bowerDeps = wiredep().js;
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +010087 if(!bowerDeps){
88 return;
89 }
90
91 // remove angular (it's already loaded)
92 _.remove(bowerDeps, function(dep){
93 return dep.indexOf('angular/angular.js') !== -1;
94 });
95
Matteo Scandoloe53ee052015-11-03 14:32:00 +010096 return gulp.src(bowerDeps)
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +010097 .pipe(concat('xos<%= fileName %>Vendor.js'))
Matteo Scandoloe53ee052015-11-03 14:32:00 +010098 .pipe(uglify())
Matteo Scandolo8a1f2362015-11-06 09:52:01 +010099 .pipe(gulp.dest(options.static + 'js/vendor/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100100 });
101
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100102 gulp.task('lint', function () {
103 return gulp.src(['src/js/**/*.js'])
104 .pipe(eslint())
105 .pipe(eslint.format())
106 .pipe(eslint.failAfterError());
107 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100108
109 gulp.task('build', function() {
110 runSequence(
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100111 'templates',
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100112 'babel',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100113 'scripts',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100114 'wiredep',
Matteo Scandolo8a1f2362015-11-06 09:52:01 +0100115 'copyHtml',
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100116 'cleanTmp'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100117 );
118 });
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100119};