blob: 6eb20af960661608dc5443e390bd363944ab6a0f [file] [log] [blame]
Matteo Scandoloe53ee052015-11-03 14:32:00 +01001'use strict';
2
3// BUILD
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +01004//
Matteo Scandoloe53ee052015-11-03 14:32:00 +01005// 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 Scandoloe3bc18d2015-11-05 18:36:10 +01008//
Matteo Scandoloe53ee052015-11-03 14:32:00 +01009// The template are parsed and added to js with angular $templateCache
10
11var gulp = require('gulp');
Matteo Scandolod838fac2015-11-04 16:10:09 +010012var ngAnnotate = require('gulp-ng-annotate');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010013var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
Matteo Scandolodd55c132016-04-13 10:46:08 -070016var concat = require('gulp-concat-util');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010017var del = require('del');
18var wiredep = require('wiredep');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010019var angularFilesort = require('gulp-angular-filesort');
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +010020var _ = require('lodash');
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010021var eslint = require('gulp-eslint');
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010022var inject = require('gulp-inject');
23var rename = require('gulp-rename');
Matteo Scandolo61a714d2015-11-06 11:16:12 +010024var replace = require('gulp-replace');
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -070025var postcss = require('gulp-postcss');
26var autoprefixer = require('autoprefixer');
27var mqpacker = require('css-mqpacker');
28var csswring = require('csswring');
Matteo Scandoloeaa69222016-08-05 15:24:28 -070029var yaml = require('js-yaml');
30var colors = require('colors/safe');
Matteo Scandolo262385f2016-08-08 09:28:14 -070031var fs = require('fs');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010032
Matteo Scandolodd55c132016-04-13 10:46:08 -070033const TEMPLATE_FOOTER = `
34angular.module('xos.<%= name %>')
35.run(['$location', function(a){
36 a.path('/');
37}])
38`
39
Matteo Scandoloe53ee052015-11-03 14:32:00 +010040module.exports = function(options){
41
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010042 // delete previous builded file
Matteo Scandoloe53ee052015-11-03 14:32:00 +010043 gulp.task('clean', function(){
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010044 return del(
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -070045 [
46 options.dashboards + 'xos<%= fileName %>.html',
Arpit Agarwalfecac782016-07-18 17:30:32 -070047 options.static + 'css/xos<%= fileName %>.css',
48 options.static + 'images/<%= name %>-icon.png',
49 options.static + 'images/<%= name %>-icon-active.png'
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -070050 ],
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010051 {force: true}
52 );
Matteo Scandoloe53ee052015-11-03 14:32:00 +010053 });
54
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070055 // 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 Scandolo7ae7d8d2016-04-13 11:41:31 -070072 gulp.task('copyCss', ['wait'], function(){
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070073 return gulp.src([`${options.tmp}/css/*.css`])
Matteo Scandolodd55c132016-04-13 10:46:08 -070074 .pipe(concat('xos<%= fileName %>.css'))
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070075 .pipe(gulp.dest(options.static + 'css/'))
76 });
77
Arpit Agarwalfecac782016-07-18 17:30:32 -070078 // copy images in correct folder
79 gulp.task('copyImages', ['wait'], function(){
Matteo Scandolo262385f2016-08-08 09:28:14 -070080 return gulp.src([`${options.icon}/<%= name %>-icon.png`, `${options.icon}/<%= name %>-icon-active.png`])
Arpit Agarwalfecac782016-07-18 17:30:32 -070081 .pipe(gulp.dest(options.static + 'images/'))
82 });
83
Matteo Scandoloe53ee052015-11-03 14:32:00 +010084 // compile and minify scripts
85 gulp.task('scripts', function() {
86 return gulp.src([
Matteo Scandolo8a1f2362015-11-06 09:52:01 +010087 options.tmp + '**/*.js'
88 ])
89 .pipe(ngAnnotate())
90 .pipe(angularFilesort())
91 .pipe(concat('xos<%= fileName %>.js'))
Matteo Scandolodd55c132016-04-13 10:46:08 -070092 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
93 .pipe(concat.footer(TEMPLATE_FOOTER))
Matteo Scandolo8a1f2362015-11-06 09:52:01 +010094 .pipe(uglify())
95 .pipe(gulp.dest(options.static + 'js/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +010096 });
97
98 // set templates in cache
99 gulp.task('templates', function(){
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100100 return gulp.src('./src/templates/*.html')
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100101 .pipe(templateCache({
Matteo Scandolo6be0cd22015-11-03 16:27:07 +0100102 module: 'xos.<%= name %>',
Matteo Scandolob7c91cf2016-03-29 14:23:20 -0700103 root: 'templates/'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100104 }))
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100105 .pipe(gulp.dest(options.tmp));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100106 });
107
Matteo Scandolo6be0cd22015-11-03 16:27:07 +0100108 // copy html index to Django Folder
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700109 gulp.task('copyHtml', function(){
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100110 return gulp.src(options.src + 'index.html')
Matteo Scandolo61a714d2015-11-06 11:16:12 +0100111 // remove dev dependencies from html
Matteo Scandolo34d35962016-05-10 14:58:47 -0700112 .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, ''))
113 .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, ''))
Matteo Scandolo61a714d2015-11-06 11:16:12 +0100114 // injecting minified files
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100115 .pipe(
116 inject(
117 gulp.src([
Matteo Scandolo195dde92016-07-25 16:43:16 -0700118 options.static + 'vendor/xos<%= fileName %>Vendor.js',
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700119 options.static + 'js/xos<%= fileName %>.js',
120 options.static + 'css/xos<%= fileName %>.css'
Matteo Scandoloc0c94092016-02-02 17:25:18 -0800121 ]),
122 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100123 )
124 )
125 .pipe(rename('xos<%= fileName %>.html'))
126 .pipe(gulp.dest(options.dashboards));
127 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100128
129 // minify vendor js files
130 gulp.task('wiredep', function(){
131 var bowerDeps = wiredep().js;
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100132 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 Scandoloe53ee052015-11-03 14:32:00 +0100141 return gulp.src(bowerDeps)
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100142 .pipe(concat('xos<%= fileName %>Vendor.js'))
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100143 .pipe(uglify())
Matteo Scandolo195dde92016-07-25 16:43:16 -0700144 .pipe(gulp.dest(options.static + 'vendor/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100145 });
146
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100147 gulp.task('lint', function () {
148 return gulp.src(['src/js/**/*.js'])
149 .pipe(eslint())
150 .pipe(eslint.format())
151 .pipe(eslint.failAfterError());
152 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100153
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700154 gulp.task('wait', function (cb) {
155 // setTimeout could be any async task
156 setTimeout(function () {
157 cb();
158 }, 1000);
159 });
160
Matteo Scandoloeaa69222016-08-05 15:24:28 -0700161 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 Scandolo262385f2016-08-08 09:28:14 -0700171
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 Scandoloeaa69222016-08-05 15:24:28 -0700180 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 Scandoloe53ee052015-11-03 14:32:00 +0100211 gulp.task('build', function() {
212 runSequence(
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700213 'clean',
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700214 'sass',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100215 'templates',
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100216 'babel',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100217 'scripts',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100218 'wiredep',
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700219 'css',
220 'copyCss',
Arpit Agarwalfecac782016-07-18 17:30:32 -0700221 'copyImages',
Matteo Scandolo8a1f2362015-11-06 09:52:01 +0100222 'copyHtml',
Matteo Scandoloeaa69222016-08-05 15:24:28 -0700223 'cleanTmp',
224 'tosca'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100225 );
226 });
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100227};