From 5f6214926bcc2b0f6d40fc07137b95dab67c3687 Mon Sep 17 00:00:00 2001 From: Danil Karpenko <81296950+LIMPIX31@users.noreply.github.com> Date: Tue, 6 Aug 2024 13:38:11 +0300 Subject: [PATCH] feat: move var setting in args to macro, add justfile (#20) --- crates/spuz_wrench/src/internal.rs | 31 +++++++++++++----------------- crates/spuz_wrench/src/lib.rs | 1 + crates/spuz_wrench/src/macros.rs | 13 +++++++++++++ crates/spuz_wrench/src/mandep.rs | 4 ++-- justfile | 6 ++++++ 5 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 crates/spuz_wrench/src/macros.rs create mode 100644 justfile diff --git a/crates/spuz_wrench/src/internal.rs b/crates/spuz_wrench/src/internal.rs index 23c32db..ab4ba76 100644 --- a/crates/spuz_wrench/src/internal.rs +++ b/crates/spuz_wrench/src/internal.rs @@ -5,6 +5,8 @@ use itertools::Itertools; use spuz_spawner::{LaunchMod, Layer}; use tracing::debug; +use crate::set_vars; + cfg_if! { if #[cfg(target_os = "windows")] { const SEP: &str = ";"; @@ -36,9 +38,8 @@ where it.as_ref().to_string_lossy().into_owned() }) .join(SEP); - for arg in &mut *launch_mod.java_args { - *arg = arg.replace("${classpath}", &cp); - } + + set_vars!(launch_mod.java_args, "classpath", &cp); debug!("{len} items added to the classpath"); } } @@ -49,9 +50,7 @@ pub struct NativesDir<'a>(pub &'a Path); impl<'a> Layer for NativesDir<'a> { fn apply(self, launch_mod: &mut LaunchMod) { let str_path = self.0.to_string_lossy(); - for arg in &mut *launch_mod.java_args { - *arg = arg.replace("${natives_directory}", &str_path); - } + set_vars!(launch_mod.java_args, "natives_directory", &str_path); debug!("Library(natives) path set to {:?}", self.0); } } @@ -65,12 +64,12 @@ pub struct VersionInfo<'a> { impl Layer for VersionInfo<'_> { fn apply(self, launch_mod: &mut LaunchMod) { - for arg in &mut *launch_mod.app_args { - *arg = arg.replace("${version_name}", self.id); - *arg = arg.replace("${version_type}", self.version_type); - *arg = arg.replace("${user_type}", "msa"); - *arg = arg.replace("${assets_index_name}", self.asset_index_id); - } + set_vars!(launch_mod.app_args, { + "version_name" => self.id, + "version_type" => self.version_type, + "user_type" => "msa", + "assets_index_name" => self.asset_index_id, + }); } } @@ -80,9 +79,7 @@ pub struct AssersDir<'a>(pub &'a Path); impl<'a> Layer for AssersDir<'a> { fn apply(self, launch_mod: &mut LaunchMod) { let str_path = self.0.to_string_lossy(); - for arg in &mut *launch_mod.app_args { - *arg = arg.replace("${assets_root}", &str_path); - } + set_vars!(launch_mod.app_args, "assets_root", &str_path); debug!("Assets directory set to {:?}", &self.0); } } @@ -93,9 +90,7 @@ pub struct GameDir<'a>(pub &'a Path); impl<'a> Layer for GameDir<'a> { fn apply(self, launch_mod: &mut LaunchMod) { let str_path = self.0.to_string_lossy(); - for arg in &mut *launch_mod.app_args { - *arg = arg.replace("${game_directory}", &str_path); - } + set_vars!(launch_mod.app_args, "game_directory", &str_path); debug!("Game(instance) directory set to {:?}", &self.0); } } diff --git a/crates/spuz_wrench/src/lib.rs b/crates/spuz_wrench/src/lib.rs index 30341f2..59121ae 100644 --- a/crates/spuz_wrench/src/lib.rs +++ b/crates/spuz_wrench/src/lib.rs @@ -1,4 +1,5 @@ mod internal; +pub mod macros; mod mandep; mod opts; diff --git a/crates/spuz_wrench/src/macros.rs b/crates/spuz_wrench/src/macros.rs new file mode 100644 index 0000000..dcb782e --- /dev/null +++ b/crates/spuz_wrench/src/macros.rs @@ -0,0 +1,13 @@ +#[macro_export] +macro_rules! set_vars { + ($target:expr, $name:literal, $value:expr) => { + for arg in &mut *$target { + *arg = arg.replace(concat!("${", $name, "}"), $value); + } + }; + ($target:expr, { $($name:literal => $value:expr,)* }) => { + for arg in &mut *$target { + $(*arg = arg.replace(concat!("${", $name, "}"), $value);)* + } + }; +} diff --git a/crates/spuz_wrench/src/mandep.rs b/crates/spuz_wrench/src/mandep.rs index 9666ac8..50175aa 100644 --- a/crates/spuz_wrench/src/mandep.rs +++ b/crates/spuz_wrench/src/mandep.rs @@ -59,11 +59,11 @@ impl<'a> Layer for ManifestLayer<'a> { } fn push_modern_arguments(rulcomp: &RuleCompilance, args: &Arguments, launch_mod: &mut LaunchMod) { - for arg in args.jvm.iter() { + for arg in &args.jvm { push_modern_arg(rulcomp, launch_mod.java_args, arg); } - for arg in args.game.iter() { + for arg in &args.game { push_modern_arg(rulcomp, launch_mod.app_args, arg); } } diff --git a/justfile b/justfile new file mode 100644 index 0000000..5b73c81 --- /dev/null +++ b/justfile @@ -0,0 +1,6 @@ +fmt: + cargo +nightly fmt --all + +lint: + cargo clippy --all-targets --all-features -- -D warnings + cargo +nightly fmt --all -- --check