Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nsis): add an option to opt out tauri utils plugin #9526

Closed
wants to merge 12 commits into from
7 changes: 7 additions & 0 deletions .changes/opt-in-nsis-plugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
tauri-utils: minor:feat
tauri-cli: minor:feat
tauri-bundler: minor:feat
---

Add an option to replace nsis util plugin with built-in ones, this can reduce the final bundle size by ~0.8 MB.
5 changes: 5 additions & 0 deletions core/tauri-config-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,11 @@
"type": "null"
}
]
},
"useTauriPlugin": {
"description": "Whether to includede and use nsis-tauri-utils.dll plugin to compare semver, check and terminate running app, and download webview2 bootstrapper. This will increase the installer size by around 0.8 MB.\n\nIf disabled, the installer will use alternative tools provided by the OS and built-in NSIS plugins but this comes with downsides: - Can't compare semver with pre-release labels like `2.0.0-alpha.1` - Using `webviewInstallMode: downloadBootstrapper` may fail on Windows 7.\n\nDefaults to `true`.",
"default": true,
"type": "boolean"
}
},
"additionalProperties": false
Expand Down
12 changes: 12 additions & 0 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,18 @@ pub struct NsisConfig {
///
/// See <https://nsis.sourceforge.io/Reference/SetCompressor>
pub compression: Option<NsisCompression>,
/// Whether to includede and use nsis-tauri-utils.dll plugin to
/// compare semver, check and terminate running app, and download webview2 bootstrapper.
/// This will increase the installer size by around 0.8 MB.
///
/// If disabled, the installer will use alternative tools provided by the OS
/// and built-in NSIS plugins but this comes with downsides:
/// - Can't compare semver with pre-release labels like `2.0.0-alpha.1`
/// - Using `webviewInstallMode: downloadBootstrapper` may fail on Windows 7.
///
/// Defaults to `true`.
#[serde(default = "default_true")]
pub use_tauri_plugin: bool,
}

/// Install Modes for the NSIS installer.
Expand Down
11 changes: 11 additions & 0 deletions tooling/bundler/src/bundle/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,17 @@ pub struct NsisSettings {
pub display_language_selector: bool,
/// Set compression algorithm used to compress files in the installer.
pub compression: Option<NsisCompression>,
/// Whether to includede and use nsis-tauri-utils.dll plugin to
/// compare semver, check and terminate running app, and download webview2 bootstrapper.
/// This will increase the installer size by around 0.8 MB.
///
/// If disabled, the installer will use alternative tools provided by the OS
/// and built-in NSIS plugins but this comes with downsides:
/// - Can't compare semver with pre-release labels like `2.0.0-alpha.1`
/// - Using `webviewInstallMode: downloadBootstrapper` may fail on Windows 7.
///
/// Defaults to `true`.
pub use_tauri_plugin: bool,
}

/// The Windows bundle settings.
Expand Down
2 changes: 2 additions & 0 deletions tooling/bundler/src/bundle/windows/nsis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ fn build_nsis_app_installer(
"display_language_selector",
to_json(nsis.display_language_selector && languages.len() > 1),
);

