From 4f17d5fa9ecd19f4cb890bd9023377bedfcaa3b7 Mon Sep 17 00:00:00 2001 From: TheAlan404 Date: Sat, 13 Jul 2024 11:32:14 +0000 Subject: [PATCH] improvements --- src/api/app/actions/script/mod.rs | 31 +++++++++++++++++++++--------- src/api/models/launcher/mod.rs | 2 +- src/api/tools/java/installation.rs | 27 +++++++++++++++++++++++++- src/api/tools/java/mod.rs | 21 +------------------- src/commands/java/list.rs | 2 +- 5 files changed, 51 insertions(+), 32 deletions(-) diff --git a/src/api/app/actions/script/mod.rs b/src/api/app/actions/script/mod.rs index 858abb7..d1ae447 100644 --- a/src/api/app/actions/script/mod.rs +++ b/src/api/app/actions/script/mod.rs @@ -2,10 +2,10 @@ use std::path::Path; use anyhow::Result; -use crate::api::{app::App, models::server::Server, utils::script::Shell}; +use crate::api::{app::App, models::server::Server, tools::java::{get_java_installation_for, JavaInstallation}, utils::script::Shell}; impl App { - fn get_script_lines_for(&self, shell: &Shell, server: &Server) -> Vec { + async fn get_script_lines_for(&self, shell: &Shell, server: &Server) -> Result> { let mut lines = vec![]; if *shell == Shell::Bat { @@ -13,25 +13,38 @@ impl App { } lines.extend(server.launcher.prelaunch.clone()); - let mut args = server.get_arguments(); + + let mut args = vec![]; + + let java = if let Some(v) = &server.launcher.java_version { + get_java_installation_for(*v).await.map(|j| j.path.to_string_lossy().into_owned()).unwrap_or(String::from("java")) + } else { + String::from("java") + }; + + args.push(java); + args.extend(server.get_arguments()); args.push(shell.script_args().to_owned()); + lines.push(args.join(" ")); + lines.extend(server.launcher.postlaunch.clone()); - lines + Ok(lines) } - pub fn action_generate_script(&self, shell: Shell, server: &Server, base: &Path) -> Result<()> { - let script = shell.generate_script(self.get_script_lines_for(&shell, server)); + pub async fn action_generate_script(&self, shell: Shell, server: &Server, base: &Path) -> Result<()> { + let script = shell.generate_script(self.get_script_lines_for(&shell, server).await?); - std::fs::write(base.join(format!("start.{}", shell.file_ext())), script)?; + tokio::fs::write(base.join(format!("start.{}", shell.file_ext())), script) + .await?; Ok(()) } pub async fn action_generate_scripts(&self, base: &Path) -> Result<()> { if let Some((_, server)) = &*self.server.read().await { - self.action_generate_script(Shell::Bat, server, base)?; - self.action_generate_script(Shell::Sh, server, base)?; + self.action_generate_script(Shell::Bat, server, base).await?; + self.action_generate_script(Shell::Sh, server, base).await?; } Ok(()) diff --git a/src/api/models/launcher/mod.rs b/src/api/models/launcher/mod.rs index 6f3d279..92d9d4f 100644 --- a/src/api/models/launcher/mod.rs +++ b/src/api/models/launcher/mod.rs @@ -31,7 +31,7 @@ pub struct ServerLauncher { #[serde(skip_serializing_if = "Vec::is_empty")] pub postlaunch: Vec, - pub java_version: Option, + pub java_version: Option, } impl ServerLauncher { diff --git a/src/api/tools/java/installation.rs b/src/api/tools/java/installation.rs index f2de8f6..1adfa8b 100644 --- a/src/api/tools/java/installation.rs +++ b/src/api/tools/java/installation.rs @@ -1,9 +1,11 @@ use std::path::PathBuf; use anyhow::{bail, Result}; +use futures::StreamExt; use serde::{Deserialize, Serialize}; +use tokio::sync::OnceCell; -use super::JavaVersion; +use super::{check_java, find, JavaVersion}; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct JavaInstallation { @@ -26,3 +28,26 @@ impl JavaInstallation { Ok(str.parse::()?) } } + +static JAVA_INSTALLATIONS: OnceCell> = OnceCell::const_new(); + +pub async fn get_java_installations() -> &'static Vec { + JAVA_INSTALLATIONS.get_or_init(|| async { + let paths = find::collect_possible_java_paths(); + + futures::stream::iter(paths) + .filter_map(|path| async move { + check_java(&path) + }) + .collect() + .await + }).await +} + +pub async fn get_java_installation_for(ver: JavaVersion) -> Option { + get_java_installations() + .await + .into_iter() + .find(|v| v.version == ver) + .cloned() +} diff --git a/src/api/tools/java/mod.rs b/src/api/tools/java/mod.rs index 69a6355..3e14584 100644 --- a/src/api/tools/java/mod.rs +++ b/src/api/tools/java/mod.rs @@ -7,12 +7,11 @@ mod check; use std::{path::Path, process::{ExitStatus, Stdio}}; use anyhow::{anyhow, Result}; -use futures::{Future, StreamExt}; pub use installation::*; pub use check::*; use tokio::{io::{AsyncBufReadExt, BufReader}, process::{Child, Command}}; -use crate::api::utils::pathdiff::{diff_paths, DiffTo}; +use crate::api::utils::pathdiff::DiffTo; pub struct JavaProcess { child: Child, @@ -63,21 +62,3 @@ impl JavaProcess { Ok(self.child.wait().await?) } } - -pub async fn get_java_installations() -> Vec { - let paths = find::collect_possible_java_paths(); - - futures::stream::iter(paths) - .filter_map(|path| async move { - check_java(&path) - }) - .collect() - .await -} - -pub async fn get_java_installation_for(ver: JavaVersion) -> Option { - get_java_installations() - .await - .into_iter() - .find(|v| v.version == ver) -} diff --git a/src/commands/java/list.rs b/src/commands/java/list.rs index c710843..b38b8e9 100644 --- a/src/commands/java/list.rs +++ b/src/commands/java/list.rs @@ -19,7 +19,7 @@ pub async fn run(_app: Arc, args: Args) -> Result<()> { return Ok(()); } - for install in &installs { + for install in installs { println!( "{}", style(format!("Java {}, {}", install.version, install.architecture))