fix setup bug

This commit is contained in:
Julian Freeman
2026-03-14 20:59:53 -04:00
parent cdeb52c316
commit 60113e9629
2 changed files with 41 additions and 23 deletions

View File

@@ -26,27 +26,38 @@ struct WingetPackage {
} }
pub fn ensure_winget_dependencies(handle: &AppHandle) -> Result<(), String> { 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#" let setup_script = r#"
$ErrorActionPreference = 'Stop' # 设置容错
$ErrorActionPreference = 'SilentlyContinue'
Write-Output "Step 1: Enabling TLS 1.2" Write-Output "Step 1: Enabling TLS 1.2"
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
Write-Output "Step 2: Installing NuGet provider" Write-Output "Step 2: Forcing load of PackageManagement"
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Confirm:$false Import-Module PackageManagement -ErrorAction SilentlyContinue
Import-Module PowerShellGet -ErrorAction SilentlyContinue
Write-Output "Step 3: Trusting PSGallery" Write-Output "Step 3: Checking NuGet provider"
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted $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" Write-Output "Step 5: Setting Execution Policy"
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force 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)) { 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 Install-Module -Name Microsoft.WinGet.Client -Force -AllowClobber -Scope CurrentUser -Confirm:$false
} else { } 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) => { Ok(out) => {
let msg = String::from_utf8_lossy(&out.stdout).to_string(); let msg = String::from_utf8_lossy(&out.stdout).to_string();
let err = String::from_utf8_lossy(&out.stderr).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(()) Ok(())
} else { } else {
emit_log(handle, "Environment Setup Error", &format!("OUT: {}\nERR: {}", msg, err), "error"); 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) => { Err(e) => {
@@ -78,8 +97,8 @@ pub fn list_all_software(handle: &AppHandle) -> Vec<Software> {
let script = r#" let script = r#"
$OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$ErrorActionPreference = 'SilentlyContinue' $ErrorActionPreference = 'SilentlyContinue'
Import-Module Microsoft.WinGet.Client Import-Module Microsoft.WinGet.Client -ErrorAction SilentlyContinue
$pkgs = Get-WinGetPackage $pkgs = Get-WinGetPackage -ErrorAction SilentlyContinue
if ($pkgs) { if ($pkgs) {
$pkgs | ForEach-Object { $pkgs | ForEach-Object {
[PSCustomObject]@{ [PSCustomObject]@{
@@ -101,8 +120,8 @@ pub fn list_updates(handle: &AppHandle) -> Vec<Software> {
let script = r#" let script = r#"
$OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$ErrorActionPreference = 'SilentlyContinue' $ErrorActionPreference = 'SilentlyContinue'
Import-Module Microsoft.WinGet.Client Import-Module Microsoft.WinGet.Client -ErrorAction SilentlyContinue
$pkgs = Get-WinGetPackage | Where-Object { $_.IsUpdateAvailable } $pkgs = Get-WinGetPackage -ErrorAction SilentlyContinue | Where-Object { $_.IsUpdateAvailable }
if ($pkgs) { if ($pkgs) {
$pkgs | ForEach-Object { $pkgs | ForEach-Object {
[PSCustomObject]@{ [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 stderr = String::from_utf8_lossy(&out.stderr);
let clean_json = stdout.trim_start_matches('\u{feff}').trim(); 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"); emit_log(handle, &format!("{} stderr", cmd_name), &stderr, "error");
} }
if clean_json.is_empty() || clean_json == "[]" { 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![]; 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> { fn parse_json_output(json_str: String) -> Vec<Software> {
if let Ok(packages) = serde_json::from_str::<Vec<WingetPackage>>(&json_str) { if let Ok(packages) = serde_json::from_str::<Vec<WingetPackage>>(&json_str) {
return packages.into_iter().map(map_package).collect(); return packages.into_iter().map(map_package).collect();

View File

@@ -3,7 +3,7 @@
<header class="content-header"> <header class="content-header">
<div class="header-left"> <div class="header-left">
<h1>运行日志</h1> <h1>运行日志</h1>
<p class="count">记录应用最近执行的命令和结果</p> <!-- <p class="count">记录应用最近执行的命令和结果</p> -->
</div> </div>
<div class="header-actions"> <div class="header-actions">
<button @click="store.logs = []" class="secondary-btn action-btn"> <button @click="store.logs = []" class="secondary-btn action-btn">
@@ -62,11 +62,11 @@ onMounted(() => {
color: var(--text-main); color: var(--text-main);
} }
.header-left .count { /* .header-left .count {
font-size: 14px; font-size: 14px;
color: var(--text-sec); color: var(--text-sec);
margin-top: 4px; margin-top: 4px;
} } */
.header-actions { .header-actions {
display: flex; display: flex;