This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.js
executable file
·254 lines (205 loc) · 8.99 KB
/
build.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
#!/usr/bin/env node
/* Passphrases | https://github.com/micahflee/passphrases
Copyright (C) 2015 Micah Lee <micah@micahflee.com>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var fs = require('fs-extra');
var path = require('path');
var child_process = require('child_process');
var NwBuilder = require('node-webkit-builder');
// are we building a package to distribute?
var buildPackage = (process.argv[2] == '--package');
if(buildPackage) {
process.umask(0022);
// clean up from last time
try {
fs.removeSync('./dist');
fs.mkdirSync('./dist');
} catch(e) {}
}
// learn version of app
var version = JSON.parse(fs.readFileSync('./src/package.json'))['version'];
function build(options, callback) {
var nw = new NwBuilder(options);
nw.on('log', console.log);
nw.build().then(function () {
callback();
}).catch(function(err) {
console.log('');
console.log('Build complete.');
console.log('');
callback(err);
});
}
// options for all platforms
var options = { files: './src/**' }
// Linux
if(process.platform == 'linux') {
options.platforms = ['linux32', 'linux64'];
build(options, function(err){
if(err) throw err;
if(buildPackage) {
console.log('Note that there is no simple way to build source packages yet.');
function copyBinaryPackageSync(pkgName, arch) {
try {
// create directory structure
fs.mkdirsSync('./dist/' + pkgName + '/opt');
fs.mkdirsSync('./dist/' + pkgName + '/usr/bin');
fs.mkdirsSync('./dist/' + pkgName + '/usr/share/pixmaps');
fs.mkdirsSync('./dist/' + pkgName + '/usr/share/applications');
// copy binaries
fs.copySync('./build/Passphrases/' + arch, './dist/' + pkgName + '/opt/Passphrases');
// copy icon, .desktop
fs.copySync('./packaging/passphrases.png', './dist/' + pkgName + '/usr/share/pixmaps/passphrases.png');
fs.copySync('./packaging/passphrases.desktop', './dist/' + pkgName + '/usr/share/applications/passphrases.desktop');
// create passphrases symlink
fs.symlinkSync('../../opt/Passphrases/Passphrases', './dist/' + pkgName + '/usr/bin/passphrases');
} catch(e) { throw e; }
}
function copyAndReplace(src_filename, dest_filename, arch, replaceArch) {
var text = fs.readFileSync(src_filename, { encoding: 'utf8' });
text = text.replace('{{version}}', version);
if(arch == 'linux32') text = text.replace('{{arch}}', replaceArch);
if(arch == 'linux64') text = text.replace('{{arch}}', replaceArch);
fs.writeFileSync(dest_filename, text);
}
// can we make debian packages?
child_process.exec('which dpkg-deb', function(err, stdout, stderr){
if(err || stdout == '') {
console.log('Cannot find dpkg-deb, skipping building Debian package');
return;
}
// building two .deb packages, for linux32 and linux64
['linux32', 'linux64'].forEach(function(arch){
if(!arch) return;
var debArch;
if(arch == 'linux32') debArch = 'i386';
if(arch == 'linux64') debArch = 'amd64';
var pkgName = 'passphrases_' + version + '-1_{{arch}}';
pkgName = pkgName.replace('{{arch}}', debArch);
copyBinaryPackageSync(pkgName, arch);
// write the debian control file
fs.mkdirsSync('./dist/' + pkgName + '/DEBIAN');
copyAndReplace('./packaging/DEBIAN/control', './dist/' + pkgName + '/DEBIAN/control', arch, debArch);
// build .deb packages
console.log('Building ' + pkgName + '.deb');
child_process.exec('dpkg-deb --build ' + pkgName, { cwd: './dist' }, function(err, stdout, stderr){
if(err) throw err;
});
});
});
// can we make rpm packages?
child_process.exec('which rpmbuild', function(err, stdout, stderr){
if(err || stdout == '') {
console.log('Cannot find rpmbuild, skipping building Red Hat package');
return;
}
// building two .rpm packages, for linux32 and linux64
['linux32', 'linux64'].forEach(function(arch){
if(!arch) return;
// following instructions from:
// https://stackoverflow.com/questions/880227/what-is-the-minimum-i-have-to-do-to-create-an-rpm-file
var rpmArch;
if(arch == 'linux32') rpmArch = 'i686';
if(arch == 'linux64') rpmArch = 'x86_64';
fs.mkdirsSync('./dist/' + rpmArch + '/RPMS');
fs.mkdirsSync('./dist/' + rpmArch + '/SRPMS');
fs.mkdirsSync('./dist/' + rpmArch + '/BUILD');
fs.mkdirsSync('./dist/' + rpmArch + '/SOURCES');
fs.mkdirsSync('./dist/' + rpmArch + '/SPECS');
fs.mkdirsSync('./dist/' + rpmArch + '/tmp');
var pkgName = 'passphrases-' + version;
copyBinaryPackageSync(rpmArch + '/' + pkgName, arch);
// write the spec file
copyAndReplace('./packaging/SPECS/passphrases.spec', './dist/' + rpmArch + '/SPECS/passphrases.spec', arch, rpmArch);
// tarball the source
console.log('Compressing binary for ' + arch);
child_process.exec('tar -zcf SOURCES/' + pkgName + '.tar.gz ' + pkgName + '/', { cwd: './dist/' + rpmArch }, function(err, stdout, stderr){
if(err) {
console.log('Error after compressing - ' + arch, err);
return;
}
console.log('Building ' + pkgName + '.rpm (' + arch + ')');
var topDir = path.resolve('./dist/' + rpmArch);
child_process.exec('rpmbuild --define \'_topdir ' + topDir +'\' -ba dist/' + rpmArch + '/SPECS/passphrases.spec', function(err, stdout, stderr){
if(err) {
console.log('Error after rpmbuild - ' + arch, err);
return;
}
});
});
});
});
}
});
}
// OSX
else if(process.platform == 'darwin') {
options.platforms = ['osx32'];
options.macIcns = './packaging/icon.icns';
build(options, function(err){
if(err) throw err;
if(buildPackage) {
// copy .app folder
fs.copySync('./build/Passphrases/osx32/Passphrases.app', './dist/Passphrases.app');
// codesigning
console.log('Codesigning');
var signingIdentityApp = '3rd Party Mac Developer Application: Micah Lee';
var signingIdentityInstaller = 'Developer ID Installer: Micah Lee';
child_process.exec('codesign --force --deep --verify --verbose --sign "' + signingIdentityApp + '" Passphrases.app', { cwd: './dist' }, function(err, stdout, stderr){
if(err) {
console.log('Error during codesigning', err);
return;
}
// build a package
child_process.exec('productbuild --component Passphrases.app /Applications Passphrases.pkg --sign "' + signingIdentityInstaller + '"', { cwd: './dist' }, function(err, stdout, stderr){
if(err) {
console.log('Error during productbuild', err);
return;
}
console.log('All done');
});
});
}
});
}
// Windows
else if(process.platform == 'win32') {
options.platforms = ['win32'];
options.winIco = './packaging/icon.ico';
build(options, function(err){
if(err) throw err;
if(buildPackage) {
// copy binaries
fs.copySync('./build/passphrases/win32', './dist/Passphrases');
// copy license
fs.copySync('./LICENSE.md', './dist/Passphrases/LICENSE.md');
// codesign Passphrases.exe
child_process.execSync('signtool.exe sign /v /d "Passphrases" /a /tr "http://www.startssl.com/timestamp" .\\dist\\Passphrases\\Passphrases.exe');
// make the installer
child_process.execSync('makensisw packaging\\windows_installer.nsi');
// codesign the installer
child_process.execSync('signtool.exe sign /v /d "Passphrases" /a /tr "http://www.startssl.com/timestamp" .\\dist\\Passphrases_Setup.exe');
}
});
}
// unsupported platform
else {
console.log('Error: unrecognized platform');
process.exit();
}