blob: 9af8074c1c8dd894f2d5f13440da28d9ca409d9e [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'
76 ])
77 )
78 )
79 .pipe(rename('xosContentProvider.html'))
80 .pipe(gulp.dest(options.dashboards));
Matteo Scandolod90e19f2015-10-29 11:54:54 +010081 });
82
Matteo Scandolo2729d082015-10-29 16:13:35 +010083 // minify vendor js files
Matteo Scandolod90e19f2015-10-29 11:54:54 +010084 gulp.task('wiredep', function(){
85 var bowerDeps = wiredep().js;
Matteo Scandolo82697c02015-11-04 16:30:43 +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 Scandolod90e19f2015-10-29 11:54:54 +010095 return gulp.src(bowerDeps)
Matteo Scandolo82697c02015-11-04 16:30:43 +010096 .pipe(concat('xosContentProviderVendor.js'))
Matteo Scandolod90e19f2015-10-29 11:54:54 +010097 .pipe(uglify())
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010098 .pipe(gulp.dest(options.static + 'js/vendor/'));
Matteo Scandolod90e19f2015-10-29 11:54:54 +010099 });
100
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +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 Scandolo2729d082015-10-29 16:13:35 +0100107
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100108 gulp.task('build', function() {
109 runSequence(
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100110 'templates',
Matteo Scandolo82697c02015-11-04 16:30:43 +0100111 'babel',
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100112 'scripts',
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100113 'wiredep',
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100114 'copyHtml',
Matteo Scandolo82697c02015-11-04 16:30:43 +0100115 'cleanTmp'
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100116 );
117 });
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100118};