Skip to content

Commit

Permalink
pref(nsis): calculate estimated size on build system
Browse files Browse the repository at this point in the history
port of tauri-apps/tauri@92bc7d0

Co-authored-by: Fabian-Lars <[email protected]>
  • Loading branch information
amr-crabnebula and FabianLars committed Nov 30, 2023
1 parent 193c8f7 commit 86f1fb4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
13 changes: 2 additions & 11 deletions crates/packager/src/package/nsis/installer.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ${StrLoc}
!define UNINSTKEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCTNAME}"
!define MANUPRODUCTKEY "Software\${MANUFACTURER}\${PRODUCTNAME}"
!define UNINSTALLERSIGNCOMMAND "{{uninstaller_sign_cmd}}"
!define ESTIMATEDSIZE "{{estimated_size}}"

Name "${PRODUCTNAME}"
BrandingText "${COPYRIGHT}"
Expand Down Expand Up @@ -468,17 +469,13 @@ SectionEnd
app_check_done:
!macroend

Var AppSize
Section Install
SetOutPath $INSTDIR
StrCpy $AppSize 0

!insertmacro CheckIfAppIsRunning

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

; Create resources directory structure
{{#each resources_dirs}}
Expand All @@ -488,15 +485,11 @@ Section Install
; Copy resources
{{#each resources}}
File /a "/oname={{this.[1]}}" "{{this.[0]}}"
${GetSize} "$INSTDIR" "/M={{this.[1]}} /S=0B" $0 $1 $2
IntOp $AppSize $AppSize + $0
{{/each}}

; Copy external binaries
{{#each binaries}}
File /a "/oname={{this}}" "{{@key}}"
${GetSize} "$INSTDIR" "/M={{this}} /S=0B" $0 $1 $2
IntOp $AppSize $AppSize + $0
{{/each}}

; Create file associations
Expand Down Expand Up @@ -527,9 +520,7 @@ Section Install
WriteRegStr SHCTX "${UNINSTKEY}" "UninstallString" "$\"$INSTDIR\uninstall.exe$\""
WriteRegDWORD SHCTX "${UNINSTKEY}" "NoModify" "1"
WriteRegDWORD SHCTX "${UNINSTKEY}" "NoRepair" "1"
IntOp $AppSize $AppSize / 1000
IntFmt $AppSize "0x%08X" $AppSize
WriteRegDWORD SHCTX "${UNINSTKEY}" "EstimatedSize" "$AppSize"
WriteRegDWORD SHCTX "${UNINSTKEY}" "EstimatedSize" "${ESTIMATEDSIZE}"

; Create start menu shortcut (GUI)
!insertmacro MUI_STARTMENU_WRITE_BEGIN Application
Expand Down
33 changes: 27 additions & 6 deletions crates/packager/src/package/nsis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ const NSIS_REQUIRED_FILES: &[&str] = &[
];

type DirectoriesSet = BTreeSet<PathBuf>;
type ResourcesMap = Vec<(PathBuf, PathBuf)>;
type ResourcesMap = BTreeMap<PathBuf, PathBuf>;

#[tracing::instrument(level = "trace")]
fn generate_resource_data(config: &Config) -> crate::Result<(DirectoriesSet, ResourcesMap)> {
let mut directories = BTreeSet::new();
let mut resources_map = Vec::new();
let mut resources_map = BTreeMap::new();
for r in config.resources()? {
directories.insert(
r.target
.parent()
.unwrap_or_else(|| Path::new(""))
.to_path_buf(),
);
resources_map.push((r.src, r.target))
resources_map.insert(r.src, r.target);
}
Ok((directories, resources_map))
}
Expand Down Expand Up @@ -234,6 +234,23 @@ fn add_build_number_if_needed(version_str: &str) -> crate::Result<String> {
))
}

fn generate_estimated_size<I, P, P2>(main: P, other_files: I) -> crate::Result<String>
where
I: IntoIterator<Item = P2>,
P: AsRef<Path>,
P2: AsRef<Path>,
{
let mut size = std::fs::metadata(main)?.len();

for k in other_files {
size += std::fs::metadata(k)?.len();
}

size /= 1000;

Ok(format!("{size:#08x}"))
}

#[tracing::instrument(level = "trace")]
fn get_and_extract_nsis(
#[allow(unused)] ctx: &Context,
Expand Down Expand Up @@ -421,7 +438,7 @@ fn build_nsis_app_installer(ctx: &Context, nsis_path: &Path) -> crate::Result<Ve
);

data.insert("main_binary_name", to_json(&main_binary_name));
data.insert("main_binary_path", to_json(main_binary_path));
data.insert("main_binary_path", to_json(&main_binary_path));

if let Some(file_associations) = &config.file_associations {
data.insert("file_associations", to_json(file_associations));
Expand All @@ -432,10 +449,14 @@ fn build_nsis_app_installer(ctx: &Context, nsis_path: &Path) -> crate::Result<Ve

let (resources_dirs, resources) = generate_resource_data(config)?;
data.insert("resources_dirs", to_json(resources_dirs));
data.insert("resources", to_json(resources));
data.insert("resources", to_json(&resources));

let binaries = generate_binaries_data(config)?;
data.insert("binaries", to_json(binaries));
data.insert("binaries", to_json(&binaries));

let estimated_size =
generate_estimated_size(main_binary_path, resources.keys().chain(binaries.keys()))?;
data.insert("estimated_size", to_json(estimated_size));

let mut handlebars = Handlebars::new();
handlebars.register_helper("or", Box::new(handlebars_or));
Expand Down

0 comments on commit 86f1fb4

Please sign in to comment.