forked from SchizoDuckie/DuckieTV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
401 lines (352 loc) · 13.1 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
* Gulp file (experimental) to concat all the scripts and minimize load time.
* Usage:
*
* npm install gulp gulp-autoprefixer gulp-minify-css gulp-jshint gulp-concat gulp-notify gulp-rename gulp-replace gulp-json-editor js-beautify request webdriverio --save-dev
* gulp
*
* to generate deployment packages:
*
* gulp deploy
*/
var gulp = require('gulp'),
autoprefixer = require('gulp-autoprefixer'),
minifycss = require('gulp-minify-css'),
jshint = require('gulp-jshint'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
concatCss = require('gulp-concat-css'),
replace = require('gulp-replace'),
notify = require('gulp-notify'),
jsonedit = require("gulp-json-editor"),
zip = require('gulp-zip'),
fs = require('fs'),
request = require('request'),
spawn = require('child_process').spawn;
var ver = String(fs.readFileSync('VERSION'));
var nightly = false; // for nightly builds
// scripts are provided in order to prevent any problems with the load order
var scripts = ['./js/ap*.js', './js/controllers/*.js', './js/controllers/*/*.js', './js/directives/*.js', './js/services/*.js', './js/services/*/*.js', '!./js/**/**/Nyaa.js'];
/**
* Dependencies for the app, will be rolled into deps.js
*/
var deps = [];
/**
* CSS files to be concatted. Note that there's separate code to include print.js
*/
var styles = [
'./css/bootstrap.min.css',
'./css/main.css',
'./css/anim.css',
'./css/dialogs.css',
'./css/flags.css'
];
/**
* Minimum app dependencies for background.js
*/
var background = [
"js/background.js"
];
/**
* Default and depoyment tasks:
* Concats scripts, dependencies, background page, styles, alters the main template to use dist versions and writes all of this the local dist/ directory
*/
gulp.task('default', ['concatScripts', 'concatDeps', 'concatBackgroundPage', 'concatStyles', 'print.css', 'launch.js', 'tabTemplate' /*, 'scenenames'*/ ], function() {
notify('packaging to dist/ done');
});
gulp.task('build-standalone', ['deploy'], function() {
var NwBuilder = require('node-webkit-builder');
var nw = new NwBuilder({
files: './dist/*.*', // use the glob format
platforms: ['win'], //, 'osx', 'linux32', 'linux64'],
buildDir: '../deploy/binaries',
cacheDir: '../deploy/cache',
buildType: 'versioned',
winIco: './img/favicon.ico'
});
// Log stuff you want
nw.on('log', console.log);
// Build returns a promise
return nw.build().then(function() {
console.log('all done!');
}).catch(function(error) {
console.error(error);
});
});
/**
* Create a nightly build and immediately deploy it to the webstore
*/
gulp.task('nightly', function() {
nightly = true;
var d = new Date();
ver = [d.getFullYear() + '' + d.getMonth(), d.getDate(), d.getHours(), d.getMinutes()].join('.');
spawn("java", ["-Dwebdriver.chrome.driver=..\\chromedriver.exe", ' -jar ..\\selenium-server-standalone-2.43.1.jar'], {
cwd: process.cwd()
});
gulp.start('deploy').start('webstore-nightly');
});
/**
* Push a nightly release to the webstore.
* These are
*/
gulp.task('webstore-nightly', function() {
var changelog = fs.readFileSync('./README.md').toString();
var start = changelog.indexOf('Changelog: \r\n==========');
changelog = changelog.substring(start + 26).split('*')[0];
var webdriverjs = require('webdriverio'),
client = webdriverjs.remote({
desiredCapabilities: {
browserName: 'chrome',
},
logLevel: 'error'
}).init();
// i fucking love this
client
.url('https://chrome.google.com/webstore/developer/dashboard?hl=en&gl=NL') // login
.waitFor('#Email')
.setValue('#Email', process.env.USERNAME)
.setValue('#Passwd', process.env.PWD || '');
if (process.env.PWD) {
client.click('#signIn'); // auto sign in if env user and pass provided
}
client.waitFor('form#cx-dash-form', 30000) // wait for redirect to dashboard
.url('https://chrome.google.com/webstore/developer/edit/gelgiagalkgliccemepngfeahpmihnjp?hl=en&gl=NL') // new tab mode canary version
.click('.cx-title input[type=button]') // hit upload new version
.waitFor('#browse-btn') // wait for page to load
.chooseFile('input[type=file]', '../deploy/newtab-nightly.zip') // select nightly
.click('#upload-btn') // hit upload
.waitFor('.id-publish', 30000) // wait for the redirect
.execute(function(c) { // adjust the changelog to include last update from readme
var el = document.querySelector('#cx-dev-edit-desc');
var value = el.value.split("Changelog:");
el.value = value[0] + "Changelog:\r\n* " + c.toString().trim() + "\r\n" + value[1].trim();
}, [changelog])
.click('.id-publish'); // hit the publish button
//.end(); // close the browser
});
/**
* Start the cascade to be able to create zip packages.
* This executes, via sequence dependencies:
* - default task
* - copy dist files and dependencies into 3 individual directories in ../deploy/
* - copy tab.html into place
* - adjust manifests to include version info and write that to ../deploy/<flavour>/manifest.json
* - zip files from ../deploy/<flavour>/ into ../deploy/<flavour>-<version>.zip
* - copy that file into ../deploy/<flavour>-latest.zip
*/
gulp.task('deploy', ['zipbrowseraction', 'zipnewtab', 'zipopera'], function() {
var latestTag = nightly ? 'nightly' : 'latest';
gulp.src('../deploy/newtab-' + ver + '.zip')
.pipe(rename('newtab-' + latestTag + '.zip'))
.pipe(gulp.dest('../deploy/'));
gulp.src('../deploy/browseraction-' + ver + '.zip')
.pipe(rename('browseraction-' + latestTag + '.zip'))
.pipe(gulp.dest('../deploy/'));
notify('DEPLOY done to ../deploy/ !');
});
gulp.task('scenenames', function() {
notify('downloading new scene name exceptions');
request('https://raw.githubusercontent.com/midgetspy/sb_tvdb_scene_exceptions/gh-pages/exceptions.txt', function(error, response, result) {
var output = {};
result = result.split(/,\r\n/g).map(function(line) {
var l = line.match(/([0-9]+): '(.*)'/);
if (l) {
var candidates = l[2].split("', '");
output[l[1]] = candidates[0].replace('\\\'', "'").replace(/\(US\)/, "").replace(/\([1-2][09]([0-9]{2})\)/, '').trim();
}
});
var sceneNameFile = fs.readFileSync('js/services/SceneNameResolver.js');
output = sceneNameFile.toString().replace(/exceptions \= (\{[\s\S]+\})\;/g, 'exceptions = ' + JSON.stringify(output, null, 4) + ';');
fs.writeFileSync('js/services/SceneNameResolver.js', output);
notify('SceneNameResolver.js was updated');
});
});
/**
* Tasks for internal use
* Each task called by the main task is listing the tasks that need to be executed as dependencies as an array as the second argument
* Since tasks run in parallell by default, this can seem confusing at first
*/
/*------------------------------------------------------------------------*/
/**
* Concat the scripts array into a file named dist/app.js
*/
gulp.task('concatScripts', function() {
return gulp.src(scripts)
.pipe(concat('app.js', {
newLine: ';'
}))
.pipe(gulp.dest('dist/'))
.pipe(notify({
message: 'Scripts packaged to dist/app.js'
}));
});
/**
* Concat the dependencies array into a file named dist/deps.js
*/
gulp.task('concatDeps', function() {
var tab = fs.readFileSync('tab.html').toString()
var matches = tab.match(/<!-- deploy:replace\=\'(.*)\' -->([\s\S]+?)[^\/deploy:]<!-- \/deploy:replace -->/g);
var deps = [];
matches.map(function(match) {
if (match.indexOf('dist\/deps.js') > -1) {
deps = match.match(/\.(\/js\/[a-zA-Z0-9\/\.\-]+)/g).map(function(script) {
if (script.indexOf('.min.js') >= -1) {
return script;
} else {
var mifi = script.replace('.js', '.min.js');
return fs.existsSync(mifi) ? mifi : script;
}
})
}
});
return gulp.src(deps)
.pipe(concat('deps.js', {
newLine: ';'
}))
.pipe(gulp.dest('dist/'))
.pipe(notify({
message: 'Deps packaged to dist/deps.js'
}));
});
/**
* Concat the background page and it's dependencies into dist/background.js
*/
gulp.task('concatBackgroundPage', function() {
return gulp.src(background)
.pipe(concat('background.js', {
newLine: ';'
}))
.pipe(gulp.dest('dist/'))
.pipe(notify({
message: 'Background page packaged to dist/background.js'
}));
});
/**
* Copy launch.js into place
*/
gulp.task('launch.js', function() {
return gulp.src(['launch.js', 'package.json'])
.pipe(gulp.dest('dist/'));
});
var templateCache = require('gulp-angular-templatecache');
gulp.task('buildTemplateCache', function() {
return gulp.src('templates/**/*.html')
.pipe(templateCache({
module: 'DuckieTV'
}))
.pipe(gulp.dest('dist'))
.pipe(notify({
message: 'Templatecache deployed'
}));
})
/**
* Parse tab.html and grab the deploy:replace comments sections.
* Grab the parameter value to those tags, and replace the content with that so that we're left with with just a couple of includes
*/
gulp.task('tabTemplate', ['buildTemplateCache'], function() {
return gulp.src(['tab.html'])
.pipe(replace(/<!-- deploy:replace\=\'(.*)\' -->([\s\S]+?)[^\/deploy:]<!-- \/deploy:replace -->/g, '$1'))
.pipe(replace('</body>', '<script src="dist/templates.js"></script></body>'))
.pipe(gulp.dest('dist/'))
.pipe(notify({
message: 'Tab template deployed'
}));
});
/**
* Concat the styles.js into dist/style.css
*/
gulp.task('concatStyles', function() {
return gulp.src(styles)
.pipe(concatCss("style.css"))
.pipe(gulp.dest('dist/'))
.pipe(notify({
message: 'Styles concatted'
}));
});
/**
* Move print.css into place
*/
gulp.task('print.css', function() {
return gulp.src('css/print.css')
.pipe(gulp.dest('dist/'));
});
/**
* Deployment and packaging functions
*/
gulp.task('copyToDeploy', ['default'], function() {
return gulp.src(['VERSION', 'trakt-trending-500.json', '_locales/**', 'dist/**', 'fonts/**', 'img/**', 'templates/**'], {
"base": "."
})
.pipe(gulp.dest('../deploy/browseraction'))
.pipe(gulp.dest('../deploy/newtab'));
});
/**
* Copy the altered tab.html into place
*/
gulp.task('copytab', ['copyToDeploy'], function() {
return gulp.src('dist/tab.html')
.pipe(gulp.dest('../deploy/browseraction'))
.pipe(gulp.dest('../deploy/newtab'));
});
/**
* Adjust all 3 versions of manifest.json to use the dist versions of scripts
* launch.js contains the button attach code for browser-action mode
* Also updates the manifest to include the latest version defined in the VERSION file
*/
gulp.task('manifests', ['copytab'], function() {
// js-format formatting options used in manipulating manifest.json
var formatOptions = {
'indent_char': '\t',
'indent_size': 1,
'brace_style': 'end-expand'
};
var noLaunch = function(json) {
json.version = ver;
json.background.scripts = ['dist/background.js'];
return json;
};
/**
* Modify package.json to remove the whole list of background scripts and replace it with dist version and launch.js
*/
var withLaunch = function(json) {
json.version = ver;
json.background.scripts = ['dist/background.js', 'dist/launch.js'];
return json;
};
if (nightly) {
console.log('nightly mode!');
gulp.src('./_locales/**/messages.json')
.pipe(jsonedit(function(json) {
json.appNameNewTab.message += " - Canary";
json.appShortNameNewTab.message += " - Canary";
json.appNameBrowserAction.message += " - Canary";
json.appShortNameBrowserAction.message += " - Canary";
return json;
}, formatOptions))
.pipe(gulp.dest('../deploy/newtab/_locales/'))
.pipe(gulp.dest('../deploy/browseraction/_locales/'));
}
gulp.src('manifest.json')
.pipe(jsonedit(noLaunch, formatOptions))
.pipe(gulp.dest('../deploy/newtab/'));
return gulp.src('manifest-app.json')
.pipe(rename('manifest.json'))
.pipe(jsonedit(withLaunch, formatOptions))
.pipe(gulp.dest('../deploy/browseraction/'));
});
/**
* Zip the browser action version
*/
gulp.task('zipbrowseraction', ['manifests'], function() {
return gulp.src('../deploy/browseraction/**')
.pipe(zip('browseraction-' + ver + '.zip'))
.pipe(gulp.dest('../deploy'));
});
/**
* zip the new tab version
*/
gulp.task('zipnewtab', ['manifests'], function() {
return gulp.src('../deploy/newtab/**')
.pipe(zip('newtab-' + ver + '.zip'))
.pipe(gulp.dest('../deploy'));
});