blob: 8560e7ccaf0c045171dec9841bfb4aef01320e17 [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 Scandoloe53ee052015-11-03 14:32:00 +010019'use strict';
20
21// BUILD
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010022//
Matteo Scandoloe53ee052015-11-03 14:32:00 +010023// 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
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010026//
Matteo Scandoloe53ee052015-11-03 14:32:00 +010027// The template are parsed and added to js with angular $templateCache
28
29var gulp = require('gulp');
Matteo Scandolod838fac2015-11-04 16:10:09 +010030var ngAnnotate = require('gulp-ng-annotate');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010031var uglify = require('gulp-uglify');
32var templateCache = require('gulp-angular-templatecache');
33var runSequence = require('run-sequence');
Matteo Scandolodd55c132016-04-13 10:46:08 -070034var concat = require('gulp-concat-util');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010035var del = require('del');
36var wiredep = require('wiredep');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010037var angularFilesort = require('gulp-angular-filesort');
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +010038var _ = require('lodash');
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010039var eslint = require('gulp-eslint');
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010040var inject = require('gulp-inject');
41var rename = require('gulp-rename');
Matteo Scandolo61a714d2015-11-06 11:16:12 +010042var replace = require('gulp-replace');
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -070043var postcss = require('gulp-postcss');
44var autoprefixer = require('autoprefixer');
45var mqpacker = require('css-mqpacker');
46var csswring = require('csswring');
Matteo Scandoloeaa69222016-08-05 15:24:28 -070047var yaml = require('js-yaml');
48var colors = require('colors/safe');
Matteo Scandolo262385f2016-08-08 09:28:14 -070049var fs = require('fs');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010050
Matteo Scandolodd55c132016-04-13 10:46:08 -070051const TEMPLATE_FOOTER = `
52angular.module('xos.<%= name %>')
53.run(['$location', function(a){
54 a.path('/');
55}])
56`
57
Matteo Scandoloe53ee052015-11-03 14:32:00 +010058module.exports = function(options){
59
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010060 // delete previous builded file
Matteo Scandoloe53ee052015-11-03 14:32:00 +010061 gulp.task('clean', function(){
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010062 return del(
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -070063 [
64 options.dashboards + 'xos<%= fileName %>.html',
Arpit Agarwalfecac782016-07-18 17:30:32 -070065 options.static + 'css/xos<%= fileName %>.css',
66 options.static + 'images/<%= name %>-icon.png',
67 options.static + 'images/<%= name %>-icon-active.png'
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -070068 ],
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010069 {force: true}
70 );
Matteo Scandoloe53ee052015-11-03 14:32:00 +010071 });
72
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070073 // minify css
74 gulp.task('css', function () {
75 var processors = [
76 autoprefixer({browsers: ['last 1 version']}),
77 mqpacker,
78 csswring
79 ];
80
81 gulp.src([
82 `${options.css}**/*.css`,
83 `!${options.css}dev.css`
84 ])
85 .pipe(postcss(processors))
86 .pipe(gulp.dest(options.tmp + '/css/'));
87 });
88
89 // copy css in correct folder
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -070090 gulp.task('copyCss', ['wait'], function(){
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070091 return gulp.src([`${options.tmp}/css/*.css`])
Matteo Scandolodd55c132016-04-13 10:46:08 -070092 .pipe(concat('xos<%= fileName %>.css'))
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070093 .pipe(gulp.dest(options.static + 'css/'))
94 });
95
Arpit Agarwalfecac782016-07-18 17:30:32 -070096 // copy images in correct folder
97 gulp.task('copyImages', ['wait'], function(){
Matteo Scandolo262385f2016-08-08 09:28:14 -070098 return gulp.src([`${options.icon}/<%= name %>-icon.png`, `${options.icon}/<%= name %>-icon-active.png`])
Arpit Agarwalfecac782016-07-18 17:30:32 -070099 .pipe(gulp.dest(options.static + 'images/'))
100 });
101
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100102 // compile and minify scripts
103 gulp.task('scripts', function() {
104 return gulp.src([
Matteo Scandolo8a1f2362015-11-06 09:52:01 +0100105 options.tmp + '**/*.js'
106 ])
107 .pipe(ngAnnotate())
108 .pipe(angularFilesort())
109 .pipe(concat('xos<%= fileName %>.js'))
Matteo Scandolodd55c132016-04-13 10:46:08 -0700110 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
111 .pipe(concat.footer(TEMPLATE_FOOTER))
Matteo Scandolo8a1f2362015-11-06 09:52:01 +0100112 .pipe(uglify())
113 .pipe(gulp.dest(options.static + 'js/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100114 });
115
116 // set templates in cache
117 gulp.task('templates', function(){
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100118 return gulp.src('./src/templates/*.html')
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100119 .pipe(templateCache({
Matteo Scandolo6be0cd22015-11-03 16:27:07 +0100120 module: 'xos.<%= name %>',
Matteo Scandolob7c91cf2016-03-29 14:23:20 -0700121 root: 'templates/'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100122 }))
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100123 .pipe(gulp.dest(options.tmp));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100124 });
125
Matteo Scandolo6be0cd22015-11-03 16:27:07 +0100126 // copy html index to Django Folder
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700127 gulp.task('copyHtml', function(){
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100128 return gulp.src(options.src + 'index.html')
Matteo Scandolo61a714d2015-11-06 11:16:12 +0100129 // remove dev dependencies from html
Matteo Scandolo34d35962016-05-10 14:58:47 -0700130 .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, ''))
131 .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, ''))
Matteo Scandolo61a714d2015-11-06 11:16:12 +0100132 // injecting minified files
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100133 .pipe(
134 inject(
135 gulp.src([
Matteo Scandolo195dde92016-07-25 16:43:16 -0700136 options.static + 'vendor/xos<%= fileName %>Vendor.js',
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700137 options.static + 'js/xos<%= fileName %>.js',
138 options.static + 'css/xos<%= fileName %>.css'
Matteo Scandoloc0c94092016-02-02 17:25:18 -0800139 ]),
140 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100141 )
142 )
143 .pipe(rename('xos<%= fileName %>.html'))
144 .pipe(gulp.dest(options.dashboards));
145 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100146
147 // minify vendor js files
148 gulp.task('wiredep', function(){
149 var bowerDeps = wiredep().js;
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100150 if(!bowerDeps){
151 return;
152 }
153
154 // remove angular (it's already loaded)
155 _.remove(bowerDeps, function(dep){
156 return dep.indexOf('angular/angular.js') !== -1;
157 });
158
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100159 return gulp.src(bowerDeps)
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100160 .pipe(concat('xos<%= fileName %>Vendor.js'))
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100161 .pipe(uglify())
Matteo Scandolo195dde92016-07-25 16:43:16 -0700162 .pipe(gulp.dest(options.static + 'vendor/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100163 });
164
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100165 gulp.task('lint', function () {
166 return gulp.src(['src/js/**/*.js'])
167 .pipe(eslint())
168 .pipe(eslint.format())
169 .pipe(eslint.failAfterError());
170 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100171
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700172 gulp.task('wait', function (cb) {
173 // setTimeout could be any async task
174 setTimeout(function () {
175 cb();
176 }, 1000);
177 });
178
Matteo Scandoloeaa69222016-08-05 15:24:28 -0700179 gulp.task('tosca', function (cb) {
180
181 // TOSCA to register the dashboard in the system
182 const dashboardJson = {};
183 dashboardJson['<%= fileName %>'] = {
184 type: 'tosca.nodes.DashboardView',
185 properties: {
186 url: 'template:xos<%= fileName %>'
187 }
188 };
Matteo Scandolo262385f2016-08-08 09:28:14 -0700189
190 // check for custom icons
191 if(
192 fs.existsSync(`${options.icon}/<%= name %>-icon.png`) &&
193 fs.existsSync(`${options.icon}/<%= name %>-icon-active.png`)
194 ){
195 dashboardJson['<%= fileName %>'].properties.custom_icon = true;
196 }
197
Matteo Scandoloeaa69222016-08-05 15:24:28 -0700198 const dashboardTosca = yaml.dump(dashboardJson).replace(/'/gmi, '');
199
200 // TOSCA to add the dashboard to the user
201 const userDashboardJson = {};
202 userDashboardJson['<%= name %>_dashboard'] = {
203 node: '<%= fileName %>',
204 relationship: 'tosca.relationships.UsesDashboard'
205 };
206 const userJson = {
207 'padmin@vicci.org': {
208 type: 'tosca.nodes.User',
209 properties: {
210 'no-create': true,
211 'no-delete': true
212 },
213 requirements: [userDashboardJson]
214 }
215 };
216 const userTosca = yaml.dump(userJson).replace(/'/gmi, '');
217
218
219 // the output is in a timeout so that it get printed after the gulp logs
220 setTimeout(function () {
221 console.log(colors.cyan('\n\n# You can use this recipe to load the dashboard in the system:'));
222 console.log(colors.yellow(dashboardTosca));
223 console.log(colors.cyan('# And this recipe to activate the dashboard for a user:'));
224 console.log(colors.yellow(userTosca));
225 }, 1000);
226 cb();
227 });
228
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100229 gulp.task('build', function() {
230 runSequence(
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700231 'clean',
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700232 'sass',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100233 'templates',
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100234 'babel',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100235 'scripts',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100236 'wiredep',
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700237 'css',
238 'copyCss',
Arpit Agarwalfecac782016-07-18 17:30:32 -0700239 'copyImages',
Matteo Scandolo8a1f2362015-11-06 09:52:01 +0100240 'copyHtml',
Matteo Scandoloeaa69222016-08-05 15:24:28 -0700241 'cleanTmp',
242 'tosca'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100243 );
244 });
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100245};