Skip to content

Commit

Permalink
feat(nsis): ability to recreate desktop shortcut on fresh install
Browse files Browse the repository at this point in the history
Close #2725
  • Loading branch information
develar committed Mar 21, 2018
1 parent 0ae220e commit 432b3e9
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export interface CommonWindowsInstallerConfiguration {
readonly runAfterFinish?: boolean

/**
* Whether to create desktop shortcut.
* Whether to create desktop shortcut. Set to `always` if to recreate also on reinstall (even if removed by user).
* @default true
*/
readonly createDesktopShortcut?: boolean
readonly createDesktopShortcut?: boolean | "always"

/**
* Whether to create start menu shortcut.
Expand All @@ -48,7 +48,7 @@ export interface FinalCommonWindowsInstallerOptions {
shortcutName: string
menuCategory: string | null

isCreateDesktopShortcut: boolean
isCreateDesktopShortcut: DesktopShortcutCreationPolicy
isCreateStartMenuShortcut: boolean
}

Expand All @@ -74,8 +74,24 @@ export function getEffectiveOptions(options: CommonWindowsInstallerConfiguration
isAssisted: options.oneClick === false,

shortcutName: isEmptyOrSpaces(options.shortcutName) ? appInfo.productFilename : packager.expandMacro(options.shortcutName!!),
isCreateDesktopShortcut: options.createDesktopShortcut !== false,
isCreateDesktopShortcut: convertToDesktopShortcutCreationPolicy(options.createDesktopShortcut),
isCreateStartMenuShortcut: options.createStartMenuShortcut !== false,
menuCategory,
}
}

function convertToDesktopShortcutCreationPolicy(value: boolean | undefined | string): DesktopShortcutCreationPolicy {
if (value === false) {
return DesktopShortcutCreationPolicy.NEVER
}
else if (value === "always") {
return DesktopShortcutCreationPolicy.ALWAYS
}
else {
return DesktopShortcutCreationPolicy.FRESH_INSTALL
}
}

export enum DesktopShortcutCreationPolicy {
FRESH_INSTALL, ALWAYS, NEVER
}
6 changes: 3 additions & 3 deletions packages/electron-builder-lib/src/targets/MsiTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Lazy } from "lazy-val"
import * as path from "path"
import { MsiOptions } from "../"
import { Target } from "../core"
import { FinalCommonWindowsInstallerOptions, getEffectiveOptions } from "../options/CommonWindowsInstallerConfiguration"
import { DesktopShortcutCreationPolicy, FinalCommonWindowsInstallerOptions, getEffectiveOptions } from "../options/CommonWindowsInstallerConfiguration"
import { getTemplatePath } from "../util/pathManager"
import { VmManager } from "../vm/vm"
import { WineVmManager } from "../vm/WineVm"
Expand Down Expand Up @@ -209,10 +209,10 @@ export default class MsiTarget extends Target {
else if (directoryId === null) {
result += ` Id="${path.basename(packagePath)}_f"`
}
if (isMainExecutable && (commonOptions.isCreateDesktopShortcut || commonOptions.isCreateStartMenuShortcut)) {
if (isMainExecutable && (commonOptions.isCreateDesktopShortcut !== DesktopShortcutCreationPolicy.NEVER || commonOptions.isCreateStartMenuShortcut)) {
result += `>\n`
const shortcutName = commonOptions.shortcutName
if (commonOptions.isCreateDesktopShortcut) {
if (commonOptions.isCreateDesktopShortcut !== DesktopShortcutCreationPolicy.NEVER) {
result += `${fileSpace} <Shortcut Id="desktopShortcut" Directory="DesktopFolder" Name="${shortcutName}" WorkingDirectory="APPLICATIONFOLDER" Advertise="yes" Icon="icon.ico"/>\n`
}

Expand Down
7 changes: 5 additions & 2 deletions packages/electron-builder-lib/src/targets/nsis/NsisTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { readFile, stat, unlink } from "fs-extra-p"
import { Lazy } from "lazy-val"
import * as path from "path"
import { Target } from "../../core"
import { getEffectiveOptions } from "../../options/CommonWindowsInstallerConfiguration"
import { DesktopShortcutCreationPolicy, getEffectiveOptions } from "../../options/CommonWindowsInstallerConfiguration"
import { isSafeGithubName, normalizeExt } from "../../platformPackager"
import { time } from "../../util/timer"
import { WinPackager } from "../../winPackager"
Expand Down Expand Up @@ -386,9 +386,12 @@ export class NsisTarget extends Target {
})

defines.UNINSTALL_DISPLAY_NAME = packager.expandMacro(options.uninstallDisplayName || "${productName} ${version}", null, {}, false)
if (!commonOptions.isCreateDesktopShortcut) {
if (commonOptions.isCreateDesktopShortcut === DesktopShortcutCreationPolicy.NEVER) {
defines.DO_NOT_CREATE_DESKTOP_SHORTCUT = null
}
if (commonOptions.isCreateDesktopShortcut === DesktopShortcutCreationPolicy.ALWAYS) {
defines.RECREATE_DESKTOP_SHORTCUT = null
}
if (!commonOptions.isCreateStartMenuShortcut) {
defines.DO_NOT_CREATE_START_MENU_SHORTCUT = null
}
Expand Down
14 changes: 12 additions & 2 deletions packages/electron-builder-lib/templates/nsis/include/installer.nsh
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,27 @@
${ifNot} ${isNoDesktopShortcut}
# The keepShortcuts mechanism is NOT enabled.
# Shortcuts will be recreated.
${if} $keepShortcuts == "false"
${if} $keepShortcuts == "false"
CreateShortCut "$newDesktopLink" "$appExe" "" "$appExe" 0 "" "" "${APP_DESCRIPTION}"
ClearErrors
WinShell::SetLnkAUMI "$newDesktopLink" "${APP_ID}"
# The keepShortcuts mechanism IS enabled.
# The desktop shortcut could exist in an obsolete location (due to name change).
${elseif} $oldDesktopLink != $newDesktopLink
${andIf} ${FileExists} "$oldDesktopLink"
${orIf} ${FileExists} "$oldDesktopLink"
Rename $oldDesktopLink $newDesktopLink
WinShell::UninstShortcut "$oldDesktopLink"
WinShell::SetLnkAUMI "$newDesktopLink" "${APP_ID}"

!ifdef RECREATE_DESKTOP_SHORTCUT
${elseif} $oldDesktopLink != $newDesktopLink
${orIfNot} ${FileExists} "$oldDesktopLink"
${ifNot} ${isUpdated}
CreateShortCut "$newDesktopLink" "$appExe" "" "$appExe" 0 "" "" "${APP_DESCRIPTION}"
ClearErrors
WinShell::SetLnkAUMI "$newDesktopLink" "${APP_ID}"
${endIf}
!endif
${endIf}
System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'
${endIf}
Expand Down
24 changes: 24 additions & 0 deletions test/out/windows/__snapshots__/oneClickInstallerTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ Object {
}
`;

exports[`createDesktopShortcut always 1`] = `
Object {
"win": Array [
Object {
"arch": "x64",
"file": "Test App ßW Setup 1.1.0.exe",
"safeArtifactName": "TestApp-Setup-1.1.0.exe",
"updateInfo": Object {
"sha512": "@sha512",
"size": "@size",
},
},
Object {
"file": "Test App ßW Setup 1.1.0.exe.blockmap",
"safeArtifactName": "TestApp-Setup-1.1.0.exe.blockmap",
"updateInfo": Object {
"sha512": "@sha512",
"size": "@size",
},
},
],
}
`;

exports[`custom include 1`] = `
Object {
"win": Array [
Expand Down
10 changes: 10 additions & 0 deletions test/src/windows/oneClickInstallerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ test.ifAll.ifNotCiMac("html license", app({
},
}))

test.ifAll.ifDevOrWinCi("createDesktopShortcut always", app({
targets: Platform.WINDOWS.createTarget("nsis"),
config: {
publish: null,
nsis: {
createDesktopShortcut: "always",
}
},
}))

test.ifDevOrLinuxCi("perMachine, no run after finish", app({
targets: Platform.WINDOWS.createTarget(["nsis"], Arch.ia32),
config: {
Expand Down

0 comments on commit 432b3e9

Please sign in to comment.