web-game-ecs-three-docs/tools/get-diagram.ps1

59 lines
1.5 KiB
PowerShell

param(
[string]$RepoPath = "architecture.mmd",
[string]$Branch = "dev"
)
$ErrorActionPreference = "Stop"
function Set-EnvFromFile {
param([string]$EnvPath)
if (-not (Test-Path $EnvPath)) {
return
}
foreach ($line in Get-Content $EnvPath) {
if ([string]::IsNullOrWhiteSpace($line) -or $line.StartsWith("#")) {
continue
}
$parts = $line -split "=", 2
if ($parts.Length -ne 2) {
continue
}
[System.Environment]::SetEnvironmentVariable($parts[0], $parts[1], "Process")
}
}
function Get-OriginInfo {
$origin = (git remote get-url origin).Trim()
if ($origin -match "^https?://([^/]+)/([^/]+)/([^/]+?)(?:\.git)?$") {
return @{
BaseUrl = "https://$($matches[1])"
Owner = $matches[2]
Repo = $matches[3]
}
}
throw "Unsupported origin URL format: $origin"
}
Set-EnvFromFile ".env.local"
$originInfo = Get-OriginInfo
$baseUrl = $originInfo.BaseUrl
$owner = $originInfo.Owner
$repo = $originInfo.Repo
$token = $env:FORGEJO_TOKEN
if (-not $token) {
throw "FORGEJO_TOKEN is missing. Add it to .env.local."
}
$encodedPath = [System.Uri]::EscapeDataString($RepoPath).Replace("%2F", "/")
$url = "$baseUrl/api/v1/repos/$owner/$repo/contents/${encodedPath}?ref=${Branch}"
$headers = @{ Authorization = "token $token" }
$response = Invoke-RestMethod -Headers $headers -Uri $url -Method Get
$base64Content = ($response.content -replace "`n", "")
[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($base64Content))