forked from ioBroker/ioBroker.build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
167 lines (145 loc) · 7.56 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
const gulp = require('gulp');
const fs = require('fs');
const path = require('path');
const version = require('./package.json').version;
function deleteFoldersRecursive(path, exceptions) {
if (fs.existsSync(path)) {
const files = fs.readdirSync(path);
for (const file of files) {
const curPath = `${path}/${file}`;
if (exceptions && exceptions.find(e => curPath.endsWith(e))) {
continue;
}
const stat = fs.statSync(curPath);
if (stat.isDirectory()) {
deleteFoldersRecursive(curPath);
fs.rmdirSync(curPath);
} else {
fs.unlinkSync(curPath);
}
}
}
}
gulp.task('0-clean', done => {
deleteFoldersRecursive(`${__dirname}/tmp`);
deleteFoldersRecursive(`${__dirname}/build/.windows-ready`);
deleteFoldersRecursive(`${__dirname}/delivery`);
done();
});
gulp.task('3-0-writeWindowsVersion', async () => {
!fs.existsSync(`${__dirname}/build/.windows-ready`) && fs.mkdirSync(`${__dirname}/build/.windows-ready`);
fs.writeFileSync(`${__dirname}/build/.windows-ready/version.txt`, `#define MyAppVersion "${version}"`);
});
function _checkFiles(files, callback, index) {
index = index || 0;
if (files.find(file => !fs.existsSync(`${__dirname}/${file}`))) {
if (index < 5) {
setTimeout(() => _checkFiles(files, callback, index + 1), 3000);
} else {
callback('timeout ' + files.join(', '));
}
} else {
callback(null);
}
}
function checkFiles(files) {
return new Promise((resolve, reject) => _checkFiles(files, error => error ? reject(error) : resolve()));
}
function runMSI() {
return new Promise((resolve, reject) => {
checkFiles([
'build/windows/ioBroker.iss',
'build/windows/resource/ioBroker.ico'
])
.then(() => {
// Install node modules
const cwd = `${__dirname.replace(/\\/g, '/')}/build/windows/`;
const cmd = `"${__dirname}\\build\\windows\\InnoSetup6\\ISCC.exe" "${__dirname}\\build\\windows\\ioBroker.iss"`;
console.log(`"${cmd} in ${cwd}`);
// System call used for update of js-controller itself,
// because during installation npm packet will be deleted too, but some files must be loaded even during the installation process.
const exec = require('child_process').exec;
const child = exec(cmd, {cwd});
child.stderr.pipe(process.stderr);
child.stdout.pipe(process.stdout);
child.on('exit', (code /* , signal */) => {
// code 1 is strange error that cannot be explained. Everything is installed but error :(
if (code && code !== 1) {
reject(new Error(`Cannot install: ${code}`));
} else {
console.log(`"${cmd} in ${cwd} finished.`);
// command succeeded
resolve();
}
});
})
.catch(error => reject(error));
});
}
function signExe() {
return new Promise((resolve, reject) => {
checkFiles([`delivery/iobroker-installer.exe`])
.then(() => {
// Install node modules
if (process.env.CERT_FILE) {
fs.writeFileSync(`${__dirname}/ioBrokerCodeSigningCertificate.pfx`, Buffer.from(process.env.CERT_FILE, 'base64'));
console.log(`Saved ${Buffer.from(process.env.CERT_FILE, 'base64').length} bytes in certificate`);
} else if (fs.existsSync(`${__dirname}/ioBrokerCodeSigningCertificate.pfx`) && !fs.existsSync(`${__dirname}/ioBrokerCodeSigningCertificate.base64.txt`)) {
fs.writeFileSync(`${__dirname}/ioBrokerCodeSigningCertificate.base64.txt`, fs.readFileSync(`${__dirname}/ioBrokerCodeSigningCertificate.pfx`).toString('base64'));
} else if (!fs.existsSync(`${__dirname}/ioBrokerCodeSigningCertificate.pfx`)) {
throw new Error('NO cert file found');
}
const cmd = `${__dirname}\\build\\windows\\ezsign\\EZSignIt.exe ` +
`/sn "${__dirname}\\delivery\\iobroker-installer.exe" ` +
`/f "${__dirname}\\ioBrokerCodeSigningCertificate.pfx" ` +
`/p ${process.env.CERT_PASSWORD} ` +
`/fd sha256 ` +
`/nse ` +
`/d "ioBroker windows installer" ` +
`/trs2 "http://timestamp.comodoca.com/?td=sha256"`;
console.log(`"${cmd.replace(process.env.CERT_PASSWORD, `*****${(process.env.CERT_PASSWORD || '').length + 5}`)}`);
// System call used for update of js-controller itself,
// because during an installation the npm packet will be deleted too, but some files must be loaded even during the installation process.
const exec = require('child_process').exec;
const child = exec(cmd);
child.stderr.pipe(process.stderr);
child.stdout.pipe(process.stdout);
child.on('exit', (code /* , signal */) => {
// code 1 is a strange error that cannot be explained. Everything is installed but error :(
if (code) {
const exitCodes = [
'0 = Success',
'1 = Invalid command-line or general program error',
'2 = Certificate password is incorrect',
'3 = Certificate could not be added / General certificate signing error',
'4 = Timestamp not added / Timestamp server-related error',
'5 = Signature Validation failed',
];
reject(new Error(`Cannot sign: ${exitCodes[code]} (${code})`));
} else {
console.log(`"${cmd.replace(process.env.CERT_PASSWORD, `*****${(process.env.CERT_PASSWORD || '').length + 5}`)}" finished successfully.`);
// rename to iobroker-installer-${version}.exe and to iobroker-installer-latest.exe
fs.renameSync(`${__dirname}/delivery/iobroker-installer.exe`, `${__dirname}/delivery/iobroker-installer-${version}.exe`);
fs.copyFileSync(`${__dirname}/delivery/iobroker-installer-${version}.exe`, `${__dirname}/delivery/iobroker-latest-windows-installer.exe`);
// command succeeded
resolve();
}
});
})
.catch(error => reject(error));
});
}
gulp.task('3-3-runMSI', runMSI);
gulp.task('3-4-rename', async () => {
if (fs.existsSync(`${__dirname}/delivery/ioBrokerInstaller.${version}.exe`) && fs.existsSync(`${__dirname}/delivery/iobroker-installer.exe`)) {
fs.unlinkSync(`${__dirname}/delivery/iobroker-installer.exe`);
}
fs.renameSync(`${__dirname}/delivery/ioBrokerInstaller.${version}.exe`, `${__dirname}/delivery/iobroker-installer.exe`);
});
gulp.task('3-5-signExe-manually', signExe);
if (/^win/.test(process.platform)) {
gulp.task('3-windows-msi', gulp.series(['3-0-writeWindowsVersion', '3-3-runMSI', '3-4-rename']));
} else {
gulp.task('3-windows-msi', async () => console.warn('Cannot create windows setup, while host is not windows'));
}
gulp.task('default', gulp.series(['0-clean', '3-windows-msi']));