forked from yakyak/yakyak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.coffee
513 lines (460 loc) · 15.8 KB
/
gulpfile.coffee
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
gulp = require 'gulp'
coffee = require 'gulp-coffee'
less = require 'gulp-less'
rimraf = require 'rimraf'
path = require 'path'
fs = require 'fs'
sourcemaps = require 'gulp-sourcemaps'
reinstall = require 'gulp-reinstall'
{execSync} = require 'child_process'
concat = require 'gulp-concat'
liveReload = require 'gulp-livereload'
changed = require 'gulp-changed'
rename = require 'gulp-rename'
packager = require 'electron-packager'
flatpak = try require 'electron-installer-flatpak'
debian = try require 'electron-installer-debian'
filter = require 'gulp-filter'
Q = require 'q'
Stream = require 'stream'
spawn = require('child_process').spawn
#
#
# Options
outapp = './app'
outui = outapp + '/ui'
paths =
snyk: './.snyk'
deploy: './dist/'
README: './README.md'
package: './package.json'
coffee: './src/**/*.coffee'
html: './src/**/*.html'
images: './src/**/images/*.*'
icons: './src/icons'
locales: './src/locales/*.json'
media: './src/media/*.*'
less: './src/ui/css/manifest.less'
lessd: './src/ui/css/**/*.less'
css: './src/**/*.css'
fonts: ['./src/**/*.eot', './src/**/*.svg',
'./src/**/*.ttf', './src/**/*.woff',
'./src/**/*.woff2']
#
#
# Options for packaging app
#
outdeploy = path.join __dirname, 'dist'
platformOpts = ['linux', 'darwin', 'win32']
archOpts = ['x64','ia32','arm64']
json = JSON.parse(fs.readFileSync('./package.json'))
deploy_options = {
dir: path.join __dirname, 'app'
asar:
unpackDir: "{node_modules/node-notifier/vendor/**,icons}"
icon: path.join __dirname, 'src', 'icons', 'icon'
out: outdeploy
overwrite: true
appBundleId: 'com.github.yakyak'
win32metadata: {
CompanyName: 'Yakyak'
ProductName: 'Yakyak'
OriginalFilename: 'Yakyak.exe'
FileDescription: 'Yakyak'
InternalName: 'Yakyak.exe'
FileVersion: "#{json.version}"
ProductVersion: "#{json.version}"
}
osxSign: true
arch: archOpts.join ','
platform: platformOpts.join ','
}
#
#
# End of options
# setup package stuff (README, package.json)
gulp.task 'package', ->
gulp.src paths.README
# .pipe changed outapp
.pipe gulp.dest outapp
# install runtime deps
gulp.src paths.package
# .pipe changed outapp
.pipe gulp.dest outapp
.pipe reinstall()
#.pipe reinstall({ production: true }) # electron-packager doesn't like this
# compile coffeescript
gulp.task 'coffee', ->
gulp.src paths.coffee
.pipe sourcemaps.init()
.pipe coffee()
.on 'error', (e) ->
console.log e.toString()
@emit 'end'
.pipe sourcemaps.write()
# .pipe changed outapp
.pipe gulp.dest outapp
# copy .html-files
gulp.task 'html', ->
gulp.src paths.html
.pipe gulp.dest outapp
.pipe liveReload()
# copy images
gulp.task 'locales', ->
gulp.src paths.locales
.pipe gulp.dest path.join outapp, 'locales'
# copy images
gulp.task 'media', ->
gulp.src paths.media
.pipe gulp.dest path.join outapp, 'media'
gulp.task 'snyk', ->
gulp.src paths.snyk
.pipe gulp.dest path.join outapp
# copy images
gulp.task 'images', ->
gulp.src paths.images
.pipe gulp.dest outapp
gulp.task 'icons', ->
nameMap =
# Icons
'icon_016.png': 'icon.png'
'icon_032.png': '[email protected]'
'icon_048.png': '[email protected]'
'icon_128.png': '[email protected]'
'icon_256.png': '[email protected]'
'icon_512.png': '[email protected]'
# Unread icon in tray (linux/windows)
'icon-unread_016.png': 'icon-unread.png'
'icon-unread_032.png': '[email protected]'
'icon-unread_020.png': '[email protected]'
'icon-unread_128.png': '[email protected]'
# Read icon in tray (linux/windows - colorblind)
'icon-read_016_blue.png': 'icon-read_blue.png'
'icon-read_032_blue.png': 'icon-read@2x_blue.png'
'icon-read_020_blue.png': 'icon-read@20_blue.png'
'icon-read_128_blue.png': 'icon-read@8x_blue.png'
# Read icon in tray (linux/windows)
'icon-read_016.png': 'icon-read.png'
'icon-read_032.png': '[email protected]'
'icon-read_020.png': '[email protected]'
'icon-read_128.png': '[email protected]'
# Unread icon in tray (Mac OS X)
'osx-icon-unread-Template_016.png': 'osx-icon-unread-Template.png'
'osx-icon-unread-Template_032.png': '[email protected]'
# Read icon in tray (Mac OS X)
'osx-icon-read-Template_016.png': 'osx-icon-read-Template.png'
'osx-icon-read-Template_032.png': '[email protected]'
# gulp 4 requires async notification!
new Promise (resolve, reject) ->
Object.keys(nameMap).forEach (name) ->
gulp.src path.join paths.icons, name
.pipe rename nameMap[name]
.pipe gulp.dest path.join outapp, 'icons'
resolve()
# compile less
gulp.task 'less', ->
gulp.src paths.less
.pipe sourcemaps.init()
.pipe less()
.on 'error', (e) ->
console.log e
@emit 'end'
.pipe concat('ui/app.css')
.pipe sourcemaps.write()
.pipe gulp.dest outapp
# fontello/css
gulp.task 'fontello', ->
gulp.src [paths.css, paths.fonts...]
.pipe gulp.dest outapp
gulp.task 'reloader', ->
# create an auto reload server instance
liveReload.listen()
# watch rebuilt stuff
gulp.watch "#{outui}/**/*", gulp.series('html')
gulp.task 'clean', (cb) ->
rimraf outapp, cb
gulp.task 'default',
gulp.parallel 'package', 'coffee', 'html', 'images',
'media', 'locales', 'icons', 'less', 'fontello'
gulp.task 'watch', ->
gulp.series 'default', 'reloader', 'html'
# watch to rebuild
sources = (v for k, v of paths)
gulp.watch sources, gulp.series('default')
#
#
#
# Deployment related tasks
#
#
buildDeployTask = (platform, arch) ->
# create a task per platform
taskname = "deploy:#{platform}-#{arch}"
tasknameNoDep = "#{taskname}:nodep"
# set internal task with _ (does not have dependencies)
gulp.task tasknameNoDep, (cb)->
deploy platform, arch, cb
# set task with dependencies
gulp.task taskname, gulp.series('default', tasknameNoDep)
#
tasknameNoDep
#
# task to deploy all
allNames = []
names = {linux: [], win32: [], darwin: []}
#
zipIt = (folder, filePrefix, done) ->
# use zip
ext = 'zip'
zipName = path.join outdeploy, "#{filePrefix}.#{ext}"
folder = path.basename folder
#
args = ['-r', '-q', '-y', '-X', zipName, folder]
opts = {
cwd: outdeploy
stdio: [0, 1, 'pipe']
}
compressIt('zip', args, opts, zipName, done)
tarIt = (folder, filePrefix, done) ->
# use GNU tar to make gzipped tar archive
ext = 'tar.gz'
zipName = path.join outdeploy, "#{filePrefix}.#{ext}"
folder = path.basename folder
#
args = ['-czf', zipName, folder]
opts = {
cwd: outdeploy
stdio: [0, 1, 'pipe']
}
compressIt('tar', args, opts, zipName, done)
zipItWin = (folder, filePrefix, done) ->
# use built-in tar.exe to make zip archive
ext = 'zip'
zipName = path.join outdeploy, "#{filePrefix}.#{ext}"
folder = path.basename folder
#
args = ['-cf', zipName, folder]
opts = {
cwd: outdeploy
stdio: [0, 1, 'pipe']
}
compressIt('tar', args, opts, zipName, done)
compressIt = (cmd, args, opts, zipName, done) ->
#
# create child process
child = spawn cmd
, args
, opts
# log all errors
child.on 'error', (err) ->
console.log 'Error: ' + err
process.exit(1)
# show err
child.on 'exit', (code) ->
if code == 0
console.log "Created archive (#{zipName})"
done()
else
console.log "Possible problem with archive #{zipName} " +
"-- (exit with #{code})"
done()
process.exit(1)
#
#
deploy = (platform, arch, cb) ->
deferred = Q.defer()
opts = deploy_options
opts.platform = platform
opts.arch = arch
#
# restriction darwin won't compile ia32
if platform is 'darwin' and arch is 'ia32'
cb()
deferred.resolve()
return deferred.promise
#
# package the app and create a zip
packOpts = opts
if platform == 'darwin'
packOpts.name = 'YakYak'
#
console.log('packOpts', packOpts)
packager(packOpts)
.catch (error) ->
console.error(error)
.then (appPaths) ->
if appPaths?.length > 0
if process.env.NO_ZIP
cb()
return deferred.resolve()
zippath = "#{appPaths[0]}/"
if platform == 'darwin'
fileprefix = "yakyak-#{json.version}-osx-#{arch}"
else
fileprefix = "yakyak-#{json.version}-#{platform}-#{arch}"
if platform == 'linux'
tarIt zippath, fileprefix, ->
cb()
deferred.resolve()
else if platform == 'win32' && process.platform == 'win32'
zipItWin zippath, fileprefix, ->
cb()
deferred.resolve()
else
zipIt zippath, fileprefix, ->
cb()
deferred.resolve()
deferred.promise
# create tasks for different platforms and architectures supported
platformOpts.map (plat) ->
archOpts.map (arch) ->
# create a task per platform/architecture
taskName = buildDeployTask(plat, arch)
names[plat].push taskName
allNames.push taskName
#
# create arch-independet task
gulp.task "deploy:#{plat}", gulp.series 'default', names[plat]...
#
archOpts.forEach (arch) ->
['rpm', 'pacman'].forEach (target) ->
gulp.task "deploy:linux-#{arch}:#{target}:nodep", (done) ->
archNameSuffix = archName
if arch is 'ia32'
archName = 'i386'
archNameSuffix = 'ia32'
else if target is 'deb'
archName = 'amd64'
archNameSuffix = 'amd64'
else if arch is 'x64'
archName = 'x86_64'
archNameSuffix = 'x64'
else
archName = arch
archNameSuffix = arch
if target == 'pacman'
suffix = 'tar.gz'
else
suffix = target
if target == 'pacman'
packageName = json.name + '-VERSION-linux-' + archNameSuffix + '-pacman.' + suffix
else
packageName = json.name + '-VERSION-linux-' + archNameSuffix + '.' + suffix
iconArgs = [16, 32, 48, 128, 256, 512].map (size) ->
if size < 100
src = "0#{size}"
else
src = size
"./src/icons/icon_#{src}.png=/usr/share/icons/hicolor/#{size}x#{size}/apps/#{json.name}.png"
fpmArgs = [
'-s', 'dir'
'--log', 'info'
'-t', target
'--architecture', archName
'--rpm-os', 'linux'
'--name', json.name
'--force' # Overwrite existing files
'--description', "\"#{json.description}\""
'--license', "\"#{json.license}\""
'--url', json.homepage
'--maintainer', "\"#{json.author}\""
'--vendor', "\"#{json.author}\""
'--version', json.version
'--package', "./dist/#{packageName}"
'--after-install', './resources/linux/after-install.sh'
'--after-remove', './resources/linux/after-remove.sh'
"./dist/#{json.name}-linux-#{arch}/.=/opt/#{json.name}"
"./resources/linux/app.desktop=/usr/share/applications/#{json.name}.desktop"
].concat iconArgs
child = spawn 'fpm', fpmArgs
child.stdout.on 'data', (data) ->
# do nothing
console.log("fpm: #{data}") if target == 'rpm'
done()
return true
# log all errors
child.on 'error', (err) ->
console.log 'Error: ' + err, fpmArgs
done()
process.exit(1)
# show err
child.on 'exit', (code) ->
if code == 0
console.log "Created #{target} (#{packageName})"
done()
else
console.log "Possible problem with #{target} " +
"(exit with code #{code})"
console.log 'fpm arguments: ' + fpmArgs.join(' ')
process.exit(1)
return child
#names['linux'].push 'deploy:linux-' + arch + ':' + target
#allNames.push('deploy:linux-' + arch + ':' + target)
gulp.task "deploy:linux-#{arch}:#{target}",
gulp.series("deploy:linux-#{arch}", "deploy:linux-#{arch}:#{target}:nodep")
gulp.task "deploy:linux-#{arch}:deb:nodep", (done) ->
options = {
src: "dist/#{json.name}-linux-#{arch}"
dest: 'dist/'
name: 'YakYak'
genericName: 'IM Client'
productName: 'YakYak'
version: json.version
bin: 'yakyak'
desktopTemplate: 'resources/desktop.ejs'
icon:
'16x16': 'src/icons/icon_016.png'
'32x32': 'src/icons/icon_032.png'
'48x48': 'src/icons/icon_048.png'
'128x128': 'src/icons/icon_128.png',
'256x256': 'src/icons/icon_256.png',
'512x512': 'src/icons/icon_512.png',
'scalable': 'src/icons/yakyak-logo.svg',
categories: [
'GNOME'
'GTK'
'Network'
'InstantMessaging'
]
}
if arch is 'ia32'
options.arch = 'i386'
else if arch is 'x64'
options.arch = 'amd64'
else
options.arch = arch
options.rename = (dest, src) -> path.join(dest, "#{json.name}-#{json.version}-linux-#{options.arch}.deb")
debian options
gulp.task "deploy:linux-#{arch}:deb",
gulp.series("deploy:linux-#{arch}", "deploy:linux-#{arch}:deb:nodep")
gulp.task "deploy:linux-#{arch}:flatpak:nodep", (done) ->
flatpakOptions =
id: 'com.github.yakyak.YakYak'
arch: arch
runtimeVersion: "1.6"
src: 'dist/yakyak-linux-' + arch
dest: 'dist/'
genericName: 'Internet Messenger'
productName: 'YakYak'
icon:
'16x16': 'src/icons/icon_016.png'
'32x32': 'src/icons/icon_032.png'
'48x48': 'src/icons/icon_048.png'
'128x128': 'src/icons/icon_128.png'
'256x256': 'src/icons/icon_256.png'
'512x512': 'src/icons/icon_512.png'
categories: ['Network', 'InstantMessaging']
finishArgs: ['-v']
flatpak flatpakOptions, (err) ->
if err
console.error err.stack
done()
process.exit 1
else
console.log "Created flatpak (#{json.name}_#{json.version}_#{arch}.flatpak)"
done()
gulp.task "deploy:linux-#{arch}:flatpak",
gulp.series("deploy:linux-#{arch}", "deploy:linux-#{arch}:flatpak:nodep")
#names['linux'].push 'deploy:linux-' + arch + ':flatpak'
#allNames.push('deploy:linux-' + arch + ':flatpak')
gulp.task 'deploy', gulp.series 'default', allNames...