From 4421e226f63258126099a1401be317912079884f Mon Sep 17 00:00:00 2001 From: Damian Czaja Date: Fri, 26 Nov 2021 21:55:41 +0100 Subject: [PATCH 1/3] Support nested modules --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + src/addons.rs | 32 +++++++++++++++++++++----------- src/config.rs | 6 +++--- src/lib.rs | 1 + tests/manager.rs | 27 +++++++++++++++++++++++++++ 6 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 tests/manager.rs diff --git a/Cargo.lock b/Cargo.lock index 6c5c5f3..69a0db0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -339,6 +339,7 @@ dependencies = [ "simple-error", "tempfile", "toml", + "walkdir", "zip", ] @@ -1361,6 +1362,15 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "schannel" version = "0.1.19" @@ -1830,6 +1840,17 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" +[[package]] +name = "walkdir" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" +dependencies = [ + "same-file", + "winapi 0.3.9", + "winapi-util", +] + [[package]] name = "want" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index e889db2..77c757b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,3 +29,4 @@ tempfile = "3.1" requestty = "0.1" colored = "2" prettytable-rs = "^0.8" +walkdir = "2" diff --git a/src/addons.rs b/src/addons.rs index b898113..b69fb7b 100644 --- a/src/addons.rs +++ b/src/addons.rs @@ -10,7 +10,9 @@ use std::io::{self, BufRead}; use std::path::{Path, PathBuf}; use std::{error::Error, rc::Rc}; use tempfile::tempfile; +use walkdir::WalkDir; +#[derive(Debug)] pub struct Addon { pub name: String, pub depends_on: Vec, @@ -31,34 +33,42 @@ fn extract_dependency(dep: &str) -> Option { } impl Manager { - pub fn new(addon_dir: &str) -> Manager { + pub fn new(addon_dir: &Path) -> Manager { let path = PathBuf::from(addon_dir); Manager { addon_dir: path } } pub fn get_addons(&self) -> Result> { - let read_dir = fs::read_dir(&self.addon_dir) - .chain_err(&format!("while listing addon dir {:?}", self.addon_dir))?; - let mut addon_list = AddonList { addons: vec![], errors: vec![], }; - for entry in read_dir { - let entry = entry?; - let path = entry.path(); + let re = Regex::new(r"^.+(/.+){2}\.txt$").unwrap(); + + for entry in WalkDir::new(&self.addon_dir) { + let entry_dir = entry?; + let file_path = entry_dir.path(); - if !path.is_dir() { - continue; + let file_path_str = file_path.as_os_str().to_str(); + + match file_path_str { + None => continue, + Some(path) => { + if !re.is_match(path) { + continue; + } + } } - match self.read_addon(&path) { + let addon_dir = file_path.parent().unwrap(); + + match self.read_addon(addon_dir) { Ok(addon) => addon_list.addons.push(addon), Err(err) => addon_list .errors - .push(format!("while reading addon {:?}: {}", &path, err).into()), + .push(format!("while reading addon {:?}: {}", file_path, err).into()), } } diff --git a/src/config.rs b/src/config.rs index 8c53646..aa77e71 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,7 +3,7 @@ use serde::ser::SerializeStruct; use serde_derive::{Deserialize, Serialize}; use std::error::Error; use std::fs; -use std::path::Path; +use std::path::{Path, PathBuf}; #[derive(Deserialize, Debug, Clone)] pub struct AddonEntry { @@ -21,7 +21,7 @@ fn default_dependency() -> bool { #[derive(Serialize, Deserialize, Debug)] pub struct Config { #[serde(rename = "addonDir")] - pub addon_dir: String, + pub addon_dir: PathBuf, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub addons: Vec, } @@ -82,7 +82,7 @@ fn get_initial_config() -> Config { home_dir.join("drive_c/users/user/My Documents/Elder Scrolls Online/live/AddOns"); Config { - addon_dir: addon_dir.display().to_string(), + addon_dir: addon_dir, addons: vec![], } } diff --git a/src/lib.rs b/src/lib.rs index d5c38d1..91c51e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,7 @@ extern crate regex; extern crate requestty; extern crate reqwest; extern crate tempfile; +extern crate walkdir; extern crate zip; pub mod addons; diff --git a/tests/manager.rs b/tests/manager.rs new file mode 100644 index 0000000..afd2646 --- /dev/null +++ b/tests/manager.rs @@ -0,0 +1,27 @@ +extern crate tempfile; + +use std::error::Error; + +use eso_addons::addons::Manager; + +#[test] +fn addon_manager_supports_nested_modules() -> Result<(), Box> { + let addon_dir = tempfile::tempdir()?; + let manager = Manager::new(addon_dir.path()); + + manager.download_addon("https://www.esoui.com/downloads/download1360-CombatMetrics")?; + + let addon_list = manager.get_addons()?; + assert!( + addon_list.errors.len() == 0, + "failed to list addons: {:?}", + addon_list.errors + ); + assert!( + addon_list.addons.len() == 2, + "Installed mods: {:?}", + addon_list.addons + ); + + Ok(()) +} From 647714be81fa01cb7a1d84766296770d8654176e Mon Sep 17 00:00:00 2001 From: Damian Czaja Date: Fri, 26 Nov 2021 22:05:05 +0100 Subject: [PATCH 2/3] fix --- src/config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index aa77e71..06fbd41 100644 --- a/src/config.rs +++ b/src/config.rs @@ -70,7 +70,7 @@ fn get_initial_config() -> Config { let addon_dir = home_dir.join("Documents/Elder Scrolls Online/live/AddOns"); Config { - addon_dir: addon_dir.display().to_string(), + addon_dir: addon_dir, addons: vec![], } } @@ -90,7 +90,7 @@ fn get_initial_config() -> Config { #[cfg(target_os = "macos")] fn get_initial_config() -> Config { Config { - addon_dir: String::from(""), + addon_dir: PathBuf::new(), addons: vec![], } } From 10612f5c0a67b39dd4caf17126a891ef5ffdc91f Mon Sep 17 00:00:00 2001 From: Damian Czaja Date: Fri, 26 Nov 2021 22:42:17 +0100 Subject: [PATCH 3/3] fix windows --- src/addons.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/addons.rs b/src/addons.rs index b69fb7b..cff65ef 100644 --- a/src/addons.rs +++ b/src/addons.rs @@ -51,12 +51,15 @@ impl Manager { let entry_dir = entry?; let file_path = entry_dir.path(); - let file_path_str = file_path.as_os_str().to_str(); + let file_name = entry_dir.file_name(); + let parent_dir_name = file_path.parent().map(|f| f.file_name()).flatten(); - match file_path_str { + match parent_dir_name { None => continue, - Some(path) => { - if !re.is_match(path) { + Some(parent_dir_name) => { + let mut name = parent_dir_name.to_os_string(); + name.push(".txt"); + if name != file_name { continue; } }