From 714db8193873fa5f26ebe5d192f2eb087c9a50fb Mon Sep 17 00:00:00 2001 From: Julian Freeman Date: Sat, 14 Mar 2026 22:48:10 -0400 Subject: [PATCH] fix init --- src-tauri/src/lib.rs | 9 ++------ src-tauri/src/winget.rs | 49 +++++++++++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 2ab0f97..417f819 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -36,6 +36,7 @@ pub fn emit_log(handle: &AppHandle, id: &str, command: &str, output: &str, statu #[tauri::command] async fn initialize_app(app: AppHandle) -> Result { + // 统一由前端触发环境初始化 tokio::task::spawn_blocking(move || { ensure_winget_dependencies(&app).map(|_| true) }).await.unwrap_or(Err("Initialization Task Panicked".to_string())) @@ -115,12 +116,7 @@ pub fn run() { let (tx, mut rx) = mpsc::channel::(100); app.manage(AppState { install_tx: tx }); - let init_handle = handle.clone(); - tauri::async_runtime::spawn(async move { - let _ = tokio::task::spawn_blocking(move || { - let _ = ensure_winget_dependencies(&init_handle); - }).await; - }); + // 移除了 setup 钩子中的 redundancy initialize_app 调用 // 安装队列处理器 tauri::async_runtime::spawn(async move { @@ -188,7 +184,6 @@ pub fn run() { } if !is_progress { - // 追加内容到同一个日志卡片 emit_log(&h, &log_id, "", clean_line, "info"); } } diff --git a/src-tauri/src/winget.rs b/src-tauri/src/winget.rs index 3056ecd..8442fe6 100644 --- a/src-tauri/src/winget.rs +++ b/src-tauri/src/winget.rs @@ -26,25 +26,38 @@ struct WingetPackage { } pub fn ensure_winget_dependencies(handle: &AppHandle) -> Result<(), String> { - let log_id = "env-check"; - emit_log(handle, log_id, "Environment Check", "Starting system configuration...", "info"); + emit_log(handle, "env-check", "Environment Check", "Checking system components...", "info"); + // 优化后的极速检测脚本 let setup_script = r#" + # 强制指定输出编码为 UTF8 (无 BOM) + $OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $ErrorActionPreference = 'SilentlyContinue' - Write-Output "Checking module status..." - if (Get-Module -ListAvailable Microsoft.WinGet.Client) { - Write-Output "ALREADY_READY" + + # 使用多种方式确认环境是否已就绪 + $module = Get-Module -ListAvailable Microsoft.WinGet.Client + $command = Get-Command Get-WinGetPackage -ErrorAction SilentlyContinue + + if ($module -or $command) { + Write-Output "CHECK_RESULT:READY" exit 0 } - Write-Output "Step 1: Enabling TLS 1.2" + + # 仅在确实缺失时才输出配置日志并开始安装 + Write-Output "CHECK_RESULT:NEED_INSTALL" + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 Import-Module PackageManagement -ErrorAction SilentlyContinue + if ($null -eq (Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue)) { Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Confirm:$false } + Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force + Install-Module -Name Microsoft.WinGet.Client -Force -AllowClobber -Scope CurrentUser -Confirm:$false + winget source update --accept-source-agreements "#; let output = Command::new("powershell") @@ -55,28 +68,32 @@ pub fn ensure_winget_dependencies(handle: &AppHandle) -> Result<(), String> { match output { Ok(out) => { let stdout = String::from_utf8_lossy(&out.stdout).to_string(); - emit_log(handle, log_id, "", &stdout, "info"); + let stderr = String::from_utf8_lossy(&out.stderr).to_string(); - if stdout.contains("ALREADY_READY") { - emit_log(handle, log_id, "Result", "Ready (Fast check).", "success"); + // 清理输出字符串,移除 BOM 和换行 + let clean_stdout = stdout.trim_start_matches('\u{feff}').trim(); + + if clean_stdout.contains("CHECK_RESULT:READY") { + emit_log(handle, "env-check", "Result", "Environment is already configured. Skipping install.", "success"); Ok(()) } else { - let check = Command::new("powershell") + // 如果执行了安装路径,检查最终结果 + let check_final = Command::new("powershell") .args(["-NoProfile", "-Command", "Get-Module -ListAvailable Microsoft.WinGet.Client"]) .creation_flags(0x08000000) .output(); - if check.map(|o| !o.stdout.is_empty()).unwrap_or(false) { - emit_log(handle, log_id, "Result", "Module installed successfully.", "success"); + if check_final.map(|o| !o.stdout.is_empty()).unwrap_or(false) { + emit_log(handle, "env-check", "Result", "Environment configured successfully.", "success"); Ok(()) } else { - emit_log(handle, log_id, "Result", "Module installation failed.", "error"); - Err("Setup failed".to_string()) + emit_log(handle, "env-check", "Error", &format!("OUT: {}\nERR: {}", clean_stdout, stderr), "error"); + Err("Setup verification failed".to_string()) } } }, Err(e) => { - emit_log(handle, log_id, "Fatal Error", &e.to_string(), "error"); + emit_log(handle, "env-check", "Fatal Error", &e.to_string(), "error"); Err(e.to_string()) } } @@ -149,7 +166,7 @@ fn execute_powershell(handle: &AppHandle, log_id: &str, cmd_title: &str, script: } if clean_json.is_empty() || clean_json == "[]" { - emit_log(handle, log_id, "Result", "No software found.", "info"); + emit_log(handle, log_id, "Result", "No data returned.", "info"); return vec![]; }