Skip to content

Commit 92bc7d0

Browse files
authored
fix(bundler/nsis): calculate estimated size on build system (#8233)
* fix(bundler): Fix nsis installer taking longer than expected to install resources * create dir structure for resources before extracting files * calculate size in rust on the build system * i'm sorry clippy, i programmed in unholy languages where += wasn't a thing so i forgot it exists in rust... * i'm a better clippy than clippy🌚
1 parent 9e3aff0 commit 92bc7d0

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@tauri-apps/cli": patch:bug
3+
"tauri-cli": patch:bug
4+
"tauri-bundler": patch:bug
5+
---
6+
7+
Fixes an issue in the NSIS installer which caused the installation to take much longer than expected when many `resources` were added to the bundle.

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,23 +290,27 @@ fn build_nsis_app_installer(
290290
.iter()
291291
.find(|bin| bin.main())
292292
.ok_or_else(|| anyhow::anyhow!("Failed to get main binary"))?;
293+
let main_binary_path = settings.binary_path(main_binary).with_extension("exe");
293294
data.insert(
294295
"main_binary_name",
295296
to_json(main_binary.name().replace(".exe", "")),
296297
);
297-
data.insert(
298-
"main_binary_path",
299-
to_json(settings.binary_path(main_binary).with_extension("exe")),
300-
);
298+
data.insert("main_binary_path", to_json(&main_binary_path));
301299

302300
let out_file = "nsis-output.exe";
303301
data.insert("out_file", to_json(out_file));
304302

305303
let resources = generate_resource_data(settings)?;
306-
data.insert("resources", to_json(resources));
304+
let resources_dirs =
305+
std::collections::HashSet::<PathBuf>::from_iter(resources.values().map(|r| r.0.to_owned()));
306+
data.insert("resources_dirs", to_json(resources_dirs));
307+
data.insert("resources", to_json(&resources));
307308

308309
let binaries = generate_binaries_data(settings)?;
309-
data.insert("binaries", to_json(binaries));
310+
data.insert("binaries", to_json(&binaries));
311+
312+
let estimated_size = generate_estimated_size(&main_binary_path, &binaries, &resources)?;
313+
data.insert("estimated_size", to_json(estimated_size));
310314

311315
let silent_webview2_install = if let WebviewInstallMode::DownloadBootstrapper { silent }
312316
| WebviewInstallMode::EmbedBootstrapper { silent }
@@ -552,6 +556,24 @@ fn generate_binaries_data(settings: &Settings) -> crate::Result<BinariesMap> {
552556
Ok(binaries)
553557
}
554558

559+
fn generate_estimated_size(
560+
main: &Path,
561+
binaries: &BinariesMap,
562+
resources: &ResourcesMap,
563+
) -> crate::Result<String> {
564+
use std::fs::metadata;
565+
566+
let mut size = metadata(main)?.len();
567+
568+
for k in binaries.keys().chain(resources.keys()) {
569+
size += metadata(k)?.len();
570+
}
571+
572+
size /= 1000;
573+
574+
Ok(format!("{size:#08x}"))
575+
}
576+
555577
fn get_lang_data(
556578
lang: &str,
557579
custom_lang_files: Option<&HashMap<String, PathBuf>>,

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ ${StrLoc}
4040
!define UNINSTKEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCTNAME}"
4141
!define MANUPRODUCTKEY "Software\${MANUFACTURER}\${PRODUCTNAME}"
4242
!define UNINSTALLERSIGNCOMMAND "{{uninstaller_sign_cmd}}"
43+
!define ESTIMATEDSIZE "{{estimated_size}}"
4344

4445
Name "${PRODUCTNAME}"
4546
BrandingText "${COPYRIGHT}"
@@ -522,31 +523,26 @@ SectionEnd
522523
app_check_done:
523524
!macroend
524525

525-
Var AppSize
526526
Section Install
527527
SetOutPath $INSTDIR
528-
StrCpy $AppSize 0
529528

530529
!insertmacro CheckIfAppIsRunning
531530

532531
; Copy main executable
533532
File "${MAINBINARYSRCPATH}"
534-
${GetSize} "$INSTDIR" "/M=${MAINBINARYNAME}.exe /S=0B" $0 $1 $2
535-
IntOp $AppSize $AppSize + $0
536533

537534
; Copy resources
535+
{{#each resources_dirs}}
536+
; `\\` is not a typo.
537+
CreateDirectory "$INSTDIR\\{{this}}"
538+
{{/each}}
538539
{{#each resources}}
539-
CreateDirectory "$INSTDIR\\{{this.[0]}}"
540540
File /a "/oname={{this.[1]}}" "{{@key}}"
541-
${GetSize} "$INSTDIR" "/M={{this.[1]}} /S=0B" $0 $1 $2
542-
IntOp $AppSize $AppSize + $0
543541
{{/each}}
544542

545543
; Copy external binaries
546544
{{#each binaries}}
547545
File /a "/oname={{this}}" "{{@key}}"
548-
${GetSize} "$INSTDIR" "/M={{this}} /S=0B" $0 $1 $2
549-
IntOp $AppSize $AppSize + $0
550546
{{/each}}
551547

552548
; Create uninstaller
@@ -570,9 +566,7 @@ Section Install
570566
WriteRegStr SHCTX "${UNINSTKEY}" "UninstallString" "$\"$INSTDIR\uninstall.exe$\""
571567
WriteRegDWORD SHCTX "${UNINSTKEY}" "NoModify" "1"
572568
WriteRegDWORD SHCTX "${UNINSTKEY}" "NoRepair" "1"
573-
IntOp $AppSize $AppSize / 1000
574-
IntFmt $AppSize "0x%08X" $AppSize
575-
WriteRegDWORD SHCTX "${UNINSTKEY}" "EstimatedSize" "$AppSize"
569+
WriteRegDWORD SHCTX "${UNINSTKEY}" "EstimatedSize" "${ESTIMATEDSIZE}"
576570

577571
; Create start menu shortcut (GUI)
578572
!insertmacro MUI_STARTMENU_WRITE_BEGIN Application

0 commit comments

Comments
 (0)