Add Linux version of run_jar

This might not work on macOS
This commit is contained in:
fnrir
2023-08-18 13:43:43 +02:00
parent 699eb2838e
commit b179ccc1f0
3 changed files with 66 additions and 15 deletions

48
src-tauri/Cargo.lock generated
View File

@@ -260,7 +260,7 @@ checksum = "b9ccdd8f2a161be9bd5c023df56f1b2a0bd1d83872ae53b71a84a12c9bf6e842"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.18",
"syn 2.0.29",
]
[[package]]
@@ -704,6 +704,12 @@ dependencies = [
"cache-padded",
]
[[package]]
name = "configparser"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5458d9d1a587efaf5091602c59d299696a3877a439c8f6d461a2d3cce11df87a"
[[package]]
name = "constant_time_eq"
version = "0.1.5"
@@ -927,6 +933,7 @@ dependencies = [
"sysinfo 0.28.4",
"tauri",
"tauri-build",
"term-detect",
"tokio",
"tokio-rustls",
"tokio-tungstenite 0.17.2",
@@ -1231,7 +1238,7 @@ dependencies = [
"num-traits 0.2.15",
"proc-macro2",
"quote",
"syn 2.0.18",
"syn 2.0.29",
]
[[package]]
@@ -2800,9 +2807,9 @@ dependencies = [
[[package]]
name = "once_cell"
version = "1.17.1"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "opaque-debug"
@@ -3274,9 +3281,9 @@ checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
[[package]]
name = "proc-macro2"
version = "1.0.60"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406"
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
dependencies = [
"unicode-ident",
]
@@ -3310,9 +3317,9 @@ dependencies = [
[[package]]
name = "quote"
version = "1.0.28"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
@@ -4251,9 +4258,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.18"
version = "2.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e"
checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a"
dependencies = [
"proc-macro2",
"quote",
@@ -4594,6 +4601,17 @@ dependencies = [
"utf-8",
]
[[package]]
name = "term-detect"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c894f66f8023c49c6dc698d89c87775245dd77f738efc2eb5a5849e3b87ce36"
dependencies = [
"configparser",
"once_cell",
"thiserror",
]
[[package]]
name = "thin-slice"
version = "0.1.1"
@@ -4602,22 +4620,22 @@ checksum = "8eaa81235c7058867fa8c0e7314f33dcce9c215f535d1913822a2b3f5e289f3c"
[[package]]
name = "thiserror"
version = "1.0.31"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd829fe32373d27f76265620b5309d0340cb8550f523c1dda251d6298069069a"
checksum = "97a802ec30afc17eee47b2855fc72e0c4cd62be9b4efe6591edde0ec5bd68d8f"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.31"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0396bc89e626244658bef819e22d0cc459e795a5ebe878e6ec336d1674a8d79a"
checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.98",
"syn 2.0.29",
]
[[package]]

View File

@@ -22,6 +22,9 @@ registry = "1.2.1"
[target.'cfg(unix)'.dependencies]
sudo = "0.6.0"
[target.'cfg(target_os = "linux")'.dependencies]
term-detect = "0.1.7"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.0.9", features = ["api-all"] }

View File

@@ -16,6 +16,18 @@ use crate::AAGL_THREAD;
use anime_launcher_sdk::genshin::game;
#[cfg(target_os = "linux")]
use std::thread;
#[cfg(target_os = "linux")]
use term_detect::get_terminal;
#[cfg(target_os = "linux")]
fn guess_user_terminal() -> String {
if let Ok(term) = get_terminal() {
return term.0;
}
eprintln!("Could not guess default terminal. Try setting the $TERMINAL environment variable.");
// If everything fails, default to xterm
"xterm".to_string()
}
#[tauri::command]
pub fn run_program(path: String, args: Option<String>) {
@@ -96,6 +108,7 @@ pub fn run_jar(path: String, execute_in: String, java_path: String) {
println!("Launching .jar with command: {}", &command);
// Open the program from the specified path.
#[cfg(not(target_os = "linux"))]
match open::with(
format!("/k cd /D \"{}\" & {}", &execute_in, &command),
"C:\\Windows\\System32\\cmd.exe",
@@ -103,6 +116,23 @@ pub fn run_jar(path: String, execute_in: String, java_path: String) {
Ok(_) => (),
Err(e) => println!("Failed to open jar ({} from {}): {}", &path, &execute_in, e),
};
#[cfg(target_os = "linux")]
thread::spawn(move || {
match Command::new(guess_user_terminal())
.arg("-e")
.arg(command)
.current_dir(execute_in.clone())
.spawn()
{
Ok(mut handler) => {
// Prevent creation of zombie processes
handler
.wait()
.expect("Grasscutter exited with non-zero exit code");
}
Err(e) => println!("Failed to open jar ({} from {}): {}", &path, &execute_in, e),
}
});
}
#[cfg(target_os = "windows")]