Files
SPHLite/tools/publish_to_cclite.ps1
2026-07-05 17:00:13 +08:00

321 lines
9.4 KiB
PowerShell

[CmdletBinding()]
param(
[string]$Version = "",
[string]$TargetRoot = "",
[string]$ReleaseBuildDir = "build-codex-release",
[string]$DebugBuildDir = "build-codex-debug",
[switch]$SkipBuild,
[switch]$DryRun
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$RepoRoot = [System.IO.Path]::GetFullPath((Join-Path $ScriptDir ".."))
function Resolve-RepoPath {
param([string]$Path)
if ([System.IO.Path]::IsPathRooted($Path)) {
return [System.IO.Path]::GetFullPath($Path)
}
return [System.IO.Path]::GetFullPath((Join-Path $RepoRoot $Path))
}
function Get-VersionPart {
param(
[string]$Text,
[string]$Name
)
$match = [regex]::Match(
$Text,
"(?m)^\s*integer,\s*parameter\s*::\s*$Name\s*=\s*(\d+)\s*$")
if (-not $match.Success) {
throw "Could not read $Name from src/core/sph_precision.f90"
}
return [int]$match.Groups[1].Value
}
function Get-SourceVersion {
$precisionFile = Join-Path $RepoRoot "src/core/sph_precision.f90"
$text = Get-Content -LiteralPath $precisionFile -Raw -Encoding UTF8
$major = Get-VersionPart $text "SPH_VERSION_MAJOR"
$minor = Get-VersionPart $text "SPH_VERSION_MINOR"
$patch = Get-VersionPart $text "SPH_VERSION_PATCH"
return "$major.$minor.$patch"
}
function Assert-SafeTargetRoot {
param([string]$Path)
$full = [System.IO.Path]::GetFullPath($Path).TrimEnd("\")
$root = [System.IO.Path]::GetPathRoot($full).TrimEnd("\")
$repo = [System.IO.Path]::GetFullPath($RepoRoot).TrimEnd("\")
if ($full -eq $root) {
throw "Refusing to publish to a drive root: $full"
}
if ($full -eq $repo) {
throw "Refusing to publish over the SPHLite source tree: $full"
}
}
function Invoke-External {
param(
[string]$File,
[string[]]$Arguments
)
Write-Host "> $File $($Arguments -join ' ')"
if ($DryRun) {
return
}
& $File @Arguments
if ($LASTEXITCODE -ne 0) {
throw "$File failed with exit code $LASTEXITCODE"
}
}
function Invoke-SPHLiteBuild {
param(
[string]$BuildDir,
[string]$Config
)
if ($SkipBuild) {
Write-Host "Skipping $Config build: $BuildDir"
return
}
Invoke-External "cmake" @(
"-S", $RepoRoot,
"-B", $BuildDir,
"-DCMAKE_BUILD_TYPE=$Config"
)
Invoke-External "cmake" @(
"--build", $BuildDir,
"--target", "sphlite", "sphlite_c_shared",
"--config", $Config,
"--parallel"
)
}
function Reset-Directory {
param([string]$Path)
if ($DryRun) {
Write-Host "[dry-run] reset directory: $Path"
return
}
if (Test-Path -LiteralPath $Path) {
Remove-Item -LiteralPath $Path -Recurse -Force
}
New-Item -ItemType Directory -Path $Path -Force | Out-Null
}
function Copy-BuildArtifacts {
param(
[string]$BuildDir,
[string]$Destination
)
$required = @("sphlite.exe", "libsphlite_c.dll")
foreach ($name in $required) {
$path = Join-Path $BuildDir $name
if (-not (Test-Path -LiteralPath $path)) {
throw "Required build artifact is missing: $path"
}
}
Reset-Directory $Destination
$patterns = @("sphlite.exe", "libsphlite_c.*", "libsphlite_core.*")
$copied = @{}
foreach ($pattern in $patterns) {
$items = Get-ChildItem -Path $BuildDir -File -Filter $pattern
foreach ($item in $items) {
if ($copied.ContainsKey($item.FullName)) {
continue
}
$copied[$item.FullName] = $true
Write-Host "copy $($item.Name) -> $Destination"
if (-not $DryRun) {
Copy-Item -LiteralPath $item.FullName -Destination $Destination -Force
}
}
}
}
function Should-SkipPath {
param([string]$RelativePath)
$parts = $RelativePath -split "[\\/]"
foreach ($part in $parts) {
if ($part -eq "__pycache__") {
return $true
}
if ($part -eq ".pytest_cache") {
return $true
}
}
if ($RelativePath -like "*.pyc") {
return $true
}
return $false
}
function Copy-Tree {
param(
[string]$Source,
[string]$Destination
)
if (-not (Test-Path -LiteralPath $Source)) {
Write-Host "skip missing directory: $Source"
return
}
Reset-Directory $Destination
if ($DryRun) {
Write-Host "[dry-run] copy tree: $Source -> $Destination"
return
}
$sourceFull = [System.IO.Path]::GetFullPath($Source).TrimEnd("\")
Get-ChildItem -LiteralPath $Source -Recurse -Force | ForEach-Object {
$relative = $_.FullName.Substring($sourceFull.Length).TrimStart("\")
if (Should-SkipPath $relative) {
return
}
$target = Join-Path $Destination $relative
if ($_.PSIsContainer) {
New-Item -ItemType Directory -Path $target -Force | Out-Null
} else {
$parent = Split-Path -Parent $target
if (-not (Test-Path -LiteralPath $parent)) {
New-Item -ItemType Directory -Path $parent -Force | Out-Null
}
Copy-Item -LiteralPath $_.FullName -Destination $target -Force
}
}
}
function Copy-ReleaseFile {
param(
[string]$Source,
[string]$Destination
)
if (-not (Test-Path -LiteralPath $Source)) {
return
}
Write-Host "copy $(Split-Path -Leaf $Source) -> $Destination"
if (-not $DryRun) {
Copy-Item -LiteralPath $Source -Destination $Destination -Force
}
}
function Update-ExampleScriptPaths {
param([string]$ExamplesRoot)
if (-not (Test-Path -LiteralPath $ExamplesRoot)) {
return
}
$publishedLookup = @'
rem BEGIN SPHLITE_EXE_LOOKUP
set "SPHLITE_ROOT=%~dp0..\.."
set "SPHLITE_RUN_EXE="
if defined SPHLITE_EXE (
if exist "%SPHLITE_EXE%" set "SPHLITE_RUN_EXE=%SPHLITE_EXE%"
)
if not defined SPHLITE_RUN_EXE if exist "%SPHLITE_ROOT%\cpu_release\sphlite.exe" set "SPHLITE_RUN_EXE=%SPHLITE_ROOT%\cpu_release\sphlite.exe"
if not defined SPHLITE_RUN_EXE if exist "%SPHLITE_ROOT%\cpu_debug\sphlite.exe" set "SPHLITE_RUN_EXE=%SPHLITE_ROOT%\cpu_debug\sphlite.exe"
if not defined SPHLITE_RUN_EXE if exist "%SPHLITE_ROOT%\sphlite.exe" set "SPHLITE_RUN_EXE=%SPHLITE_ROOT%\sphlite.exe"
rem END SPHLITE_EXE_LOOKUP
'@
$publishedHints = @'
rem BEGIN SPHLITE_EXE_HINTS
echo Set SPHLITE_EXE to the full path of sphlite.exe, or place sphlite.exe in one of:
echo %%SPHLITE_ROOT%%\cpu_release\sphlite.exe
echo %%SPHLITE_ROOT%%\cpu_debug\sphlite.exe
rem END SPHLITE_EXE_HINTS
'@
$lookupPattern = "(?s)rem BEGIN SPHLITE_EXE_LOOKUP.*?rem END SPHLITE_EXE_LOOKUP"
$hintsPattern = "(?s)\s*rem BEGIN SPHLITE_EXE_HINTS.*?rem END SPHLITE_EXE_HINTS"
$updated = 0
Get-ChildItem -LiteralPath $ExamplesRoot -Recurse -File -Include "*.bat", "*.cmd" |
ForEach-Object {
$text = Get-Content -LiteralPath $_.FullName -Raw -Encoding ASCII
$newText = [regex]::Replace(
$text,
$lookupPattern,
[System.Text.RegularExpressions.MatchEvaluator]{ param($match) $publishedLookup })
$newText = [regex]::Replace(
$newText,
$hintsPattern,
[System.Text.RegularExpressions.MatchEvaluator]{ param($match) "`r`n$publishedHints" })
if ($newText -ne $text) {
$updated += 1
Write-Host "adjust example script paths: $($_.FullName)"
if (-not $DryRun) {
Set-Content -LiteralPath $_.FullName -Value $newText -Encoding ASCII -NoNewline
}
}
}
Write-Host "example scripts adjusted: $updated"
}
function Write-Manifest {
param(
[string]$Destination,
[string]$ReleaseVersion,
[string]$ReleaseBuild,
[string]$DebugBuild
)
$manifest = @(
"SPHLite release manifest",
"version=$ReleaseVersion",
"published_at=$(Get-Date -Format o)",
"source_root=$RepoRoot",
"release_build=$ReleaseBuild",
"debug_build=$DebugBuild",
"managed_directories=cpu_release,cpu_debug,docs,examples,include,Python",
"entry_point=cpu_release\sphlite.exe"
)
$path = Join-Path $Destination "release_manifest.txt"
Write-Host "write manifest: $path"
if (-not $DryRun) {
Set-Content -LiteralPath $path -Value $manifest -Encoding ASCII
}
}
if ([string]::IsNullOrWhiteSpace($Version)) {
$Version = Get-SourceVersion
}
if ([string]::IsNullOrWhiteSpace($TargetRoot)) {
$TargetRoot = Join-Path (Split-Path -Parent $RepoRoot) "CCLite\SPHLite"
}
$TargetRoot = [System.IO.Path]::GetFullPath($TargetRoot)
$ReleaseBuildDir = Resolve-RepoPath $ReleaseBuildDir
$DebugBuildDir = Resolve-RepoPath $DebugBuildDir
Assert-SafeTargetRoot $TargetRoot
Write-Host "SPHLite version : $Version"
Write-Host "source root : $RepoRoot"
Write-Host "target root : $TargetRoot"
Write-Host "release build : $ReleaseBuildDir"
Write-Host "debug build : $DebugBuildDir"
Write-Host ""
Invoke-SPHLiteBuild $ReleaseBuildDir "Release"
Invoke-SPHLiteBuild $DebugBuildDir "Debug"
if (-not $DryRun) {
New-Item -ItemType Directory -Path $TargetRoot -Force | Out-Null
}
Copy-BuildArtifacts $ReleaseBuildDir (Join-Path $TargetRoot "cpu_release")
Copy-BuildArtifacts $DebugBuildDir (Join-Path $TargetRoot "cpu_debug")
Copy-Tree (Join-Path $RepoRoot "docs") (Join-Path $TargetRoot "docs")
Copy-Tree (Join-Path $RepoRoot "examples") (Join-Path $TargetRoot "examples")
Copy-Tree (Join-Path $RepoRoot "include") (Join-Path $TargetRoot "include")
Copy-Tree (Join-Path $RepoRoot "Python") (Join-Path $TargetRoot "Python")
Copy-ReleaseFile (Join-Path $RepoRoot "README.md") $TargetRoot
Update-ExampleScriptPaths (Join-Path $TargetRoot "examples")
Write-Manifest $TargetRoot $Version $ReleaseBuildDir $DebugBuildDir
Write-Host ""
Write-Host "Publish complete: $TargetRoot"
Write-Host "Release executable: $(Join-Path $TargetRoot 'cpu_release\sphlite.exe')"
Write-Host "Debug executable : $(Join-Path $TargetRoot 'cpu_debug\sphlite.exe')"