Skip to content

Commit

Permalink
fix(nsis): fix resources not removed on uninstallation (#114)
Browse files Browse the repository at this point in the history
* fix(nsis): fix resources not removed on uninstallation

closes #108

* clippy
  • Loading branch information
amr-crabnebula authored Dec 27, 2023
1 parent e58c7e2 commit ea6c31b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .changes/nsis-resources-leftover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cargo-packager": "patch"
"@crabnebula/packager": "patch"
---

Fix NSIS uninstaller leaving resources behind and failing to remove the installation directory.
5 changes: 2 additions & 3 deletions crates/packager/src/package/nsis/installer.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,7 @@ Section Uninstall

; Delete resources
{{#each resources}}
Delete "$INSTDIR\\{{this.[1]}}"
RMDir "$INSTDIR\\{{this.[0]}}"
Delete "$INSTDIR\\{{this}}"
{{/each}}

; Delete external binaries
Expand All @@ -591,7 +590,7 @@ Section Uninstall
${If} $DeleteAppDataCheckboxState == 1
RMDir /R /REBOOTOK "$INSTDIR"
${Else}
{{#each resources_ancestors}}
{{#each resources_dirs}}
RMDir /REBOOTOK "$INSTDIR\\{{this}}"
{{/each}}
RMDir "$INSTDIR"
Expand Down
28 changes: 11 additions & 17 deletions crates/packager/src/package/nsis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,16 @@ fn generate_resource_data(config: &Config) -> crate::Result<(DirectoriesSet, Res
let mut directories = BTreeSet::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(),
);
// only add if resource has a parent e.g. `files/a.txt`
// and is not empty. this is to ensure that we don't
// generate `CreateDirectory "$INSTDIR\"` which is useless
// since `INSTDIR` is already created.
if let Some(parent) = r.target.parent() {
if parent.as_os_str() != "" {
directories.insert(parent.to_path_buf());
}
}

resources_map.insert(r.src, r.target);
}
Ok((directories, resources_map))
Expand Down Expand Up @@ -453,19 +457,9 @@ fn build_nsis_app_installer(ctx: &Context, nsis_path: &Path) -> crate::Result<Ve
data.insert("out_file", to_json(out_file));

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

let mut resources_ancestors = resources_dirs
.iter()
.flat_map(|p| p.ancestors())
.collect::<Vec<_>>();
resources_ancestors.sort_unstable();
resources_ancestors.dedup();
resources_ancestors.sort_by_key(|p| std::cmp::Reverse(p.components().count()));
resources_ancestors.pop(); // Last one is always ""
data.insert("resources_ancestors", to_json(resources_ancestors));

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

Expand Down

0 comments on commit ea6c31b

Please sign in to comment.