data.insert("use_tauri_plugin", to_json(nsis.use_tauri_plugin));
}
data.insert(
"install_mode",
Expand Down
67 changes: 65 additions & 2 deletions tooling/bundler/src/bundle/windows/templates/installer.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ${StrLoc}
!define MANUPRODUCTKEY "Software\${MANUFACTURER}\${PRODUCTNAME}"
!define UNINSTALLERSIGNCOMMAND "{{uninstaller_sign_cmd}}"
!define ESTIMATEDSIZE "{{estimated_size}}"
!define USETAURIPLUGIN "{{use_tauri_plugin}}"

Name "${PRODUCTNAME}"
BrandingText "${COPYRIGHT}"
Expand Down Expand Up @@ -178,7 +179,12 @@ Function PageReinstall
${EndIf}
${IfThen} $R0 == "" ${|} StrCpy $R4 "$(unknown)" ${|}

nsis_tauri_utils::SemverCompare "${VERSION}" $R0
!if "${USETAURIPLUGIN}" == "true"
nsis_tauri_utils::SemverCompare "${VERSION}" $R0
!else
; https://nsis.sourceforge.io/VersionCompare
${VersionCompare} "${VERSION}" $R0 $R0
!endif
Pop $R0
; Reinstalling the same version
${If} $R0 == 0
Expand All @@ -195,7 +201,11 @@ Function PageReinstall
!insertmacro MUI_HEADER_TEXT "$(alreadyInstalled)" "$(choowHowToInstall)"
StrCpy $R5 "1"
; Downgrading
!if "${USETAURIPLUGIN}" == "true"
${ElseIf} $R0 == -1
!else
${ElseIf} $R0 == 2
!endif
StrCpy $R1 "$(newerVersionInstalled)"
StrCpy $R2 "$(uninstallBeforeInstalling)"
!if "${ALLOWDOWNGRADES}" == "true"
Expand Down Expand Up @@ -451,7 +461,11 @@ Section WebView2
!if "${INSTALLWEBVIEW2MODE}" == "downloadBootstrapper"
Delete "$TEMP\MicrosoftEdgeWebview2Setup.exe"
DetailPrint "$(webview2Downloading)"
nsis_tauri_utils::download "https://go.microsoft.com/fwlink/p/?LinkId=2124703" "$TEMP\MicrosoftEdgeWebview2Setup.exe"
!if "${USETAURIPLUGIN}" == "true"
nsis_tauri_utils::download "https://go.microsoft.com/fwlink/p/?LinkId=2124703" "$TEMP\MicrosoftEdgeWebview2Setup.exe"
!else
nsExec::Exec 'powershell -Command Invoke-WebRequest -Uri "https://go.microsoft.com/fwlink/p/?LinkId=2124703" -OutFile "$TEMP\MicrosoftEdgeWebview2Setup.exe"'
!endif
Pop $0
${If} $0 == 0
DetailPrint "$(webview2DownloadSuccess)"
Expand Down Expand Up @@ -494,6 +508,7 @@ Section WebView2
webview2_done:
SectionEnd

!if "${USETAURIPLUGIN}" == "true"
!macro CheckIfAppIsRunning
!if "${INSTALLMODE}" == "currentUser"
nsis_tauri_utils::FindProcessCurrentUser "${MAINBINARYNAME}.exe"
Expand Down Expand Up @@ -532,6 +547,54 @@ SectionEnd
${EndIf}
app_check_done:
!macroend
!else
!macro CheckIfAppIsRunning
; Modified from https://github.com/electron-userland/electron-builder/blob/master/packages/app-builder-lib/templates/nsis/include/allowOnlyOneInstallerInstance.nsh
; Pipe tasklist output to find to convert not found to an error
!macro FIND_PROCESS _FILE _ERR
!if "${INSTALLMODE}" == "currentUser"
nsExec::Exec 'cmd /c tasklist /fi "USERNAME eq %USERNAME%" /fi "IMAGENAME eq ${_FILE}" /nh | find "${_FILE}"'
Pop ${_ERR}
!else
nsExec::Exec 'cmd /c tasklist /fi "IMAGENAME eq ${_FILE}" /nh | find "${_FILE}"'
Pop ${_ERR}
!endif
!macroend

!insertmacro FIND_PROCESS "${MAINBINARYNAME}.exe" $R0
${If} $R0 == 0
IfSilent kill 0
${IfThen} $PassiveMode != 1 ${|} MessageBox MB_OKCANCEL "$(appRunningOkKill)" IDOK kill IDCANCEL cancel ${|}

kill:
!if "${INSTALLMODE}" == "currentUser"
nsExec::Exec 'taskkill /im "${MAINBINARYNAME}.exe" /fi "USERNAME eq %USERNAME%" /f'
!else
nsExec::Exec 'taskkill /im "${MAINBINARYNAME}.exe" /f'
!endif

!insertmacro FIND_PROCESS "${MAINBINARYNAME}.exe" $R0
${If} $R0 == 0
IfSilent silent ui
silent:
System::Call 'kernel32::AttachConsole(i -1)i.r0'
${If} $0 != 0
System::Call 'kernel32::GetStdHandle(i -11)i.r0'
System::call 'kernel32::SetConsoleTextAttribute(i r0, i 0x0004)' ; set red color
FileWrite $0 "$(appRunning)$\n"
${EndIf}
Abort
ui:
Abort "$(failedToKillApp)"
${Else}
Goto app_check_done
${EndIf}
cancel:
Abort "$(appRunning)"
app_check_done:
${EndIf}
!macroend
!endif

Section Install
SetOutPath $INSTDIR
Expand Down
5 changes: 5 additions & 0 deletions tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,11 @@
"type": "null"
}
]
},
"useTauriPlugin": {
"description": "Whether to includede and use nsis-tauri-utils.dll plugin to compare semver, check and terminate running app, and download webview2 bootstrapper. This will increase the installer size by around 0.8 MB.\n\nIf disabled, the installer will use alternative tools provided by the OS and built-in NSIS plugins but this comes with downsides: - Can't compare semver with pre-release labels like `2.0.0-alpha.1` - Using `webviewInstallMode: downloadBootstrapper` may fail on Windows 7.\n\nDefaults to `true`.",
"default": true,
"type": "boolean"
}
},
"additionalProperties": false
Expand Down
1 change: 1 addition & 0 deletions tooling/cli/src/helpers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub fn nsis_settings(config: NsisConfig) -> tauri_bundler::NsisSettings {
custom_language_files: config.custom_language_files,
display_language_selector: config.display_language_selector,
compression: config.compression,
use_tauri_plugin: config.use_tauri_plugin,
}
}

Expand Down