Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | // BUILD |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 4 | // |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 5 | // The only purpose of this gulpfile is to build a XOS view and copy the correct files into |
| 6 | // .html => dashboards |
| 7 | // .js (minified and concat) => static/js |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 8 | // |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 9 | // The template are parsed and added to js with angular $templateCache |
| 10 | |
| 11 | var gulp = require('gulp'); |
Matteo Scandolo | d838fac | 2015-11-04 16:10:09 +0100 | [diff] [blame] | 12 | var ngAnnotate = require('gulp-ng-annotate'); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 13 | var uglify = require('gulp-uglify'); |
| 14 | var templateCache = require('gulp-angular-templatecache'); |
| 15 | var runSequence = require('run-sequence'); |
Matteo Scandolo | dd55c13 | 2016-04-13 10:46:08 -0700 | [diff] [blame] | 16 | var concat = require('gulp-concat-util'); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 17 | var del = require('del'); |
| 18 | var wiredep = require('wiredep'); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 19 | var angularFilesort = require('gulp-angular-filesort'); |
Matteo Scandolo | cb7ea1e | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 20 | var _ = require('lodash'); |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 21 | var eslint = require('gulp-eslint'); |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 22 | var inject = require('gulp-inject'); |
| 23 | var rename = require('gulp-rename'); |
Matteo Scandolo | 61a714d | 2015-11-06 11:16:12 +0100 | [diff] [blame] | 24 | var replace = require('gulp-replace'); |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 25 | var postcss = require('gulp-postcss'); |
| 26 | var autoprefixer = require('autoprefixer'); |
| 27 | var mqpacker = require('css-mqpacker'); |
| 28 | var csswring = require('csswring'); |
Matteo Scandolo | eaa6922 | 2016-08-05 15:24:28 -0700 | [diff] [blame] | 29 | var yaml = require('js-yaml'); |
| 30 | var colors = require('colors/safe'); |
Matteo Scandolo | 262385f | 2016-08-08 09:28:14 -0700 | [diff] [blame] | 31 | var fs = require('fs'); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 32 | |
Matteo Scandolo | dd55c13 | 2016-04-13 10:46:08 -0700 | [diff] [blame] | 33 | const TEMPLATE_FOOTER = ` |
| 34 | angular.module('xos.<%= name %>') |
| 35 | .run(['$location', function(a){ |
| 36 | a.path('/'); |
| 37 | }]) |
| 38 | ` |
| 39 | |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 40 | module.exports = function(options){ |
| 41 | |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 42 | // delete previous builded file |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 43 | gulp.task('clean', function(){ |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 44 | return del( |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 45 | [ |
| 46 | options.dashboards + 'xos<%= fileName %>.html', |
Arpit Agarwal | fecac78 | 2016-07-18 17:30:32 -0700 | [diff] [blame] | 47 | options.static + 'css/xos<%= fileName %>.css', |
| 48 | options.static + 'images/<%= name %>-icon.png', |
| 49 | options.static + 'images/<%= name %>-icon-active.png' |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 50 | ], |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 51 | {force: true} |
| 52 | ); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 53 | }); |
| 54 | |
Matteo Scandolo | b7c91cf | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 55 | // minify css |
| 56 | gulp.task('css', function () { |
| 57 | var processors = [ |
| 58 | autoprefixer({browsers: ['last 1 version']}), |
| 59 | mqpacker, |
| 60 | csswring |
| 61 | ]; |
| 62 | |
| 63 | gulp.src([ |
| 64 | `${options.css}**/*.css`, |
| 65 | `!${options.css}dev.css` |
| 66 | ]) |
| 67 | .pipe(postcss(processors)) |
| 68 | .pipe(gulp.dest(options.tmp + '/css/')); |
| 69 | }); |
| 70 | |
| 71 | // copy css in correct folder |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 72 | gulp.task('copyCss', ['wait'], function(){ |
Matteo Scandolo | b7c91cf | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 73 | return gulp.src([`${options.tmp}/css/*.css`]) |
Matteo Scandolo | dd55c13 | 2016-04-13 10:46:08 -0700 | [diff] [blame] | 74 | .pipe(concat('xos<%= fileName %>.css')) |
Matteo Scandolo | b7c91cf | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 75 | .pipe(gulp.dest(options.static + 'css/')) |
| 76 | }); |
| 77 | |
Arpit Agarwal | fecac78 | 2016-07-18 17:30:32 -0700 | [diff] [blame] | 78 | // copy images in correct folder |
| 79 | gulp.task('copyImages', ['wait'], function(){ |
Matteo Scandolo | 262385f | 2016-08-08 09:28:14 -0700 | [diff] [blame] | 80 | return gulp.src([`${options.icon}/<%= name %>-icon.png`, `${options.icon}/<%= name %>-icon-active.png`]) |
Arpit Agarwal | fecac78 | 2016-07-18 17:30:32 -0700 | [diff] [blame] | 81 | .pipe(gulp.dest(options.static + 'images/')) |
| 82 | }); |
| 83 | |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 84 | // compile and minify scripts |
| 85 | gulp.task('scripts', function() { |
| 86 | return gulp.src([ |
Matteo Scandolo | 8a1f236 | 2015-11-06 09:52:01 +0100 | [diff] [blame] | 87 | options.tmp + '**/*.js' |
| 88 | ]) |
| 89 | .pipe(ngAnnotate()) |
| 90 | .pipe(angularFilesort()) |
| 91 | .pipe(concat('xos<%= fileName %>.js')) |
Matteo Scandolo | dd55c13 | 2016-04-13 10:46:08 -0700 | [diff] [blame] | 92 | .pipe(concat.header('//Autogenerated, do not edit!!!\n')) |
| 93 | .pipe(concat.footer(TEMPLATE_FOOTER)) |
Matteo Scandolo | 8a1f236 | 2015-11-06 09:52:01 +0100 | [diff] [blame] | 94 | .pipe(uglify()) |
| 95 | .pipe(gulp.dest(options.static + 'js/')); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 96 | }); |
| 97 | |
| 98 | // set templates in cache |
| 99 | gulp.task('templates', function(){ |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 100 | return gulp.src('./src/templates/*.html') |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 101 | .pipe(templateCache({ |
Matteo Scandolo | 6be0cd2 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 102 | module: 'xos.<%= name %>', |
Matteo Scandolo | b7c91cf | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 103 | root: 'templates/' |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 104 | })) |
Matteo Scandolo | ce1dd31 | 2015-11-04 11:46:54 +0100 | [diff] [blame] | 105 | .pipe(gulp.dest(options.tmp)); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 106 | }); |
| 107 | |
Matteo Scandolo | 6be0cd2 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 108 | // copy html index to Django Folder |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 109 | gulp.task('copyHtml', function(){ |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 110 | return gulp.src(options.src + 'index.html') |
Matteo Scandolo | 61a714d | 2015-11-06 11:16:12 +0100 | [diff] [blame] | 111 | // remove dev dependencies from html |
Matteo Scandolo | 34d3596 | 2016-05-10 14:58:47 -0700 | [diff] [blame] | 112 | .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, '')) |
| 113 | .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, '')) |
Matteo Scandolo | 61a714d | 2015-11-06 11:16:12 +0100 | [diff] [blame] | 114 | // injecting minified files |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 115 | .pipe( |
| 116 | inject( |
| 117 | gulp.src([ |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 118 | options.static + 'vendor/xos<%= fileName %>Vendor.js', |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 119 | options.static + 'js/xos<%= fileName %>.js', |
| 120 | options.static + 'css/xos<%= fileName %>.css' |
Matteo Scandolo | c0c9409 | 2016-02-02 17:25:18 -0800 | [diff] [blame] | 121 | ]), |
| 122 | {ignorePath: '/../../../xos/core/xoslib'} |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 123 | ) |
| 124 | ) |
| 125 | .pipe(rename('xos<%= fileName %>.html')) |
| 126 | .pipe(gulp.dest(options.dashboards)); |
| 127 | }); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 128 | |
| 129 | // minify vendor js files |
| 130 | gulp.task('wiredep', function(){ |
| 131 | var bowerDeps = wiredep().js; |
Matteo Scandolo | cb7ea1e | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 132 | if(!bowerDeps){ |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | // remove angular (it's already loaded) |
| 137 | _.remove(bowerDeps, function(dep){ |
| 138 | return dep.indexOf('angular/angular.js') !== -1; |
| 139 | }); |
| 140 | |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 141 | return gulp.src(bowerDeps) |
Matteo Scandolo | cb7ea1e | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 142 | .pipe(concat('xos<%= fileName %>Vendor.js')) |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 143 | .pipe(uglify()) |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 144 | .pipe(gulp.dest(options.static + 'vendor/')); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 145 | }); |
| 146 | |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 147 | gulp.task('lint', function () { |
| 148 | return gulp.src(['src/js/**/*.js']) |
| 149 | .pipe(eslint()) |
| 150 | .pipe(eslint.format()) |
| 151 | .pipe(eslint.failAfterError()); |
| 152 | }); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 153 | |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 154 | gulp.task('wait', function (cb) { |
| 155 | // setTimeout could be any async task |
| 156 | setTimeout(function () { |
| 157 | cb(); |
| 158 | }, 1000); |
| 159 | }); |
| 160 | |
Matteo Scandolo | eaa6922 | 2016-08-05 15:24:28 -0700 | [diff] [blame] | 161 | gulp.task('tosca', function (cb) { |
| 162 | |
| 163 | // TOSCA to register the dashboard in the system |
| 164 | const dashboardJson = {}; |
| 165 | dashboardJson['<%= fileName %>'] = { |
| 166 | type: 'tosca.nodes.DashboardView', |
| 167 | properties: { |
| 168 | url: 'template:xos<%= fileName %>' |
| 169 | } |
| 170 | }; |
Matteo Scandolo | 262385f | 2016-08-08 09:28:14 -0700 | [diff] [blame] | 171 | |
| 172 | // check for custom icons |
| 173 | if( |
| 174 | fs.existsSync(`${options.icon}/<%= name %>-icon.png`) && |
| 175 | fs.existsSync(`${options.icon}/<%= name %>-icon-active.png`) |
| 176 | ){ |
| 177 | dashboardJson['<%= fileName %>'].properties.custom_icon = true; |
| 178 | } |
| 179 | |
Matteo Scandolo | eaa6922 | 2016-08-05 15:24:28 -0700 | [diff] [blame] | 180 | const dashboardTosca = yaml.dump(dashboardJson).replace(/'/gmi, ''); |
| 181 | |
| 182 | // TOSCA to add the dashboard to the user |
| 183 | const userDashboardJson = {}; |
| 184 | userDashboardJson['<%= name %>_dashboard'] = { |
| 185 | node: '<%= fileName %>', |
| 186 | relationship: 'tosca.relationships.UsesDashboard' |
| 187 | }; |
| 188 | const userJson = { |
| 189 | 'padmin@vicci.org': { |
| 190 | type: 'tosca.nodes.User', |
| 191 | properties: { |
| 192 | 'no-create': true, |
| 193 | 'no-delete': true |
| 194 | }, |
| 195 | requirements: [userDashboardJson] |
| 196 | } |
| 197 | }; |
| 198 | const userTosca = yaml.dump(userJson).replace(/'/gmi, ''); |
| 199 | |
| 200 | |
| 201 | // the output is in a timeout so that it get printed after the gulp logs |
| 202 | setTimeout(function () { |
| 203 | console.log(colors.cyan('\n\n# You can use this recipe to load the dashboard in the system:')); |
| 204 | console.log(colors.yellow(dashboardTosca)); |
| 205 | console.log(colors.cyan('# And this recipe to activate the dashboard for a user:')); |
| 206 | console.log(colors.yellow(userTosca)); |
| 207 | }, 1000); |
| 208 | cb(); |
| 209 | }); |
| 210 | |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 211 | gulp.task('build', function() { |
| 212 | runSequence( |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 213 | 'clean', |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 214 | 'sass', |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 215 | 'templates', |
Matteo Scandolo | ce1dd31 | 2015-11-04 11:46:54 +0100 | [diff] [blame] | 216 | 'babel', |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 217 | 'scripts', |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 218 | 'wiredep', |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 219 | 'css', |
| 220 | 'copyCss', |
Arpit Agarwal | fecac78 | 2016-07-18 17:30:32 -0700 | [diff] [blame] | 221 | 'copyImages', |
Matteo Scandolo | 8a1f236 | 2015-11-06 09:52:01 +0100 | [diff] [blame] | 222 | 'copyHtml', |
Matteo Scandolo | eaa6922 | 2016-08-05 15:24:28 -0700 | [diff] [blame] | 223 | 'cleanTmp', |
| 224 | 'tosca' |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 225 | ); |
| 226 | }); |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 227 | }; |