75 lines
2.5 KiB
PowerShell
75 lines
2.5 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'
|
|
|
|
if (-not (Test-Path (Join-Path $SingBox '.git'))) {
|
|
Write-Error 'works/sing-box not found. Run update first.'
|
|
exit 1
|
|
}
|
|
|
|
Write-Host 'Initializing works/core/...'
|
|
if (Test-Path $Output) { Remove-Item -Recurse -Force $Output }
|
|
New-Item -ItemType Directory -Path $Output -Force | Out-Null
|
|
git init $Output
|
|
|
|
Write-Host 'Syncing sing-box files to works/core/...'
|
|
Get-ChildItem -Path $SingBox -Recurse -File | Where-Object {
|
|
$_.FullName -notmatch [regex]::Escape((Join-Path $SingBox '.git'))
|
|
} | ForEach-Object {
|
|
$rel = $_.FullName.Substring($SingBox.Length + 1)
|
|
$dst = Join-Path $Output $rel
|
|
$dstDir = Split-Path -Parent $dst
|
|
if (-not (Test-Path $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
|
|
Copy-Item -Path $_.FullName -Destination $dst -Force
|
|
}
|
|
$version = git -C $SingBox describe --tags --always 2>$null
|
|
if (-not $version) { $version = git -C $SingBox rev-parse --short HEAD }
|
|
git -C $Output add -A
|
|
git -C $Output commit -m "upstream: $version"
|
|
|
|
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
|
|
}
|
|
Write-Host ""
|
|
Write-Host "Resolve conflicts in works/core/, then run 'bin/generate' to update patches."
|
|
exit 1
|
|
}
|
|
|
|
Write-Host 'Done.'
|