Skip to content

Commit 68c39b8

Browse files
refactor(nsis): use nsis's built-in com plugin instead of ApplicationID plugin (#9527)
* Use WinShell instead of ApplicationID * Uninst shortcut before removing start menu one * Use nsis's buit-in com plugin instead of WinShell * Remove download ApplicationID code * Add change file * Clippy and format * Allow dead code on extract_zip * Qualify extract_zip path to make clippy happy * Move macro up
1 parent 12b4159 commit 68c39b8

File tree

4 files changed

+74
-26
lines changed

4 files changed

+74
-26
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri-bundler': 'patch:enhance'
3+
---
4+
5+
Use nsis's built-in com plugin instead of ApplicationID plugin, this reduces the installer size by 150-200 KB, and also fixes pinned shortcut not getting cleaned up on uninstall

tooling/bundler/src/bundle/windows/nsis.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ use crate::{
88
bundle::{
99
common::CommandExt,
1010
windows::util::{
11-
download, download_and_verify, download_webview2_bootstrapper,
12-
download_webview2_offline_installer, extract_zip, verify_file_hash, HashAlgorithm,
13-
NSIS_OUTPUT_FOLDER_NAME, NSIS_UPDATER_OUTPUT_FOLDER_NAME,
11+
download_and_verify, download_webview2_bootstrapper, download_webview2_offline_installer,
12+
verify_file_hash, HashAlgorithm, NSIS_OUTPUT_FOLDER_NAME, NSIS_UPDATER_OUTPUT_FOLDER_NAME,
1413
},
1514
},
1615
Settings,
@@ -23,7 +22,7 @@ use tauri_utils::config::{NSISInstallerMode, NsisCompression, WebviewInstallMode
2322

2423
use std::{
2524
collections::{BTreeMap, HashMap},
26-
fs::{copy, create_dir_all, remove_dir_all, rename, write},
25+
fs::{create_dir_all, remove_dir_all, rename, write},
2726
path::{Path, PathBuf},
2827
process::Command,
2928
};
@@ -34,7 +33,6 @@ const NSIS_URL: &str =
3433
"https://github.com/tauri-apps/binary-releases/releases/download/nsis-3/nsis-3.zip";
3534
#[cfg(target_os = "windows")]
3635
const NSIS_SHA1: &str = "057e83c7d82462ec394af76c87d06733605543d4";
37-
const NSIS_APPLICATIONID_URL: &str = "https://github.com/tauri-apps/binary-releases/releases/download/nsis-plugins-v0/NSIS-ApplicationID.zip";
3836
const NSIS_TAURI_UTILS_URL: &str =
3937
"https://github.com/tauri-apps/nsis-tauri-utils/releases/download/nsis_tauri_utils-v0.2.2/nsis_tauri_utils.dll";
4038
const NSIS_TAURI_UTILS_SHA1: &str = "16DF1D1A5B4D5DF3859447279C55BE36D4109DFB";
@@ -45,7 +43,6 @@ const NSIS_REQUIRED_FILES: &[&str] = &[
4543
"Bin/makensis.exe",
4644
"Stubs/lzma-x86-unicode",
4745
"Stubs/lzma_solid-x86-unicode",
48-
"Plugins/x86-unicode/ApplicationID.dll",
4946
"Plugins/x86-unicode/nsis_tauri_utils.dll",
5047
"Include/MUI2.nsh",
5148
"Include/FileFunc.nsh",
@@ -54,10 +51,7 @@ const NSIS_REQUIRED_FILES: &[&str] = &[
5451
"Include/WinMessages.nsh",
5552
];
5653
#[cfg(not(target_os = "windows"))]
57-
const NSIS_REQUIRED_FILES: &[&str] = &[
58-
"Plugins/x86-unicode/ApplicationID.dll",
59-
"Plugins/x86-unicode/nsis_tauri_utils.dll",
60-
];
54+
const NSIS_REQUIRED_FILES: &[&str] = &["Plugins/x86-unicode/nsis_tauri_utils.dll"];
6155

6256
const NSIS_REQUIRED_FILES_HASH: &[(&str, &str, &str, HashAlgorithm)] = &[(
6357
"Plugins/x86-unicode/nsis_tauri_utils.dll",
@@ -109,25 +103,12 @@ fn get_and_extract_nsis(nsis_toolset_path: &Path, _tauri_tools_path: &Path) -> c
109103
{
110104
let data = download_and_verify(NSIS_URL, NSIS_SHA1, HashAlgorithm::Sha1)?;
111105
log::info!("extracting NSIS");
112-
extract_zip(&data, _tauri_tools_path)?;
106+
crate::bundle::windows::util::extract_zip(&data, _tauri_tools_path)?;
113107
rename(_tauri_tools_path.join("nsis-3.08"), nsis_toolset_path)?;
114108
}
115109

116110
let nsis_plugins = nsis_toolset_path.join("Plugins");
117111

118-
let data = download(NSIS_APPLICATIONID_URL)?;
119-
log::info!("extracting NSIS ApplicationID plugin");
120-
extract_zip(&data, &nsis_plugins)?;
121-
122-
create_dir_all(nsis_plugins.join("x86-unicode"))?;
123-
124-
copy(
125-
nsis_plugins
126-
.join("ReleaseUnicode")
127-
.join("ApplicationID.dll"),
128-
nsis_plugins.join("x86-unicode").join("ApplicationID.dll"),
129-
)?;
130-
131112
let data = download_and_verify(
132113
NSIS_TAURI_UTILS_URL,
133114
NSIS_TAURI_UTILS_SHA1,

tooling/bundler/src/bundle/windows/templates/installer.nsi

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ ManifestDPIAware true
1313
!include WordFunc.nsh
1414
!include "FileAssociation.nsh"
1515
!include "StrFunc.nsh"
16+
!include "Win\COM.nsh"
17+
!include "Win\Propkey.nsh"
1618
${StrCase}
1719
${StrLoc}
1820

@@ -644,6 +646,35 @@ Function un.onInit
644646
!insertmacro MUI_UNGETLANGUAGE
645647
FunctionEnd
646648

649+
!macro DeleteAppUserModelId
650+
!insertmacro ComHlpr_CreateInProcInstance ${CLSID_DestinationList} ${IID_ICustomDestinationList} r1 ""
651+
${If} $1 P<> 0
652+
${ICustomDestinationList::DeleteList} $1 '("${BUNDLEID}")'
653+
${IUnknown::Release} $1 ""
654+
${EndIf}
655+
!insertmacro ComHlpr_CreateInProcInstance ${CLSID_ApplicationDestinations} ${IID_IApplicationDestinations} r1 ""
656+
${If} $1 P<> 0
657+
${IApplicationDestinations::SetAppID} $1 '("${BUNDLEID}")i.r0'
658+
${If} $0 >= 0
659+
${IApplicationDestinations::RemoveAllDestinations} $1 ''
660+
${EndIf}
661+
${IUnknown::Release} $1 ""
662+
${EndIf}
663+
!macroend
664+
665+
; From https://stackoverflow.com/a/42816728/16993372
666+
!macro UnpinShortcut shortcut
667+
!insertmacro ComHlpr_CreateInProcInstance ${CLSID_StartMenuPin} ${IID_IStartMenuPinnedList} r0 ""
668+
${If} $0 P<> 0
669+
System::Call 'SHELL32::SHCreateItemFromParsingName(ws, p0, g "${IID_IShellItem}", *p0r1)' "${shortcut}"
670+
${If} $1 P<> 0
671+
${IStartMenuPinnedList::RemoveFromList} $0 '(r1)'
672+
${IUnknown::Release} $1 ""
673+
${EndIf}
674+
${IUnknown::Release} $0 ""
675+
${EndIf}
676+
!macroend
677+
647678
Section Uninstall
648679
!insertmacro CheckIfAppIsRunning
649680

@@ -685,6 +716,10 @@ Section Uninstall
685716
{{/each}}
686717
RMDir "$INSTDIR"
687718

719+
!insertmacro DeleteAppUserModelId
720+
!insertmacro UnpinShortcut "$SMPROGRAMS\$AppStartMenuFolder\${MAINBINARYNAME}.lnk"
721+
!insertmacro UnpinShortcut "$DESKTOP\${MAINBINARYNAME}.lnk"
722+
688723
; Remove start menu shortcut
689724
!insertmacro MUI_STARTMENU_GETFOLDER Application $AppStartMenuFolder
690725
Delete "$SMPROGRAMS\$AppStartMenuFolder\${MAINBINARYNAME}.lnk"
@@ -726,13 +761,39 @@ Function SkipIfPassive
726761
${IfThen} $PassiveMode == 1 ${|} Abort ${|}
727762
FunctionEnd
728763

764+
!macro SetLnkAppUserModelId shortcut
765+
!insertmacro ComHlpr_CreateInProcInstance ${CLSID_ShellLink} ${IID_IShellLink} r0 ""
766+
${If} $0 P<> 0
767+
${IUnknown::QueryInterface} $0 '("${IID_IPersistFile}",.r1)'
768+
${If} $1 P<> 0
769+
${IPersistFile::Load} $1 '("${shortcut}", ${STGM_READWRITE})'
770+
${IUnknown::QueryInterface} $0 '("${IID_IPropertyStore}",.r2)'
771+
${If} $2 P<> 0
772+
System::Call 'Oleaut32::SysAllocString(w "${BUNDLEID}") i.r3'
773+
System::Call '*${SYSSTRUCT_PROPERTYKEY}(${PKEY_AppUserModel_ID})p.r4'
774+
System::Call '*${SYSSTRUCT_PROPVARIANT}(${VT_BSTR},,&i4 $3)p.r5'
775+
${IPropertyStore::SetValue} $2 '($4,$5)'
776+
777+
System::Call 'Oleaut32::SysFreeString($3)'
778+
System::Free $4
779+
System::Free $5
780+
${IPropertyStore::Commit} $2 ""
781+
${IUnknown::Release} $2 ""
782+
${IPersistFile::Save} $1 '("${shortcut}",1)'
783+
${EndIf}
784+
${IUnknown::Release} $1 ""
785+
${EndIf}
786+
${IUnknown::Release} $0 ""
787+
${EndIf}
788+
!macroend
789+
729790
Function CreateDesktopShortcut
730791
CreateShortcut "$DESKTOP\${MAINBINARYNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
731-
ApplicationID::Set "$DESKTOP\${MAINBINARYNAME}.lnk" "${BUNDLEID}"
792+
!insertmacro SetLnkAppUserModelId "$DESKTOP\${MAINBINARYNAME}.lnk"
732793
FunctionEnd
733794

734795
Function CreateStartMenuShortcut
735796
CreateDirectory "$SMPROGRAMS\$AppStartMenuFolder"
736797
CreateShortcut "$SMPROGRAMS\$AppStartMenuFolder\${MAINBINARYNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
737-
ApplicationID::Set "$SMPROGRAMS\$AppStartMenuFolder\${MAINBINARYNAME}.lnk" "${BUNDLEID}"
798+
!insertmacro SetLnkAppUserModelId "$SMPROGRAMS\$AppStartMenuFolder\${MAINBINARYNAME}.lnk"
738799
FunctionEnd

tooling/bundler/src/bundle/windows/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ pub fn verify_file_hash<P: AsRef<Path>>(
132132
}
133133

134134
/// Extracts the zips from memory into a usable path.
135+
#[allow(dead_code)]
135136
pub fn extract_zip(data: &[u8], path: &Path) -> crate::Result<()> {
136137
let cursor = Cursor::new(data);
137138

0 commit comments

Comments
 (0)