Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Jul 13, 2024
1 parent 5229fed commit 4f17d5f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 32 deletions.
31 changes: 22 additions & 9 deletions src/api/app/actions/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,49 @@ 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};

Check warning on line 5 in src/api/app/actions/script/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `JavaInstallation`

warning: unused import: `JavaInstallation` --> src/api/app/actions/script/mod.rs:5:93 | 5 | use crate::api::{app::App, models::server::Server, tools::java::{get_java_installation_for, JavaInstallation}, utils::script::Shell}; | ^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

impl App {
fn get_script_lines_for(&self, shell: &Shell, server: &Server) -> Vec<String> {
async fn get_script_lines_for(&self, shell: &Shell, server: &Server) -> Result<Vec<String>> {
let mut lines = vec![];

if *shell == Shell::Bat {
lines.push(format!("title {}", server.name));
}

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"))

Check warning on line 20 in src/api/app/actions/script/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

called `map(<f>).unwrap_or(<a>)` on an `Option` value

warning: called `map(<f>).unwrap_or(<a>)` on an `Option` value --> src/api/app/actions/script/mod.rs:20:13 | 20 | get_java_installation_for(*v).await.map(|j| j.path.to_string_lossy().into_owned()).unwrap_or(String::from("java")) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_unwrap_or = note: `-W clippy::map-unwrap-or` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::map_unwrap_or)]` help: use `map_or(<a>, <f>)` instead | 20 - get_java_installation_for(*v).await.map(|j| j.path.to_string_lossy().into_owned()).unwrap_or(String::from("java")) 20 + get_java_installation_for(*v).await.map_or(String::from("java"), |j| j.path.to_string_lossy().into_owned()) |
} 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(())
Expand Down
2 changes: 1 addition & 1 deletion src/api/models/launcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct ServerLauncher {
#[serde(skip_serializing_if = "Vec::is_empty")]
pub postlaunch: Vec<String>,

pub java_version: Option<String>,
pub java_version: Option<u32>,
}

impl ServerLauncher {
Expand Down
27 changes: 26 additions & 1 deletion src/api/tools/java/installation.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -26,3 +28,26 @@ impl JavaInstallation {
Ok(str.parse::<u32>()?)
}
}

static JAVA_INSTALLATIONS: OnceCell<Vec<JavaInstallation>> = OnceCell::const_new();

pub async fn get_java_installations() -> &'static Vec<JavaInstallation> {
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<JavaInstallation> {
get_java_installations()
.await
.into_iter()

Check failure on line 50 in src/api/tools/java/installation.rs

View workflow job for this annotation

GitHub Actions / clippy

this `.into_iter()` call is equivalent to `.iter()` and will not consume the `Vec`

error: this `.into_iter()` call is equivalent to `.iter()` and will not consume the `Vec` --> src/api/tools/java/installation.rs:50:10 | 50 | .into_iter() | ^^^^^^^^^ help: call directly: `iter` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_on_ref
.find(|v| v.version == ver)
.cloned()
}
21 changes: 1 addition & 20 deletions src/api/tools/java/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -63,21 +62,3 @@ impl JavaProcess {
Ok(self.child.wait().await?)
}
}

pub async fn get_java_installations() -> Vec<JavaInstallation> {
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<JavaInstallation> {
get_java_installations()
.await
.into_iter()
.find(|v| v.version == ver)
}
2 changes: 1 addition & 1 deletion src/commands/java/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub async fn run(_app: Arc<App>, args: Args) -> Result<()> {
return Ok(());
}

for install in &installs {
for install in installs {
println!(
"{}",
style(format!("Java {}, {}", install.version, install.architecture))
Expand Down

0 comments on commit 4f17d5f

Please sign in to comment.