81 lines
2.8 KiB
PowerShell
81 lines
2.8 KiB
PowerShell
param(
|
|
[string]$Protoc = "",
|
|
[string]$IncludePath = $env:PROTOC_INCLUDE,
|
|
[string]$OutputPath = ""
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
$repositoryRoot = Split-Path -Parent $PSScriptRoot
|
|
$protoRoot = Join-Path $repositoryRoot "api/proto"
|
|
$source = Join-Path $protoRoot "controlplane/v1/controlplane.proto"
|
|
|
|
if ([string]::IsNullOrWhiteSpace($Protoc)) {
|
|
$Protoc = & (Join-Path $PSScriptRoot "install-protoc.ps1")
|
|
}
|
|
$protocCommand = Get-Command $Protoc -ErrorAction Stop
|
|
$protocVersion = (& $protocCommand.Source --version).Trim()
|
|
if ($protocVersion -ne "libprotoc 35.0") {
|
|
throw "protoc 35.0 is required"
|
|
}
|
|
if ([string]::IsNullOrWhiteSpace($IncludePath)) {
|
|
$installationRoot = Split-Path (Split-Path $protocCommand.Source -Parent) -Parent
|
|
$candidates = @(
|
|
(Join-Path $installationRoot "include"),
|
|
"/usr/include",
|
|
"/usr/local/include"
|
|
)
|
|
$IncludePath = $candidates |
|
|
Where-Object { Test-Path (Join-Path $_ "google/protobuf/timestamp.proto") } |
|
|
Select-Object -First 1
|
|
}
|
|
if ([string]::IsNullOrWhiteSpace($IncludePath) -or
|
|
-not (Test-Path (Join-Path $IncludePath "google/protobuf/timestamp.proto"))) {
|
|
throw "protoc well-known type include directory was not found; set PROTOC_INCLUDE"
|
|
}
|
|
|
|
if ([string]::IsNullOrWhiteSpace($OutputPath)) {
|
|
$outputDirectory = Join-Path $repositoryRoot ".tmp-proto"
|
|
New-Item -ItemType Directory -Force -Path $outputDirectory | Out-Null
|
|
$OutputPath = Join-Path $outputDirectory "controlplane.pb"
|
|
}
|
|
|
|
& $protocCommand.Source `
|
|
"--proto_path=$protoRoot" `
|
|
"--proto_path=$IncludePath" `
|
|
"--include_imports" `
|
|
"--include_source_info" `
|
|
"--descriptor_set_out=$OutputPath" `
|
|
$source
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "protoc descriptor compilation failed with exit code $LASTEXITCODE"
|
|
}
|
|
|
|
$descriptor = Get-Item -LiteralPath $OutputPath
|
|
if ($descriptor.Length -le 0) {
|
|
throw "protoc produced an empty descriptor set"
|
|
}
|
|
Write-Host "descriptor: $($descriptor.FullName) ($($descriptor.Length) bytes)"
|
|
|
|
$outputRoot = Join-Path $repositoryRoot ".tmp-proto/generated"
|
|
& (Join-Path $PSScriptRoot "generate-proto.ps1") `
|
|
-Protoc $protocCommand.Source `
|
|
-IncludePath $IncludePath `
|
|
-OutputRoot $outputRoot
|
|
|
|
$generated = @(
|
|
"gen/controlplane/v1/controlplane.pb.go",
|
|
"gen/controlplane/v1/controlplane_grpc.pb.go"
|
|
)
|
|
foreach ($relative in $generated) {
|
|
$committed = Join-Path $repositoryRoot $relative
|
|
$candidate = Join-Path $outputRoot $relative
|
|
if (-not (Test-Path -LiteralPath $committed) -or
|
|
-not (Test-Path -LiteralPath $candidate) -or
|
|
-not [System.Linq.Enumerable]::SequenceEqual(
|
|
[System.IO.File]::ReadAllBytes($committed),
|
|
[System.IO.File]::ReadAllBytes($candidate))) {
|
|
throw "generated protobuf drift: $relative"
|
|
}
|
|
}
|