forked from ets-berkeley-edu/suitec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
301 lines (265 loc) · 8.97 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* Copyright ©2017. The Regents of the University of California (Regents). All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software and its documentation
* for educational, research, and not-for-profit purposes, without fee and without a
* signed licensing agreement, is hereby granted, provided that the above copyright
* notice, this paragraph and the following two paragraphs appear in all copies,
* modifications, and distributions.
*
* Contact The Office of Technology Licensing, UC Berkeley, 2150 Shattuck Avenue,
* Suite 510, Berkeley, CA 94720-1620, (510) 643-7201, [email protected],
* http://ipira.berkeley.edu/industry-info for commercial licensing opportunities.
*
* IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF
* THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
* SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED
* "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
* ENHANCEMENTS, OR MODIFICATIONS.
*/
var addsrc = require('gulp-add-src');
var csslint = require('gulp-csslint');
var cssmin = require('gulp-cssmin');
var del = require('del');
var es = require('event-stream');
var filter = require('gulp-filter');
var fs = require('fs');
var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var jscs = require('gulp-jscs');
var minifyHtml = require('gulp-htmlmin');
var mocha = require('gulp-mocha');
var ngAnnotate = require('gulp-ng-annotate');
var rev = require('gulp-rev');
var revReplace = require('gulp-rev-replace');
var runSequence = require('run-sequence');
var templateCache = require('gulp-angular-templatecache');
var uglify = require('gulp-uglify');
var usemin = require('gulp-usemin');
/**
* Delete the build directory
*/
gulp.task('clean', function(cb) {
del(['target/*']).then(function() {
return cb();
});
});
/**
* Copy the fonts to the build directory
*/
gulp.task('copyFonts', function() {
return gulp.src('public/lib/fontawesome/fonts/*')
.pipe(gulp.dest('target/fonts/'));
});
/**
* Copy the bookmarklet files. Note that these files cannot be versioned as they are part of the
* javascript logic that gets copied in the bookmarks bar
*/
gulp.task('copyBookmarkletFiles', function() {
return es.merge(
gulp.src('public/assets/js/bookmarklet-init.js', {'base': 'public'})
.pipe(uglify())
.pipe(gulp.dest('target')),
gulp.src('public/bookmarklet.html', {'base': 'public'})
.pipe(minifyHtml({'empty': true}))
.pipe(gulp.dest('target'))
);
});
/**
* Copy Canvas customization code. Do not version or minify, since this code serves as a public reference.
*/
gulp.task('copyCanvasCustomization', function() {
return gulp.src('public/assets/js/canvas-customization.js', {'base': 'public'})
.pipe(gulp.dest('target'));
});
/**
* Copy the bookmarklet dependencies to the build directory
*/
gulp.task('minifyBookmarkletFiles', ['copyBookmarkletFiles'], function() {
// Parse the dependencies out of the bookmarklet init script
var contents = fs.readFileSync('./public/assets/js/bookmarklet-init.js').toString('utf8');
var re = new RegExp('baseUrl \\+ \'(.+?)"', 'g');
var matches = contents.match(re);
// Map the matched dependencies to their path on disk
matches = matches.map(function(match) {
return 'public/' + match.substring(12, match.length - 1);
});
var jsFilter = filter('**/*.js', {'restore': true});
var cssFilter = filter('**/*.css', {'restore': true});
// Hash and version the dependencies
return gulp.src(matches, {'base': 'public'})
// Hash the JS files
.pipe(jsFilter)
.pipe(uglify())
.pipe(rev())
.pipe(gulp.dest('target/static'))
.pipe(jsFilter.restore)
// Hash the CSS files
.pipe(cssFilter)
.pipe(cssmin({'keepSpecialComments': 0}))
.pipe(rev())
.pipe(gulp.dest('target/static/'))
.pipe(cssFilter.restore)
// Write out a file that maps the original filename to its hashed counterpart
.pipe(rev.manifest('bookmarklet-rev-manifest.json'))
.pipe(gulp.dest('target'));
});
/**
* Replace the dependencies in the bookmarklet init file with their optimized versions
*/
gulp.task('replaceBookmarkletDependencies', ['minifyBookmarkletFiles'], function() {
var manifest = gulp.src('./target/bookmarklet-rev-manifest.json');
return gulp.src('target/assets/js/*')
.pipe(revReplace({
'manifest': manifest,
'prefix': '/static'
}))
.pipe(gulp.dest('target/assets/js'));
});
/**
* Minify the HTML, CSS and JS assets
*/
gulp.task('minify', function() {
var pipelines = {
'css': [cssmin({'keepSpecialComments': 0}), rev()],
'html': [minifyHtml({'empty': true})],
// We need to register 2 pipelines with usemin as it's not able to re-use a pipeline
// for multiple result files
'vendor': [ngAnnotate(), uglify(), rev()],
'app': [ngAnnotate(), uglify(), rev()],
// Unfortunately, usemin has no way to determine the HTML partials from the index.html file.
// We have to explicitly specify a matching glob here. All HTML partials matching the glob
// will be returned and written to the templateCache.js
'templateCache': [
addsrc('public/app/**/*.html'),
templateCache('/static/templateCache.js', {
'module': 'collabosphere.templates',
'root': '/app',
'standalone': true
}),
rev()
]
};
return gulp.src('./public/index.html')
.pipe(usemin(pipelines))
.pipe(gulp.dest('target'));
});
/**
* Minify the viewer's HTML, CSS and JS assets
*/
gulp.task('minifyViewer', function() {
var pipelines = {
'css': [cssmin({'keepSpecialComments': 0}), rev()],
'html': [minifyHtml({'empty': true})],
'js': [uglify(), rev()]
};
return gulp.src('./public/viewer/*.html')
.pipe(usemin(pipelines))
.pipe(gulp.dest('target/viewer'));
});
/**
* Copy the viewer's static assets to the build directory
*/
gulp.task('copyViewerAssets', function() {
var dirs = [
'public/viewer/cmaps/**/*',
'public/viewer/images/**/*',
'public/viewer/locale/**/*',
'public/viewer/pdf.worker.js'
];
return gulp.src(dirs, {'base': 'public/viewer'})
.pipe(gulp.dest('target/viewer'));
});
/**
* Optimize the images
*/
gulp.task('optimizeImages', function() {
return gulp.src('public/assets/img/*', {'base': 'public'})
// Optimize the images
.pipe(imagemin({
'progressive': true
}))
// Hash each image and append a version string at the end of the file
.pipe(rev())
.pipe(gulp.dest('target/static/'))
// Write out a manifest file so we can replace the image URLs in the CSS / html files
.pipe(rev.manifest('images-rev-manifest.json'))
.pipe(gulp.dest('target'));
});
/**
* Replace the images with their optimized versions
*/
gulp.task('replaceImages', ['optimizeImages'], function() {
var manifest = gulp.src('./target/images-rev-manifest.json');
return gulp.src('target/static/*')
.pipe(revReplace({
'manifest': manifest,
'prefix': '/static'
}))
.pipe(gulp.dest('target/static'));
});
/**
* Create a build
*/
gulp.task('build', function() {
return runSequence('clean', ['replaceBookmarkletDependencies', 'copyFonts', 'minify'], 'copyCanvasCustomization', 'replaceImages', 'minifyViewer', 'copyViewerAssets');
});
/**
* Run the JSCS code style linter
*/
gulp.task('jscs', function() {
return gulp
.src(['app.js', 'gulpfile.js', 'apache/**/*.js', 'node_modules/col-*/**/*.js', 'public/**/*.js', '!public/lib/**/*.js', '!public/viewer/**/*.js'])
.pipe(jscs());
});
/**
* Run the CSS code style linter
*/
gulp.task('csslint', function() {
return gulp
.src(['public/**/*.css', '!public/lib/**/*.css', '!public/viewer/**/*.css'])
.pipe(csslint({
'adjoining-classes': false,
'box-model': false,
'ids': false,
'overqualified-elements': false,
'qualified-headings': false
}))
.pipe(csslint.reporter());
});
/**
* Run the Mocha test suite
*/
gulp.task('mocha', function() {
return gulp
.src(['node_modules/col-tests/lib/beforeTests.js', 'node_modules/col-*/tests/**/*.js'])
.pipe(mocha({
'fullStackTrace': true,
'grep': process.env.MOCHA_GREP,
'timeout': 10000
}))
.once('end', function() {
process.exit();
});
});
/**
* Run the full Collabosphere test suite
*/
gulp.task('test', function() {
// Set the environment to `test`
process.env.NODE_ENV = 'test';
runSequence('jscs', 'csslint', 'mocha');
});
/**
* Run the full Collabosphere TravisCI test suite (including code coverage)
*/
gulp.task('test-travis', function() {
// Set the environment to `travis`
process.env.NODE_ENV = 'travis';
runSequence('jscs', 'csslint', 'mocha');
});