This repository has been archived by the owner on Nov 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
builder
executable file
·406 lines (380 loc) · 12.3 KB
/
builder
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
#!/usr/bin/env node
'use strict'
const fs = require('fs.extra')
const del = require('del')
const swig = require('swig')
const async = require('async')
const appdmg = require('appdmg')
const program = require('commander')
const Promise = require('promise')
const archiver = require('archiver')
const NwBuilder = require('nw-builder')
const Connection = require('ssh2')
const packageInfo = require('./src/package')
const childProcess = require('child_process')
const commanderTabTab = require('commander-tabtab')
const builderPackageInfo = require('./package')
const buildConf = require('./config/buildConf')
// Console app
program._name = './builder'
program.version(builderPackageInfo.version)
// build
program
.command('build')
.description('Build binaries')
.action(build)
// Create distributables .zip / .dmg
program
.command('createDist')
.description('Create distributables (.zip/.dmg)')
.action(createDistributables)
// Upload to sourceforge
program
.command('upload')
.description('Upload to Sourceforge')
.action(() => {
deployToSourceforge(buildConf.deployDir, packageInfo.version)
.then(files => {
console.log('UPLOAD DONE')
console.log('Updating README.md')
updateReadme(files)
})
})
// Update README
program
.command('updateReadme')
.description('Update README from template.')
.action(() => {
const files = [
'Pullover_' + packageInfo.version + '.dmg',
'Pullover_' + packageInfo.version + '_Installer.exe',
'Pullover_' + packageInfo.version + '_linux32.zip',
'Pullover_' + packageInfo.version + '_linux64.zip'
]
updateReadme(files)
})
program
.command('run')
.description('Run app')
.action(runApp)
// Display info on how to enable tabcompletion
program.on('--help', () => {
console.log('')
console.log(' Tab-Completion:')
console.log('')
console.log(' To enable tab completion execute: source <(./builder completion)')
console.log(' For permanent tab completion add the output of "./builder completion"')
console.log(' to your .bashrc/.zshrc: ./builder completion >> ~/.zshrc')
console.log('')
})
// Show help if invoked without cli options/command
if (!process.argv.slice(2).length) {
program.outputHelp()
}
// Tab completion
commanderTabTab.init(program, './builder')
program.parse(process.argv)
// Show help if command didn't match
if (typeof program.args[0] !== 'object') {
program.outputHelp()
}
function runApp() {
const nw = new NwBuilder(buildConf.nwbuild)
// Enable output to console
nw.on('stdout', data => {
process.stdout.write(data.toString())
})
nw.on('stderr', data => {
process.stdout.write(data.toString())
})
nw.on('log', console.log)
nw.run().catch((err) => {
if (err.code === 'ENOTFOUND') {
console.log('nwBuilder failed because there was no internet connection')
console.log('Trying to run it manually with a cached version...')
const nwjsPath = './cache/' + buildConf.nwbuild.version + '-sdk/osx64/nwjs.app/Contents/MacOS/nwjs'
const nwjs = childProcess.spawn(nwjsPath, ['./dist'])
nwjs.stdout.pipe(process.stdout)
nwjs.stderr.pipe(process.stderr)
}
})
}
/**
* Bundle App with node-webkit for different platforms
* @return {promise}
*/
function build() {
return new Promise((resolve, reject) => {
const nw = new NwBuilder(buildConf.nwbuild)
console.log('BUILD...')
// Log stuff you want
nw.on('log', console.log)
/**
* @todo Still needed?
*/
// Delete src/node_modules/ws/build to make it cross platform (there are fallbacks)
del(['src/node_modules/ws/build']).then(() => {
// Build returns a promise
nw.build().then(() => {
console.log('Build done.')
resolve()
}).catch(error => {
console.log('BUILD PROCESS FAILED')
console.error(error)
reject()
})
})
})
}
/**
* Create installer (.dmg/.exe/.zip) for different platforms
* Mac can build for all three platforms. Install makensis (brew install makensis)
* and wine first.
*
* @return {promise}
*/
function createDistributables() {
return new Promise((resolve, reject) => {
const deploymentPath = buildConf.deployDir
const version = packageInfo.version
// Clear deploy folder
del.sync(buildConf.deployDir)
// Create deploy folder
fs.mkdirSync(deploymentPath)
console.log('Start packaging for distribution ...')
async.map(buildConf.nwbuild.platforms, (platform, done) => {
const path = './bin/pullover/' + platform
// DMG?
if (platform === 'osx64') {
createDMG().then(done)
return
}
// Windows installer?
else if (platform === 'win32') {
createWindowsInstaller().then(done)
return
}
// Zip everything else
const zipName = 'Pullover_' + version + '_' + platform + '.zip'
const zipPath = deploymentPath + '/' + zipName
if (fs.existsSync(zipPath)) {
console.log(zipPath + ' already existed. Deleting it.')
fs.unlinkSync(zipPath)
}
const output = fs.createWriteStream(zipPath)
const archive = archiver('zip')
output.on('close', () => {
console.log(zipName + ' done. ' + Math.floor(archive.pointer() / 1024 / 1024) + ' MB')
done()
})
archive.on('error', err => {
throw err
})
archive.pipe(output)
archive.glob('**/*', { cwd: path })
archive.finalize()
}, err => {
// All zipped
console.log('ALL DONE, errors: ', err)
resolve()
})
}).catch(error => {
console.log('DIST CREATION PROCESS FAILED')
console.error(error)
console.log(error.stack)
})
}
/**
* Upload files in localDir to remote dir
* deployToSourceforge('./bin/deployZip', '0.x.x', options);
*
* @param {string} localDir './some/Dir'
* @param {string} remoteDir 'dirName' only first level supported
* @param {object} options Connection info
* @return {promise}
*/
function deployToSourceforge(localDir, remoteDir) {
console.log('Upload', localDir, remoteDir)
const conn = new Connection()
let files
return new Promise((resolve, reject) => {
remoteDir = '/home/pfs/p/' + buildConf.sourceforge.project + '/' + remoteDir
conn.on('ready', () => {
console.log('Connection :: ready')
// Init SFTP
conn.sftp((err, sftp) => {
if (err) console.log(err)
// Create dir and ignore error (dir already exists)
sftp.mkdir(remoteDir, err => {
if (err) console.log('Dir already exists', err)
else console.log('Dir created')
// Upload files
files = fs.readdirSync(localDir)
async.eachSeries(files, (file, callback) => {
console.log('Uploading file: ', localDir + '/' + file)
sftp.fastPut(localDir + '/' + file, remoteDir + '/' + file, callback)
}, err => {
if (err) console.log('Upload error:', err)
console.log('Upload done')
sftp.end()
resolve(files)
})
})
})
}).connect(buildConf.sourceforge.sftp)
}).then(result => {
conn.end()
return result
})
}
/**
* Update readme with links to latest release downloads
* @param {array} files Filenames
* @return {promise}
*/
function updateReadme(files) {
return new Promise((resolve, reject) => {
if (files === undefined || files.length === 0) return
const downloads = []
// URLs: https://sourceforge.net/projects/pullover/files/0.1.2/Pullover_0.1.2_win.zip/download
// https://sourceforge.net/projects/[PROJECT]/files/[VERSION]/[FILENAME]/download
const baseURL = 'https://sourceforge.net/projects/' +
buildConf.sourceforge.project +
'/files/' +
packageInfo.version + '/'
function getPlatformName(file) {
if (/\.exe/.test(file)) return 'Windows x32'
else if (/\.dmg/.test(file)) return 'Mac OS 10.9+ x64'
else if (/_linux32/.test(file)) return 'Linux x32'
else if (/_linux64/.test(file)) return 'Linux x64'
else return 'Unkown'
}
// Build downloads array
for (let i = 0; i < files.length; i++) {
var file = files[i]
const download = {
fileName: file,
url: baseURL + file + '/download',
platformName: getPlatformName(file)
}
downloads.push(download)
}
// Template
const template = swig.compileFile('./res/README.md.tpl')
const templateVars = {
downloads: downloads.reverse(),
version: packageInfo.version
}
// Write new readme
fs.writeFile('./README.md', template(templateVars), err => {
if (err) reject(err)
resolve()
})
})
}
/**
* Create a Mac .dmg
* @return {promise}
*/
function createDMG() {
return new Promise((resolve, reject) => {
const targetPath = buildConf.deployDir + '/Pullover_' + packageInfo.version + '.dmg'
if (fs.existsSync(targetPath)) {
console.log(targetPath + ' already existed. Deleting it.')
fs.unlinkSync(targetPath)
}
const ee = appdmg({ source: 'config/dmgConf.json', target: targetPath })
ee.on('finish', () => {
console.log('Pullover_' + packageInfo.version + '.dmg done. ' + Math.floor(fs.statSync(targetPath).size / 1024 / 1024) + ' MB')
resolve()
})
// Debugging
// ee.on('progress', console.log)
ee.on('error', err => {
console.log('ERROR creating DMG', err)
reject(err)
})
})
}
/**
* Create a windows installer,
* wine and makensis needed
* @return {promise}
*/
function createWindowsInstaller() {
return new Promise((resolve, reject) => {
const buildDir = './bin/tmp'
const filename = 'Pullover_' + packageInfo.version + '_Installer.exe'
if (fs.existsSync(buildDir)) {
// Clear tmp dir
del([buildDir], createWindowsInstaller)
return
} else {
fs.mkdirSync(buildDir)
}
// Create installer template
const template = swig.compileFile('./res/windowsInstaller/windowsInstaller.nsis.tpl')
const templateVars = {
name: packageInfo.name,
prettyName: 'Pullover',
version: packageInfo.version,
src: '../pullover/win32',
dest: '../deploy/' + filename,
icon: '../../res/icon.ico',
setupIcon: '../../res/icon.ico',
banner: '../../res/windowsInstaller/winInst_header.bmp'
}
// Copy icon to win source
if (!fs.existsSync('./bin/pullover/win32/icon.ico')) {
fs.writeFileSync('./bin/pullover/win32/icon.ico', fs.readFileSync('./res/icon.ico'))
}
// Copy installer files
fs.copyRecursive('./res/windowsInstaller', buildDir, err => {
if (err) console.log(err)
// Write installer instructions
fs.writeFile('./bin/tmp/installer.nsis', template(templateVars), err => {
if (err) reject(err)
// Note: NSIS have to be added to PATH!
const nsis = childProcess.spawn('makensis', ['./bin/tmp/installer.nsis'])
//nsis.stdout.pipe(process.stdout)
nsis.stdout.on('data', () => {
process.stdout.write('.')
})
nsis.stderr.pipe(process.stderr)
nsis.on('close', () => {
console.log('\nWindows installer created')
if (buildConf.codeSigning.win === true) {
console.log('Codesigning windows installer')
// Sign installer
const signInstall = childProcess.spawn('signcode',
['-spc', 'res/cert.spc',
'-v', 'res/cert.pvk',
'-a', 'sha1', '-$', 'individual',
'-i', 'https://github.com/cgrossde/Pullover',
'-t', 'http://timestamp.verisign.com/scripts/timstamp.dll',
'-tr', '10',
'bin/deploy/' + filename
])
signInstall.stdout.on('data', data => {
process.stdout.write(data)
// It's a hack -.-
if (data.toString().indexOf('.pvk:') !== -1) {
console.log('Entering password')
signInstall.stdin.write(buildConf.certPassword + '\n')
}
})
signInstall.stderr.pipe(process.stdout)
signInstall.on('close', () => {
console.log('Installer signed')
// Clear tmp and return
del([buildDir, 'bin/deploy/' + filename + '.bak'], resolve)
})
} else {
// Clear tmp and return
del([buildDir, 'bin/deploy/' + filename + '.bak'], resolve)
}
})
})
})
})
}