blob: e53042c31eee260c4148f4103bd748a815855e35 [file] [log] [blame]
Matteo Scandoloecf088a2016-10-20 10:25:34 +02001'use strict';
2
3// BUILD
4//
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
8//
9// The template are parsed and added to js with angular $templateCache
10
11var gulp = require('gulp');
12var ngAnnotate = require('gulp-ng-annotate');
13var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
16var concat = require('gulp-concat-util');
17var del = require('del');
18var wiredep = require('wiredep');
19var angularFilesort = require('gulp-angular-filesort');
20var _ = require('lodash');
21var eslint = require('gulp-eslint');
22var inject = require('gulp-inject');
23var rename = require('gulp-rename');
24var replace = require('gulp-replace');
25var postcss = require('gulp-postcss');
26var autoprefixer = require('autoprefixer');
27var mqpacker = require('css-mqpacker');
28var csswring = require('csswring');
29var yaml = require('js-yaml');
30var colors = require('colors/safe');
31var fs = require('fs');
32
33const TEMPLATE_FOOTER = `
34angular.module('xos.globalXos')
35.run(['$location', function(a){
36 a.path('/');
37}])
38`
39
40module.exports = function(options){
41
42 // delete previous builded file
43 gulp.task('clean', function(){
44 return del(
45 [
46 options.dashboards + 'xosGlobalXos.html',
47 options.static + 'css/xosGlobalXos.css',
48 options.static + 'images/globalXos-icon.png',
49 options.static + 'images/globalXos-icon-active.png'
50 ],
51 {force: true}
52 );
53 });
54
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
72 gulp.task('copyCss', ['wait'], function(){
73 return gulp.src([`${options.tmp}/css/*.css`])
74 .pipe(concat('xosGlobalXos.css'))
75 .pipe(gulp.dest(options.static + 'css/'))
76 });
77
78 // copy images in correct folder
79 gulp.task('copyImages', ['wait'], function(){
80 return gulp.src([`${options.icon}/globalXos-icon.png`, `${options.icon}/globalXos-icon-active.png`])
81 .pipe(gulp.dest(options.static + 'images/'))
82 });
83
84 // compile and minify scripts
85 gulp.task('scripts', function() {
86 return gulp.src([
87 options.tmp + '**/*.js'
88 ])
89 .pipe(ngAnnotate())
90 .pipe(angularFilesort())
91 .pipe(concat('xosGlobalXos.js'))
92 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
93 .pipe(concat.footer(TEMPLATE_FOOTER))
94 .pipe(uglify())
95 .pipe(gulp.dest(options.static + 'js/'));
96 });
97
98 // set templates in cache
99 gulp.task('templates', function(){
100 return gulp.src('./src/templates/*.html')
101 .pipe(templateCache({
102 module: 'xos.globalXos',
103 root: 'templates/'
104 }))
105 .pipe(gulp.dest(options.tmp));
106 });
107
108 // copy html index to Django Folder
109 gulp.task('copyHtml', function(){
110 return gulp.src(options.src + 'index.html')
111 // remove dev dependencies from html
112 .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, ''))
113 .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, ''))
114 // injecting minified files
115 .pipe(
116 inject(
117 gulp.src([
118 options.static + 'vendor/xosGlobalXosVendor.js',
119 options.static + 'js/xosGlobalXos.js',
120 options.static + 'css/xosGlobalXos.css'
121 ]),
122 {ignorePath: '/../../../xos/core/xoslib'}
123 )
124 )
125 .pipe(rename('xosGlobalXos.html'))
126 .pipe(gulp.dest(options.dashboards));
127 });
128
129 // minify vendor js files
130 gulp.task('wiredep', function(){
131 var bowerDeps = wiredep().js;
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
141 return gulp.src(bowerDeps)
142 .pipe(concat('xosGlobalXosVendor.js'))
143 .pipe(uglify())
144 .pipe(gulp.dest(options.static + 'vendor/'));
145 });
146
147 gulp.task('lint', function () {
148 return gulp.src(['src/js/**/*.js'])
149 .pipe(eslint())
150 .pipe(eslint.format())
151 .pipe(eslint.failAfterError());
152 });
153
154 gulp.task('wait', function (cb) {
155 // setTimeout could be any async task
156 setTimeout(function () {
157 cb();
158 }, 1000);
159 });
160
161 gulp.task('tosca', function (cb) {
162
163 // TOSCA to register the dashboard in the system
164 const dashboardJson = {};
165 dashboardJson['GlobalXos'] = {
166 type: 'tosca.nodes.DashboardView',
167 properties: {
168 url: 'template:xosGlobalXos'
169 }
170 };
171
172 // check for custom icons
173 if(
174 fs.existsSync(`${options.icon}/globalXos-icon.png`) &&
175 fs.existsSync(`${options.icon}/globalXos-icon-active.png`)
176 ){
177 dashboardJson['GlobalXos'].properties.custom_icon = true;
178 }
179
180 const dashboardTosca = yaml.dump(dashboardJson).replace(/'/gmi, '');
181
182 // TOSCA to add the dashboard to the user
183 const userDashboardJson = {};
184 userDashboardJson['globalXos_dashboard'] = {
185 node: 'GlobalXos',
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
211 gulp.task('build', function() {
212 runSequence(
213 'clean',
214 'sass',
215 'templates',
216 'babel',
217 'scripts',
218 'wiredep',
219 'css',
220 'copyCss',
221 'copyImages',
222 'copyHtml',
223 'cleanTmp',
224 'tosca'
225 );
226 });
227};