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 | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 19 | 'use strict'; |
| 20 | |
| 21 | // BUILD |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 22 | // |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 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 |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 26 | // |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 27 | // The template are parsed and added to js with angular $templateCache |
| 28 | |
| 29 | var gulp = require('gulp'); |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 30 | var ngAnnotate = require('gulp-ng-annotate'); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 31 | var uglify = require('gulp-uglify'); |
| 32 | var templateCache = require('gulp-angular-templatecache'); |
| 33 | var runSequence = require('run-sequence'); |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 34 | var concat = require('gulp-concat-util'); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 35 | var del = require('del'); |
| 36 | var wiredep = require('wiredep'); |
Matteo Scandolo | 6d97264 | 2015-10-29 15:50:15 +0100 | [diff] [blame] | 37 | var angularFilesort = require('gulp-angular-filesort'); |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 38 | var _ = require('lodash'); |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 39 | var eslint = require('gulp-eslint'); |
| 40 | var inject = require('gulp-inject'); |
| 41 | var rename = require('gulp-rename'); |
| 42 | var replace = require('gulp-replace'); |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 43 | var postcss = require('gulp-postcss'); |
| 44 | var autoprefixer = require('autoprefixer'); |
| 45 | var mqpacker = require('css-mqpacker'); |
| 46 | var csswring = require('csswring'); |
Matteo Scandolo | 262385f | 2016-08-08 09:28:14 -0700 | [diff] [blame] | 47 | var yaml = require('js-yaml'); |
| 48 | var colors = require('colors/safe'); |
| 49 | var fs = require('fs'); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 50 | |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 51 | const TEMPLATE_FOOTER = ` |
| 52 | angular.module('xos.contentProvider') |
| 53 | .run(['$location', function(a){ |
| 54 | a.path('/'); |
| 55 | }]) |
| 56 | ` |
Matteo Scandolo | 9a60726 | 2015-11-10 17:13:04 +0100 | [diff] [blame] | 57 | |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 58 | module.exports = function(options){ |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 59 | |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 60 | // delete previous builded file |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 61 | gulp.task('clean', function(){ |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 62 | return del( |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 63 | [ |
| 64 | options.dashboards + 'xosContentProvider.html', |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 65 | options.static + 'css/xosContentProvider.css', |
| 66 | options.static + 'images/contentProvider-icon.png', |
| 67 | options.static + 'images/contentProvider-icon-active.png' |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 68 | ], |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 69 | {force: true} |
| 70 | ); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 71 | }); |
| 72 | |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 73 | // 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 |
| 90 | gulp.task('copyCss', ['wait'], function(){ |
| 91 | return gulp.src([`${options.tmp}/css/*.css`]) |
| 92 | .pipe(concat('xosContentProvider.css')) |
| 93 | .pipe(gulp.dest(options.static + 'css/')) |
| 94 | }); |
| 95 | |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 96 | // copy images in correct folder |
| 97 | gulp.task('copyImages', ['wait'], function(){ |
Matteo Scandolo | 262385f | 2016-08-08 09:28:14 -0700 | [diff] [blame] | 98 | return gulp.src([`${options.icon}/contentProvider-icon.png`, `${options.icon}/contentProvider-icon-active.png`]) |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 99 | .pipe(gulp.dest(options.static + 'images/')) |
| 100 | }); |
| 101 | |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 102 | // compile and minify scripts |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 103 | gulp.task('scripts', function() { |
| 104 | return gulp.src([ |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 105 | options.tmp + '**/*.js' |
| 106 | ]) |
| 107 | .pipe(ngAnnotate()) |
| 108 | .pipe(angularFilesort()) |
| 109 | .pipe(concat('xosContentProvider.js')) |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 110 | .pipe(concat.header('//Autogenerated, do not edit!!!\n')) |
| 111 | .pipe(concat.footer(TEMPLATE_FOOTER)) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 112 | .pipe(uglify()) |
| 113 | .pipe(gulp.dest(options.static + 'js/')); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 114 | }); |
| 115 | |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 116 | // set templates in cache |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 117 | gulp.task('templates', function(){ |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 118 | return gulp.src('./src/templates/*.html') |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 119 | .pipe(templateCache({ |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 120 | module: 'xos.contentProvider', |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 121 | root: 'templates/' |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 122 | })) |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 123 | .pipe(gulp.dest(options.tmp)); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 124 | }); |
| 125 | |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 126 | // copy html index to Django Folder |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 127 | gulp.task('copyHtml', function(){ |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 128 | return gulp.src(options.src + 'index.html') |
| 129 | // remove dev dependencies from html |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 130 | .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, '')) |
| 131 | .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, '')) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 132 | // injecting minified files |
| 133 | .pipe( |
| 134 | inject( |
| 135 | gulp.src([ |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 136 | options.static + 'vendor/xosContentProviderVendor.js', |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 137 | options.static + 'js/xosContentProvider.js', |
| 138 | options.static + 'css/xosContentProvider.css' |
Matteo Scandolo | c0c9409 | 2016-02-02 17:25:18 -0800 | [diff] [blame] | 139 | ]), |
| 140 | {ignorePath: '/../../../xos/core/xoslib'} |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 141 | ) |
| 142 | ) |
| 143 | .pipe(rename('xosContentProvider.html')) |
| 144 | .pipe(gulp.dest(options.dashboards)); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 145 | }); |
| 146 | |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 147 | // minify vendor js files |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 148 | gulp.task('wiredep', function(){ |
| 149 | var bowerDeps = wiredep().js; |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 150 | 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 Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 159 | return gulp.src(bowerDeps) |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 160 | .pipe(concat('xosContentProviderVendor.js')) |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 161 | .pipe(uglify()) |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 162 | .pipe(gulp.dest(options.static + 'vendor/')); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 163 | }); |
| 164 | |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 165 | gulp.task('lint', function () { |
| 166 | return gulp.src(['src/js/**/*.js']) |
| 167 | .pipe(eslint()) |
| 168 | .pipe(eslint.format()) |
| 169 | .pipe(eslint.failAfterError()); |
| 170 | }); |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 171 | |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 172 | gulp.task('wait', function (cb) { |
| 173 | // setTimeout could be any async task |
| 174 | setTimeout(function () { |
| 175 | cb(); |
| 176 | }, 1000); |
| 177 | }); |
| 178 | |
Matteo Scandolo | 262385f | 2016-08-08 09:28:14 -0700 | [diff] [blame] | 179 | gulp.task('tosca', function (cb) { |
| 180 | |
| 181 | // TOSCA to register the dashboard in the system |
| 182 | const dashboardJson = {}; |
| 183 | dashboardJson['ContentProvider'] = { |
| 184 | type: 'tosca.nodes.DashboardView', |
| 185 | properties: { |
| 186 | url: 'template:xosContentProvider' |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | // check for custom icons |
| 191 | if( |
| 192 | fs.existsSync(`${options.icon}/contentProvider-icon.png`) && |
| 193 | fs.existsSync(`${options.icon}/contentProvider-icon-active.png`) |
| 194 | ){ |
| 195 | dashboardJson['ContentProvider'].properties.custom_icon = true; |
| 196 | } |
| 197 | |
| 198 | const dashboardTosca = yaml.dump(dashboardJson).replace(/'/gmi, ''); |
| 199 | |
| 200 | // TOSCA to add the dashboard to the user |
| 201 | const userDashboardJson = {}; |
| 202 | userDashboardJson['contentProvider_dashboard'] = { |
| 203 | node: 'ContentProvider', |
| 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 Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 229 | gulp.task('build', function() { |
| 230 | runSequence( |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 231 | 'clean', |
| 232 | 'sass', |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 233 | 'templates', |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 234 | 'babel', |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 235 | 'scripts', |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 236 | 'wiredep', |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 237 | 'css', |
| 238 | 'copyCss', |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 239 | 'copyImages', |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 240 | 'copyHtml', |
Matteo Scandolo | 262385f | 2016-08-08 09:28:14 -0700 | [diff] [blame] | 241 | 'cleanTmp', |
| 242 | 'tosca' |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 243 | ); |
| 244 | }); |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 245 | }; |