|
| 1 | +/** |
| 2 | + * Create a .exe installer that bundles one or more .kmp files, together with |
| 3 | + * setup.exe, keymandesktop.msi, and generates and includes a setup.inf also. |
| 4 | + * |
| 5 | + * This module is effectively deprecated, but is present to keep parity with the |
| 6 | + * legacy kmcomp compiler. Thus, it is included as part of the package compiler, |
| 7 | + * and will be removed in a future version. |
| 8 | + * |
| 9 | + * This tool assumes that the installer .msi is the same version as the |
| 10 | + * compiler, unlike the legacy compiler which read this metadata from the .msi. |
| 11 | + */ |
| 12 | + |
| 13 | +import JSZip from 'jszip'; |
| 14 | +import { CompilerCallbacks, KeymanFileTypes, KmpJsonFile, KpsFile } from "@keymanapp/common-types"; |
| 15 | +import KEYMAN_VERSION from "@keymanapp/keyman-version"; |
| 16 | +import { KmpCompiler } from "./kmp-compiler.js"; |
| 17 | +import { CompilerMessages } from "./messages.js"; |
| 18 | + |
| 19 | +const SETUP_INF_FILENAME = 'setup.inf'; |
| 20 | +const PRODUCT_NAME = 'Keyman'; |
| 21 | + |
| 22 | +export interface WindowsPackageInstallerSources { |
| 23 | + msiFilename: string; |
| 24 | + setupExeFilename: string; |
| 25 | + licenseFilename: string; // MIT license |
| 26 | + titleImageFilename?: string; |
| 27 | + |
| 28 | + appName?: string; |
| 29 | + startDisabled: boolean; |
| 30 | + startWithConfiguration: boolean; |
| 31 | +}; |
| 32 | + |
| 33 | +export class WindowsPackageInstallerCompiler { |
| 34 | + private kmpCompiler: KmpCompiler; |
| 35 | + |
| 36 | + constructor(private callbacks: CompilerCallbacks) { |
| 37 | + this.kmpCompiler = new KmpCompiler(this.callbacks); |
| 38 | + } |
| 39 | + |
| 40 | + public async compile(kpsFilename: string, sources: WindowsPackageInstallerSources): Promise<Uint8Array> { |
| 41 | + const kps = this.kmpCompiler.loadKpsFile(kpsFilename); |
| 42 | + |
| 43 | + // Check existence of required files |
| 44 | + for(const filename of [sources.licenseFilename, sources.msiFilename, sources.setupExeFilename]) { |
| 45 | + if(!this.callbacks.fs.existsSync(filename)) { |
| 46 | + this.callbacks.reportMessage(CompilerMessages.Error_FileDoesNotExist({filename})); |
| 47 | + return null; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Check existence of optional files |
| 52 | + for(const filename of [sources.titleImageFilename]) { |
| 53 | + if(filename && !this.callbacks.fs.existsSync(filename)) { |
| 54 | + this.callbacks.reportMessage(CompilerMessages.Error_FileDoesNotExist({filename})); |
| 55 | + return null; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Note: we never use the MSIFileName field from the .kps any more |
| 60 | + // Nor do we use the MSIOptions field. |
| 61 | + |
| 62 | + // Build the zip |
| 63 | + const zipBuffer = await this.buildZip(kps, kpsFilename, sources); |
| 64 | + if(!zipBuffer) { |
| 65 | + // Error messages already reported by buildZip |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + // Build the sfx |
| 70 | + const sfxBuffer = this.buildSfx(zipBuffer, sources); |
| 71 | + return sfxBuffer; |
| 72 | + } |
| 73 | + |
| 74 | + private async buildZip(kps: KpsFile.KpsFile, kpsFilename: string, sources: WindowsPackageInstallerSources): Promise<Uint8Array> { |
| 75 | + const kmpJson: KmpJsonFile.KmpJsonFile = this.kmpCompiler.transformKpsFileToKmpObject(kpsFilename, kps); |
| 76 | + if(!kmpJson.info?.name?.description) { |
| 77 | + this.callbacks.reportMessage(CompilerMessages.Error_PackageNameCannotBeBlank()); |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + const kmpFilename = this.callbacks.path.basename(kpsFilename, KeymanFileTypes.Source.Package) + KeymanFileTypes.Binary.Package; |
| 82 | + const setupInfBuffer = this.buildSetupInf(sources, kmpJson, kmpFilename, kps); |
| 83 | + const kmpBuffer = await this.kmpCompiler.buildKmpFile(kpsFilename, kmpJson); |
| 84 | + |
| 85 | + // Note that this does not technically generate a "valid" sfx according to |
| 86 | + // the zip spec, because the offsets in the .zip are relative to the start |
| 87 | + // of the zip, rather than to the start of the sfx. However, as Keyman's |
| 88 | + // installer chops out the zip from the sfx and loads it in a new stream, it |
| 89 | + // works as expected. |
| 90 | + const zip = JSZip(); |
| 91 | + zip.file(SETUP_INF_FILENAME, setupInfBuffer); |
| 92 | + zip.file(kmpFilename, kmpBuffer); |
| 93 | + zip.file(this.callbacks.path.basename(sources.msiFilename), this.callbacks.loadFile(sources.msiFilename)); |
| 94 | + zip.file(this.callbacks.path.basename(sources.licenseFilename), this.callbacks.loadFile(sources.licenseFilename)); |
| 95 | + if(sources.titleImageFilename) { |
| 96 | + zip.file(this.callbacks.path.basename(sources.titleImageFilename), this.callbacks.loadFile(sources.titleImageFilename)); |
| 97 | + } |
| 98 | + |
| 99 | + return zip.generateAsync({type: 'uint8array', compression:'DEFLATE'}); |
| 100 | + } |
| 101 | + |
| 102 | + private buildSetupInf(sources: WindowsPackageInstallerSources, kmpJson: KmpJsonFile.KmpJsonFile, kmpFilename: string, kps: KpsFile.KpsFile) { |
| 103 | + let setupInf = `[Setup] |
| 104 | +Version=${KEYMAN_VERSION.VERSION} |
| 105 | +MSIFileName=${this.callbacks.path.basename(sources.msiFilename)} |
| 106 | +MSIOptions= |
| 107 | +AppName=${sources.appName ?? PRODUCT_NAME} |
| 108 | +License=${this.callbacks.path.basename(sources.licenseFilename)} |
| 109 | +`; |
| 110 | + if (sources.titleImageFilename) { |
| 111 | + setupInf += `TitleImage=${this.callbacks.path.basename(sources.titleImageFilename)}\n`; |
| 112 | + } |
| 113 | + if (kmpJson.options.graphicFile) { |
| 114 | + setupInf += `BitmapFileName=${this.callbacks.path.basename(kmpJson.options.graphicFile)}\n`; |
| 115 | + } |
| 116 | + if (sources.startDisabled) { |
| 117 | + setupInf += `StartDisabled=True\n`; |
| 118 | + } |
| 119 | + if (sources.startWithConfiguration) { |
| 120 | + setupInf += `StartWithConfiguration=True\n`; |
| 121 | + } |
| 122 | + |
| 123 | + setupInf += `\n[Packages]\n`; |
| 124 | + setupInf += kmpFilename + '\n'; |
| 125 | + // TODO: multiple packages? |
| 126 | + const strings = !kps.strings?.string ? [] : (Array.isArray(kps.strings.string) ? kps.strings.string : [kps.strings.string]); |
| 127 | + if (strings.length) { |
| 128 | + setupInf += `\n[Strings]\n`; |
| 129 | + for (const str of strings) { |
| 130 | + setupInf += `${str.$?.name}=${str.$?.value}\n`; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + const setupInfBuffer = new TextEncoder().encode(setupInf); |
| 135 | + return setupInfBuffer; |
| 136 | + } |
| 137 | + |
| 138 | + private buildSfx(zipBuffer: Uint8Array, sources: WindowsPackageInstallerSources): Uint8Array { |
| 139 | + const setupRedistBuffer = this.callbacks.loadFile(sources.setupExeFilename); |
| 140 | + const buffer = new Uint8Array(setupRedistBuffer.length + zipBuffer.length); |
| 141 | + buffer.set(setupRedistBuffer, 0); |
| 142 | + buffer.set(zipBuffer, setupRedistBuffer.length); |
| 143 | + return buffer; |
| 144 | + } |
| 145 | +} |
0 commit comments