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 | |
Rizwan Haider | 8e5f477 | 2016-08-17 18:04:35 -0400 | [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 | |
| 48 | const TEMPLATE_FOOTER = ` |
| 49 | angular.module('xos.ecordTopology') |
| 50 | .run(['$location', function(a){ |
| 51 | a.path('/'); |
| 52 | }]) |
| 53 | ` |
| 54 | |
| 55 | module.exports = function(options){ |
| 56 | |
| 57 | // delete previous builded file |
| 58 | gulp.task('clean', function(){ |
| 59 | return del( |
| 60 | [ |
| 61 | options.dashboards + 'xosEcordTopology.html', |
| 62 | options.static + 'css/xosEcordTopology.css' |
| 63 | ], |
| 64 | {force: true} |
| 65 | ); |
| 66 | }); |
| 67 | |
| 68 | // minify css |
| 69 | gulp.task('css', function () { |
| 70 | var processors = [ |
| 71 | autoprefixer({browsers: ['last 1 version']}), |
| 72 | mqpacker, |
| 73 | csswring |
| 74 | ]; |
| 75 | |
| 76 | gulp.src([ |
| 77 | `${options.css}**/*.css`, |
| 78 | `!${options.css}dev.css` |
| 79 | ]) |
| 80 | .pipe(postcss(processors)) |
| 81 | .pipe(gulp.dest(options.tmp + '/css/')); |
| 82 | }); |
| 83 | |
| 84 | // copy css in correct folder |
| 85 | gulp.task('copyCss', ['wait'], function(){ |
| 86 | return gulp.src([`${options.tmp}/css/*.css`]) |
| 87 | .pipe(concat('xosEcordTopology.css')) |
| 88 | .pipe(gulp.dest(options.static + 'css/')) |
| 89 | }); |
| 90 | |
| 91 | // compile and minify scripts |
| 92 | gulp.task('scripts', function() { |
| 93 | return gulp.src([ |
| 94 | options.tmp + '**/*.js' |
| 95 | ]) |
| 96 | .pipe(ngAnnotate()) |
| 97 | .pipe(angularFilesort()) |
| 98 | .pipe(concat('xosEcordTopology.js')) |
| 99 | .pipe(concat.header('//Autogenerated, do not edit!!!\n')) |
| 100 | .pipe(concat.footer(TEMPLATE_FOOTER)) |
| 101 | .pipe(uglify()) |
| 102 | .pipe(gulp.dest(options.static + 'js/')); |
| 103 | }); |
| 104 | |
| 105 | // set templates in cache |
| 106 | gulp.task('templates', function(){ |
| 107 | return gulp.src('./src/templates/*.html') |
| 108 | .pipe(templateCache({ |
| 109 | module: 'xos.ecordTopology', |
| 110 | root: 'templates/' |
| 111 | })) |
| 112 | .pipe(gulp.dest(options.tmp)); |
| 113 | }); |
| 114 | |
| 115 | // copy html index to Django Folder |
| 116 | gulp.task('copyHtml', function(){ |
| 117 | return gulp.src(options.src + 'index.html') |
| 118 | // remove dev dependencies from html |
| 119 | .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, '')) |
| 120 | .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, '')) |
| 121 | // injecting minified files |
| 122 | .pipe( |
| 123 | inject( |
| 124 | gulp.src([ |
| 125 | options.static + 'js/vendor/xosEcordTopologyVendor.js', |
| 126 | options.static + 'js/xosEcordTopology.js', |
| 127 | options.static + 'css/xosEcordTopology.css' |
| 128 | ]), |
| 129 | {ignorePath: '/../../../xos/core/xoslib'} |
| 130 | ) |
| 131 | ) |
| 132 | .pipe(rename('xosEcordTopology.html')) |
| 133 | .pipe(gulp.dest(options.dashboards)); |
| 134 | }); |
| 135 | |
| 136 | // minify vendor js files |
| 137 | gulp.task('wiredep', function(){ |
| 138 | var bowerDeps = wiredep().js; |
| 139 | if(!bowerDeps){ |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | // remove angular (it's already loaded) |
| 144 | _.remove(bowerDeps, function(dep){ |
| 145 | return dep.indexOf('angular/angular.js') !== -1; |
| 146 | }); |
| 147 | |
| 148 | return gulp.src(bowerDeps) |
| 149 | .pipe(concat('xosEcordTopologyVendor.js')) |
| 150 | .pipe(uglify()) |
| 151 | .pipe(gulp.dest(options.static + 'js/vendor/')); |
| 152 | }); |
| 153 | |
| 154 | gulp.task('lint', function () { |
| 155 | return gulp.src(['src/js/**/*.js']) |
| 156 | .pipe(eslint()) |
| 157 | .pipe(eslint.format()) |
| 158 | .pipe(eslint.failAfterError()); |
| 159 | }); |
| 160 | |
| 161 | gulp.task('wait', function (cb) { |
| 162 | // setTimeout could be any async task |
| 163 | setTimeout(function () { |
| 164 | cb(); |
| 165 | }, 1000); |
| 166 | }); |
| 167 | |
| 168 | gulp.task('build', function() { |
| 169 | runSequence( |
| 170 | 'clean', |
| 171 | 'sass', |
| 172 | 'templates', |
| 173 | 'babel', |
| 174 | 'scripts', |
| 175 | 'wiredep', |
| 176 | 'css', |
| 177 | 'copyCss', |
| 178 | 'copyHtml', |
| 179 | 'cleanTmp' |
| 180 | ); |
| 181 | }); |
| 182 | }; |