blob: 4a042188d098345c89d3426674e96d5bba86a40e [file] [log] [blame]
Matteo Scandolof813b6a2015-11-03 14:32:00 +01001'use strict';
2
3// BUILD
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +01004//
Matteo Scandolof813b6a2015-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 Scandolo7cf73ad2015-11-05 18:36:10 +01008//
Matteo Scandolof813b6a2015-11-03 14:32:00 +01009// The template are parsed and added to js with angular $templateCache
10
11var gulp = require('gulp');
Matteo Scandoloff81c692015-11-04 16:10:09 +010012var ngAnnotate = require('gulp-ng-annotate');
Matteo Scandolof813b6a2015-11-03 14:32:00 +010013var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +010016var concat = require('gulp-concat');
Matteo Scandolof813b6a2015-11-03 14:32:00 +010017var del = require('del');
18var wiredep = require('wiredep');
Matteo Scandolof813b6a2015-11-03 14:32:00 +010019var angularFilesort = require('gulp-angular-filesort');
Matteo Scandolo352b6012015-11-04 15:07:36 +010020var _ = require('lodash');
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +010021var eslint = require('gulp-eslint');
Matteo Scandoloc7794962015-11-06 10:35:20 +010022var inject = require('gulp-inject');
23var rename = require('gulp-rename');
Matteo Scandolo15855792015-11-06 11:16:12 +010024var replace = require('gulp-replace');
Matteo Scandolof813b6a2015-11-03 14:32:00 +010025
Matteo Scandolo807fb012015-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 Scandolof813b6a2015-11-03 14:32:00 +010030module.exports = function(options){
31
Matteo Scandoloc7794962015-11-06 10:35:20 +010032 // delete previous builded file
Matteo Scandolof813b6a2015-11-03 14:32:00 +010033 gulp.task('clean', function(){
Matteo Scandoloc7794962015-11-06 10:35:20 +010034 return del(
35 [options.dashboards + 'xos<%= fileName %>.html'],
36 {force: true}
37 );
Matteo Scandolof813b6a2015-11-03 14:32:00 +010038 });
39
40 // compile and minify scripts
41 gulp.task('scripts', function() {
42 return gulp.src([
Matteo Scandolo724747c2015-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 Scandolof813b6a2015-11-03 14:32:00 +010050 });
51
52 // set templates in cache
53 gulp.task('templates', function(){
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +010054 return gulp.src('./src/templates/*.html')
Matteo Scandolof813b6a2015-11-03 14:32:00 +010055 .pipe(templateCache({
Matteo Scandolodc88bf12015-11-03 16:27:07 +010056 module: 'xos.<%= name %>',
Matteo Scandoloab91f432015-11-06 18:25:52 +010057 root: 'templates/',
58 templateFooter: TEMPLATE_FOOTER
Matteo Scandolof813b6a2015-11-03 14:32:00 +010059 }))
Matteo Scandoloac92b722015-11-04 11:46:54 +010060 .pipe(gulp.dest(options.tmp));
Matteo Scandolof813b6a2015-11-03 14:32:00 +010061 });
62
Matteo Scandolodc88bf12015-11-03 16:27:07 +010063 // copy html index to Django Folder
Matteo Scandoloc7794962015-11-06 10:35:20 +010064 gulp.task('copyHtml', ['clean'], function(){
65 return gulp.src(options.src + 'index.html')
Matteo Scandolo15855792015-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 Scandoloab91f432015-11-06 18:25:52 +010069 .pipe(replace(/ng-app=".*"\s/, ''))
Matteo Scandolo15855792015-11-06 11:16:12 +010070 // injecting minified files
Matteo Scandoloc7794962015-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'
76 ])
77 )
78 )
79 .pipe(rename('xos<%= fileName %>.html'))
80 .pipe(gulp.dest(options.dashboards));
81 });
Matteo Scandolof813b6a2015-11-03 14:32:00 +010082
83 // minify vendor js files
84 gulp.task('wiredep', function(){
85 var bowerDeps = wiredep().js;
Matteo Scandolo352b6012015-11-04 15:07:36 +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 Scandolof813b6a2015-11-03 14:32:00 +010095 return gulp.src(bowerDeps)
Matteo Scandolo352b6012015-11-04 15:07:36 +010096 .pipe(concat('xos<%= fileName %>Vendor.js'))
Matteo Scandolof813b6a2015-11-03 14:32:00 +010097 .pipe(uglify())
Matteo Scandolo724747c2015-11-06 09:52:01 +010098 .pipe(gulp.dest(options.static + 'js/vendor/'));
Matteo Scandolof813b6a2015-11-03 14:32:00 +010099 });
100
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +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 Scandolof813b6a2015-11-03 14:32:00 +0100107
108 gulp.task('build', function() {
109 runSequence(
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100110 'templates',
Matteo Scandoloac92b722015-11-04 11:46:54 +0100111 'babel',
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100112 'scripts',
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100113 'wiredep',
Matteo Scandolo724747c2015-11-06 09:52:01 +0100114 'copyHtml',
Matteo Scandoloac92b722015-11-04 11:46:54 +0100115 'cleanTmp'
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100116 );
117 });
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +0100118};