docs: 添加 CLAUDE.md 行为准则文档并重构 README 新手使用说明

This commit is contained in:
2026-06-11 17:41:44 +08:00
parent b4a04af2b4
commit b918e8a2d9
4 changed files with 967 additions and 61 deletions

View File

@@ -1,8 +1,11 @@
param(
[switch]$Force
[switch]$Force,
[string]$InstallDir = "$env:APPDATA\pngquant",
[switch]$NoModifyPath
)
$ErrorActionPreference = "Stop"
$DownloadUrl = "https://pngquant.org/pngquant-windows.zip"
function Test-PngquantInstalled {
try {
@@ -21,51 +24,102 @@ function Test-ScoopInstalled {
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 {
function Install-ViaScoop {
Write-Host "正在使用 scoop 安装 pngquant..." -ForegroundColor Cyan
try {
scoop install pngquant
Write-Host "pngquant 安装成功" -ForegroundColor Green
return $true
} catch {
Write-Host "pngquant 安装失败: $_" -ForegroundColor Red
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) {
Write-Host ""
Write-Host "pngquant 已经安装,无需重复安装。" -ForegroundColor Green
if (-not $Force) {
Write-Host ""
Write-Host "pngquant 已经安装,无需重复安装。" -ForegroundColor Green
Write-Host "如需强制重新安装,请使用 -Force 参数。" -ForegroundColor Yellow
exit 0
}
Write-Host "使用 -Force 参数强制重新安装..." -ForegroundColor Yellow
@@ -73,42 +127,40 @@ if (Test-PngquantInstalled) {
Write-Host ""
if (-not (Test-ScoopInstalled)) {
Write-Host ""
Write-Host "scoop 未安装,正在安装 scoop..." -ForegroundColor Yellow
if (-not (Install-Scoop)) {
if (Test-ScoopInstalled) {
Write-Host "检测到 scoop使用 scoop 安装..." -ForegroundColor Cyan
if (Install-ViaScoop) {
Write-Host ""
Write-Host "scoop 安装失败,请手动安装后重试。" -ForegroundColor Red
Write-Host "手动安装命令: irm get.scoop.sh | iex" -ForegroundColor Yellow
exit 1
pngquant --version
exit 0
}
Write-Host ""
Write-Host "请重新运行此脚本以继续安装 pngquant。" -ForegroundColor Yellow
Write-Host "或者手动运行: scoop install pngquant" -ForegroundColor Yellow
exit 0
Write-Host "scoop 安装失败,回退到手动安装方式..." -ForegroundColor Yellow
} else {
Write-Host "未检测到 scoop使用手动安装方式..." -ForegroundColor Cyan
}
Write-Host "安装目录: $InstallDir" -ForegroundColor Cyan
Write-Host ""
if (Install-Pngquant) {
if (Install-Manual) {
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 "安装路径: $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
}
Write-Host ""
Write-Host "验证安装:" -ForegroundColor Cyan
pngquant --version
} else {
Write-Host ""
Write-Host "pngquant 安装失败。" -ForegroundColor Red
Write-Host "请尝试手动安装: scoop install pngquant" -ForegroundColor Yellow
Write-Host "请尝试手动下载: $DownloadUrl" -ForegroundColor Yellow
exit 1
}