From 4a9c451aedd7067b19823c5b09fc295103be8e9e Mon Sep 17 00:00:00 2001 From: Al Cheung Date: Fri, 16 Feb 2024 17:00:00 +0800 Subject: [PATCH] powershell path --- Cargo.lock | 7 +++++++ crates/lcu/Cargo.toml | 1 + crates/lcu/src/cmd.rs | 47 +++++++++++++++++-------------------------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea852ccf..e6bb294b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1970,6 +1970,7 @@ dependencies = [ "kv-log-macro", "lazy_static", "log", + "powershell_script", "rand", "regex", "reqwest", @@ -2562,6 +2563,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "powershell_script" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef8336090917f3d3a044256bc0e5c51d5420e5d09dfa1df4868083c5231a454" + [[package]] name = "ppv-lite86" version = "0.2.17" diff --git a/crates/lcu/Cargo.toml b/crates/lcu/Cargo.toml index 1d7cc113..5217babd 100644 --- a/crates/lcu/Cargo.toml +++ b/crates/lcu/Cargo.toml @@ -30,3 +30,4 @@ flate2 = "1.0.28" tar = "0.4.40" femme = "2.2.1" kv-log-macro = "1.0.7" +powershell_script = "1.1.0" diff --git a/crates/lcu/src/cmd.rs b/crates/lcu/src/cmd.rs index 92379bcf..bcf8f7c6 100644 --- a/crates/lcu/src/cmd.rs +++ b/crates/lcu/src/cmd.rs @@ -20,7 +20,8 @@ lazy_static! { static ref REGION_REGEXP: regex::Regex = regex::Regex::new(r"--region=\S+").unwrap(); static ref DIR_REGEXP: regex::Regex = regex::Regex::new(r#"--install-directory=(.*?)""#).unwrap(); - static ref MAC_DIR_REGEXP: regex::Regex = regex::Regex::new(r"--install-directory=([^\s]+).*?--").unwrap(); + static ref MAC_DIR_REGEXP: regex::Regex = + regex::Regex::new(r"--install-directory=([^\s]+).*?--").unwrap(); } pub fn make_auth_url(token: &String, port: &String) -> String { @@ -38,33 +39,23 @@ pub struct CommandLineOutput { #[cfg(target_os = "windows")] pub fn get_commandline() -> CommandLineOutput { - use std::{os::windows::process::CommandExt, process::Command}; - - match Command::new("powershell") - .args([ - "-ExecutionPolicy", - "Bypass", - "-NoLogo", - "-NoProfile", - "-NonInteractive", - "-WindowStyle", - "Hidden", - "-Command", - LCU_COMMAND, - ]) - .creation_flags(0x08000000) - .output() - { - Ok(output) => { - if output.status.success() { - let output = String::from_utf8_lossy(&output.stdout); - - #[cfg(not(debug_assertions))] - info!("output: {:?}", &output); - - if !output.is_empty() { - return match_stdout(&String::from(output)); - } + use powershell_script::PsScriptBuilder; + + let ps = PsScriptBuilder::new() + .no_profile(true) + .non_interactive(true) + .hidden(true) + .print_commands(false) + .build(); + + match ps.run(LCU_COMMAND) { + Ok(out) => { + let output = out.stdout(); + + #[cfg(not(debug_assertions))] + info!("output: {:?}", &output); + if output.is_some() { + return match_stdout(&String::from(output.unwrap())); } } Err(err) => error!("cmd error: {:?}", err),