diff --git a/src-tauri/src/winget.rs b/src-tauri/src/winget.rs index 5cae794..afcafcd 100644 --- a/src-tauri/src/winget.rs +++ b/src-tauri/src/winget.rs @@ -26,27 +26,38 @@ struct WingetPackage { } pub fn ensure_winget_dependencies(handle: &AppHandle) -> Result<(), String> { - emit_log(handle, "Check Environment", "Checking Winget and Microsoft.WinGet.Client...", "info"); + emit_log(handle, "Check Environment", "Initializing system components...", "info"); let setup_script = r#" - $ErrorActionPreference = 'Stop' + # 设置容错 + $ErrorActionPreference = 'SilentlyContinue' + Write-Output "Step 1: Enabling TLS 1.2" [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - Write-Output "Step 2: Installing NuGet provider" - Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Confirm:$false + Write-Output "Step 2: Forcing load of PackageManagement" + Import-Module PackageManagement -ErrorAction SilentlyContinue + Import-Module PowerShellGet -ErrorAction SilentlyContinue - Write-Output "Step 3: Trusting PSGallery" - Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted + Write-Output "Step 3: Checking NuGet provider" + $provider = Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue + if ($null -eq $provider) { + Write-Output "Installing NuGet provider..." + Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Confirm:$false -ErrorAction SilentlyContinue + } + + Write-Output "Step 4: Configuring Repository Trust" + Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted -ErrorAction SilentlyContinue - Write-Output "Step 4: Setting Execution Policy" - Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force + Write-Output "Step 5: Setting Execution Policy" + Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force -ErrorAction SilentlyContinue + Write-Output "Step 6: Checking Microsoft.WinGet.Client" if (-not (Get-Module -ListAvailable Microsoft.WinGet.Client)) { - Write-Output "Step 5: Installing Microsoft.WinGet.Client module" + Write-Output "Installing Winget Client module (this may take 1-2 minutes)..." Install-Module -Name Microsoft.WinGet.Client -Force -AllowClobber -Scope CurrentUser -Confirm:$false } else { - Write-Output "Step 5: Module already installed" + Write-Output "Winget Client module is already present." } "#; @@ -59,12 +70,20 @@ pub fn ensure_winget_dependencies(handle: &AppHandle) -> Result<(), String> { Ok(out) => { let msg = String::from_utf8_lossy(&out.stdout).to_string(); let err = String::from_utf8_lossy(&out.stderr).to_string(); - if out.status.success() { - emit_log(handle, "Environment Setup", &format!("Success: {}", msg), "success"); + // 只要最终模块存在,就认为成功,忽略过程中的次要警告 + let check_final = Command::new("powershell") + .args(["-NoProfile", "-Command", "Get-Module -ListAvailable Microsoft.WinGet.Client"]) + .creation_flags(0x08000000) + .output(); + + let is_success = check_final.map(|o| !o.stdout.is_empty()).unwrap_or(false); + + if is_success { + emit_log(handle, "Environment Setup", "Winget module is ready.", "success"); Ok(()) } else { emit_log(handle, "Environment Setup Error", &format!("OUT: {}\nERR: {}", msg, err), "error"); - Err(format!("Setup failed: {}", err)) + Err("Setup verification failed".to_string()) } }, Err(e) => { @@ -78,8 +97,8 @@ pub fn list_all_software(handle: &AppHandle) -> Vec { let script = r#" $OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $ErrorActionPreference = 'SilentlyContinue' - Import-Module Microsoft.WinGet.Client - $pkgs = Get-WinGetPackage + Import-Module Microsoft.WinGet.Client -ErrorAction SilentlyContinue + $pkgs = Get-WinGetPackage -ErrorAction SilentlyContinue if ($pkgs) { $pkgs | ForEach-Object { [PSCustomObject]@{ @@ -101,8 +120,8 @@ pub fn list_updates(handle: &AppHandle) -> Vec { let script = r#" $OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $ErrorActionPreference = 'SilentlyContinue' - Import-Module Microsoft.WinGet.Client - $pkgs = Get-WinGetPackage | Where-Object { $_.IsUpdateAvailable } + Import-Module Microsoft.WinGet.Client -ErrorAction SilentlyContinue + $pkgs = Get-WinGetPackage -ErrorAction SilentlyContinue | Where-Object { $_.IsUpdateAvailable } if ($pkgs) { $pkgs | ForEach-Object { [PSCustomObject]@{ @@ -134,12 +153,12 @@ fn execute_powershell(handle: &AppHandle, cmd_name: &str, script: &str) -> Vec Vec Vec { if let Ok(packages) = serde_json::from_str::>(&json_str) { return packages.into_iter().map(map_package).collect(); diff --git a/src/views/Logs.vue b/src/views/Logs.vue index 6958a53..bc612a1 100644 --- a/src/views/Logs.vue +++ b/src/views/Logs.vue @@ -3,7 +3,7 @@

运行日志

-

记录应用最近执行的命令和结果

+