Added a new command

This commit is contained in:
Warren Hood 2024-08-14 22:28:40 +02:00
parent 9d2817f721
commit 52efc9780d
2 changed files with 77 additions and 19 deletions

View file

@ -16,9 +16,26 @@ struct Cli {
enum Commands { enum Commands {
/// Initialises a new mcmpmgr project in the specified directory (or current dir if not specified) /// Initialises a new mcmpmgr project in the specified directory (or current dir if not specified)
Init { Init {
/// The root modpack project directory
directory: Option<PathBuf>, directory: Option<PathBuf>,
/// Name of the modpack project
#[arg(long)]
name: Option<String>,
/// The modpack's Minecraft version
#[arg(long, default_value_t = String::from("1.20.1"))] #[arg(long, default_value_t = String::from("1.20.1"))]
mc_version: String, 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)] #[arg(long, default_value_t = modpack::ModLoader::Fabric)]
modloader: modpack::ModLoader, modloader: modpack::ModLoader,
}, },
@ -33,25 +50,43 @@ fn main() -> Result<(), Box<dyn Error>> {
directory, directory,
mc_version, mc_version,
modloader, modloader,
name,
} => { } => {
let dir = directory.unwrap_or(std::env::current_dir()?); let dir = directory.unwrap_or(std::env::current_dir()?);
let mc_modpack_meta = ModpackMeta::new(&mc_version, modloader); let pack_name = if let Some(name) = name {
let modpack_meta_file_path = dir.clone().join(PathBuf::from("mcmodpack.toml")); name
if modpack_meta_file_path.exists() { } else {
return Err(format!( dir.file_name()
"mcmodpack.toml already exists at {}", .ok_or(format!(
modpack_meta_file_path.display() "Cannot find pack name based on directory '{}'",
) dir.display()
.into()); ))?
.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 {
std::fs::write( name,
modpack_meta_file_path, mc_version,
toml::to_string(&mc_modpack_meta) modloader,
.expect("MC Modpack Meta should be serializable"), } => {
)?; let dir = std::env::current_dir()?.join(PathBuf::from(&name));
println!(
println!("MC modpack project initialized at {}", dir.display()); "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)?;
} }
} }
}; };

View file

@ -1,4 +1,4 @@
use std::borrow::BorrowMut; use std::{borrow::BorrowMut, error::Error, path::PathBuf};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -99,6 +99,7 @@ impl std::str::FromStr for ModLoader {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct ModpackMeta { pub struct ModpackMeta {
pack_name: String,
mc_version: String, mc_version: String,
modloader: ModLoader, modloader: ModLoader,
mods: Vec<ModMeta>, mods: Vec<ModMeta>,
@ -106,8 +107,9 @@ pub struct ModpackMeta {
} }
impl 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 { Self {
pack_name: pack_name.into(),
mc_version: mc_version.into(), mc_version: mc_version.into(),
modloader: modloader, modloader: modloader,
..Default::default() ..Default::default()
@ -127,11 +129,32 @@ impl ModpackMeta {
} }
self self
} }
pub fn init_project(&self, directory: &PathBuf) -> Result<(), Box<dyn Error>> {
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 { impl std::default::Default for ModpackMeta {
fn default() -> Self { fn default() -> Self {
Self { Self {
pack_name: "my_modpack".into(),
mc_version: "1.20.1".into(), mc_version: "1.20.1".into(),
modloader: ModLoader::Forge, modloader: ModLoader::Forge,
mods: Default::default(), mods: Default::default(),