Files
2026-07-16 00:58:59 +08:00

94 lines
2.8 KiB
PowerShell

$ErrorActionPreference = 'Stop'
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$Root = Split-Path -Parent $ScriptDir
$SingBox = Join-Path $Root 'works/sing-box'
$PatchDir = Join-Path $Root 'works/patch'
$Output = Join-Path $Root 'works/core'
$RevFile = Join-Path $Root 'works/rev'
# defaults
$RepoUrl = 'https://git.sfclub.cc/icybear/sing-box.git'
$UpstreamBranch = 'main'
if ($env:CI_JOB_TOKEN) {
git config --global user.name imabot
git config --global user.email icybear+ci@sab.ee
}
$ConfigFile = Join-Path $Root 'works/config'
if (Test-Path $ConfigFile) {
Get-Content $ConfigFile | ForEach-Object {
if ($_ -match '^([^#=]+)=(.*)$') {
$key = $Matches[1].Trim()
$val = $Matches[2].Trim()
if ($key -eq 'REPO_URL') { $RepoUrl = $val }
elseif ($key -eq 'UPSTREAM_BRANCH') { $UpstreamBranch = $val }
}
}
}
# Clone if not present
if (-not (Test-Path (Join-Path $SingBox '.git'))) {
Write-Host 'Cloning sing-box...'
if ($UpstreamBranch) {
git clone -b $UpstreamBranch $RepoUrl $SingBox
} else {
git clone $RepoUrl $SingBox
}
}
# Checkout stored rev
if (Test-Path $RevFile) {
$Rev = (Get-Content $RevFile -Raw).Trim()
Write-Host "Checking out stored rev: $Rev"
git -C $SingBox fetch --all
git -C $SingBox checkout $Rev
} else {
Write-Host 'Warning: works/rev not found, using current HEAD.' -ForegroundColor Yellow
}
Write-Host 'Initializing works/core/ from works/sing-box/ (retaining .git)...'
if (Test-Path $Output) { Remove-Item -Recurse -Force $Output }
Copy-Item -Path $SingBox -Destination $Output -Recurse -Force
Write-Host 'Applying patches...'
$applied = 0
$failed = 0
$failedList = @()
if (Test-Path $PatchDir) {
Get-ChildItem -Path $PatchDir -Recurse -Filter '*.patch' | Sort-Object FullName | ForEach-Object {
$rel = $_.FullName.Substring($PatchDir.Length + 1) -replace '\.patch$', ''
$dst = Join-Path $Output $rel
$dstDir = Split-Path -Parent $dst
if (-not (Test-Path $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
& patch --merge --no-backup-if-mismatch --force -i $_.FullName $dst 2>&1
if ($LASTEXITCODE -eq 0) {
$applied++
} else {
Write-Host " FAILED: $rel" -ForegroundColor Red
$failed++
$failedList += $rel
}
}
}
git -C $Output add -A
git -C $Output commit -m 'patches applied' --allow-empty
$total = $applied + $failed
Write-Host ""
Write-Host "Result: $applied/$total patches applied."
if ($failed -gt 0) {
Write-Host ""
Write-Host "Failed patches (merge conflicts written to files):" -ForegroundColor Red
foreach ($f in $failedList) {
Write-Host " $f" -ForegroundColor Red
}
exit 1
}
Write-Host 'Done.'