fix setup bug
This commit is contained in:
@@ -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<Software> {
|
||||
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<Software> {
|
||||
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<S
|
||||
let stderr = String::from_utf8_lossy(&out.stderr);
|
||||
let clean_json = stdout.trim_start_matches('\u{feff}').trim();
|
||||
|
||||
if !out.status.success() || !stderr.is_empty() {
|
||||
if !out.status.success() && !stderr.is_empty() {
|
||||
emit_log(handle, &format!("{} stderr", cmd_name), &stderr, "error");
|
||||
}
|
||||
|
||||
if clean_json.is_empty() || clean_json == "[]" {
|
||||
emit_log(handle, cmd_name, "No software found (Empty JSON)", "info");
|
||||
emit_log(handle, cmd_name, "No data returned from PowerShell.", "info");
|
||||
return vec![];
|
||||
}
|
||||
|
||||
@@ -154,7 +173,6 @@ fn execute_powershell(handle: &AppHandle, cmd_name: &str, script: &str) -> Vec<S
|
||||
}
|
||||
}
|
||||
|
||||
// 修正:Rust 并没有 .length(),应使用 .len()
|
||||
fn parse_json_output(json_str: String) -> Vec<Software> {
|
||||
if let Ok(packages) = serde_json::from_str::<Vec<WingetPackage>>(&json_str) {
|
||||
return packages.into_iter().map(map_package).collect();
|
||||
|
||||
Reference in New Issue
Block a user