blob: 26fc1febe17c45927549644feeeca5a72148d1d9 [file] [log] [blame]
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -07001'use strict';
2
3// BUILD
4//
5// 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
8//
9// The template are parsed and added to js with angular $templateCache
10
11var gulp = require('gulp');
12var ngAnnotate = require('gulp-ng-annotate');
13var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
Matteo Scandoloe993b882016-05-17 17:30:14 -070016var concat = require('gulp-concat-util');
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -070017var del = require('del');
18var wiredep = require('wiredep');
19var angularFilesort = require('gulp-angular-filesort');
20var _ = require('lodash');
21var eslint = require('gulp-eslint');
22var inject = require('gulp-inject');
23var rename = require('gulp-rename');
24var replace = require('gulp-replace');
Jeremy Moweryee139912016-04-17 19:20:01 -070025var postcss = require('gulp-postcss');
26var autoprefixer = require('autoprefixer');
27var mqpacker = require('css-mqpacker');
28var csswring = require('csswring');
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -070029
Matteo Scandoloe993b882016-05-17 17:30:14 -070030const TEMPLATE_FOOTER = `
31angular.module('xos.openVPNDashboard')
32.run(['$location', function(a){
33 a.path('/');
34}])
35`
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -070036
37module.exports = function(options){
Matteo Scandoloe993b882016-05-17 17:30:14 -070038
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -070039 // delete previous builded file
40 gulp.task('clean', function(){
41 return del(
Matteo Scandoloe993b882016-05-17 17:30:14 -070042 [
43 options.dashboards + 'xosOpenVPNDashboard.html',
Matteo Scandolo195dde92016-07-25 16:43:16 -070044 options.static + 'css/xosOpenVPNDashboard.css',
45 options.static + 'images/openVPNDashboard-icon.png',
46 options.static + 'images/openVPNDashboard-icon-active.png'
Matteo Scandoloe993b882016-05-17 17:30:14 -070047 ],
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -070048 {force: true}
49 );
50 });
51
Jeremy Moweryee139912016-04-17 19:20:01 -070052 // minify css
53 gulp.task('css', function () {
54 var processors = [
55 autoprefixer({browsers: ['last 1 version']}),
56 mqpacker,
57 csswring
58 ];
59
60 gulp.src([
61 `${options.css}**/*.css`,
62 `!${options.css}dev.css`
63 ])
64 .pipe(postcss(processors))
65 .pipe(gulp.dest(options.tmp + '/css/'));
66 });
67
Matteo Scandoloe993b882016-05-17 17:30:14 -070068 // copy css in correct folder
69 gulp.task('copyCss', ['wait'], function(){
Jeremy Moweryee139912016-04-17 19:20:01 -070070 return gulp.src([`${options.tmp}/css/*.css`])
Jeremy Mowery81e84cc2016-04-19 17:01:48 -070071 .pipe(concat('xosOpenVPNDashboard.css'))
Jeremy Moweryee139912016-04-17 19:20:01 -070072 .pipe(gulp.dest(options.static + 'css/'))
73 });
74
Matteo Scandolo195dde92016-07-25 16:43:16 -070075 // copy images in correct folder
76 gulp.task('copyImages', ['wait'], function(){
77 return gulp.src([`${options.icon}/openVPNDashboard-icon.png`,`${options.icon}/openVPNDashboard-icon-active.png`])
78 .pipe(gulp.dest(options.static + 'images/'))
79 });
80
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -070081 // compile and minify scripts
82 gulp.task('scripts', function() {
83 return gulp.src([
84 options.tmp + '**/*.js'
85 ])
86 .pipe(ngAnnotate())
87 .pipe(angularFilesort())
Jeremy Mowery81e84cc2016-04-19 17:01:48 -070088 .pipe(concat('xosOpenVPNDashboard.js'))
Matteo Scandoloe993b882016-05-17 17:30:14 -070089 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
90 .pipe(concat.footer(TEMPLATE_FOOTER))
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -070091 .pipe(uglify())
92 .pipe(gulp.dest(options.static + 'js/'));
93 });
94
95 // set templates in cache
96 gulp.task('templates', function(){
97 return gulp.src('./src/templates/*.html')
98 .pipe(templateCache({
Jeremy Mowery81e84cc2016-04-19 17:01:48 -070099 module: 'xos.openVPNDashboard',
Matteo Scandoloe993b882016-05-17 17:30:14 -0700100 root: 'templates/'
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -0700101 }))
102 .pipe(gulp.dest(options.tmp));
103 });
104
105 // copy html index to Django Folder
Matteo Scandoloe993b882016-05-17 17:30:14 -0700106 gulp.task('copyHtml', function(){
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -0700107 return gulp.src(options.src + 'index.html')
108 // remove dev dependencies from html
Matteo Scandoloe993b882016-05-17 17:30:14 -0700109 .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, ''))
110 .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, ''))
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -0700111 // injecting minified files
112 .pipe(
113 inject(
114 gulp.src([
Matteo Scandolo195dde92016-07-25 16:43:16 -0700115 options.static + 'vendor/xosOpenVPNDashboardVendor.js',
Jeremy Mowery81e84cc2016-04-19 17:01:48 -0700116 options.static + 'js/xosOpenVPNDashboard.js',
117 options.static + 'css/xosOpenVPNDashboard.css'
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -0700118 ]),
119 {ignorePath: '/../../../xos/core/xoslib'}
120 )
121 )
Jeremy Mowery81e84cc2016-04-19 17:01:48 -0700122 .pipe(rename('xosOpenVPNDashboard.html'))
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -0700123 .pipe(gulp.dest(options.dashboards));
124 });
125
126 // minify vendor js files
127 gulp.task('wiredep', function(){
128 var bowerDeps = wiredep().js;
129 if(!bowerDeps){
130 return;
131 }
132
133 // remove angular (it's already loaded)
134 _.remove(bowerDeps, function(dep){
135 return dep.indexOf('angular/angular.js') !== -1;
136 });
137
138 return gulp.src(bowerDeps)
Jeremy Mowery81e84cc2016-04-19 17:01:48 -0700139 .pipe(concat('xosOpenVPNDashboardVendor.js'))
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -0700140 .pipe(uglify())
Matteo Scandolo195dde92016-07-25 16:43:16 -0700141 .pipe(gulp.dest(options.static + 'vendor/'));
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -0700142 });
143
144 gulp.task('lint', function () {
145 return gulp.src(['src/js/**/*.js'])
146 .pipe(eslint())
147 .pipe(eslint.format())
148 .pipe(eslint.failAfterError());
149 });
150
Matteo Scandoloe993b882016-05-17 17:30:14 -0700151 gulp.task('wait', function (cb) {
152 // setTimeout could be any async task
153 setTimeout(function () {
154 cb();
155 }, 1000);
156 });
157
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -0700158 gulp.task('build', function() {
159 runSequence(
Matteo Scandoloe993b882016-05-17 17:30:14 -0700160 'clean',
161 'sass',
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -0700162 'templates',
163 'babel',
164 'scripts',
165 'wiredep',
Matteo Scandoloe993b882016-05-17 17:30:14 -0700166 'css',
Jeremy Moweryee139912016-04-17 19:20:01 -0700167 'copyCss',
Matteo Scandolo195dde92016-07-25 16:43:16 -0700168 'copyImages',
Matteo Scandoloe993b882016-05-17 17:30:14 -0700169 'copyHtml',
Jeremy Mowery6efeb7a2016-02-25 09:32:00 -0700170 'cleanTmp'
171 );
172 });
Matteo Scandoloe993b882016-05-17 17:30:14 -0700173};