forked from electron-userland/electron-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMsiTarget.ts
287 lines (249 loc) · 13.1 KB
/
MsiTarget.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
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
import BluebirdPromise from "bluebird-lst"
import { Arch, asArray, log, deepAssign } from "builder-util"
import { UUID } from "builder-util-runtime"
import { getBinFromUrl } from "../binDownload"
import { walk } from "builder-util/out/fs"
import { createHash } from "crypto"
import * as ejs from "ejs"
import { readFile, writeFile } from "fs/promises"
import { Lazy } from "lazy-val"
import * as path from "path"
import { MsiOptions } from "../"
import { Target } from "../core"
import { DesktopShortcutCreationPolicy, FinalCommonWindowsInstallerOptions, getEffectiveOptions } from "../options/CommonWindowsInstallerConfiguration"
import { normalizeExt } from "../platformPackager"
import { getTemplatePath } from "../util/pathManager"
import { VmManager } from "../vm/vm"
import { WineVmManager } from "../vm/WineVm"
import { WinPackager } from "../winPackager"
import { createStageDir, getWindowsInstallationDirName } from "./targetUtil"
const ELECTRON_BUILDER_UPGRADE_CODE_NS_UUID = UUID.parse("d752fe43-5d44-44d5-9fc9-6dd1bf19d5cc")
const ROOT_DIR_ID = "APPLICATIONFOLDER"
const ASSISTED_UI_FILE_NAME = "WixUI_Assisted.wxs"
const projectTemplate = new Lazy<(data: any) => string>(async () => {
const template = (await readFile(path.join(getTemplatePath("msi"), "template.xml"), "utf8"))
.replace(/{{/g, "<%")
.replace(/}}/g, "%>")
.replace(/\${([^}]+)}/g, "<%=$1%>")
return ejs.compile(template)
})
// WiX doesn't support Mono, so, dontnet462 is required to be installed for wine (preinstalled in our bundled wine)
export default class MsiTarget extends Target {
private readonly vm = process.platform === "win32" ? new VmManager() : new WineVmManager()
readonly options: MsiOptions = deepAssign(this.packager.platformSpecificBuildOptions, this.packager.config.msi)
constructor(private readonly packager: WinPackager, readonly outDir: string) {
super("msi")
}
/**
* A product-specific string that can be used in an [MSI Identifier](https://docs.microsoft.com/en-us/windows/win32/msi/identifier).
*/
private get productMsiIdPrefix() {
const sanitizedId = this.packager.appInfo.productFilename.replace(/[^\w.]/g, "").replace(/^[^A-Za-z_]+/, "")
return sanitizedId.length > 0 ? sanitizedId : "ElectronApp"
}
private get iconId() {
return this.options.iconId ?? `${this.productMsiIdPrefix}Icon.exe`
}
async build(appOutDir: string, arch: Arch) {
const packager = this.packager
const artifactName = packager.expandArtifactBeautyNamePattern(this.options, "msi", arch)
const artifactPath = path.join(this.outDir, artifactName)
await packager.info.callArtifactBuildStarted({
targetPresentableName: "MSI",
file: artifactPath,
arch,
})
const stageDir = await createStageDir(this, packager, arch)
const vm = this.vm
const commonOptions = getEffectiveOptions(this.options, this.packager)
if (commonOptions.isAssisted) {
// F*** *** *** *** *** *** *** *** *** *** *** *** *** WiX *** *** *** *** *** *** *** *** ***
// cannot understand how to set MSIINSTALLPERUSER on radio box change. In any case installed per user.
log.warn(`MSI DOESN'T SUPPORT assisted installer. Please use NSIS instead.`)
}
const projectFile = stageDir.getTempFile("project.wxs")
const objectFiles = ["project.wixobj"]
const uiFile = commonOptions.isAssisted ? stageDir.getTempFile(ASSISTED_UI_FILE_NAME) : null
await writeFile(projectFile, await this.writeManifest(appOutDir, arch, commonOptions))
if (uiFile !== null) {
await writeFile(uiFile, await readFile(path.join(getTemplatePath("msi"), ASSISTED_UI_FILE_NAME), "utf8"))
objectFiles.push(ASSISTED_UI_FILE_NAME.replace(".wxs", ".wixobj"))
}
await packager.info.callMsiProjectCreated(projectFile)
// noinspection SpellCheckingInspection
const vendorPath = await getBinFromUrl("wix", "4.0.0.5512.2", "/X5poahdCc3199Vt6AP7gluTlT1nxi9cbbHhZhCMEu+ngyP1LiBMn+oZX7QAZVaKeBMc2SjVp7fJqNLqsUnPNQ==")
// noinspection SpellCheckingInspection
const candleArgs = ["-arch", arch === Arch.ia32 ? "x86" : arch === Arch.arm64 ? "arm64" : "x64", `-dappDir=${vm.toVmFile(appOutDir)}`].concat(this.getCommonWixArgs())
candleArgs.push("project.wxs")
if (uiFile !== null) {
candleArgs.push(ASSISTED_UI_FILE_NAME)
}
await vm.exec(vm.toVmFile(path.join(vendorPath, "candle.exe")), candleArgs, {
cwd: stageDir.dir,
})
await this.light(objectFiles, vm, artifactPath, appOutDir, vendorPath, stageDir.dir)
await stageDir.cleanup()
await packager.sign(artifactPath)
await packager.info.callArtifactBuildCompleted({
file: artifactPath,
packager,
arch,
safeArtifactName: packager.computeSafeArtifactName(artifactName, "msi"),
target: this,
isWriteUpdateInfo: false,
})
}
private async light(objectFiles: Array<string>, vm: VmManager, artifactPath: string, appOutDir: string, vendorPath: string, tempDir: string) {
// noinspection SpellCheckingInspection
const lightArgs = [
"-out",
vm.toVmFile(artifactPath),
"-v",
// https://github.com/wixtoolset/issues/issues/5169
"-spdb",
// https://sourceforge.net/p/wix/bugs/2405/
// error LGHT1076 : ICE61: This product should remove only older versions of itself. The Maximum version is not less than the current product. (1.1.0.42 1.1.0.42)
"-sw1076",
`-dappDir=${vm.toVmFile(appOutDir)}`,
// "-dcl:high",
].concat(this.getCommonWixArgs())
// http://windows-installer-xml-wix-toolset.687559.n2.nabble.com/Build-3-5-2229-0-give-me-the-following-error-error-LGHT0216-An-unexpected-Win32-exception-with-errorn-td5707443.html
if (process.platform !== "win32") {
// noinspection SpellCheckingInspection
lightArgs.push("-sval")
}
if (this.options.oneClick === false) {
lightArgs.push("-ext", "WixUIExtension")
}
// objectFiles - only filenames, we set current directory to our temp stage dir
lightArgs.push(...objectFiles)
await vm.exec(vm.toVmFile(path.join(vendorPath, "light.exe")), lightArgs, {
cwd: tempDir,
})
}
private getCommonWixArgs() {
const args: Array<string> = ["-pedantic"]
if (this.options.warningsAsErrors !== false) {
args.push("-wx")
}
if (this.options.additionalWixArgs != null) {
args.push(...this.options.additionalWixArgs)
}
return args
}
private async writeManifest(appOutDir: string, arch: Arch, commonOptions: FinalCommonWindowsInstallerOptions) {
const appInfo = this.packager.appInfo
const { files, dirs } = await this.computeFileDeclaration(appOutDir)
const companyName = appInfo.companyName
if (!companyName) {
log.warn(`Manufacturer is not set for MSI — please set "author" in the package.json`)
}
const compression = this.packager.compression
const options = this.options
const iconPath = await this.packager.getIconPath()
return (await projectTemplate.value)({
...commonOptions,
isCreateDesktopShortcut: commonOptions.isCreateDesktopShortcut !== DesktopShortcutCreationPolicy.NEVER,
isRunAfterFinish: options.runAfterFinish !== false,
iconPath: iconPath == null ? null : this.vm.toVmFile(iconPath),
iconId: this.iconId,
compressionLevel: compression === "store" ? "none" : "high",
version: appInfo.getVersionInWeirdWindowsForm(),
productName: appInfo.productName,
upgradeCode: (options.upgradeCode || UUID.v5(appInfo.id, ELECTRON_BUILDER_UPGRADE_CODE_NS_UUID)).toUpperCase(),
manufacturer: companyName || appInfo.productName,
appDescription: appInfo.description,
// https://stackoverflow.com/questions/1929038/compilation-error-ice80-the-64bitcomponent-uses-32bitdirectory
programFilesId: arch === Arch.x64 ? "ProgramFiles64Folder" : "ProgramFilesFolder",
// wix in the name because special wix format can be used in the name
installationDirectoryWixName: getWindowsInstallationDirName(appInfo, commonOptions.isPerMachine === true),
dirs,
files,
})
}
private async computeFileDeclaration(appOutDir: string) {
const appInfo = this.packager.appInfo
let isRootDirAddedToRemoveTable = false
const dirNames = new Set<string>()
const dirs: Array<string> = []
const fileSpace = " ".repeat(6)
const commonOptions = getEffectiveOptions(this.options, this.packager)
const files = await BluebirdPromise.map(walk(appOutDir), file => {
const packagePath = file.substring(appOutDir.length + 1)
const lastSlash = packagePath.lastIndexOf(path.sep)
const fileName = lastSlash > 0 ? packagePath.substring(lastSlash + 1) : packagePath
let directoryId: string | null = null
let dirName = ""
// Wix Directory.FileSource doesn't work - https://stackoverflow.com/questions/21519388/wix-filesource-confusion
if (lastSlash > 0) {
// This Name attribute may also define multiple directories using the inline directory syntax.
// For example, "ProgramFilesFolder:\My Company\My Product\bin" would create a reference to a Directory element with Id="ProgramFilesFolder" then create directories named "My Company" then "My Product" then "bin" nested beneath each other.
// This syntax is a shortcut to defining each directory in an individual Directory element.
dirName = packagePath.substring(0, lastSlash)
// https://github.com/electron-userland/electron-builder/issues/3027
directoryId = "d" + createHash("md5").update(dirName).digest("base64").replace(/\//g, "_").replace(/\+/g, ".").replace(/=+$/, "")
if (!dirNames.has(dirName)) {
dirNames.add(dirName)
dirs.push(`<Directory Id="${directoryId}" Name="${ROOT_DIR_ID}:\\${dirName.replace(/\//g, "\\")}\\"/>`)
}
} else if (!isRootDirAddedToRemoveTable) {
isRootDirAddedToRemoveTable = true
}
// since RegistryValue can be part of Component, *** *** *** *** *** *** *** *** *** wix cannot auto generate guid
// https://stackoverflow.com/questions/1405100/change-my-component-guid-in-wix
let result = `<Component${directoryId === null ? "" : ` Directory="${directoryId}"`}>`
result += `\n${fileSpace} <File Name="${fileName}" Source="$(var.appDir)${path.sep}${packagePath}" ReadOnly="yes" KeyPath="yes"`
const isMainExecutable = packagePath === `${appInfo.productFilename}.exe`
if (isMainExecutable) {
result += ' Id="mainExecutable"'
} else if (directoryId === null) {
result += ` Id="${path.basename(packagePath)}_f"`
}
const isCreateDesktopShortcut = commonOptions.isCreateDesktopShortcut !== DesktopShortcutCreationPolicy.NEVER
if (isMainExecutable && (isCreateDesktopShortcut || commonOptions.isCreateStartMenuShortcut)) {
result += `>\n`
const shortcutName = commonOptions.shortcutName
if (isCreateDesktopShortcut) {
result += `${fileSpace} <Shortcut Id="desktopShortcut" Directory="DesktopFolder" Name="${shortcutName}" WorkingDirectory="APPLICATIONFOLDER" Advertise="yes" Icon="${this.iconId}"/>\n`
}
const hasMenuCategory = commonOptions.menuCategory != null
const startMenuShortcutDirectoryId = hasMenuCategory ? "AppProgramMenuDir" : "ProgramMenuFolder"
if (commonOptions.isCreateStartMenuShortcut) {
if (hasMenuCategory) {
dirs.push(`<Directory Id="${startMenuShortcutDirectoryId}" Name="ProgramMenuFolder:\\${commonOptions.menuCategory}\\"/>`)
}
result += `${fileSpace} <Shortcut Id="startMenuShortcut" Directory="${startMenuShortcutDirectoryId}" Name="${shortcutName}" WorkingDirectory="APPLICATIONFOLDER" Advertise="yes" Icon="${this.iconId}">\n`
result += `${fileSpace} <ShortcutProperty Key="System.AppUserModel.ID" Value="${this.packager.appInfo.id}"/>\n`
result += `${fileSpace} </Shortcut>\n`
}
result += `${fileSpace}</File>`
if (hasMenuCategory) {
result += `<RemoveFolder Id="${startMenuShortcutDirectoryId}" Directory="${startMenuShortcutDirectoryId}" On="uninstall"/>\n`
}
} else {
result += `/>`
}
const fileAssociations = this.packager.fileAssociations
if (isMainExecutable && fileAssociations.length !== 0) {
for (const item of fileAssociations) {
const extensions = asArray(item.ext).map(normalizeExt)
for (const ext of extensions) {
result += `${fileSpace} <ProgId Id="${this.productMsiIdPrefix}.${ext}" Advertise="yes" Icon="${this.iconId}" ${
item.description ? `Description="${item.description}"` : ""
}>\n`
result += `${fileSpace} <Extension Id="${ext}" Advertise="yes">\n`
result += `${fileSpace} <Verb Id="open" Command="Open with ${this.packager.appInfo.productName}" Argument=""%1""/>\n`
result += `${fileSpace} </Extension>\n`
result += `${fileSpace} </ProgId>\n`
}
}
}
return `${result}\n${fileSpace}</Component>`
})
return { dirs: listToString(dirs, 2), files: listToString(files, 3) }
}
}
function listToString(list: Array<string>, indentLevel: number) {
const space = " ".repeat(indentLevel * 2)
return list.join(`\n${space}`)
}