Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 19 | '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 | |
| 29 | var gulp = require('gulp'); |
| 30 | var ngAnnotate = require('gulp-ng-annotate'); |
| 31 | var uglify = require('gulp-uglify'); |
| 32 | var templateCache = require('gulp-angular-templatecache'); |
| 33 | var runSequence = require('run-sequence'); |
| 34 | var concat = require('gulp-concat-util'); |
| 35 | var del = require('del'); |
| 36 | var wiredep = require('wiredep'); |
| 37 | var angularFilesort = require('gulp-angular-filesort'); |
| 38 | var _ = require('lodash'); |
| 39 | var eslint = require('gulp-eslint'); |
| 40 | var inject = require('gulp-inject'); |
| 41 | var rename = require('gulp-rename'); |
| 42 | var replace = require('gulp-replace'); |
| 43 | var postcss = require('gulp-postcss'); |
| 44 | var autoprefixer = require('autoprefixer'); |
| 45 | var mqpacker = require('css-mqpacker'); |
| 46 | var csswring = require('csswring'); |
| 47 | |
Matteo Scandolo | 2919495 | 2016-06-17 11:57:05 -0700 | [diff] [blame] | 48 | const TEMPLATE_FOOTER = ``; |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 49 | |
| 50 | module.exports = function(options){ |
| 51 | |
| 52 | // delete previous builded file |
| 53 | gulp.task('clean', function(){ |
| 54 | return del( |
| 55 | [ |
| 56 | options.dashboards + 'xosSynchronizerNotifier.html', |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 57 | options.static + 'css/xosSynchronizerNotifier.css', |
| 58 | options.static + 'images/synchronizerNotifier-icon.png', |
| 59 | options.static + 'images/synchronizerNotifier-icon-active.png' |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 60 | ], |
| 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 Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 88 | // 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 Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 94 | // 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 Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 128 | options.static + 'vendor/xosSynchronizerNotifierVendor.js', |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 129 | 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 Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 154 | .pipe(gulp.dest(options.static + 'vendor/')); |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 155 | }); |
| 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 Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 181 | 'copyImages', |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 182 | 'copyHtml', |
| 183 | 'cleanTmp' |
| 184 | ); |
| 185 | }); |
| 186 | }; |