param( [switch]$Force ) $ErrorActionPreference = "Stop" function Test-PngquantInstalled { try { $result = Get-Command pngquant -ErrorAction Stop Write-Host "pngquant 已安装: $($result.Source)" -ForegroundColor Green return $true } catch { Write-Host "pngquant 未安装" -ForegroundColor Yellow return $false } } function Test-ScoopInstalled { try { $result = Get-Command scoop -ErrorAction Stop Write-Host "scoop 已安装: $($result.Source)" -ForegroundColor Green return $true } catch { Write-Host "scoop 未安装" -ForegroundColor Yellow return $false } } function Install-Scoop { Write-Host "正在安装 scoop..." -ForegroundColor Cyan $scoopInstallScript = "irm get.scoop.sh | iex" try { Invoke-Expression $scoopInstallScript Write-Host "scoop 安装成功" -ForegroundColor Green $env:Path = [System.Environment]::GetEnvironmentVariable("Path","User") + ";" + [System.Environment]::GetEnvironmentVariable("Path","Machine") return $true } catch { Write-Host "scoop 安装失败: $_" -ForegroundColor Red return $false } } function Install-Pngquant { Write-Host "正在使用 scoop 安装 pngquant..." -ForegroundColor Cyan try { scoop install pngquant Write-Host "pngquant 安装成功" -ForegroundColor Green return $true } catch { Write-Host "pngquant 安装失败: $_" -ForegroundColor Red return $false } } Write-Host "========================================" -ForegroundColor Cyan Write-Host " pngquant 安装脚本" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" if (Test-PngquantInstalled) { Write-Host "" Write-Host "pngquant 已经安装,无需重复安装。" -ForegroundColor Green if (-not $Force) { exit 0 } Write-Host "使用 -Force 参数强制重新安装..." -ForegroundColor Yellow } Write-Host "" if (-not (Test-ScoopInstalled)) { Write-Host "" Write-Host "scoop 未安装,正在安装 scoop..." -ForegroundColor Yellow if (-not (Install-Scoop)) { Write-Host "" Write-Host "scoop 安装失败,请手动安装后重试。" -ForegroundColor Red Write-Host "手动安装命令: irm get.scoop.sh | iex" -ForegroundColor Yellow exit 1 } Write-Host "" Write-Host "请重新运行此脚本以继续安装 pngquant。" -ForegroundColor Yellow Write-Host "或者手动运行: scoop install pngquant" -ForegroundColor Yellow exit 0 } Write-Host "" if (Install-Pngquant) { Write-Host "" Write-Host "========================================" -ForegroundColor Green Write-Host " pngquant 安装完成!" -ForegroundColor Green Write-Host "========================================" -ForegroundColor Green $pngquantPath = Get-Command pngquant -ErrorAction SilentlyContinue if ($pngquantPath) { Write-Host "安装路径: $($pngquantPath.Source)" -ForegroundColor Cyan } Write-Host "" Write-Host "验证安装:" -ForegroundColor Cyan pngquant --version } else { Write-Host "" Write-Host "pngquant 安装失败。" -ForegroundColor Red Write-Host "请尝试手动安装: scoop install pngquant" -ForegroundColor Yellow exit 1 }