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 {
/// Initialises a new mcmpmgr project in the specified directory (or current dir if not specified)
Init {
/// The root modpack project directory
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"))]
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<dyn Error>> {
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());
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)?;
}
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());
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)?;
}
}
};

View file

@ -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<ModMeta>,
@ -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<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 {
fn default() -> Self {
Self {
pack_name: "my_modpack".into(),
mc_version: "1.20.1".into(),
modloader: ModLoader::Forge,
mods: Default::default(),