From 53e0c168573fa093dcf30535bb0209c78c8fd5e6 Mon Sep 17 00:00:00 2001 From: 21pages Date: Thu, 10 Nov 2022 14:38:25 +0800 Subject: [PATCH] fix command line support Signed-off-by: 21pages --- libs/portable/src/main.rs | 37 +++++++++++++++++++++---------------- src/common.rs | 4 ++-- src/core_main.rs | 25 ++++++++----------------- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/libs/portable/src/main.rs b/libs/portable/src/main.rs index 1c9dc11ef..ad05e7376 100644 --- a/libs/portable/src/main.rs +++ b/libs/portable/src/main.rs @@ -51,24 +51,29 @@ fn execute(path: PathBuf, args: Vec) { .expect(&format!("failed to execute {:?}", exe_name)); } -fn is_setup(name: &str) -> bool { - name.to_lowercase().ends_with("install.exe") || name.to_lowercase().ends_with("安装.exe") -} - fn main() { - let is_setup = is_setup( - &std::env::current_exe() - .unwrap() - .to_string_lossy() - .to_string(), - ); - let reader = BinaryReader::default(); - if let Some(exe) = setup(reader, None, is_setup) { - let args = if is_setup { - vec!["--install".to_owned()] + let mut args = Vec::new(); + let mut arg_exe = Default::default(); + let mut i = 0; + for arg in std::env::args() { + if i == 0 { + arg_exe = arg.clone(); } else { - vec![] - }; + args.push(arg); + } + i += 1; + } + let click_setup = args.is_empty() && arg_exe.to_lowercase().ends_with("install.exe"); + + let reader = BinaryReader::default(); + if let Some(exe) = setup( + reader, + None, + click_setup || args.contains(&"--silent-install".to_owned()), + ) { + if click_setup { + args = vec!["--install".to_owned()] + } execute(exe, args); } } diff --git a/src/common.rs b/src/common.rs index 78e3147ec..85fcae304 100644 --- a/src/common.rs +++ b/src/common.rs @@ -26,7 +26,7 @@ pub type NotifyMessageBox = fn(String, String, String, String) -> dyn Future bool { } pub fn is_setup(name: &str) -> bool { - name.to_lowercase().ends_with("install.exe") || name.to_lowercase().ends_with("安装.exe") + name.to_lowercase().ends_with("install.exe") } pub fn get_custom_rendezvous_server(custom: String) -> String { diff --git a/src/core_main.rs b/src/core_main.rs index 2baff12ad..86f77946f 100644 --- a/src/core_main.rs +++ b/src/core_main.rs @@ -1,5 +1,3 @@ -use std::env::Args; - use hbb_common::log; // shared by flutter and sciter main function @@ -10,14 +8,14 @@ pub fn core_main() -> Option> { let mut args = Vec::new(); let mut flutter_args = Vec::new(); let mut i = 0; - let mut is_setup = false; let mut _is_elevate = false; let mut _is_run_as_system = false; let mut _is_flutter_connect = false; + let mut arg_exe = Default::default(); for arg in std::env::args() { // to-do: how to pass to flutter? - if i == 0 && crate::common::is_setup(&arg) { - is_setup = true; + if i == 0 { + arg_exe = arg; } else if i > 0 { #[cfg(feature = "flutter")] if arg == "--connect" { @@ -33,21 +31,14 @@ pub fn core_main() -> Option> { } i += 1; } - if args.contains(&"--install".to_string()) { - is_setup = true; - } #[cfg(feature = "flutter")] if _is_flutter_connect { return core_main_invoke_new_connection(std::env::args()); } - if args.contains(&"--install".to_string()) { - is_setup = true; - } - if is_setup { - if args.is_empty() { - args.push("--install".to_owned()); - flutter_args.push("--install".to_string()); - } + let click_setup = cfg!(windows) && args.is_empty() && crate::common::is_setup(&arg_exe); + if click_setup { + args.push("--install".to_owned()); + flutter_args.push("--install".to_string()); } if args.contains(&"--noinstall".to_string()) { args.clear(); @@ -233,7 +224,7 @@ fn import_config(path: &str) { /// [Note] /// this is for invoke new connection from dbus. #[cfg(feature = "flutter")] -fn core_main_invoke_new_connection(mut args: Args) -> Option> { +fn core_main_invoke_new_connection(mut args: std::env::Args) -> Option> { args.position(|element| { return element == "--connect"; })