|
| 1 | +# autodev-v3-upgrade.ps1 - Download brain v3 from GitHub and inject keys |
| 2 | +$ErrorActionPreference = "Continue" |
| 3 | +[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 |
| 4 | + |
| 5 | +$brainPath = "C:\Users\Administrator\autodev-brain.ps1" |
| 6 | +$logFile = "C:\Users\Administrator\autodev_brain.log" |
| 7 | + |
| 8 | +function Log($msg) { |
| 9 | + $ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss" |
| 10 | + $line = "[$ts] [V3-Upgrade] $msg" |
| 11 | + Add-Content -Path $logFile -Value $line -Encoding UTF8 |
| 12 | + echo $line |
| 13 | +} |
| 14 | + |
| 15 | +Log "Starting v3 upgrade..." |
| 16 | + |
| 17 | +# Backup |
| 18 | +if (Test-Path $brainPath) { |
| 19 | + Copy-Item $brainPath "$brainPath.v2.bak" -Force |
| 20 | + Log "Backed up to $brainPath.v2.bak" |
| 21 | +} |
| 22 | + |
| 23 | +# Extract keys from old brain |
| 24 | +$sfKey = "" |
| 25 | +$giteeToken = "" |
| 26 | +if (Test-Path "$brainPath.v2.bak") { |
| 27 | + $old = Get-Content "$brainPath.v2.bak" -Raw -Encoding UTF8 |
| 28 | + $m1 = [regex]::Match($old, '\$SILICONFLOW_KEY\s*=\s*"([^"]+)"') |
| 29 | + if ($m1.Success) { $sfKey = $m1.Groups[1].Value; Log "Extracted SILICONFLOW_KEY" } |
| 30 | + $m2 = [regex]::Match($old, '\$GITEE_TOKEN\s*=\s*"([^"]+)"') |
| 31 | + if ($m2.Success) { $giteeToken = $m2.Groups[1].Value; Log "Extracted GITEE_TOKEN" } |
| 32 | +} |
| 33 | + |
| 34 | +# Download brain v3 template from GitHub |
| 35 | +$v3Url = "https://raw.githubusercontent.com/autodev-botbit/autodev-scripts/main/brain_v3_template.ps1" |
| 36 | +$v3Temp = "$env:TEMP\brain_v3_template.ps1" |
| 37 | +Log "Downloading brain v3 template from $v3Url" |
| 38 | + |
| 39 | +try { |
| 40 | + Invoke-WebRequest -Uri $v3Url -OutFile $v3Temp -TimeoutSec 30 |
| 41 | + Log "Downloaded template OK" |
| 42 | +} catch { |
| 43 | + Log "Download failed: $_ - trying alternative..." |
| 44 | + # Fallback: try GitHub API |
| 45 | + try { |
| 46 | + $apiUrl = "https://api.github.com/repos/autodev-botbit/autodev-scripts/contents/brain_v3_template.ps1" |
| 47 | + $resp = Invoke-RestMethod -Uri $apiUrl -TimeoutSec 15 |
| 48 | + $bytes = [Convert]::FromBase64String($resp.content) |
| 49 | + [IO.File]::WriteAllBytes($v3Temp, $bytes) |
| 50 | + Log "Downloaded via API OK" |
| 51 | + } catch { |
| 52 | + Log "API download also failed: $_" |
| 53 | + Log "ABORT: Cannot download brain template" |
| 54 | + exit 1 |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +# Read template and inject keys |
| 59 | +$content = Get-Content $v3Temp -Raw -Encoding UTF8 |
| 60 | +Log "Template loaded, injecting keys..." |
| 61 | + |
| 62 | +if ($sfKey) { |
| 63 | + $content = $content -replace 'PLACEHOLDER_SF_KEY', $sfKey |
| 64 | + Log "Injected SILICONFLOW_KEY" |
| 65 | +} |
| 66 | +if ($giteeToken) { |
| 67 | + $content = $content -replace 'PLACEHOLDER_GITEE_TOKEN', $giteeToken |
| 68 | + Log "Injected GITEE_TOKEN" |
| 69 | +} |
| 70 | + |
| 71 | +# Write GitHub token (known, was added in v3) |
| 72 | +$content = $content -replace 'PLACEHOLDER_GH_TOKEN', 'GH_TOKEN_PLACEHOLDER' |
| 73 | +Log "Injected GITHUB_TOKEN" |
| 74 | + |
| 75 | +# Save new brain |
| 76 | +[IO.File]::WriteAllText($brainPath, $content, [System.Text.Encoding]::UTF8) |
| 77 | +Log "Brain v3 written to $brainPath" |
| 78 | + |
| 79 | +# Verify |
| 80 | +$verify = Get-Content $brainPath -Raw -Encoding UTF8 |
| 81 | +$hasPR = $verify -match "GitHubForkCommitPR" |
| 82 | +$hasGHPages = $verify -match "DeployToGitHubPages" |
| 83 | +if ($hasPR -and $hasGHPages) { |
| 84 | + Log "VERIFIED: Brain v3 contains GitHubForkCommitPR + DeployToGitHubPages" |
| 85 | +} else { |
| 86 | + Log "WARNING: Verification issue - PR=$hasPR GitHubPages=$hasGHPages" |
| 87 | +} |
| 88 | + |
| 89 | +# Update bat |
| 90 | +$batPath = "C:\Users\Administrator\Desktop\AutoDev-Brain.bat" |
| 91 | +$batContent = "@echo off`r`npowershell -ExecutionPolicy Bypass -File `"C:\Users\Administrator\autodev-brain.ps1`"`r`npause" |
| 92 | +[IO.File]::WriteAllText($batPath, $batContent, [System.Text.Encoding]::ASCII) |
| 93 | +Log "Updated AutoDev-Brain.bat" |
| 94 | + |
| 95 | +# Cleanup |
| 96 | +Remove-Item $v3Temp -Force -ErrorAction SilentlyContinue |
| 97 | + |
| 98 | +Log "v3 UPGRADE COMPLETE!" |
0 commit comments