Files
SGF/scripts/payables-demo.ps1
2026-10-30 08:13:34 +02:00

40 lines
4.3 KiB
PowerShell

param([ValidateSet('start','stop','status','proof')][string]$Action='status',[switch]$HistoryOnly,[switch]$Settlements)
$ErrorActionPreference='Stop'
$payRoot=[IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..'))
if($payRoot -ne 'C:\laragon\www\sgf-tempo'){throw 'Unexpected project path'}
$payPrivate='C:/laragon/data/sgf-tempo/secrets/'
$payRun='C:\laragon\data\sgf-tempo\run'
$payPhp='C:\Users\macbook\.config\investmoz\php-8.5.11\php.exe'
$payProof=$Action -eq 'proof';$payTag=if($payProof){'proof'}else{'demo'}
if($Action -eq 'status'){Get-NetTCPConnection -LocalPort 8005,8006 -State Listen -ErrorAction SilentlyContinue|Select-Object LocalAddress,LocalPort,OwningProcess;exit}
if($Action -eq 'stop'){
foreach($payMode in @('compras','sgf')){$payRecord=Join-Path $payRun "payables-$payMode.json";if(Test-Path -LiteralPath $payRecord){$paySaved=Get-Content -Raw -LiteralPath $payRecord|ConvertFrom-Json;$payProcess=Get-Process -Id $paySaved.pid -ErrorAction SilentlyContinue;if($payProcess){if($payProcess.Path -ne $paySaved.executable -or $payProcess.StartTime.ToUniversalTime().ToString('o') -ne $paySaved.started){throw 'Process mismatch'};Stop-Process -Id $payProcess.Id};Remove-Item -LiteralPath $payRecord}}
Write-Output 'Payables demo servers stopped; synthetic scenarios retained.';exit
}
$payPorts=if($payProof){@(8007,8008)}else{@(8005,8006)}
foreach($payPort in $payPorts){if(Get-NetTCPConnection -LocalPort $payPort -State Listen -ErrorAction SilentlyContinue){throw "Port $payPort occupied; no process stopped."}}
$payProcesses=@()
try{
if($payProof){& $payPhp (Join-Path $PSScriptRoot 'payables-provision.php') --proof}else{& $payPhp (Join-Path $PSScriptRoot 'payables-provision.php')}
if($LASTEXITCODE){throw 'Payables preparation failed'}
$payOwner=[Security.Principal.WindowsIdentity]::GetCurrent().Name
foreach($payFile in @(Get-ChildItem -LiteralPath $payPrivate -Filter "payables-$payTag-*.json" -File)){& icacls.exe $payFile.FullName /inheritance:r /grant:r "${payOwner}:F" 'SYSTEM:F'|Out-Null}
$payIndex=0
foreach($payMode in @('compras','sgf')){
$payHost=if($payMode -eq 'compras'){'C:\laragon\www\compras'}else{$payRoot}
$payWebPhp=if($payMode -eq 'compras'){$payPhp}else{'C:\laragon\tools\sgf-tempo\php-8.4.27\php.exe'}
$payPort=$payPorts[$payIndex];$payIndex++
$env:PAYABLE_SETTINGS_FILE=$payPrivate+"payables-$payTag-$payMode.json"
$payProcess=Start-Process -FilePath $payWebPhp -ArgumentList @('-S',"127.0.0.1:$payPort",'-t','public',"$payRoot\scripts\payables-router.php") -WorkingDirectory $payHost -WindowStyle Hidden -PassThru -RedirectStandardOutput "$payRun\payables-$payTag-$payMode.stdout.log" -RedirectStandardError "$payRun\payables-$payTag-$payMode.stderr.log"
$payProcesses+=$payProcess
Remove-Item Env:PAYABLE_SETTINGS_FILE
if(!$payProof){@{pid=$payProcess.Id;started=$payProcess.StartTime.ToUniversalTime().ToString('o');executable=$payWebPhp;port=$payPort}|ConvertTo-Json|Set-Content -LiteralPath "$payRun\payables-$payMode.json" -Encoding UTF8}
$payReady=$false;for($payAttempt=0;$payAttempt -lt 30;$payAttempt++){if(Get-NetTCPConnection -LocalPort $payPort -State Listen -ErrorAction SilentlyContinue){$payReady=$true;break};Start-Sleep -Milliseconds 100};if(!$payReady){throw 'Payables server failed to start'}
}
if($payProof){Push-Location $payRoot;try{if($Settlements){foreach($settlementMode in @('compras','sgf')){& $payPhp scripts/settlements-install.php $settlementMode proof;if($LASTEXITCODE){throw 'Settlement schema failed'}};& 'C:\laragon\tools\sgf-tempo\node-v24.22.1-win-arm64\node.exe' scripts/settlements-http-proof.mjs}elseif($HistoryOnly){& 'C:\laragon\tools\sgf-tempo\node-v24.22.1-win-arm64\node.exe' scripts/payables-http-proof.mjs --history-only}else{& 'C:\laragon\tools\sgf-tempo\node-v24.22.1-win-arm64\node.exe' scripts/payables-http-proof.mjs};if($LASTEXITCODE){throw 'Payables HTTP proof failed'}}finally{Pop-Location}}
else{Write-Output 'Synthetic payables: Compras http://127.0.0.1:8005/obrigacoes-fornecedores ; SGF http://127.0.0.1:8006. Budget demonstration preserved.'}
}finally{
Remove-Item Env:PAYABLE_SETTINGS_FILE -ErrorAction SilentlyContinue
if($payProof){foreach($payProcess in $payProcesses){if(!$payProcess.HasExited){Stop-Process -Id $payProcess.Id}};& $payPhp (Join-Path $PSScriptRoot 'payables-provision.php') --cleanup-proof}
}