loadsmith_install/ops/uninstall.rs
1use std::{fs, path::Path};
2
3use loadsmith_core::InstalledPackage;
4use tracing::{debug, trace};
5
6use crate::Result;
7
8/// Remove all files that belong to an installed package from a profile directory.
9///
10/// Removes each file recorded in `package.files()` from the `profile` directory
11/// and cleans up any empty parent directories along the way.
12///
13/// # Examples
14///
15/// ```rust,no_run
16/// use loadsmith_core::InstalledPackage;
17/// use loadsmith_install::uninstall;
18///
19/// // Load a previously installed package, then uninstall it.
20/// # let package: InstalledPackage = unimplemented!();
21/// uninstall(&package, "C:\\games\\Valheim\\profile").unwrap();
22/// ```
23pub fn uninstall(package: &InstalledPackage, profile: impl AsRef<Path>) -> Result<()> {
24 let profile = profile.as_ref();
25
26 for file in package.files() {
27 let target_path = profile.join(file.relative_path());
28
29 if target_path.exists() {
30 fs::remove_file(&target_path)?;
31 trace!(?file, "remove file");
32
33 loadsmith_util::remove_empty_parents(target_path)?;
34 } else {
35 debug!(?file, "file does not exist, skipping");
36 }
37 }
38
39 Ok(())
40}