-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathindex.ts
230 lines (183 loc) · 7.04 KB
/
index.ts
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
import * as asar from '@electron/asar';
import { createTempDir } from './temp-utils';
import * as fs from 'fs-extra';
import { Metadata, SquirrelWindowsOptions, PersonMetadata } from './options';
import * as path from 'path';
import spawn from './spawn-promise';
import template from 'lodash.template';
export { SquirrelWindowsOptions } from './options';
export { SquirrelWindowsOptions as Options} from './options';
const log = require('debug')('electron-windows-installer:main');
export function convertVersion(version: string): string {
const parts = version.split('+')[0].split('-');
const mainVersion = parts.shift();
if (parts.length > 0) {
return [mainVersion, parts.join('-').replace(/\./g, '')].join('-');
} else {
return mainVersion as string;
}
}
export async function createWindowsInstaller(options: SquirrelWindowsOptions): Promise<void> {
let useMono = false;
const monoExe = 'mono';
const wineExe = process.arch === 'x64' ? 'wine64' : 'wine';
if (process.platform !== 'win32') {
useMono = true;
if (!wineExe || !monoExe) {
throw new Error('You must install both Mono and Wine on non-Windows');
}
log(`Using Mono: '${monoExe}'`);
log(`Using Wine: '${wineExe}'`);
}
// eslint-disable-next-line prefer-const
let { appDirectory, outputDirectory, loadingGif } = options;
outputDirectory = path.resolve(outputDirectory || 'installer');
const vendorPath = path.join(__dirname, '..', 'vendor');
const vendorUpdate = path.join(vendorPath, 'Squirrel.exe');
const appUpdate = path.join(appDirectory, 'Squirrel.exe');
await fs.copy(vendorUpdate, appUpdate);
if (options.setupIcon && (options.skipUpdateIcon !== true)) {
let cmd = path.join(vendorPath, 'rcedit.exe');
const args = [
appUpdate,
'--set-icon', options.setupIcon
];
if (useMono) {
args.unshift(cmd);
cmd = wineExe;
}
await spawn(cmd, args);
}
const defaultLoadingGif = path.join(__dirname, '..', 'resources', 'install-spinner.gif');
loadingGif = loadingGif ? path.resolve(loadingGif) : defaultLoadingGif;
const { certificateFile, certificatePassword, remoteReleases, signWithParams, remoteToken } = options;
const metadata: Metadata = {
description: '',
iconUrl: 'https://raw.githubusercontent.com/electron/electron/main/shell/browser/resources/win/electron.ico'
};
if (options.usePackageJson !== false) {
const appResources = path.join(appDirectory, 'resources');
const asarFile = path.join(appResources, 'app.asar');
let appMetadata;
if (await fs.pathExists(asarFile)) {
appMetadata = JSON.parse(asar.extractFile(asarFile, 'package.json').toString());
} else {
appMetadata = await fs.readJson(path.join(appResources, 'app', 'package.json'));
}
Object.assign(metadata, {
exe: `${appMetadata.name}.exe`,
title: appMetadata.productName || appMetadata.name
}, appMetadata);
}
Object.assign(metadata, options);
if (!metadata.authors) {
if (typeof (metadata.author) === 'string') {
metadata.authors = metadata.author;
} else {
metadata.authors = (metadata.author || ({} as PersonMetadata)).name || '';
}
}
metadata.owners = metadata.owners || metadata.authors;
metadata.version = convertVersion(metadata.version as string);
metadata.copyright = metadata.copyright ||
`Copyright © ${new Date().getFullYear()} ${metadata.authors || metadata.owners}`;
metadata.additionalFiles = metadata.additionalFiles || [];
if (await fs.pathExists(path.join(appDirectory, 'swiftshader'))) {
metadata.additionalFiles.push({ src: 'swiftshader\\**', target: 'lib\\net45\\swiftshader' });
}
if (await fs.pathExists(path.join(appDirectory, 'vk_swiftshader_icd.json'))) {
metadata.additionalFiles.push({ src: 'vk_swiftshader_icd.json', target: 'lib\\net45' });
}
let templateData = await fs.readFile(path.join(__dirname, '..', 'template.nuspectemplate'), 'utf8');
if (path.sep === '/') {
templateData = templateData.replace(/\\/g, '/');
for (const f of metadata.additionalFiles) {
f.src = f.src.replace(/\\/g, '/');
f.target = f.target.replace(/\\/g, '/');
}
}
const nuspecContent = template(templateData)(metadata);
log(`Created NuSpec file:\n${nuspecContent}`);
const nugetOutput = await createTempDir('si-');
const targetNuspecPath = path.join(nugetOutput, metadata.name + '.nuspec');
await fs.writeFile(targetNuspecPath, nuspecContent);
let cmd = path.join(vendorPath, 'nuget.exe');
let args = [
'pack', targetNuspecPath,
'-BasePath', appDirectory,
'-OutputDirectory', nugetOutput,
'-NoDefaultExcludes'
];
if (useMono) {
args.unshift(cmd);
cmd = monoExe;
}
// Call NuGet to create our package
log(await spawn(cmd, args));
const nupkgPath = path.join(nugetOutput, `${metadata.name}.${metadata.version}.nupkg`);
if (remoteReleases) {
cmd = path.join(vendorPath, 'SyncReleases.exe');
args = ['-u', remoteReleases, '-r', outputDirectory];
if (useMono) {
args.unshift(cmd);
cmd = monoExe;
}
if (remoteToken) {
args.push('-t', remoteToken);
}
log(await spawn(cmd, args));
}
cmd = path.join(vendorPath, 'Squirrel.exe');
args = [
'--releasify', nupkgPath,
'--releaseDir', outputDirectory,
'--loadingGif', loadingGif
];
if (useMono) {
args.unshift(path.join(vendorPath, 'Squirrel-Mono.exe'));
cmd = monoExe;
}
if (signWithParams) {
args.push('--signWithParams');
if (!signWithParams.includes('/f') && !signWithParams.includes('/p') && certificateFile && certificatePassword) {
args.push(`${signWithParams} /a /f "${path.resolve(certificateFile)}" /p "${certificatePassword}"`);
} else {
args.push(signWithParams);
}
} else if (certificateFile && certificatePassword) {
args.push('--signWithParams');
args.push(`/a /f "${path.resolve(certificateFile)}" /p "${certificatePassword}"`);
}
if (options.setupIcon) {
args.push('--setupIcon');
args.push(path.resolve(options.setupIcon));
}
if (options.noMsi) {
args.push('--no-msi');
}
if (options.noDelta) {
args.push('--no-delta');
}
if (options.frameworkVersion) {
args.push('--framework-version');
args.push(options.frameworkVersion);
}
log(await spawn(cmd, args));
if (options.fixUpPaths !== false) {
log('Fixing up paths');
if (metadata.productName || options.setupExe) {
const setupPath = path.join(outputDirectory, options.setupExe || `${metadata.productName}Setup.exe`);
const unfixedSetupPath = path.join(outputDirectory, 'Setup.exe');
log(`Renaming ${unfixedSetupPath} => ${setupPath}`);
await fs.rename(unfixedSetupPath, setupPath);
}
if (metadata.productName || options.setupMsi) {
const msiPath = path.join(outputDirectory, options.setupMsi || `${metadata.productName}Setup.msi`);
const unfixedMsiPath = path.join(outputDirectory, 'Setup.msi');
if (await fs.pathExists(unfixedMsiPath)) {
log(`Renaming ${unfixedMsiPath} => ${msiPath}`);
await fs.rename(unfixedMsiPath, msiPath);
}
}
}
}