Skip to content

Commit

Permalink
feat: NSIS script customization — script option
Browse files Browse the repository at this point in the history
Close #583
  • Loading branch information
develar committed Jul 28, 2016
1 parent 86e158f commit a807b17
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 14 deletions.
27 changes: 27 additions & 0 deletions docs/NSIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@

If you build both ia32 and xha arch, you in any case get one installer. Appropriate arch will be installed automatically.

# Custom NSIS script

Two options are available — [include](https://github.com/electron-userland/electron-builder/wiki/Options#NsisOptions-include) and [script](https://github.com/electron-userland/electron-builder/wiki/Options#NsisOptions-script). `script` allows you to provide completely different NSIS script. For most cases it is not required as you need only to customise some aspects, but still use well-tested and maintained default NSIS script. So, `include` is recommended.

Keep in mind — if you customize NSIS script, you should always state about it in the issue reports. And don't expect that your issue will be resolved.

1. Add file `build/installer.nsh`.
2. Define wanted macro to customise: `customHeader`, `customInit`, `customUnInit`, `customInstall`, `customUnInstall`. Example:
```nsis
!macro customHeader
!system "echo '' > ${BUILD_RESOURCES_DIR}/customHeader"
!macroend
!macro customInit
!system "echo '' > ${BUILD_RESOURCES_DIR}/customInit"
!macroend
!macro customInstall
!system "echo '' > ${BUILD_RESOURCES_DIR}/customInstall"
!macroend
```

* `BUILD_RESOURCES_DIR` and `PROJECT_DIR` are defined.
* `build` is added as `addincludedir` (i.e. you don't need to use `BUILD_RESOURCES_DIR` to include files).
* File associations macro `registerFileAssociations` and `unregisterFileAssociations` are still defined.
* All other electron-builder specific flags (e.g. `ONE_CLICK`) are still defined.

# GUID vs Application Name

Windows requires to use registry keys (e.g. INSTALL/UNINSTALL info). Squirrel.Windows simply uses application name as key.
Expand Down
4 changes: 3 additions & 1 deletion docs/Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ See [NSIS target notes](https://github.com/electron-userland/electron-builder/wi
| perMachine | <a name="NsisOptions-perMachine"></a>Install per all users (per-machine). Defaults to `false`.
| allowElevation | <a name="NsisOptions-allowElevation"></a>*boring installer only.* Allow requesting for elevation. If false, user will have to restart installer with elevated permissions. Defaults to `true`.
| runAfterFinish | <a name="NsisOptions-runAfterFinish"></a>*one-click installer only.* Run application after finish. Defaults to `true`.
| guid | <a name="NsisOptions-guid"></a>See [GUID vs Application Name](https://github.com/electron-userland/electron-builder/wiki/NSIS#guid-vs-application-name).
| installerHeader | <a name="NsisOptions-installerHeader"></a>*boring installer only.* `MUI_HEADERIMAGE`, relative to the project directory. Defaults to `build/installerHeader.bmp`
| installerHeaderIcon | <a name="NsisOptions-installerHeaderIcon"></a>*one-click installer only.* The path to header icon (above the progress bar), relative to the project directory. Defaults to `build/installerHeaderIcon.ico` or application icon.
| include | <a name="NsisOptions-include"></a>The path to NSIS include script to customize installer. Defaults to `build/installer.nsh`
| include | <a name="NsisOptions-include"></a>The path to NSIS include script to customize installer. Defaults to `build/installer.nsh`. See [Custom NSIS script](https://github.com/electron-userland/electron-builder/wiki/NSIS#custom-nsis-script).
| script | <a name="NsisOptions-script"></a>The path to NSIS script to customize installer. Defaults to `build/installer.nsi`. See [Custom NSIS script](https://github.com/electron-userland/electron-builder/wiki/NSIS#custom-nsis-script).

<a name="LinuxBuildOptions"></a>
### `.build.linux`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"pre-git": "^3.10.0",
"should": "^10.0.0",
"ts-babel": "^1.0.3",
"tslint": "3.14.0",
"tslint": "3.14.0-dev.1",
"typescript": "^2.1.0-dev.20160726",
"whitespace": "^2.0.0"
},
Expand Down
10 changes: 9 additions & 1 deletion src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ export interface NsisOptions {
*/
readonly runAfterFinish?: boolean | null

/*
See [GUID vs Application Name](https://github.com/electron-userland/electron-builder/wiki/NSIS#guid-vs-application-name).
*/
readonly guid?: string | null

/*
Expand All @@ -389,9 +392,14 @@ export interface NsisOptions {
readonly installerHeaderIcon?: string | null

/*
The path to NSIS include script to customize installer. Defaults to `build/installer.nsh`
The path to NSIS include script to customize installer. Defaults to `build/installer.nsh`. See [Custom NSIS script](https://github.com/electron-userland/electron-builder/wiki/NSIS#custom-nsis-script).
*/
readonly include?: string | null

/*
The path to NSIS script to customize installer. Defaults to `build/installer.nsi`. See [Custom NSIS script](https://github.com/electron-userland/electron-builder/wiki/NSIS#custom-nsis-script).
*/
readonly script?: string | null
}

/*
Expand Down
17 changes: 11 additions & 6 deletions src/targets/fpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ export default class FpmTarget extends TargetEx {
const tempDir = await tempDirPromise
const defaultTemplatesDir = path.join(__dirname, "..", "..", "templates", "linux")

const packager = this.packager
const templateOptions = Object.assign({
// old API compatibility
executable: this.packager.appInfo.productFilename,
}, this.packager.platformSpecificBuildOptions)
executable: packager.appInfo.productFilename,
}, packager.platformSpecificBuildOptions)

const afterInstallTemplate = this.packager.platformSpecificBuildOptions.afterInstall || path.join(defaultTemplatesDir, "after-install.tpl")
const afterInstallFilePath = writeConfigFile(tempDir, afterInstallTemplate, templateOptions)
function getResource(value: string | n, defaultFile: string) {
if (value == null) {
return path.join(defaultTemplatesDir, defaultFile)
}
return path.resolve(packager.projectDir, value)
}

const afterRemoveTemplate = this.packager.platformSpecificBuildOptions.afterRemove || path.join(defaultTemplatesDir, "after-remove.tpl")
const afterRemoveFilePath = writeConfigFile(tempDir, afterRemoveTemplate, templateOptions)
const afterInstallFilePath = writeConfigFile(tempDir, getResource(packager.platformSpecificBuildOptions.afterInstall, "after-install.tpl"), templateOptions)
const afterRemoveFilePath = writeConfigFile(tempDir, getResource(packager.platformSpecificBuildOptions.afterRemove, "after-remove.tpl"), templateOptions)

return await BluebirdPromise.all<string>([afterInstallFilePath, afterRemoveFilePath])
}
Expand Down
6 changes: 4 additions & 2 deletions src/targets/nsis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export default class NsisTarget extends Target {
const fileAssociations = asArray(packager.devMetadata.build.fileAssociations).concat(asArray(packager.platformSpecificBuildOptions.fileAssociations))
let registerFileAssociationsScript = ""
let unregisterFileAssociationsScript = ""
let script = await readFile(path.join(nsisTemplatesDir, "installer.nsi"), "utf8")
let script = await readFile((await this.getResource(this.options.script, "installer.nsi")) || path.join(nsisTemplatesDir, "installer.nsi"), "utf8")

const customInclude = await this.getResource(this.options.include, "installer.nsh")
if (customInclude != null) {
Expand All @@ -223,7 +223,9 @@ export default class NsisTarget extends Target {
script = script.replace("!insertmacro registerFileAssociations", registerFileAssociationsScript)
script = script.replace("!insertmacro unregisterFileAssociations", unregisterFileAssociationsScript)

debug(script)
if (debug.enabled) {
process.stdout.write("\n\nNSIS script:\n\n" + script + "\n\n---\nEnd of NSIS script.\n\n")
}

await new BluebirdPromise<any>((resolve, reject) => {
const command = path.join(nsisPath, binDir, process.platform === "win32" ? "makensis.exe" : "makensis")
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/installer.nsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
!system "echo '' > ${BUILD_RESOURCES_DIR}/customInstallerScript"

Section
SectionEnd
16 changes: 14 additions & 2 deletions test/src/winPackagerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ test.ifNotCiOsx("nsis boring, MUI_HEADER as option", () => {
tempDirCreated: projectDir => {
installerHeaderPath = path.join(projectDir, "foo.bmp")
return copy(getTestAsset("installerHeader.bmp"), installerHeaderPath)
}
},
}
)
})
Expand All @@ -142,6 +142,18 @@ test.ifDevOrLinuxCi("nsis - custom include", () => assertPack("test-app-one", {
targets: Platform.WINDOWS.createTarget(["nsis"]),
}, {
tempDirCreated: projectDir => copy(getTestAsset("installer.nsh"), path.join(projectDir, "build", "installer.nsh")),
packed: projectDir => BluebirdPromise.all([
assertThat(path.join(projectDir, "build", "customHeader")).isFile(),
assertThat(path.join(projectDir, "build", "customInit")).isFile(),
assertThat(path.join(projectDir, "build", "customInstall")).isFile(),
]),
}))

test.ifDevOrLinuxCi("nsis - custom script", () => assertPack("test-app-one", {
targets: Platform.WINDOWS.createTarget(["nsis"]),
}, {
tempDirCreated: projectDir => copy(getTestAsset("installer.nsi"), path.join(projectDir, "build", "installer.nsi")),
packed: projectDir => assertThat(path.join(projectDir, "build", "customInstallerScript")).isFile(),
}))

// very slow
Expand All @@ -152,7 +164,7 @@ test.skip("delta and msi", () => assertPack("test-app-one", {
win: {
remoteReleases: "https://github.com/develar/__test-app-releases",
msi: true,
}
},
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "tslint:latest",
"extends": "tslint:recommended",
"rules": {
"member-ordering": [
"static-before-instance",
Expand Down

0 comments on commit a807b17

Please sign in to comment.