Files
mxaccessgw/scripts/publish-client-proto-inputs.ps1
T
Joseph Doherty d5248f61a2 fix(archreview): CI pipeline + codegen freshness guards (IPC-01/09/19/20, TST-03)
- IPC-01: regenerate the stale client descriptor set and add ClientProtoInputTests
  (semantic, protoc-free) so a missing contract symbol fails the build.
- IPC-20: make publish-client-proto-inputs.ps1 -Check source_code_info-normalized
  so it is protoc-version tolerant.
- IPC-19: document + guard the "Generated/ must be committed for net48" rule
  (docs/Contracts.md, csproj comment, check-codegen.ps1).
- IPC-09: harden the per-client generate-proto scripts (PATH resolution + pinned
  grpcio/protobuf/java version asserts) so regeneration is reproducible.
- TST-03: add .gitea/workflows/ci.yml (portable/java/windows/live jobs) running the
  build, tests, client checks, and the codegen guards.
- Also: check-codegen.ps1 Check 3 guards the CLI-02 vendored Rust protos against drift.

archreview: IPC-01/09/19/20 Done, TST-03 In review (pipeline authored + validated,
not yet run on a Gitea runner). Verified on macOS: NonWindows build clean,
ClientProtoInputTests 5/5, -Check exit 0.
2026-07-09 06:17:56 -04:00

178 lines
6.8 KiB
PowerShell

