blob: f9162129033ca6d3dd014c54334dce76e5558f91 [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 Scandoloe53ee052015-11-03 14:32:00 +010031
Matteo Scandolodd55c132016-04-13 10:46:08 -070032const TEMPLATE_FOOTER = `
33angular.module('xos.<%= name %>')
34.run(['$location', function(a){
35 a.path('/');
36}])
37`
38
Matteo Scandoloe53ee052015-11-03 14:32:00 +010039module.exports = function(options){
40
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010041 // delete previous builded file
Matteo Scandoloe53ee052015-11-03 14:32:00 +010042 gulp.task('clean', function(){
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010043 return del(
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -070044 [
45 options.dashboards + 'xos<%= fileName %>.html',
Arpit Agarwalfecac782016-07-18 17:30:32 -070046 options.static + 'css/xos<%= fileName %>.css',
47 options.static + 'images/<%= name %>-icon.png',
48 options.static + 'images/<%= name %>-icon-active.png'
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -070049 ],
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010050 {force: true}
51 );
Matteo Scandoloe53ee052015-11-03 14:32:00 +010052 });
53
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070054 // minify css
55 gulp.task('css', function () {
56 var processors = [
57 autoprefixer({browsers: ['last 1 version']}),
58 mqpacker,
59 csswring
60 ];
61
62 gulp.src([
63 `${options.css}**/*.css`,
64 `!${options.css}dev.css`
65 ])
66 .pipe(postcss(processors))
67 .pipe(gulp.dest(options.tmp + '/css/'));
68 });
69
70 // copy css in correct folder
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -070071 gulp.task('copyCss', ['wait'], function(){
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070072 return gulp.src([`${options.tmp}/css/*.css`])
Matteo Scandolodd55c132016-04-13 10:46:08 -070073 .pipe(concat('xos<%= fileName %>.css'))
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070074 .pipe(gulp.dest(options.static + 'css/'))
75 });
76
Arpit Agarwalfecac782016-07-18 17:30:32 -070077 // copy images in correct folder
78 gulp.task('copyImages', ['wait'], function(){
79 return gulp.src([`${options.icon}/<%= name %>-icon.png`,`${options.icon}/<%= name %>-icon-active.png`])
80 .pipe(gulp.dest(options.static + 'images/'))
81 });
82
Matteo Scandoloe53ee052015-11-03 14:32:00 +010083 // compile and minify scripts
84 gulp.task('scripts', function() {
85 return gulp.src([
Matteo Scandolo8a1f2362015-11-06 09:52:01 +010086 options.tmp + '**/*.js'
87 ])
88 .pipe(ngAnnotate())
89 .pipe(angularFilesort())
90 .pipe(concat('xos<%= fileName %>.js'))
Matteo Scandolodd55c132016-04-13 10:46:08 -070091 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
92 .pipe(concat.footer(TEMPLATE_FOOTER))
Matteo Scandolo8a1f2362015-11-06 09:52:01 +010093 .pipe(uglify())
94 .pipe(gulp.dest(options.static + 'js/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +010095 });
96
97 // set templates in cache
98 gulp.task('templates', function(){
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010099 return gulp.src('./src/templates/*.html')
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100100 .pipe(templateCache({
Matteo Scandolo6be0cd22015-11-03 16:27:07 +0100101 module: 'xos.<%= name %>',
Matteo Scandolob7c91cf2016-03-29 14:23:20 -0700102 root: 'templates/'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100103 }))
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100104 .pipe(gulp.dest(options.tmp));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100105 });
106
Matteo Scandolo6be0cd22015-11-03 16:27:07 +0100107 // copy html index to Django Folder
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700108 gulp.task('copyHtml', function(){
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100109 return gulp.src(options.src + 'index.html')
Matteo Scandolo61a714d2015-11-06 11:16:12 +0100110 // remove dev dependencies from html
Matteo Scandolo34d35962016-05-10 14:58:47 -0700111 .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, ''))
112 .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, ''))
Matteo Scandolo61a714d2015-11-06 11:16:12 +0100113 // injecting minified files
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100114 .pipe(
115 inject(
116 gulp.src([
Matteo Scandolo195dde92016-07-25 16:43:16 -0700117 options.static + 'vendor/xos<%= fileName %>Vendor.js',
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700118 options.static + 'js/xos<%= fileName %>.js',
119 options.static + 'css/xos<%= fileName %>.css'
Matteo Scandoloc0c94092016-02-02 17:25:18 -0800120 ]),
121 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100122 )
123 )
124 .pipe(rename('xos<%= fileName %>.html'))
125 .pipe(gulp.dest(options.dashboards));
126 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100127
128 // minify vendor js files
129 gulp.task('wiredep', function(){
130 var bowerDeps = wiredep().js;
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100131 if(!bowerDeps){
132 return;
133 }
134
135 // remove angular (it's already loaded)
136 _.remove(bowerDeps, function(dep){
137 return dep.indexOf('angular/angular.js') !== -1;
138 });
139
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100140 return gulp.src(bowerDeps)
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100141 .pipe(concat('xos<%= fileName %>Vendor.js'))
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100142 .pipe(uglify())
Matteo Scandolo195dde92016-07-25 16:43:16 -0700143 .pipe(gulp.dest(options.static + 'vendor/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100144 });
145
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100146 gulp.task('lint', function () {
147 return gulp.src(['src/js/**/*.js'])
148 .pipe(eslint())
149 .pipe(eslint.format())
150 .pipe(eslint.failAfterError());
151 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100152
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700153 gulp.task('wait', function (cb) {
154 // setTimeout could be any async task
155 setTimeout(function () {
156 cb();
157 }, 1000);
158 });
159
Matteo Scandoloeaa69222016-08-05 15:24:28 -0700160 gulp.task('tosca', function (cb) {
161
162 // TOSCA to register the dashboard in the system
163 const dashboardJson = {};
164 dashboardJson['<%= fileName %>'] = {
165 type: 'tosca.nodes.DashboardView',
166 properties: {
167 url: 'template:xos<%= fileName %>'
168 }
169 };
170 const dashboardTosca = yaml.dump(dashboardJson).replace(/'/gmi, '');
171
172 // TOSCA to add the dashboard to the user
173 const userDashboardJson = {};
174 userDashboardJson['<%= name %>_dashboard'] = {
175 node: '<%= fileName %>',
176 relationship: 'tosca.relationships.UsesDashboard'
177 };
178 const userJson = {
179 'padmin@vicci.org': {
180 type: 'tosca.nodes.User',
181 properties: {
182 'no-create': true,
183 'no-delete': true
184 },
185 requirements: [userDashboardJson]
186 }
187 };
188 const userTosca = yaml.dump(userJson).replace(/'/gmi, '');
189
190
191 // the output is in a timeout so that it get printed after the gulp logs
192 setTimeout(function () {
193 console.log(colors.cyan('\n\n# You can use this recipe to load the dashboard in the system:'));
194 console.log(colors.yellow(dashboardTosca));
195 console.log(colors.cyan('# And this recipe to activate the dashboard for a user:'));
196 console.log(colors.yellow(userTosca));
197 }, 1000);
198 cb();
199 });
200
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100201 gulp.task('build', function() {
202 runSequence(
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700203 'clean',
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700204 'sass',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100205 'templates',
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100206 'babel',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100207 'scripts',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100208 'wiredep',
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700209 'css',
210 'copyCss',
Arpit Agarwalfecac782016-07-18 17:30:32 -0700211 'copyImages',
Matteo Scandolo8a1f2362015-11-06 09:52:01 +0100212 'copyHtml',
Matteo Scandoloeaa69222016-08-05 15:24:28 -0700213 'cleanTmp',
214 'tosca'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100215 );
216 });
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100217};