From 52383f72ebc76e267617e3150afc95f54384be71 Mon Sep 17 00:00:00 2001 From: Krusty/Benediction Date: Sun, 18 Aug 2024 08:28:42 +0200 Subject: [PATCH] [emucontrol] Add the ability to clear the download cache --- cpclib-basm/pgo.sh | 0 cpclib-bdasm/tries.sh | 0 cpclib-runner/src/delegated.rs | 19 ++++++++++++++----- cpclib-runner/src/emucontrol.rs | 20 +++++++++++++++++++- 4 files changed, 33 insertions(+), 6 deletions(-) mode change 100644 => 100755 cpclib-basm/pgo.sh mode change 100644 => 100755 cpclib-bdasm/tries.sh diff --git a/cpclib-basm/pgo.sh b/cpclib-basm/pgo.sh old mode 100644 new mode 100755 diff --git a/cpclib-bdasm/tries.sh b/cpclib-bdasm/tries.sh old mode 100644 new mode 100755 diff --git a/cpclib-runner/src/delegated.rs b/cpclib-runner/src/delegated.rs index 2c9d4dd4..8f2cec15 100644 --- a/cpclib-runner/src/delegated.rs +++ b/cpclib-runner/src/delegated.rs @@ -22,17 +22,26 @@ pub struct DelegateApplicationDescription { pub compile: Option Result<(), String>>> } + +pub fn base_cache_foder() -> Utf8PathBuf { + let proj_dirs = ProjectDirs::from("net.cpcscene", "benediction", "bnd build").unwrap(); + Utf8Path::from_path(proj_dirs.cache_dir()).unwrap().to_owned() +} + +pub fn clear_base_cache_folder() -> std::io::Result<()> { + std::fs::remove_dir_all(base_cache_foder()) +} + impl DelegateApplicationDescription { pub fn is_cached(&self) -> bool { self.cache_folder().exists() } pub fn cache_folder(&self) -> Utf8PathBuf { - let proj_dirs = ProjectDirs::from("net.cpcscene", "benediction", "bnd build").unwrap(); - let base_cache = proj_dirs.cache_dir(); + let base_cache = base_cache_foder(); if !base_cache.exists() { - std::fs::create_dir_all(base_cache); + std::fs::create_dir_all(&base_cache).unwrap(); } base_cache.join(self.folder).try_into().unwrap() @@ -46,8 +55,7 @@ impl DelegateApplicationDescription { // get the file let dest = self.cache_folder(); - println!(">> Download file"); - let resp = self.download().unwrap(); + let resp = self.download().map_err(|e| format!("Unable to download the expected file. {}", e.to_string()))?; let mut input = resp.into_reader(); // uncompress it @@ -91,6 +99,7 @@ impl DelegateApplicationDescription { } fn download(&self) -> Result { + println!(">> Download file {}", self.download_url); ureq::get(self.download_url).call() } } diff --git a/cpclib-runner/src/emucontrol.rs b/cpclib-runner/src/emucontrol.rs index 358e75ee..63ab7834 100644 --- a/cpclib-runner/src/emucontrol.rs +++ b/cpclib-runner/src/emucontrol.rs @@ -13,7 +13,7 @@ use xcap::Window; #[cfg(windows)] use fs_extra; -use crate::delegated::DelegatedRunner; +use crate::delegated::{clear_base_cache_folder, DelegatedRunner}; use crate::runner::emulator::Emulator; use crate::runner::runner::RunnerWithClap; use crate::runner::Runner; @@ -594,6 +594,9 @@ pub struct Cli { #[arg(short, long, action = ArgAction::SetTrue, help = "Keep the emulator open after the interaction")] keepemulator: bool, + #[arg(short, long, action = ArgAction::SetTrue, help = "Clear the cache folder")] + clear_cache: bool, + #[command(subcommand)] command: Commands } @@ -665,6 +668,13 @@ impl RunnerWithClap for EmuControlledRunner { pub fn handle_arguments(mut cli: Cli) -> Result<(), String> { + + if cli.clear_cache { + clear_base_cache_folder() + .map_err(|e| format!("Unable to clear the cache folder. {}", e.to_string()))?; + } + + let builder = EmulatorConf::builder() .maybe_drive_a(cli.drive_a.clone()) .maybe_drive_b(cli.drive_b.clone()); @@ -676,6 +686,14 @@ pub fn handle_arguments(mut cli: Cli) -> Result<(), String> { Emu::Cpcec => Emulator::Cpcec(Default::default()) }; + + { // ensure emulator is isntalled + let conf = emu.configuration(); + if ! conf.is_cached() { + conf.install()?; + } + } + // setup emulator // TODO do it conditionally // copy the non standard roms