From dac3092502d0d16a6548d1bc3767f778e376153b Mon Sep 17 00:00:00 2001 From: xeovalyte Date: Mon, 3 Jun 2024 16:28:49 +0200 Subject: [PATCH] Added the ability to update mods --- src/cli.rs | 6 ++++ src/commands/mod.rs | 1 + src/commands/update.rs | 67 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 ++ src/modrinth/versions.rs | 2 +- 5 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/commands/update.rs diff --git a/src/cli.rs b/src/cli.rs index ac7a75b..94c5568 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -24,4 +24,10 @@ pub enum Commands { /// Remove a mod from the modpack Remove, + + /// Update a mod or all mods + Update, + + /// Migrate to a other Minecraft version + Migrate, } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 48ba65f..df345f4 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -2,3 +2,4 @@ pub mod add; pub mod init; pub mod list; pub mod remove; +pub mod update; diff --git a/src/commands/update.rs b/src/commands/update.rs new file mode 100644 index 0000000..4e7f091 --- /dev/null +++ b/src/commands/update.rs @@ -0,0 +1,67 @@ +use crate::{ + config, + modrinth::{project::get_project_versions, versions::ProjectVersion}, + pack::{Mod, Modpack}, +}; +use colored::*; +use inquire::MultiSelect; +use std::fmt; + +struct Diff { + r#mod: Mod, + new_version: ProjectVersion, +} + +impl fmt::Display for Diff { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{} {} -> {}", + self.r#mod.title.bold(), + self.r#mod.version.as_ref().unwrap().version_number.red(), + self.new_version.version_number.green() + ) + } +} + +pub async fn update() { + let mut config = config::Config::get().unwrap(); + let modpack = Modpack::from_config(&config).await.unwrap(); + + let mut diffs: Vec = vec![]; + + // Find the newest compatible version of the mod + for (project_id, r#mod) in modpack.mods { + let project_versions = get_project_versions(&project_id, &config.game_version) + .await + .unwrap(); + + let new_version = project_versions.get(0).unwrap(); + + if r#mod.version.as_ref().unwrap().version_number == new_version.version_number { + continue; + } + + diffs.push(Diff { + r#mod, + new_version: new_version.clone(), + }) + } + + if diffs.len() == 0 { + return println!("There are no mods left to update :)"); + } + + let updated_diffs = MultiSelect::new("Select mods to update", diffs) + .prompt() + .unwrap(); + + // Bumb the new versions to the config + for diff in updated_diffs { + config + .projects + .insert(diff.r#mod.project_id, diff.new_version.id.to_string()); + } + + config.save().unwrap(); +} diff --git a/src/main.rs b/src/main.rs index b570849..db405a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,9 @@ async fn main() { cli::Commands::List => { commands::list::list().await; } + cli::Commands::Update => { + commands::update::update().await; + } _ => (), }; } diff --git a/src/modrinth/versions.rs b/src/modrinth/versions.rs index d21614b..c3f7dd5 100644 --- a/src/modrinth/versions.rs +++ b/src/modrinth/versions.rs @@ -4,7 +4,7 @@ use serde::Deserialize; type Response = Vec; -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub struct ProjectVersion { pub name: String, pub version_number: String,