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