Skip to content

Commit 963941e

Browse files
committed
xbar5090 0.2.2 update: scripts/upload_github_api.ps1
1 parent dc5d466 commit 963941e

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

‎scripts/upload_github_api.ps1‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Upload xbar5090 files to GitHub using the REST Contents API.
2+
#
3+
# This script is intentionally NOT run automatically. It is the prepared
4+
# "upload with API" path for after the user confirms and provides the 3DMark
5+
# screenshot.
6+
#
7+
# Requirements:
8+
# - gh CLI authenticated with repo scope (gh auth status)
9+
# - Run from the project root:
10+
# powershell -ExecutionPolicy Bypass -File scripts/upload_github_api.ps1
11+
#
12+
# Optional:
13+
# -ScreenshotPath C:\path\to\3dmark.png
14+
# Uploads the screenshot to docs/images/hall-of-fame.png and inserts it
15+
# into README.md / README.zh-CN.md under a prominent Hall of Fame section.
16+
17+
param(
18+
[string]$SourceDir = (Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) 'xbar5090-upload'),
19+
[string]$Repo = 'SHANAjam/rtx5090-xbar-control',
20+
[string]$Branch = 'main',
21+
[string]$ScreenshotPath = '',
22+
[switch]$WhatIf
23+
)
24+
25+
$ErrorActionPreference = 'Stop'
26+
27+
if (-not (Get-Command gh -ErrorAction SilentlyContinue)) {
28+
throw 'gh CLI is required. Install GitHub CLI and run "gh auth login".'
29+
}
30+
31+
gh auth status *> $null
32+
if ($LASTEXITCODE -ne 0) {
33+
throw 'gh is not authenticated. Run "gh auth login" first.'
34+
}
35+
36+
if (-not (Test-Path $SourceDir)) {
37+
throw "Source directory not found: $SourceDir"
38+
}
39+
40+
function Get-ContentSha([string]$path) {
41+
$oldEAP = $ErrorActionPreference
42+
$ErrorActionPreference = 'Continue'
43+
try {
44+
$out = & gh api "repos/$Repo/contents/$path" --jq '.sha' 2>&1
45+
if ($LASTEXITCODE -eq 0 -and $out) {
46+
return ($out | Out-String).Trim()
47+
}
48+
return $null
49+
} finally {
50+
$ErrorActionPreference = $oldEAP
51+
}
52+
}
53+
54+
function Put-File([string]$path, [string]$localFile, [string]$commitMessage) {
55+
$bytes = [System.IO.File]::ReadAllBytes((Resolve-Path $localFile))
56+
$b64 = [Convert]::ToBase64String($bytes)
57+
$sha = Get-ContentSha $path
58+
$body = @{
59+
message = $commitMessage
60+
content = $b64
61+
branch = $Branch
62+
}
63+
if ($sha) {
64+
$body.sha = $sha
65+
}
66+
$json = $body | ConvertTo-Json -Compress
67+
if ($WhatIf) {
68+
Write-Host "[WhatIf] PUT $path (sha=$sha)"
69+
return
70+
}
71+
$json | gh api --method PUT "repos/$Repo/contents/$path" --input - *> $null
72+
if ($LASTEXITCODE -ne 0) {
73+
throw "Failed to upload $path"
74+
}
75+
Write-Host "Uploaded $path"
76+
}
77+
78+
# 1. Upload the full staging tree.
79+
$root = (Resolve-Path $SourceDir).Path
80+
Get-ChildItem -Path $root -Recurse -File | ForEach-Object {
81+
$rel = $_.FullName.Substring($root.Length + 1).Replace('\', '/')
82+
# Never upload local build artifacts/backups from the staging folder.
83+
if ($rel -match '^(build|dist|backups|__pycache__|\.pytest_cache)/') {
84+
return
85+
}
86+
Put-File $rel $_.FullName "xbar5090 0.2.2 update: $rel"
87+
}
88+
89+
# 2. Optional screenshot + README Hall of Fame section.
90+
if ($ScreenshotPath) {
91+
if (-not (Test-Path $ScreenshotPath)) {
92+
throw "Screenshot not found: $ScreenshotPath"
93+
}
94+
$imgPath = 'docs/images/hall-of-fame.png'
95+
Put-File $imgPath $ScreenshotPath "Add 3DMark Hall of Fame screenshot"
96+
97+
$imgMarkdown = "![3DMark Time Spy Extreme Graphics Hall of Fame](./$imgPath)"
98+
foreach ($readme in @('README.md', 'README.zh-CN.md')) {
99+
$local = Join-Path $root $readme
100+
if (-not (Test-Path $local)) {
101+
continue
102+
}
103+
$content = Get-Content $local -Raw -Encoding UTF8
104+
$section = "## Hall of Fame`n`n$imgMarkdown`n"
105+
if ($content -notmatch '## Hall of Fame') {
106+
$content = $section + "`n" + $content
107+
} else {
108+
$content = $content -replace '(?ms)## Hall of Fame.*?(?=## |\z)', $section
109+
}
110+
$tmp = [System.IO.Path]::GetTempFileName()
111+
[System.IO.File]::WriteAllText($tmp, $content, [System.Text.UTF8Encoding]::new($false))
112+
Put-File $readme $tmp "Add 3DMark Hall of Fame screenshot to $readme"
113+
Remove-Item $tmp -Force
114+
}
115+
}
116+
117+
Write-Host 'Upload script completed. (Run without -WhatIf after user confirmation.)'

0 commit comments

Comments
 (0)