Implemented profiles

This commit is contained in:
Warren Hood 2024-08-20 21:00:43 +02:00
parent 834d3337db
commit 4d851be49b
5 changed files with 270 additions and 14 deletions

10
Cargo.lock generated
View file

@ -419,6 +419,15 @@ version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "home"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "http"
version = "1.1.0"
@ -668,6 +677,7 @@ version = "0.1.0"
dependencies = [
"clap",
"git2",
"home",
"lhash",
"reqwest",
"semver",

View file

@ -1,11 +1,13 @@
[package]
name = "mcmpmgr"
authors = ["Warren Hood <nullbyte001@gmail.com>"]
version = "0.1.0"
edition = "2021"
[dependencies]
clap = { version = "4.5.15", features = ["derive"] }
git2 = "0.19.0"
home = "0.5.9"
lhash = { version = "1.1.0", features = ["sha1", "sha512"] }
reqwest = { version = "0.12.5", features = ["json"] }
semver = { version = "1.0.23", features = ["serde"] }

View file

@ -1,17 +1,19 @@
mod mod_meta;
mod modpack;
mod profiles;
mod providers;
mod resolver;
use clap::{Parser, Subcommand};
use clap::{Args, Parser, Subcommand};
use mod_meta::{ModMeta, ModProvider};
use modpack::ModpackMeta;
use profiles::{PackSource, Profile};
use providers::DownloadSide;
use std::{error::Error, path::PathBuf};
/// A Minecraft Modpack Manager
#[derive(Parser)]
#[command(version, about, long_about = None)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
@ -88,7 +90,7 @@ enum Commands {
/// Mods directory
mods_dir: PathBuf,
/// Side to download for
#[arg(long, short, default_value_t = DownloadSide::Both)]
#[arg(long, default_value_t = DownloadSide::Server)]
side: DownloadSide,
/// Download mods from a remote modpack in a git repo
#[arg(long)]
@ -103,6 +105,50 @@ enum Commands {
#[arg(long, short, action)]
locked: bool,
},
/// Manage mcmpmgr profiles
Profile(ProfileArgs),
}
#[derive(Debug, Args)]
#[command(args_conflicts_with_subcommands = true)]
struct ProfileArgs {
#[command(subcommand)]
command: Option<ProfileCommands>,
}
#[derive(Debug, Subcommand)]
enum ProfileCommands {
/// List all profiles
List,
/// Add or overwrite a profile
Add {
/// Name of the profile
name: String,
/// Side to download the profile for. (Client, Server, or Both)
#[arg(long, default_value_t = DownloadSide::Server)]
side: DownloadSide,
/// A local file path to a modpack directory or a git repo url prefixed with 'git+'
#[arg(long, short)]
pack_source: PackSource,
/// Mods directory
#[arg(long, short)]
mods_directory: PathBuf,
},
/// Install a profile
Install {
/// Name of the profile to install
name: String,
},
/// Show information about a profile
Show {
/// Name of the profile to show
name: String,
},
/// Delete a profile
Remove {
/// Profile to remove
name: String,
},
}
#[tokio::main(flavor = "multi_thread")]
@ -304,9 +350,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
let pack_lock = if let Some(git_url) = git {
let (lock_meta, repo_dir) =
resolver::PinnedPackMeta::load_from_git_repo(&git_url, true).await?;
// Hold on to the repo directory until pack_dir is dropped
let _ = pack_dir.insert(repo_dir);
lock_meta
// Hold on to the repo directory until pack_dir is dropped
let _ = pack_dir.insert(repo_dir);
lock_meta
} else if let Some(local_path) = path {
resolver::PinnedPackMeta::load_from_directory(&local_path, true).await?
} else {
@ -322,6 +368,55 @@ async fn main() -> Result<(), Box<dyn Error>> {
pack_lock.init(&modpack_meta, !locked).await?;
pack_lock.save_current_dir_lock()?;
}
Commands::Profile(ProfileArgs { command }) => {
if let Some(command) = command {
match command {
ProfileCommands::List => {
let userdata = profiles::Data::load()?;
println!("Profiles:");
for profile in userdata.get_profile_names().iter() {
println!("- {profile}");
}
}
ProfileCommands::Add {
name,
side,
pack_source,
mods_directory,
} => {
let mut userdata = profiles::Data::load()?;
let profile = Profile::new(&mods_directory, pack_source, side);
userdata.add_profile(&name, profile);
userdata.save()?;
println!("Saved profile '{name}'");
}
ProfileCommands::Install { name } => {
let userdata = profiles::Data::load()?;
let profile = userdata
.get_profile(&name)
.ok_or(format!("Profile '{name}' does not exist"))?;
println!("Installing profile '{name}'...");
profile.install().await?;
println!("Installed profile '{name}' successfully");
}
ProfileCommands::Remove { name } => {
let mut userdata = profiles::Data::load()?;
userdata.remove_profile(&name);
println!("Removed profile '{name}'");
}
ProfileCommands::Show { name } => {
let userdata = profiles::Data::load()?;
let profile = userdata
.get_profile(&name)
.ok_or(format!("Profile '{name}' does not exist"))?;
println!("Profile name : {name}");
println!("Mods folder : {}", profile.mods_folder.display());
println!("Modpack source: {}", profile.pack_source);
println!("Side : {}", profile.side);
}
}
}
}
}
};

