From 52efc9780d28ffa20eee024893d8ddd727c3d6ff Mon Sep 17 00:00:00 2001 From: Warren Hood Date: Wed, 14 Aug 2024 22:28:40 +0200 Subject: [PATCH] Added a `new` command --- src/main.rs | 69 +++++++++++++++++++++++++++++++++++++------------- src/modpack.rs | 27 ++++++++++++++++++-- 2 files changed, 77 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index b5c53fb..b5848be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,9 +16,26 @@ struct Cli { enum Commands { /// Initialises a new mcmpmgr project in the specified directory (or current dir if not specified) Init { + /// The root modpack project directory directory: Option, + /// Name of the modpack project + #[arg(long)] + name: Option, + /// The modpack's Minecraft version #[arg(long, default_value_t = String::from("1.20.1"))] mc_version: String, + /// The modpack's modloader + #[arg(long, default_value_t = modpack::ModLoader::Fabric)] + modloader: modpack::ModLoader, + }, + /// Creates and initialises a new mcmpmgr project in the current directory + New { + /// Name of the new modpack project + name: String, + /// The modpack's Minecraft version + #[arg(long, default_value_t = String::from("1.20.1"))] + mc_version: String, + /// The modpack's modloader #[arg(long, default_value_t = modpack::ModLoader::Fabric)] modloader: modpack::ModLoader, }, @@ -33,25 +50,43 @@ fn main() -> Result<(), Box> { directory, mc_version, modloader, + name, } => { let dir = directory.unwrap_or(std::env::current_dir()?); - let mc_modpack_meta = ModpackMeta::new(&mc_version, modloader); - let modpack_meta_file_path = dir.clone().join(PathBuf::from("mcmodpack.toml")); - if modpack_meta_file_path.exists() { - return Err(format!( - "mcmodpack.toml already exists at {}", - modpack_meta_file_path.display() - ) - .into()); - } - - std::fs::write( - modpack_meta_file_path, - toml::to_string(&mc_modpack_meta) - .expect("MC Modpack Meta should be serializable"), - )?; - - println!("MC modpack project initialized at {}", dir.display()); + let pack_name = if let Some(name) = name { + name + } else { + dir.file_name() + .ok_or(format!( + "Cannot find pack name based on directory '{}'", + dir.display() + ))? + .to_string_lossy() + .into() + }; + println!( + "Initializing project '{}' at '{}'...", + &pack_name, + dir.display() + ); + let mc_modpack_meta: ModpackMeta = + ModpackMeta::new(&pack_name, &mc_version, modloader); + mc_modpack_meta.init_project(&dir)?; + } + Commands::New { + name, + mc_version, + modloader, + } => { + let dir = std::env::current_dir()?.join(PathBuf::from(&name)); + println!( + "Creating new modpack project '{}' at '{}'...", + &name, + dir.display() + ); + std::fs::create_dir_all(&dir)?; + let mc_modpack_meta: ModpackMeta = ModpackMeta::new(&name, &mc_version, modloader); + mc_modpack_meta.init_project(&dir)?; } } }; diff --git a/src/modpack.rs b/src/modpack.rs index e6b6962..9ea433c 100644 --- a/src/modpack.rs +++ b/src/modpack.rs @@ -1,4 +1,4 @@ -use std::borrow::BorrowMut; +use std::{borrow::BorrowMut, error::Error, path::PathBuf}; use serde::{Deserialize, Serialize}; @@ -99,6 +99,7 @@ impl std::str::FromStr for ModLoader { #[derive(Debug, Serialize, Deserialize)] pub struct ModpackMeta { + pack_name: String, mc_version: String, modloader: ModLoader, mods: Vec, @@ -106,8 +107,9 @@ pub struct ModpackMeta { } impl ModpackMeta { - pub fn new(mc_version: &str, modloader: ModLoader) -> Self { + pub fn new(pack_name: &str, mc_version: &str, modloader: ModLoader) -> Self { Self { + pack_name: pack_name.into(), mc_version: mc_version.into(), modloader: modloader, ..Default::default() @@ -127,11 +129,32 @@ impl ModpackMeta { } self } + + pub fn init_project(&self, directory: &PathBuf) -> Result<(), Box> { + let modpack_meta_file_path = directory.clone().join(PathBuf::from("mcmodpack.toml")); + if modpack_meta_file_path.exists() { + return Err(format!( + "mcmodpack.toml already exists at {}", + modpack_meta_file_path.display() + ) + .into()); + } + + std::fs::write( + modpack_meta_file_path, + toml::to_string(self) + .expect("MC Modpack Meta should be serializable"), + )?; + + println!("MC modpack project initialized at {}", directory.display()); + Ok(()) + } } impl std::default::Default for ModpackMeta { fn default() -> Self { Self { + pack_name: "my_modpack".into(), mc_version: "1.20.1".into(), modloader: ModLoader::Forge, mods: Default::default(),