blob: c38adfc8125171fb3ec88a9e72bb85048145ef06 [file] [log] [blame]
Matteo Scandolod90e19f2015-10-29 11:54:54 +01001'use strict';
2
3// BUILD
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +01004//
Matteo Scandolod90e19f2015-10-29 11:54:54 +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 Scandoloc5ee7b52015-11-06 17:14:28 +01008//
Matteo Scandolod90e19f2015-10-29 11:54:54 +01009// The template are parsed and added to js with angular $templateCache
10
11var gulp = require('gulp');
Matteo Scandolo82697c02015-11-04 16:30:43 +010012var ngAnnotate = require('gulp-ng-annotate');
Matteo Scandolod90e19f2015-10-29 11:54:54 +010013var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010016var concat = require('gulp-concat');
Matteo Scandolod90e19f2015-10-29 11:54:54 +010017var del = require('del');
18var wiredep = require('wiredep');
Matteo Scandolocb11e0c2015-10-29 15:50:15 +010019var angularFilesort = require('gulp-angular-filesort');
Matteo Scandolo82697c02015-11-04 16:30:43 +010020var _ = require('lodash');
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010021var eslint = require('gulp-eslint');
22var inject = require('gulp-inject');
23var rename = require('gulp-rename');
24var replace = require('gulp-replace');
Matteo Scandolod90e19f2015-10-29 11:54:54 +010025
Matteo Scandolo2d2484f2015-11-10 17:13:04 +010026var TEMPLATE_FOOTER = `}]);
27angular.module('xos.contentProvider').run(function($location){$location.path('/')});
28angular.bootstrap(angular.element('#xosContentProvider'), ['xos.contentProvider']);`;
29
Matteo Scandolod90e19f2015-10-29 11:54:54 +010030module.exports = function(options){
Matteo Scandolo2729d082015-10-29 16:13:35 +010031
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010032 // delete previous builded file
Matteo Scandolod90e19f2015-10-29 11:54:54 +010033 gulp.task('clean', function(){
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010034 return del(
35 [options.dashboards + 'xosContentProvider.html'],
36 {force: true}
37 );
Matteo Scandolod90e19f2015-10-29 11:54:54 +010038 });
39
Matteo Scandolo2729d082015-10-29 16:13:35 +010040 // compile and minify scripts
Matteo Scandolod90e19f2015-10-29 11:54:54 +010041 gulp.task('scripts', function() {
42 return gulp.src([
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010043 options.tmp + '**/*.js'
44 ])
45 .pipe(ngAnnotate())
46 .pipe(angularFilesort())
47 .pipe(concat('xosContentProvider.js'))
48 .pipe(uglify())
49 .pipe(gulp.dest(options.static + 'js/'));
Matteo Scandolod90e19f2015-10-29 11:54:54 +010050 });
51
Matteo Scandolo2729d082015-10-29 16:13:35 +010052 // set templates in cache
Matteo Scandolod90e19f2015-10-29 11:54:54 +010053 gulp.task('templates', function(){
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010054 return gulp.src('./src/templates/*.html')
Matteo Scandolod90e19f2015-10-29 11:54:54 +010055 .pipe(templateCache({
Matteo Scandolo82697c02015-11-04 16:30:43 +010056 module: 'xos.contentProvider',
Matteo Scandolo6f7f9742015-11-06 18:49:33 +010057 root: 'templates/',
58 templateFooter: TEMPLATE_FOOTER
Matteo Scandolod90e19f2015-10-29 11:54:54 +010059 }))
Matteo Scandolo82697c02015-11-04 16:30:43 +010060 .pipe(gulp.dest(options.tmp));
Matteo Scandolod90e19f2015-10-29 11:54:54 +010061 });
62
Matteo Scandolo82697c02015-11-04 16:30:43 +010063 // copy html index to Django Folder
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010064 gulp.task('copyHtml', ['clean'], function(){
65 return gulp.src(options.src + 'index.html')
66 // remove dev dependencies from html
67 .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, ''))
68 .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, ''))
Matteo Scandolo6f7f9742015-11-06 18:49:33 +010069 .pipe(replace(/ng-app=".*"\s/, ''))
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010070 // injecting minified files
71 .pipe(
72 inject(
73 gulp.src([
74 options.static + 'js/vendor/xosContentProviderVendor.js',
75 options.static + 'js/xosContentProvider.js'
Matteo Scandolo0c523482016-02-02 17:25:18 -080076 ]),
77 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010078 )
79 )
80 .pipe(rename('xosContentProvider.html'))
81 .pipe(gulp.dest(options.dashboards));
Matteo Scandolod90e19f2015-10-29 11:54:54 +010082 });
83
Matteo Scandolo2729d082015-10-29 16:13:35 +010084 // minify vendor js files
Matteo Scandolod90e19f2015-10-29 11:54:54 +010085 gulp.task('wiredep', function(){
86 var bowerDeps = wiredep().js;
Matteo Scandolo82697c02015-11-04 16:30:43 +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 Scandolod90e19f2015-10-29 11:54:54 +010096 return gulp.src(bowerDeps)
Matteo Scandolo82697c02015-11-04 16:30:43 +010097 .pipe(concat('xosContentProviderVendor.js'))
Matteo Scandolod90e19f2015-10-29 11:54:54 +010098 .pipe(uglify())
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010099 .pipe(gulp.dest(options.static + 'js/vendor/'));
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100100 });
101
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +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 Scandolo2729d082015-10-29 16:13:35 +0100108
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100109 gulp.task('build', function() {
110 runSequence(
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100111 'templates',
Matteo Scandolo82697c02015-11-04 16:30:43 +0100112 'babel',
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100113 'scripts',
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100114 'wiredep',
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100115 'copyHtml',
Matteo Scandolo82697c02015-11-04 16:30:43 +0100116 'cleanTmp'
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100117 );
118 });
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100119};