[CmdletBinding()]
param(
[switch]$Check
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
# Pinned protoc toolchain version used to generate the committed client descriptor set.
# See docs/ToolchainLinks.md. Generating (committing) the canonical artifact must use this
# exact version so the checked-in bytes are reproducible. The -Check comparison is made
# tolerant of protoc-version encoding drift (chiefly source_code_info, see IPC-20) by
# normalizing both the committed and the freshly built descriptor through the *same* protoc
# with source info stripped, so it does not false-fail across protoc releases.
$PinnedProtocVersion = "34.1"
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot "..")
$protoRoot = Join-Path $repoRoot "src/ZB.MOM.WW.MxGateway.Contracts/Protos"
$manifestPath = Join-Path $repoRoot "clients/proto/proto-inputs.json"
$descriptorPath = Join-Path $repoRoot "clients/proto/descriptors/mxaccessgw-client-v1.protoset"
$protoFiles = @("mxaccess_gateway.proto", "mxaccess_worker.proto", "galaxy_repository.proto")
function Resolve-Protoc {
# Prefer protoc on PATH (protoc on Linux/macOS, protoc.exe on Windows), then fall back to
# the documented winget install location on Windows.
foreach ($name in @("protoc", "protoc.exe")) {
$pathCommand = Get-Command $name -ErrorAction SilentlyContinue
if ($null -ne $pathCommand) {
return $pathCommand.Source
}
}
if ($env:LOCALAPPDATA) {
$documentedPath = Join-Path $env:LOCALAPPDATA "Microsoft/WinGet/Packages/Google.Protobuf_Microsoft.Winget.Source_8wekyb3d8bbwe/bin/protoc.exe"
if (Test-Path $documentedPath) {
return $documentedPath
}
}
throw "Could not find protoc on PATH. See docs/ToolchainLinks.md for the documented protobuf toolchain (protoc $PinnedProtocVersion)."
}
function Get-ProtocVersion {
param([string]$Protoc)
# `protoc --version` prints e.g. "libprotoc 34.1".
$raw = (& $Protoc --version) | Select-Object -First 1
return ($raw -replace '^libprotoc\s+', '').Trim()
}
function Ensure-Directory {
param([string]$Path)
if (-not (Test-Path $Path)) {
New-Item -ItemType Directory -Path $Path | Out-Null
}
}
function New-SourceInfoFreeDescriptor {
# Re-emits an existing descriptor set with source_code_info stripped, by round-tripping it
# through protoc with --descriptor_set_in and *without* --include_source_info. Because both
# the committed and freshly built descriptors are normalized through the same protoc binary,
# any protoc-version-specific encoding cancels out and the byte comparison is stable.
param(
[string]$Protoc,
[string]$InputDescriptor,
[string]$OutputDescriptor
)
& $Protoc `
"--descriptor_set_in=$InputDescriptor" `
"--include_imports" `
"--descriptor_set_out=$OutputDescriptor" `
@protoFiles
if ($LASTEXITCODE -ne 0) {
throw "protoc normalization (--descriptor_set_in) failed with exit code $LASTEXITCODE."
}
}
function Test-FileBytesEqual {
param(
[string]$ExpectedPath,
[string]$ActualPath
)
if (-not (Test-Path $ExpectedPath)) {
return $false
}
$expected = [System.IO.File]::ReadAllBytes($ExpectedPath)
$actual = [System.IO.File]::ReadAllBytes($ActualPath)
if ($expected.Length -ne $actual.Length) {
return $false
}
for ($index = 0; $index -lt $expected.Length; $index++) {
if ($expected[$index] -ne $actual[$index]) {
return $false
}
}
return $true
}
$manifest = Get-Content -Raw $manifestPath | ConvertFrom-Json
Ensure-Directory (Split-Path $descriptorPath -Parent)
foreach ($output in $manifest.generatedOutputs.PSObject.Properties.Value) {
Ensure-Directory (Join-Path $repoRoot $output)
}
$protoc = Resolve-Protoc
$protocVersion = Get-ProtocVersion $protoc
if ($Check) {
# Version drift no longer produces a false "stale" failure: the comparison is normalized
# below. A mismatch is surfaced as a warning so it is visible without blocking the gate.
if ($protocVersion -ne $PinnedProtocVersion) {
Write-Warning "protoc version '$protocVersion' differs from the pinned '$PinnedProtocVersion'. The freshness comparison is normalized and tolerant of this, but regeneration must use the pinned version (see docs/ClientProtoGeneration.md)."
}
$freshDescriptor = Join-Path ([System.IO.Path]::GetTempPath()) ("mxaccessgw-client-v1-fresh-" + [System.Guid]::NewGuid().ToString("N") + ".protoset")
$committedNormalized = Join-Path ([System.IO.Path]::GetTempPath()) ("mxaccessgw-client-v1-committed-" + [System.Guid]::NewGuid().ToString("N") + ".protoset")
try {
# Fresh descriptor from the current .proto sources, source info omitted (already normalized).
& $protoc `
"--proto_path=$protoRoot" `
"--include_imports" `
"--descriptor_set_out=$freshDescriptor" `
@protoFiles
if ($LASTEXITCODE -ne 0) {
throw "protoc failed with exit code $LASTEXITCODE."
}
if (-not (Test-Path $descriptorPath)) {
throw "Committed descriptor '$descriptorPath' does not exist. Run scripts/publish-client-proto-inputs.ps1 to create it."
}
# Normalize the committed descriptor (which carries source info) through the same protoc.
New-SourceInfoFreeDescriptor -Protoc $protoc -InputDescriptor $descriptorPath -OutputDescriptor $committedNormalized
if (-not (Test-FileBytesEqual -ExpectedPath $committedNormalized -ActualPath $freshDescriptor)) {
throw "Client proto descriptor is stale. Run scripts/publish-client-proto-inputs.ps1 with the pinned protoc ($PinnedProtocVersion) and commit the updated descriptor."
}
Write-Host "Client proto descriptor is up to date."
}
finally {
foreach ($temp in @($freshDescriptor, $committedNormalized)) {
if (Test-Path $temp) {
Remove-Item -LiteralPath $temp
}
}
}
}
else {
# Generating the canonical committed artifact must use the pinned protoc so the bytes are reproducible.
if ($protocVersion -ne $PinnedProtocVersion) {
throw "protoc version '$protocVersion' does not match the pinned toolchain version '$PinnedProtocVersion'. Install the pinned protoc (see docs/ToolchainLinks.md) before regenerating the committed descriptor, or run with -Check to only verify freshness."
}
& $protoc `
"--proto_path=$protoRoot" `
"--include_imports" `
"--include_source_info" `
"--descriptor_set_out=$descriptorPath" `
@protoFiles
if ($LASTEXITCODE -ne 0) {
throw "protoc failed with exit code $LASTEXITCODE."
}
Write-Host "Wrote client proto descriptor to $descriptorPath (protoc $protocVersion)."
}