150
src/profiles.rs Normal file
View file

@ -0,0 +1,150 @@
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
error::Error,
fmt::Display,
path::{Path, PathBuf},
str::FromStr,
};
use crate::{providers::DownloadSide, resolver::PinnedPackMeta};
const CONFIG_DIR_NAME: &str = "mcmpmgr";
const DATA_FILENAME: &str = "data.toml";
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PackSource {
Git { url: String },
Local { path: PathBuf },
}
impl FromStr for PackSource {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with("git+") {
let url = s.trim_start_matches("git+").to_string();
Ok(PackSource::Git { url })
} else {
let path = PathBuf::from(s);
Ok(PackSource::Local { path })
}
}
}
impl Display for PackSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PackSource::Git { url } => write!(f, "git+{url}"),
PackSource::Local { path } => write!(f, "{}", path.display()),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Profile {
pub mods_folder: PathBuf,
pub pack_source: PackSource,
pub side: DownloadSide,
}
impl Profile {
pub fn new(mods_folder: &Path, pack_source: PackSource, side: DownloadSide) -> Self {
Self {
mods_folder: mods_folder.into(),
pack_source,
side,
}
}
pub async fn install(&self) -> Result<(), Box<dyn Error>> {
let (pack_lock, temp_dir) = match &self.pack_source {
PackSource::Git { url } => {
let (pack_lock, packdir) = PinnedPackMeta::load_from_git_repo(&url, true).await?;
(pack_lock, Some(packdir))
}
PackSource::Local { path } => (
PinnedPackMeta::load_from_directory(&path, true).await?,
None,
),
};
pack_lock
.download_mods(&self.mods_folder, self.side)
.await?;
Ok(())
}
}
/// User data and configs for the modpack manager
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Data {
profiles: HashMap<String, Profile>,
}
impl Default for Data {
fn default() -> Self {
Self {
profiles: Default::default(),
}
}
}
impl Data {
pub fn get_profile_names(&self) -> Vec<String> {
self.profiles.keys().cloned().collect()
}
// Add or update a profile
pub fn add_profile(&mut self, profile_name: &str, profile: Profile) {
self.profiles.insert(profile_name.into(), profile);
}
pub fn get_profile(&self, profile_name: &str) -> Option<&Profile> {
self.profiles.get(profile_name)
}
pub fn get_profile_mut(&mut self, profile_name: &str) -> Option<&mut Profile> {
self.profiles.get_mut(profile_name)
}
pub fn remove_profile(&mut self, profile_name: &str) {
self.profiles.remove(profile_name);
}
fn get_config_folder_path() -> Result<PathBuf, Box<dyn Error>> {
home::home_dir()
.and_then(|home_dir| Some(home_dir.join(format!(".config/{CONFIG_DIR_NAME}"))))
.ok_or("Unable to locate home directory".into())
}
pub fn load() -> Result<Self, Box<dyn Error>> {
let config_dir = Self::get_config_folder_path()?;
if !config_dir.exists() {
println!("Creating config directory {config_dir:#?}...");
std::fs::create_dir_all(&config_dir)?;
}
let datafile = config_dir.join(DATA_FILENAME);
Ok(if !datafile.exists() {
Self::default()
} else {
let data_string = std::fs::read_to_string(datafile)?;
toml::from_str(&data_string)?
})
}
pub fn save(&self) -> Result<(), Box<dyn Error>> {
let config_dir = Self::get_config_folder_path()?;
if !config_dir.exists() {
println!("Creating config directory {config_dir:#?}...");
std::fs::create_dir_all(&config_dir)?;
}
let datafile = config_dir.join(DATA_FILENAME);
std::fs::write(datafile, toml::to_string(self)?)?;
println!("Saved user profiles configuration");
Ok(())
}
}

View file

@ -1,6 +1,6 @@
use crate::mod_meta::ModMeta;
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, path::PathBuf, str::FromStr};
use std::{collections::HashSet, fmt::Display, path::PathBuf, str::FromStr};
pub mod modrinth;
pub mod raw;
@ -21,7 +21,7 @@ pub enum FileSource {
},
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum DownloadSide {
Both,
Server,
@ -44,14 +44,13 @@ impl FromStr for DownloadSide {
}
}
impl ToString for DownloadSide {
fn to_string(&self) -> String {
impl Display for DownloadSide {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DownloadSide::Both => "Both",
DownloadSide::Server => "Server",
DownloadSide::Client => "Client",
DownloadSide::Both => write!(f, "Both"),
DownloadSide::Server => write!(f, "Server"),
DownloadSide::Client => write!(f, "Client"),
}
.into()
}
}