bun和opencode的包缓存和配置路径修改

修改基础路径后,直接粘贴到管理员权限的powershell即可,会设置各种路径的环境变量

# 强制管理员权限检查
if (-Not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Host "错误:设置全局环境变量需要管理员权限!请关闭当前窗口,右键 PowerShell 选择『以管理员身份运行』后再试。" -ForegroundColor Red
    return
}

# 1. 定义基础路径
$opencodePath = "E:\package\.opencode"
$bunPath = "E:\package\.bun"

# 2. 自动创建所需的所有文件夹
$dirs = @(
    "$opencodePath\Cache", "$opencodePath\Config", "$opencodePath\Data", "$opencodePath\Logs", "$opencodePath\State",
    "$bunPath\install\cache", "$bunPath\install\global", "$bunPath\bin"
)
foreach ($dir in $dirs) {
    if (-not (Test-Path $dir)) {
        New-Item -ItemType Directory -Path $dir -Force | Out-Null
        Write-Host "已创建目录: $dir" -ForegroundColor Green
    }
}

# 3. 写入 OpenCode 环境变量 (Machine/全局系统级别)
[Environment]::SetEnvironmentVariable("OPENCODE_CACHE_DIR", "$opencodePath\Cache", "Machine")
[Environment]::SetEnvironmentVariable("OPENCODE_CONFIG_DIR", "$opencodePath\Config", "Machine")
[Environment]::SetEnvironmentVariable("OPENCODE_DATA_DIR", "$opencodePath\Data", "Machine")
[Environment]::SetEnvironmentVariable("OPENCODE_LOG_DIR", "$opencodePath\Logs", "Machine")
[Environment]::SetEnvironmentVariable("OPENCODE_STATE_DIR", "$opencodePath\State", "Machine")
Write-Host "OpenCode 全局环境变量写入成功!" -ForegroundColor Cyan

# 4. 写入 Bun 环境变量 (Machine/全局系统级别)
[Environment]::SetEnvironmentVariable("BUN_INSTALL_CACHE_DIR", "$bunPath\install\cache", "Machine")
[Environment]::SetEnvironmentVariable("BUN_INSTALL_GLOBAL_DIR", "$bunPath\install\global", "Machine")
[Environment]::SetEnvironmentVariable("BUN_INSTALL_BIN", "$bunPath\bin", "Machine")
Write-Host "Bun 全局环境变量写入成功!" -ForegroundColor Cyan

# 5. 安全追加系统 Path 环境变量 (Machine/全局系统级别)
$machinePath = [Environment]::GetEnvironmentVariable("Path", "Machine")
$bunBin = "$bunPath\bin"
if ($machinePath -notmatch [regex]::Escape($bunBin)) {
    if (!$machinePath.EndsWith(";")) { $machinePath += ";" }
    $newPath = $machinePath + $bunBin + ";"
    [Environment]::SetEnvironmentVariable("Path", $newPath, "Machine")
    Write-Host "已安全将 Bun Bin 目录加入系统全局 Path!" -ForegroundColor Yellow
} else {
    Write-Host "Bun Bin 目录已存在于系统全局 Path 中,跳过添加。" -ForegroundColor Gray
}

Write-Host "====== 全局配置完成!请关闭所有终端窗口使其生效 ======" -ForegroundColor Magenta