Skip to content

Commit

Permalink
Support nested modules (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trojan295 authored Nov 29, 2021
1 parent 7b1730a commit 9f3c434
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 16 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ tempfile = "3.1"
requestty = "0.1"
colored = "2"
prettytable-rs = "^0.8"
walkdir = "2"
35 changes: 24 additions & 11 deletions src/addons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand All @@ -31,34 +33,45 @@ fn extract_dependency(dep: &str) -> Option<String> {
}

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<AddonList, Box<dyn Error>> {
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_name = entry_dir.file_name();
let parent_dir_name = file_path.parent().map(|f| f.file_name()).flatten();

match parent_dir_name {
None => continue,
Some(parent_dir_name) => {
let mut name = parent_dir_name.to_os_string();
name.push(".txt");
if name != file_name {
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()),
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<AddonEntry>,
}
Expand Down Expand Up @@ -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![],
}
}
Expand All @@ -82,15 +82,15 @@ 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![],
}
}

#[cfg(target_os = "macos")]
fn get_initial_config() -> Config {
Config {
addon_dir: String::from(""),
addon_dir: PathBuf::new(),
addons: vec![],
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions tests/manager.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
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(())
}

0 comments on commit 9f3c434

Please sign in to comment.