blob: c38adfc8125171fb3ec88a9e72bb85048145ef06 [file] [log] [blame]
Matteo Scandolo608f91d2015-10-29 11:54:54 +01001'use strict';
2
3// BUILD
Matteo Scandolod44ceb42015-11-06 17:14:28 +01004//
Matteo Scandolo608f91d2015-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 Scandolod44ceb42015-11-06 17:14:28 +01008//
Matteo Scandolo608f91d2015-10-29 11:54:54 +01009// The template are parsed and added to js with angular $templateCache
10
11var gulp = require('gulp');
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010012var ngAnnotate = require('gulp-ng-annotate');
Matteo Scandolo608f91d2015-10-29 11:54:54 +010013var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
Matteo Scandolod44ceb42015-11-06 17:14:28 +010016var concat = require('gulp-concat');
Matteo Scandolo608f91d2015-10-29 11:54:54 +010017var del = require('del');
18var wiredep = require('wiredep');
Matteo Scandolo6d972642015-10-29 15:50:15 +010019var angularFilesort = require('gulp-angular-filesort');
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010020var _ = require('lodash');
Matteo Scandolod44ceb42015-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 Scandolo608f91d2015-10-29 11:54:54 +010025
Matteo Scandolo9a607262015-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 Scandolo608f91d2015-10-29 11:54:54 +010030module.exports = function(options){
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010031
Matteo Scandolod44ceb42015-11-06 17:14:28 +010032 // delete previous builded file
Matteo Scandolo608f91d2015-10-29 11:54:54 +010033 gulp.task('clean', function(){
Matteo Scandolod44ceb42015-11-06 17:14:28 +010034 return del(
35 [options.dashboards + 'xosContentProvider.html'],
36 {force: true}
37 );
Matteo Scandolo608f91d2015-10-29 11:54:54 +010038 });
39
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010040 // compile and minify scripts
Matteo Scandolo608f91d2015-10-29 11:54:54 +010041 gulp.task('scripts', function() {
42 return gulp.src([
Matteo Scandolod44ceb42015-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 Scandolo608f91d2015-10-29 11:54:54 +010050 });
51
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010052 // set templates in cache
Matteo Scandolo608f91d2015-10-29 11:54:54 +010053 gulp.task('templates', function(){
Matteo Scandolod44ceb42015-11-06 17:14:28 +010054 return gulp.src('./src/templates/*.html')
Matteo Scandolo608f91d2015-10-29 11:54:54 +010055 .pipe(templateCache({
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010056 module: 'xos.contentProvider',
Matteo Scandolo7db08432015-11-06 18:49:33 +010057 root: 'templates/',
58 templateFooter: TEMPLATE_FOOTER
Matteo Scandolo608f91d2015-10-29 11:54:54 +010059 }))
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010060 .pipe(gulp.dest(options.tmp));
Matteo Scandolo608f91d2015-10-29 11:54:54 +010061 });
62
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010063 // copy html index to Django Folder
Matteo Scandolod44ceb42015-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 Scandolo7db08432015-11-06 18:49:33 +010069 .pipe(replace(/ng-app=".*"\s/, ''))
Matteo Scandolod44ceb42015-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 Scandoloc0c94092016-02-02 17:25:18 -080076 ]),
77 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandolod44ceb42015-11-06 17:14:28 +010078 )
79 )
80 .pipe(rename('xosContentProvider.html'))
81 .pipe(gulp.dest(options.dashboards));
Matteo Scandolo608f91d2015-10-29 11:54:54 +010082 });
83
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010084 // minify vendor js files
Matteo Scandolo608f91d2015-10-29 11:54:54 +010085 gulp.task('wiredep', function(){
86 var bowerDeps = wiredep().js;
Matteo Scandolobbcc01b2015-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 Scandolo608f91d2015-10-29 11:54:54 +010096 return gulp.src(bowerDeps)
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010097 .pipe(concat('xosContentProviderVendor.js'))
Matteo Scandolo608f91d2015-10-29 11:54:54 +010098 .pipe(uglify())
Matteo Scandolod44ceb42015-11-06 17:14:28 +010099 .pipe(gulp.dest(options.static + 'js/vendor/'));
Matteo Scandolo608f91d2015-10-29 11:54:54 +0100100 });
101
Matteo Scandolod44ceb42015-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 Scandoloc2d0f532015-10-29 16:13:35 +0100108
Matteo Scandolo608f91d2015-10-29 11:54:54 +0100109 gulp.task('build', function() {
110 runSequence(
Matteo Scandolo608f91d2015-10-29 11:54:54 +0100111 'templates',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100112 'babel',
Matteo Scandolo608f91d2015-10-29 11:54:54 +0100113 'scripts',
Matteo Scandolo608f91d2015-10-29 11:54:54 +0100114 'wiredep',
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100115 'copyHtml',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100116 'cleanTmp'
Matteo Scandolo608f91d2015-10-29 11:54:54 +0100117 );
118 });
Matteo Scandolod44ceb42015-11-06 17:14:28 +0100119};