param( [switch]$Force, [string]$InstallDir = "$env:APPDATA\pngquant", [switch]$NoModifyPath ) $ErrorActionPreference = "Stop" $DownloadUrl = "https://pngquant.org/pngquant-windows.zip" 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 { return $false } } function Install-ViaScoop { Write-Host "正在使用 scoop 安装 pngquant..." -ForegroundColor Cyan try { scoop install pngquant Write-Host "pngquant 安装成功" -ForegroundColor Green return $true } catch { Write-Host "scoop 安装 pngquant 失败: $_" -ForegroundColor Red return $false } } function Install-Manual { Write-Host "正在从 $DownloadUrl 下载 pngquant..." -ForegroundColor Cyan $zipPath = "$env:TEMP\pngquant-windows.zip" $extractTemp = "$env:TEMP\pngquant_extract" try { # Download $wc = New-Object System.Net.WebClient $wc.DownloadFile($DownloadUrl, $zipPath) # Clean up previous temp extract if (Test-Path $extractTemp) { Remove-Item $extractTemp -Recurse -Force } # Extract Expand-Archive -Path $zipPath -DestinationPath $extractTemp # Create install dir if (-not (Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null } # Copy exe files to install dir $exeFiles = Get-ChildItem -Path $extractTemp -Filter "*.exe" -Recurse foreach ($exe in $exeFiles) { Copy-Item -Path $exe.FullName -Destination $InstallDir -Force Write-Host " 已安装: $($exe.Name)" -ForegroundColor Green } # Cleanup Remove-Item $zipPath -Force -ErrorAction SilentlyContinue Remove-Item $extractTemp -Recurse -Force -ErrorAction SilentlyContinue # Add to PATH if (-not $NoModifyPath) { Add-ToPath $InstallDir } return $true } catch { Write-Host "手动安装失败: $_" -ForegroundColor Red return $false } } function Add-ToPath($LiteralPath) { $RegistryPath = 'registry::HKEY_CURRENT_USER\Environment' $CurrentDirs = (Get-Item -LiteralPath $RegistryPath).GetValue( 'Path', '', 'DoNotExpandEnvironmentNames' ) -split ';' -ne '' if ($LiteralPath -in $CurrentDirs) { Write-Host "安装目录已在 PATH 中: $LiteralPath" -ForegroundColor Green return } $NewPath = (,$LiteralPath + $CurrentDirs) -join ';' Set-ItemProperty -Type ExpandString -LiteralPath $RegistryPath Path $NewPath # 通知系统环境变量已更新 $DummyName = 'pngquant-install-' + [guid]::NewGuid().ToString() [Environment]::SetEnvironmentVariable($DummyName, 'dummy', 'User') [Environment]::SetEnvironmentVariable($DummyName, [NullString]::value, 'User') Write-Host "已将 $LiteralPath 添加到用户 PATH" -ForegroundColor Green } # ============================================================ Write-Host "========================================" -ForegroundColor Cyan Write-Host " pngquant 安装脚本" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" if (Test-PngquantInstalled) { if (-not $Force) { Write-Host "" Write-Host "pngquant 已经安装,无需重复安装。" -ForegroundColor Green Write-Host "如需强制重新安装,请使用 -Force 参数。" -ForegroundColor Yellow exit 0 } Write-Host "使用 -Force 参数强制重新安装..." -ForegroundColor Yellow } Write-Host "" if (Test-ScoopInstalled) { Write-Host "检测到 scoop,使用 scoop 安装..." -ForegroundColor Cyan if (Install-ViaScoop) { Write-Host "" pngquant --version exit 0 } Write-Host "scoop 安装失败,回退到手动安装方式..." -ForegroundColor Yellow } else { Write-Host "未检测到 scoop,使用手动安装方式..." -ForegroundColor Cyan } Write-Host "安装目录: $InstallDir" -ForegroundColor Cyan Write-Host "" if (Install-Manual) { Write-Host "" Write-Host "========================================" -ForegroundColor Green Write-Host " pngquant 安装完成!" -ForegroundColor Green Write-Host "========================================" -ForegroundColor Green Write-Host "安装路径: $InstallDir" -ForegroundColor Cyan try { $env:Path = "$InstallDir;$env:Path" Write-Host "" Write-Host "验证安装:" -ForegroundColor Cyan pngquant --version } catch { Write-Host "" Write-Host "安装文件已就绪,请重启终端后运行 pngquant --version 验证。" -ForegroundColor Yellow } } else { Write-Host "" Write-Host "pngquant 安装失败。" -ForegroundColor Red Write-Host "请尝试手动下载: $DownloadUrl" -ForegroundColor Yellow exit 1 }