blob: a8ad95b6b6b55f5cb0ca1362ab2441a53bd7d208 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070019'use strict';
20
21// BUILD
22//
23// The only purpose of this gulpfile is to build a XOS view and copy the correct files into
24// .html => dashboards
25// .js (minified and concat) => static/js
26//
27// The template are parsed and added to js with angular $templateCache
28
29var gulp = require('gulp');
30var ngAnnotate = require('gulp-ng-annotate');
31var uglify = require('gulp-uglify');
32var templateCache = require('gulp-angular-templatecache');
33var runSequence = require('run-sequence');
34var concat = require('gulp-concat-util');
35var del = require('del');
36var wiredep = require('wiredep');
37var angularFilesort = require('gulp-angular-filesort');
38var _ = require('lodash');
39var eslint = require('gulp-eslint');
40var inject = require('gulp-inject');
41var rename = require('gulp-rename');
42var replace = require('gulp-replace');
43var postcss = require('gulp-postcss');
44var autoprefixer = require('autoprefixer');
45var mqpacker = require('css-mqpacker');
46var csswring = require('csswring');
47
Matteo Scandolo29194952016-06-17 11:57:05 -070048const TEMPLATE_FOOTER = ``;
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070049
50module.exports = function(options){
51
52 // delete previous builded file
53 gulp.task('clean', function(){
54 return del(
55 [
56 options.dashboards + 'xosSynchronizerNotifier.html',
Matteo Scandolo195dde92016-07-25 16:43:16 -070057 options.static + 'css/xosSynchronizerNotifier.css',
58 options.static + 'images/synchronizerNotifier-icon.png',
59 options.static + 'images/synchronizerNotifier-icon-active.png'
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070060 ],
61 {force: true}
62 );
63 });
64
65 // minify css
66 gulp.task('css', function () {
67 var processors = [
68 autoprefixer({browsers: ['last 1 version']}),
69 mqpacker,
70 csswring
71 ];
72
73 gulp.src([
74 `${options.css}**/*.css`,
75 `!${options.css}dev.css`
76 ])
77 .pipe(postcss(processors))
78 .pipe(gulp.dest(options.tmp + '/css/'));
79 });
80
81 // copy css in correct folder
82 gulp.task('copyCss', ['wait'], function(){
83 return gulp.src([`${options.tmp}/css/*.css`])
84 .pipe(concat('xosSynchronizerNotifier.css'))
85 .pipe(gulp.dest(options.static + 'css/'))
86 });
87
Matteo Scandolo195dde92016-07-25 16:43:16 -070088 // copy images in correct folder
89 gulp.task('copyImages', ['wait'], function(){
90 return gulp.src([`${options.icon}/synchronizerNotifier-icon.png`,`${options.icon}/synchronizerNotifier-icon-active.png`])
91 .pipe(gulp.dest(options.static + 'images/'))
92 });
93
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070094 // compile and minify scripts
95 gulp.task('scripts', function() {
96 return gulp.src([
97 options.tmp + '**/*.js'
98 ])
99 .pipe(ngAnnotate())
100 .pipe(angularFilesort())
101 .pipe(concat('xosSynchronizerNotifier.js'))
102 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
103 .pipe(concat.footer(TEMPLATE_FOOTER))
104 .pipe(uglify())
105 .pipe(gulp.dest(options.static + 'js/'));
106 });
107
108 // set templates in cache
109 gulp.task('templates', function(){
110 return gulp.src('./src/templates/*.html')
111 .pipe(templateCache({
112 module: 'xos.synchronizerNotifier',
113 root: 'templates/'
114 }))
115 .pipe(gulp.dest(options.tmp));
116 });
117
118 // copy html index to Django Folder
119 gulp.task('copyHtml', function(){
120 return gulp.src(options.src + 'index.html')
121 // remove dev dependencies from html
122 .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, ''))
123 .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, ''))
124 // injecting minified files
125 .pipe(
126 inject(
127 gulp.src([
Matteo Scandolo195dde92016-07-25 16:43:16 -0700128 options.static + 'vendor/xosSynchronizerNotifierVendor.js',
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700129 options.static + 'js/xosSynchronizerNotifier.js',
130 options.static + 'css/xosSynchronizerNotifier.css'
131 ]),
132 {ignorePath: '/../../../xos/core/xoslib'}
133 )
134 )
135 .pipe(rename('xosSynchronizerNotifier.html'))
136 .pipe(gulp.dest(options.dashboards));
137 });
138
139 // minify vendor js files
140 gulp.task('wiredep', function(){
141 var bowerDeps = wiredep().js;
142 if(!bowerDeps){
143 return;
144 }
145
146 // remove angular (it's already loaded)
147 _.remove(bowerDeps, function(dep){
148 return dep.indexOf('angular/angular.js') !== -1;
149 });
150
151 return gulp.src(bowerDeps)
152 .pipe(concat('xosSynchronizerNotifierVendor.js'))
153 .pipe(uglify())
Matteo Scandolo195dde92016-07-25 16:43:16 -0700154 .pipe(gulp.dest(options.static + 'vendor/'));
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700155 });
156
157 gulp.task('lint', function () {
158 return gulp.src(['src/js/**/*.js'])
159 .pipe(eslint())
160 .pipe(eslint.format())
161 .pipe(eslint.failAfterError());
162 });
163
164 gulp.task('wait', function (cb) {
165 // setTimeout could be any async task
166 setTimeout(function () {
167 cb();
168 }, 1000);
169 });
170
171 gulp.task('build', function() {
172 runSequence(
173 'clean',
174 'sass',
175 'templates',
176 'babel',
177 'scripts',
178 'wiredep',
179 'css',
180 'copyCss',
Matteo Scandolo195dde92016-07-25 16:43:16 -0700181 'copyImages',
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700182 'copyHtml',
183 'cleanTmp'
184 );
185 });
186};