Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | // BUILD |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 4 | // |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +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 | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 8 | // |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 9 | // The template are parsed and added to js with angular $templateCache |
| 10 | |
| 11 | var gulp = require('gulp'); |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 12 | var ngAnnotate = require('gulp-ng-annotate'); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 13 | var uglify = require('gulp-uglify'); |
| 14 | var templateCache = require('gulp-angular-templatecache'); |
| 15 | var runSequence = require('run-sequence'); |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 16 | var concat = require('gulp-concat'); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 17 | var del = require('del'); |
| 18 | var wiredep = require('wiredep'); |
Matteo Scandolo | 6d97264 | 2015-10-29 15:50:15 +0100 | [diff] [blame] | 19 | var angularFilesort = require('gulp-angular-filesort'); |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 20 | var _ = require('lodash'); |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 21 | var eslint = require('gulp-eslint'); |
| 22 | var inject = require('gulp-inject'); |
| 23 | var rename = require('gulp-rename'); |
| 24 | var replace = require('gulp-replace'); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 25 | |
Matteo Scandolo | 9a60726 | 2015-11-10 17:13:04 +0100 | [diff] [blame] | 26 | var TEMPLATE_FOOTER = `}]); |
| 27 | angular.module('xos.contentProvider').run(function($location){$location.path('/')}); |
| 28 | angular.bootstrap(angular.element('#xosContentProvider'), ['xos.contentProvider']);`; |
| 29 | |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 30 | module.exports = function(options){ |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 31 | |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 32 | // delete previous builded file |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 33 | gulp.task('clean', function(){ |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 34 | return del( |
| 35 | [options.dashboards + 'xosContentProvider.html'], |
| 36 | {force: true} |
| 37 | ); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 38 | }); |
| 39 | |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 40 | // compile and minify scripts |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 41 | gulp.task('scripts', function() { |
| 42 | return gulp.src([ |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 43 | options.tmp + '**/*.js' |
| 44 | ]) |
| 45 | .pipe(ngAnnotate()) |
| 46 | .pipe(angularFilesort()) |
| 47 | .pipe(concat('xosContentProvider.js')) |
| 48 | .pipe(uglify()) |
| 49 | .pipe(gulp.dest(options.static + 'js/')); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 50 | }); |
| 51 | |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 52 | // set templates in cache |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 53 | gulp.task('templates', function(){ |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 54 | return gulp.src('./src/templates/*.html') |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 55 | .pipe(templateCache({ |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 56 | module: 'xos.contentProvider', |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 57 | root: 'templates/', |
| 58 | templateFooter: TEMPLATE_FOOTER |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 59 | })) |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 60 | .pipe(gulp.dest(options.tmp)); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 61 | }); |
| 62 | |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 63 | // copy html index to Django Folder |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 64 | gulp.task('copyHtml', ['clean'], function(){ |
| 65 | return gulp.src(options.src + 'index.html') |
| 66 | // remove dev dependencies from html |
| 67 | .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, '')) |
| 68 | .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, '')) |
Matteo Scandolo | 7db0843 | 2015-11-06 18:49:33 +0100 | [diff] [blame] | 69 | .pipe(replace(/ng-app=".*"\s/, '')) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 70 | // injecting minified files |
| 71 | .pipe( |
| 72 | inject( |
| 73 | gulp.src([ |
| 74 | options.static + 'js/vendor/xosContentProviderVendor.js', |
| 75 | options.static + 'js/xosContentProvider.js' |
| 76 | ]) |
| 77 | ) |
| 78 | ) |
| 79 | .pipe(rename('xosContentProvider.html')) |
| 80 | .pipe(gulp.dest(options.dashboards)); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 81 | }); |
| 82 | |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 83 | // minify vendor js files |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 84 | gulp.task('wiredep', function(){ |
| 85 | var bowerDeps = wiredep().js; |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 86 | if(!bowerDeps){ |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | // remove angular (it's already loaded) |
| 91 | _.remove(bowerDeps, function(dep){ |
| 92 | return dep.indexOf('angular/angular.js') !== -1; |
| 93 | }); |
| 94 | |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 95 | return gulp.src(bowerDeps) |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 96 | .pipe(concat('xosContentProviderVendor.js')) |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 97 | .pipe(uglify()) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 98 | .pipe(gulp.dest(options.static + 'js/vendor/')); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 99 | }); |
| 100 | |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 101 | gulp.task('lint', function () { |
| 102 | return gulp.src(['src/js/**/*.js']) |
| 103 | .pipe(eslint()) |
| 104 | .pipe(eslint.format()) |
| 105 | .pipe(eslint.failAfterError()); |
| 106 | }); |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 107 | |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 108 | gulp.task('build', function() { |
| 109 | runSequence( |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 110 | 'templates', |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 111 | 'babel', |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 112 | 'scripts', |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 113 | 'wiredep', |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 114 | 'copyHtml', |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 115 | 'cleanTmp' |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 116 | ); |
| 117 | }); |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 118 | }; |