Skip to content

Commit

Permalink
Add --remove flag
Browse files Browse the repository at this point in the history
raiguard committed Nov 3, 2021
1 parent 746d0fe commit 776623b
Showing 3 changed files with 49 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# v0.2.0 - ??????????
## Features
- Added `--remove` flag
## Bugfixes
- Fixed GitHub release workflow

2 changes: 1 addition & 1 deletion Cargo.lock

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

49 changes: 46 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(iter_intersperse)]

use semver::Version;
use semver::{Version, VersionReq};
use std::collections::HashMap;
use std::error::Error;
use std::fs;
@@ -40,6 +40,9 @@ struct App {
/// Enables the given mods. Mods are formatted as `Name` or `Name@Version`
#[structopt(short, long)]
enable: Vec<InputMod>,
/// Removes the given mods from the mods directory. Mods are formatted as `Name` or `Name@Version`
#[structopt(short, long)]
remove: Vec<InputMod>,
}

impl App {
@@ -94,7 +97,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let dir = app.dir.unwrap();

// Get all mods in the directory
let mod_entries = fs::read_dir(&dir)?
let mut mod_entries = fs::read_dir(&dir)?
.filter_map(|entry| {
let entry = entry.ok()?;
let file_name = entry.file_name();
@@ -134,9 +137,49 @@ fn main() -> Result<(), Box<dyn Error>> {
// Parse mod-list.json
let mut mlj_path = dir;
mlj_path.push("mod-list.json");
let enabled_versions = std::fs::read_to_string(&mlj_path)?;
let enabled_versions = fs::read_to_string(&mlj_path)?;
let mut mod_list_json: ModListJson = serde_json::from_str(&enabled_versions)?;

// Remove specified mods
for mod_ident in app.remove {
if mod_ident.name != "base" {
let version_req = mod_ident
.version_req
.as_ref()
.cloned()
.unwrap_or_else(VersionReq::any);
if let Some(mod_versions) = mod_entries.get(&mod_ident.name) {
mod_versions
.iter()
.filter(|version| version_req.matches(&version.version))
.for_each(|version| {
let result = version.entry.metadata().and_then(|metadata| {
if metadata.is_dir() {
fs::remove_dir_all(version.entry.path())
} else {
fs::remove_file(version.entry.path())
}
});
if result.is_ok() {
println!("Removed {} v{}", &mod_ident.name, version.version);
} else {
println!("Could not remove {} v{}", &mod_ident.name, version.version);
}
});
mod_entries.remove(&mod_ident.name);
}

if let Some((index, _)) = mod_list_json
.mods
.iter()
.enumerate()
.find(|(_, mod_state)| mod_ident.name == mod_state.name)
{
mod_list_json.mods.remove(index);
}
}
}

// Disable all mods
if app.disable_all {
println!("Disabled all mods");

0 comments on commit 776623b

Please sign in to comment.