forked from yakyak/yakyak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.coffee
394 lines (350 loc) · 11.2 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
gulp = require 'gulp'
coffee = require 'gulp-coffee'
less = require 'gulp-less'
rimraf = require 'rimraf'
path = require 'path'
fs = require 'fs'
gutil = require 'gulp-util'
sourcemaps = require 'gulp-sourcemaps'
install = require 'gulp-install'
{execSync} = require 'child_process'
concat = require 'gulp-concat'
autoReload = require 'gulp-auto-reload'
changed = require 'gulp-changed'
rename = require 'gulp-rename'
packager = require 'electron-packager'
filter = require 'gulp-filter'
Q = require 'q'
Stream = require 'stream'
spawn = require('child_process').spawn
# running tasks in sequence
runSequence = require('run-sequence')
#
#
# Options
outapp = './app'
outui = outapp + '/ui'
paths =
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']
json = JSON.parse(fs.readFileSync('./package.json'))
deploy_options = {
dir: path.join __dirname, 'app'
asar: false
icon: path.join __dirname, 'src', 'icons', 'icon'
out: outdeploy
overwrite: true
'app-bundle-id': 'com.github.yakyak'
win32metadata: {
CompanyName: 'Yakyak'
ProductName: 'Yakyak'
OriginalFilename: 'Yakyak.exe'
FileDescription: 'Yakyak'
InternalName: 'Yakyak.exe'
FileVersion: "#{json.version}"
ProductVersion: "#{json.version}"
}
'osx-sign': 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 install(production:true)
# 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
# reloader will inject <script> tag
htmlInject = -> gutil.noop()
# copy .html-files
gulp.task 'html', ->
gulp.src paths.html
.pipe htmlInject()
.pipe gulp.dest outapp
# 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'
# 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_128.png': '[email protected]'
# Read icon in tray (linux/windows)
'icon-read_016.png': 'icon-read.png'
'icon-read_032.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
reloader = autoReload()
# copy the client side script
reloader.script()
.pipe gulp.dest outui
# inject scripts in html
htmlInject = reloader.inject
# watch rebuilt stuff
gulp.watch "#{outui}/**/*", reloader.onChange
gulp.task 'clean', (cb) ->
rimraf outapp, cb
gulp.task 'default', ['package', 'coffee', 'html', 'images', 'media',
'locales', 'icons', 'less', 'fontello']
gulp.task 'watch', ['default', 'reloader', 'html'], ->
# watch to rebuild
sources = (v for k, v of paths)
gulp.watch sources, ['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, ()->
deploy platform, arch
# set task with dependencies
gulp.task taskname, (cb) ->
runSequence 'default', tasknameNoDep, cb
#
tasknameNoDep
#
# task to deploy all
allNames = []
#
# create tasks for different platforms and architectures supported
platformOpts.map (plat) ->
names = []
archOpts.map (arch) ->
# create a task per platform/architecture
taskName = buildDeployTask(plat, arch)
names.push taskName
allNames.push taskName
#
# create arch-independet task
gulp.task "deploy:#{plat}", (cb) ->
# add callback to arguments
names.push cb
runSequence 'default', names...
#
gulp.task 'deploy', (cb)->
allNames.push cb
runSequence 'default', allNames...
zipIt = (folder, filePrefix, done) ->
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) ->
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)
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) ->
deferred = Q.defer()
opts = deploy_options
opts.platform = platform
opts.arch = arch
#
# restriction darwin won't compile ia32
if platform == 'darwin' && arch == 'ia32'
deferred.resolve()
deferred.promise
#
# necessary to add a callback to pipe (which is used to signal end of task)
gulpCallback = (obj) ->
stream = new Stream.Transform({objectMode: true})
stream._transform = (file, unused, callback) ->
obj()
callback(null, file)
stream
#
# package the app and create a zip
packOpts = opts
if platform == 'darwin'
packOpts.name = 'YakYak'
packager packOpts, (err, appPaths) ->
if err?
console.log ('Error: ' + err) if err?
else if appPaths?.length > 0
if process.env.NO_ZIP
return deferred.resolve()
json = JSON.parse(fs.readFileSync('./package.json'))
zippath = "#{appPaths[0]}/"
if platform == 'darwin'
fileprefix = "yakyak-#{json.version}-osx"
else
fileprefix = "yakyak-#{json.version}-#{platform}-#{arch}"
if platform == 'linux'
tarIt zippath, fileprefix, -> deferred.resolve()
else
zipIt zippath, fileprefix, -> deferred.resolve()
deferred.promise
["ia32", "x64"].forEach (arch) ->
['deb', 'rpm'].forEach (target) ->
gulp.task 'deploy:linux-' + arch + ':' + target, (done) ->
if arch is 'ia32'
archName = 'i386'
else if target is 'deb'
archName = 'amd64'
else
archName = 'x86_64'
packageName = json.name + '-VERSION-linux-ARCH.' + target
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'
'-t', target
'--architecture', archName
'--rpm-os', 'linux'
'--name', json.name
'--force' # Overwrite existing files
'--license', json.license
'--description', json.description
'--url', json.homepage
'--maintainer', json.author
'--vendor', json.authorName
'--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
# 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 #{target} (#{packageName})"
done()
else
console.log "Possible problem with #{target} #{packageName} " +
"-- (exit with #{code})"
done()
process.exit(1)