fix: replace claude setup symlinks with files
This commit is contained in:
@@ -17,6 +17,30 @@ if (-not $ApiKey -and (Test-Path Variable:key)) { $ApiKey = $key }
|
||||
$DefaultBaseUrl = "https://api2.xcodecli.com"
|
||||
$GeminiConfigDir = "$env:USERPROFILE\.gemini"
|
||||
$GeminiEnvFile = "$GeminiConfigDir\.env"
|
||||
$ToolCommand = "gemini"
|
||||
$ToolPackage = "@google/gemini-cli"
|
||||
$ToolName = "Gemini CLI"
|
||||
|
||||
# ========== 工具函数 ==========
|
||||
function Test-Command {
|
||||
param([string]$Name)
|
||||
return [bool](Get-Command $Name -ErrorAction SilentlyContinue)
|
||||
}
|
||||
|
||||
function Refresh-Path {
|
||||
$env:Path = [Environment]::GetEnvironmentVariable("Path", "User") + ";" + [Environment]::GetEnvironmentVariable("Path", "Machine")
|
||||
}
|
||||
|
||||
# Function to write environment variable (User level)
|
||||
function Set-EnvVariable {
|
||||
param(
|
||||
[string]$Name,
|
||||
[string]$Value
|
||||
)
|
||||
[Environment]::SetEnvironmentVariable($Name, $Value, [System.EnvironmentVariableTarget]::Process)
|
||||
[Environment]::SetEnvironmentVariable($Name, $Value, [System.EnvironmentVariableTarget]::User)
|
||||
Write-Info "Environment variable set: $Name"
|
||||
}
|
||||
|
||||
# Color functions for output
|
||||
function Write-Info {
|
||||
@@ -43,6 +67,155 @@ function Write-Error {
|
||||
Write-Host " $Message"
|
||||
}
|
||||
|
||||
# ========== Node.js 环境检测 ==========
|
||||
function Get-NodeVersion {
|
||||
if (Test-Command "node") {
|
||||
try {
|
||||
$versionStr = (& node --version 2>$null) -replace 'v', ''
|
||||
$major = [int]($versionStr -split '\.')[0]
|
||||
return @{ Version = $versionStr; Major = $major }
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
return $null
|
||||
}
|
||||
|
||||
function Install-Fnm {
|
||||
Write-Host ""
|
||||
Write-Info "正在安装 fnm (Fast Node Manager)..."
|
||||
|
||||
try {
|
||||
if (Test-Command "winget") {
|
||||
Write-Info "使用 winget 安装 fnm..."
|
||||
& winget install Schniz.fnm -e --accept-source-agreements --accept-package-agreements
|
||||
}
|
||||
else {
|
||||
Write-Info "使用 PowerShell 脚本安装 fnm..."
|
||||
Invoke-Expression "& { $(Invoke-RestMethod https://fnm.vercel.app/install.ps1) }"
|
||||
}
|
||||
|
||||
Refresh-Path
|
||||
|
||||
if (Test-Command "fnm") {
|
||||
Write-Success "fnm 安装成功!"
|
||||
$fnmEnv = & fnm env --use-on-cd 2>$null
|
||||
if ($fnmEnv) {
|
||||
$fnmEnv | Out-String | Invoke-Expression
|
||||
}
|
||||
return $true
|
||||
}
|
||||
else {
|
||||
Write-Warning "fnm 可能已安装,但需要重新打开终端才能生效"
|
||||
Write-Info "请重新打开 PowerShell 后再运行此脚本"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Error "fnm 安装失败: $($_.Exception.Message)"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Install-NodeWithFnm {
|
||||
Write-Info "使用 fnm 安装 Node.js 24.x..."
|
||||
|
||||
try {
|
||||
& fnm install 24
|
||||
& fnm use 24
|
||||
& fnm default 24
|
||||
Refresh-Path
|
||||
|
||||
if (Test-Command "node") {
|
||||
$nodeInfo = Get-NodeVersion
|
||||
Write-Success "Node.js v$($nodeInfo.Version) 安装成功!"
|
||||
return $true
|
||||
}
|
||||
else {
|
||||
Write-Warning "Node.js 可能已安装,但需要重新打开终端才能生效"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Error "Node.js 安装失败: $($_.Exception.Message)"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Ensure-NodeEnvironment {
|
||||
$nodeInfo = Get-NodeVersion
|
||||
|
||||
if ($nodeInfo) {
|
||||
Write-Info "检测到 Node.js v$($nodeInfo.Version)"
|
||||
|
||||
if ($nodeInfo.Major -lt 20) {
|
||||
Write-Warning "Node.js 版本过低 (需要 >= 20.x)"
|
||||
|
||||
$upgrade = Read-Host "是否使用 fnm 安装 Node.js 24.x? (Y/n)"
|
||||
if ($upgrade -eq "n" -or $upgrade -eq "N") {
|
||||
Write-Error "Node.js 版本不满足要求,请手动升级后重试"
|
||||
return $false
|
||||
}
|
||||
|
||||
if (-not (Test-Command "fnm")) {
|
||||
if (-not (Install-Fnm)) { return $false }
|
||||
}
|
||||
|
||||
return Install-NodeWithFnm
|
||||
}
|
||||
|
||||
return $true
|
||||
}
|
||||
|
||||
Write-Warning "未检测到 Node.js"
|
||||
Write-Info "将使用 fnm 安装 Node.js 24.x"
|
||||
|
||||
$install = Read-Host "是否继续? (Y/n)"
|
||||
if ($install -eq "n" -or $install -eq "N") {
|
||||
return $false
|
||||
}
|
||||
|
||||
if (-not (Test-Command "fnm")) {
|
||||
if (-not (Install-Fnm)) { return $false }
|
||||
}
|
||||
|
||||
return Install-NodeWithFnm
|
||||
}
|
||||
|
||||
function Install-Tool {
|
||||
if (-not (Ensure-NodeEnvironment)) {
|
||||
return $false
|
||||
}
|
||||
|
||||
Write-Info "使用 npm 安装 $ToolName..."
|
||||
$installCmd = "npm install -g $ToolPackage"
|
||||
Write-Host " 执行: $installCmd" -ForegroundColor Gray
|
||||
|
||||
try {
|
||||
Invoke-Expression $installCmd
|
||||
$exitCode = $LASTEXITCODE
|
||||
Refresh-Path
|
||||
|
||||
if ($exitCode -ne 0) {
|
||||
Write-Error "安装命令返回错误码: $exitCode"
|
||||
return $false
|
||||
}
|
||||
|
||||
if (Test-Command $ToolCommand) {
|
||||
Write-Success "$ToolName 安装成功!"
|
||||
return $true
|
||||
}
|
||||
else {
|
||||
Write-Warning "$ToolName 可能已安装,但需要重新打开终端才能生效"
|
||||
$continue = Read-Host "是否继续进行配置? (Y/n)"
|
||||
return ($continue -ne "n" -and $continue -ne "N")
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Error "安装失败: $($_.Exception.Message)"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
# Function to show help
|
||||
function Show-Help {
|
||||
Write-Host @"
|
||||
@@ -106,7 +279,7 @@ function New-SettingsDirectory {
|
||||
# Function to validate API key format
|
||||
function Test-ApiKey {
|
||||
param([string]$ApiKey)
|
||||
|
||||
|
||||
if ($ApiKey -match '^[A-Za-z0-9_-]+$') {
|
||||
return $true
|
||||
} else {
|
||||
@@ -115,6 +288,15 @@ function Test-ApiKey {
|
||||
}
|
||||
}
|
||||
|
||||
# Function to extract model count from API response (supports both data[] and models[])
|
||||
function Get-ModelCount {
|
||||
param([object]$Response)
|
||||
if ($null -eq $Response) { return 0 }
|
||||
if ($Response.data -is [Array] -and $Response.data.Count -gt 0) { return $Response.data.Count }
|
||||
if ($Response.models -is [Array] -and $Response.models.Count -gt 0) { return $Response.models.Count }
|
||||
return 0
|
||||
}
|
||||
|
||||
# Function to test API connection and return working base URL
|
||||
function Test-ApiConnection {
|
||||
param([string]$ApiKey)
|
||||
@@ -138,9 +320,8 @@ function Test-ApiConnection {
|
||||
$testEndpoint = "$baseUrl/v1/models"
|
||||
$response = Invoke-RestMethod -Uri $testEndpoint -Method Get -Headers $headers -ErrorAction Stop
|
||||
|
||||
# Check if response contains models (supports both data and models arrays)
|
||||
if ($response.data -or $response.models) {
|
||||
$modelCount = if ($response.data) { $response.data.Count } else { $response.models.Count }
|
||||
$modelCount = Get-ModelCount -Response $response
|
||||
if ($modelCount -gt 0) {
|
||||
Write-Success "API connection successful! Found $modelCount models at $baseUrl"
|
||||
return $baseUrl
|
||||
} else {
|
||||
@@ -168,14 +349,18 @@ function New-Settings {
|
||||
[string]$BaseUrl,
|
||||
[string]$ApiKey
|
||||
)
|
||||
|
||||
|
||||
$envContent = @"
|
||||
GOOGLE_GEMINI_BASE_URL="$BaseUrl"
|
||||
GEMINI_API_KEY="$ApiKey"
|
||||
GEMINI_MODEL="gemini-3-pro-preview"
|
||||
GOOGLE_GEMINI_BASE_URL="$BaseUrl"
|
||||
GEMINI_MODEL="gemini-2.5-pro"
|
||||
"@
|
||||
|
||||
$settingsJson = @{
|
||||
ide = @{
|
||||
enabled = $true
|
||||
hasSeenNudge = $true
|
||||
}
|
||||
general = @{
|
||||
previewFeatures = $true
|
||||
}
|
||||
@@ -193,6 +378,14 @@ GEMINI_MODEL="gemini-3-pro-preview"
|
||||
$settingsJsonPath = "$GeminiConfigDir\settings.json"
|
||||
$settingsJson | ConvertTo-Json -Depth 10 | Set-Content -Path $settingsJsonPath -Encoding UTF8
|
||||
Write-Success "Gemini CLI settings written to: $settingsJsonPath"
|
||||
|
||||
# Set environment variables
|
||||
Write-Info "Setting environment variables..."
|
||||
Set-EnvVariable -Name "GEMINI_API_KEY" -Value $ApiKey
|
||||
Set-EnvVariable -Name "GOOGLE_GEMINI_BASE_URL" -Value $BaseUrl
|
||||
Set-EnvVariable -Name "GEMINI_MODEL" -Value "gemini-2.5-pro"
|
||||
Write-Success "Environment variables configured"
|
||||
|
||||
return $true
|
||||
}
|
||||
catch {
|
||||
@@ -231,6 +424,25 @@ function Main {
|
||||
exit 0
|
||||
}
|
||||
|
||||
# 检测工具是否已安装
|
||||
if (-not (Test-Command $ToolCommand)) {
|
||||
Write-Host ""
|
||||
Write-Warning "$ToolName 未安装"
|
||||
$install = Read-Host "是否立即安装? (Y/n)"
|
||||
|
||||
if ($install -eq "n" -or $install -eq "N") {
|
||||
Write-Info "已取消"
|
||||
exit 0
|
||||
}
|
||||
|
||||
if (-not (Install-Tool)) {
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Success "$ToolName 已安装"
|
||||
}
|
||||
|
||||
# Interactive mode if no API key provided
|
||||
if ([string]::IsNullOrWhiteSpace($ApiKey)) {
|
||||
Write-Info "Interactive setup mode"
|
||||
@@ -316,6 +528,9 @@ function Main {
|
||||
Write-Info "Current settings:"
|
||||
Get-Content $GeminiEnvFile
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Warning "Please restart your terminal for environment variables to take effect."
|
||||
} else {
|
||||
Write-Error "Failed to create Gemini CLI settings"
|
||||
exit 1
|
||||
|
||||
Reference in New Issue
Block a user