TH1/Tools/CommitTH1.ps1

102 lines
3.3 KiB
PowerShell

param(
[Parameter(Mandatory = $true)]
[string]$Message,
[switch]$SkipBuild,
[switch]$AllowProtected,
[switch]$BuildAot,
[switch]$BuildEditor,
[switch]$BuildConfig,
[switch]$NoVerify
)
$ErrorActionPreference = "Stop"
$repoRoot = git rev-parse --show-toplevel 2>$null
if (-not $repoRoot) {
throw "Not inside a git repository."
}
$repoRoot = [System.IO.Path]::GetFullPath($repoRoot.Trim())
Set-Location $repoRoot
$staged = @(git diff --cached --name-only | ForEach-Object { $_ -replace '\\', '/' })
if ($staged.Count -eq 0) {
throw "No staged changes. Stage a coherent commit group first."
}
Write-Host "Staged files:"
$staged | ForEach-Object { Write-Host " $_" }
$protectedPatterns = @(
'Unity/Assets/Resources/Export/*',
'Tools/Multilingual.xlsx',
'Tools/MultilingualTxt.txt',
'Unity/Assets/StreamingAssets/HybridCLR/*',
'Unity/Assets/StreamingAssets/Bundles/*',
'HybridCLRData/*',
'Library/*',
'Temp/*',
'Logs/*',
'Obj/*',
'Build/*',
'Builds/*',
'graphify-out/graph.json',
'graphify-out/.graphify_python',
'graphify-out/needs_update',
'graphify-out/cache/*',
'graphify-out/converted/*',
'graphify-out/obsidian/*',
'Unity/graphify-out/graph.json',
'Unity/graphify-out/.graphify_python',
'Unity/graphify-out/needs_update',
'Unity/graphify-out/cache/*',
'Unity/graphify-out/converted/*',
'Unity/graphify-out/obsidian/*'
)
function Test-PathLike([string]$Path, [string[]]$Patterns) {
foreach ($pattern in $Patterns) {
if ($Path -like $pattern) {
return $true
}
}
return $false
}
$blocked = @($staged | Where-Object { Test-PathLike $_ $protectedPatterns } | Sort-Object -Unique)
if ($blocked.Count -gt 0 -and -not $AllowProtected) {
Write-Host "Protected staged paths:"
$blocked | ForEach-Object { Write-Host " $_" }
throw "Refusing to commit protected generated/build paths. Re-run with -AllowProtected only for an explicit export/build artifact commit."
}
$touchesHotfix = @($staged | Where-Object { $_ -like 'Unity/Assets/Scripts/*' }).Count -gt 0
$touchesAot = @($staged | Where-Object { $_ -like 'Unity/Assets/Scripts/*AOT*' -or $_ -like 'Unity/Assets/Scripts/*HybridCLR*' -or $_ -like 'Unity/Assets/Scripts/*YooAsset*' }).Count -gt 0
$touchesEditor = @($staged | Where-Object { $_ -like 'Unity/Assets/Scripts/*Editor*' -or $_ -like 'Unity/Assets/Editor/*' }).Count -gt 0
$touchesConfig = @($staged | Where-Object { $_ -like 'ExcelExport/*' -or $_ -like 'Unity/Assets/Scripts/TH1_Config/*' }).Count -gt 0
if (-not $SkipBuild) {
if ($touchesHotfix) {
dotnet build Unity/TH1.Hotfix.csproj --no-restore
}
if ($BuildAot -or $touchesAot) {
dotnet build Unity/TH1.AOT.csproj --no-restore
}
if ($BuildEditor -or $touchesEditor) {
dotnet build Unity/TH1.Editor.csproj --no-restore
dotnet build Unity/TH1.Logic.Editor.csproj --no-restore
dotnet build Unity/TH1.Steam.Editor.csproj --no-restore
}
if ($BuildConfig -or $touchesConfig) {
dotnet build ExcelExport/ExcelExport.csproj --no-restore
}
} else {
Write-Host "Skipping build verification because -SkipBuild was provided."
}
$commitArgs = @('commit', '-m', $Message)
if ($NoVerify) {
$commitArgs += '--no-verify'
}
git @commitArgs