fix start speed
This commit is contained in:
@@ -26,39 +26,35 @@ 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", "Initializing system components and updating sources...", "info");
|
emit_log(handle, "Check Environment", "Performing fast environment check...", "info");
|
||||||
|
|
||||||
|
// 优化后的脚本:先尝试极速检测,存在则直接退出
|
||||||
let setup_script = r#"
|
let setup_script = r#"
|
||||||
$ErrorActionPreference = 'SilentlyContinue'
|
$ErrorActionPreference = 'SilentlyContinue'
|
||||||
|
|
||||||
Write-Output "Step 1: Enabling TLS 1.2"
|
# 极速路径:如果模块已安装,直接输出 Success 并退出
|
||||||
|
if (Get-Module -ListAvailable Microsoft.WinGet.Client) {
|
||||||
|
Write-Output "ALREADY_READY"
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# 慢速路径:仅在模块缺失时执行完整初始化
|
||||||
|
Write-Output "CONFIGURING_ENV"
|
||||||
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
|
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
|
||||||
|
|
||||||
Write-Output "Step 2: Forcing load of PackageManagement"
|
|
||||||
Import-Module PackageManagement -ErrorAction SilentlyContinue
|
Import-Module PackageManagement -ErrorAction SilentlyContinue
|
||||||
Import-Module PowerShellGet -ErrorAction SilentlyContinue
|
|
||||||
|
|
||||||
Write-Output "Step 3: Checking NuGet provider"
|
if ($null -eq (Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue)) {
|
||||||
$provider = Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue
|
|
||||||
if ($null -eq $provider) {
|
|
||||||
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Confirm:$false
|
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Confirm:$false
|
||||||
}
|
}
|
||||||
|
|
||||||
Write-Output "Step 4: Configuring Repository Trust"
|
|
||||||
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
|
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
|
||||||
|
|
||||||
Write-Output "Step 5: Setting Execution Policy"
|
|
||||||
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
|
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
|
||||||
|
|
||||||
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 "Installing Winget Client module..."
|
|
||||||
Install-Module -Name Microsoft.WinGet.Client -Force -AllowClobber -Scope CurrentUser -Confirm:$false
|
Install-Module -Name Microsoft.WinGet.Client -Force -AllowClobber -Scope CurrentUser -Confirm:$false
|
||||||
}
|
}
|
||||||
|
|
||||||
Write-Output "Step 7: Updating Winget Sources (apt-get update style)"
|
# 注意:此处不再执行 winget source update,因为它太慢了,移至后台或按需执行
|
||||||
# 这一步非常关键,确保 winget 的本地数据库是最新的
|
|
||||||
winget source update --accept-source-agreements
|
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let output = Command::new("powershell")
|
let output = Command::new("powershell")
|
||||||
@@ -68,22 +64,25 @@ pub fn ensure_winget_dependencies(handle: &AppHandle) -> Result<(), String> {
|
|||||||
|
|
||||||
match output {
|
match output {
|
||||||
Ok(out) => {
|
Ok(out) => {
|
||||||
let msg = String::from_utf8_lossy(&out.stdout).to_string();
|
let stdout = String::from_utf8_lossy(&out.stdout).to_string();
|
||||||
let err = String::from_utf8_lossy(&out.stderr).to_string();
|
if stdout.contains("ALREADY_READY") {
|
||||||
|
emit_log(handle, "Environment Setup", "Fast check passed: System is ready.", "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 and sources are ready.", "success");
|
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
emit_log(handle, "Environment Setup Error", &format!("OUT: {}\nERR: {}", msg, err), "error");
|
// 验证安装结果
|
||||||
Err("Setup verification failed".to_string())
|
let check = 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, "Environment Setup", "Full configuration successful.", "success");
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
let err = String::from_utf8_lossy(&out.stderr).to_string();
|
||||||
|
emit_log(handle, "Environment Setup Error", &format!("STDOUT: {}\nSTDERR: {}", stdout, err), "error");
|
||||||
|
Err("Setup failed".to_string())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -121,6 +120,10 @@ pub fn list_updates(handle: &AppHandle) -> Vec<Software> {
|
|||||||
$OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
$OutputEncoding = [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||||
$ErrorActionPreference = 'SilentlyContinue'
|
$ErrorActionPreference = 'SilentlyContinue'
|
||||||
Import-Module Microsoft.WinGet.Client -ErrorAction SilentlyContinue
|
Import-Module Microsoft.WinGet.Client -ErrorAction SilentlyContinue
|
||||||
|
|
||||||
|
# 在检查更新前,尝试静默更新源(可选,为了准确性)
|
||||||
|
# winget source update --accept-source-agreements | Out-Null
|
||||||
|
|
||||||
$pkgs = Get-WinGetPackage -ErrorAction SilentlyContinue | Where-Object { $_.IsUpdateAvailable }
|
$pkgs = Get-WinGetPackage -ErrorAction SilentlyContinue | Where-Object { $_.IsUpdateAvailable }
|
||||||
if ($pkgs) {
|
if ($pkgs) {
|
||||||
$pkgs | ForEach-Object {
|
$pkgs | ForEach-Object {
|
||||||
@@ -158,7 +161,7 @@ fn execute_powershell(handle: &AppHandle, cmd_name: &str, script: &str) -> Vec<S
|
|||||||
}
|
}
|
||||||
|
|
||||||
if clean_json.is_empty() || clean_json == "[]" {
|
if clean_json.is_empty() || clean_json == "[]" {
|
||||||
emit_log(handle, cmd_name, "No data returned from PowerShell.", "info");
|
emit_log(handle, cmd_name, "No data returned.", "info");
|
||||||
return vec![];
|
return vec![];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user