# gov_pipeline_interactive_write_v1.0.ps1 # Purpose: Passe standard gouvernance = Preview -> Confirmation O/N -> Ecriture réelle # PS 5.1-safe, UTF-8 no BOM, CRLF, ASCII-safe param( [string]$Root = '\\DS-918\chatgpt\ChatGPT-Gouvernance-Projets\_registry', [switch]$Utf8Console, [switch]$NoPrompt ) if($Utf8Console){ try{ [Console]::OutputEncoding = [Text.Encoding]::UTF8 }catch{} } $scripts = Join-Path $Root 'scripts' $inbox = Join-Path $Root 'updates\inbox\kb' $emit = Join-Path $scripts 'kb_emit_bootpack_dual_v1.1.ps1' $ingest = Join-Path $scripts 'kb_quicklog_and_sync_v1.2.1.ps1' $posthk = Join-Path $scripts 'post_bootpack_pointer_refresh_v1.0.2.ps1' $accept = Join-Path $scripts 'kb_acceptance_tests_v1.0.1.ps1' function Exists([string]$p){ return (-not [string]::IsNullOrWhiteSpace($p)) -and (Test-Path -LiteralPath $p) } function Run-Tool([string]$path,[string[]]$argv){ $null = & powershell -NoProfile -ExecutionPolicy Bypass -File $path @argv return [int]$LASTEXITCODE } function Run-Tool-Write([string]$path,[string[]]$argv){ # Essaie -Execute, sinon -Preview:$false, sinon sans flag $rc = 0 try{ $rc = Run-Tool $path ($argv + @('-Execute')) ; if($rc -eq 0){ return 0 } function Read-Text-UTF8([string]$p){ if(-not (Test-Path -LiteralPath $p)){ return '' } try{ return Get-Content -LiteralPath $p -Raw -Encoding UTF8 }catch{ return (Get-Content -LiteralPath $p -Raw) } } function Count-EntriesFromJson-Simple([string]$t){ if([string]::IsNullOrWhiteSpace($t)){ return -1 } if($t.Length -gt 0 -and ([int]$t[0] -eq 0xFEFF)){ $t=$t.Substring(1) } try{ $o=$t|ConvertFrom-Json; if($o -and $o.PSObject.Properties.Name -contains 'entries'){ return @($o.entries).Count } }catch{} try{ $m=[regex]::Match($t,'(?ms)"entries"\s*:\s*\[(.*?)\]'); if($m.Success){ return ([regex]::Matches($m.Groups[1].Value,'(?i)"id"\s*:')).Count } }catch{} -1 } function SelfHeal-Pointer([string]$Root){ $boot = Join-Path $Root 'bootpack\bootpack.txt' $paste= Join-Path $Root 'bootpack\bootpack_paste.txt' if(-not (Test-Path -LiteralPath $boot) -or -not (Test-Path -LiteralPath $paste)){ return 1 } $raw = Read-Text-UTF8 $boot $has = [regex]::IsMatch($raw,'(?m)^\[BUG_KB_JSON_POINTER\]\s*$') $okPath = $false if($has){ $m=[regex]::Match($raw,'(?m)^\s*Path\s*:\s*(.+?)\s*$'); if($m.Success){ $p=$m.Groups[1].Value.Trim(); $okPath=(Test-Path -LiteralPath $p) } } if(-not $has -or -not $okPath){ $sha=(Get-FileHash -Algorithm SHA256 -LiteralPath $paste).Hash.ToUpperInvariant() $size=(Get-Item -LiteralPath $paste).Length $ent=Count-EntriesFromJson-Simple (Read-Text-UTF8 $paste) $blk="[BUG_KB_JSON_POINTER]`r`nPath : $paste`r`nSHA256 : $sha`r`nEntries : $ent`r`nSizeBytes : $size`r`n" $pat='(?ms)^\[BUG_KB_JSON_POINTER\].*?(?=^\[|$\Z)' if([regex]::IsMatch($raw,$pat)){ $new=[regex]::Replace($raw,$pat,$blk) } else { $sep=if($raw -match '\S'){"`r`n`r`n"}else{""}; $new=$raw+$sep+$blk+"`r`n" } try{ $enc=New-Object System.Text.UTF8Encoding($false) [IO.File]::WriteAllText($boot, ($new -replace "`r?`n","`r`n"), $enc) Write-Host "[SELF-HEAL] Bloc pointer (ré)écrit dans bootpack.txt" }catch{ Write-Host "[SELF-HEAL] Ecriture impossible: $($_.Exception.Message)"; return 2 } } return 0 } }catch{} try{ $rc = Run-Tool $path ($argv + @('-Preview:$false')) ; if($rc -eq 0){ return 0 } }catch{} $rc = Run-Tool $path $argv return $rc } Write-Host "== GOV PIPELINE :: INTERACTIVE WRITE v1.0 ==" Write-Host ("Root : {0}" -f $Root) # -- PLAN -------------------------------------------------------------- $countInbox = 0 if(Exists $inbox){ $countInbox = (Get-ChildItem -LiteralPath $inbox -Filter 'AUTO_*.txt' -ErrorAction SilentlyContinue).Count } Write-Host ("KB inbox : {0} AUTO_*.txt" -f $countInbox) if(Exists $emit){ Write-Host "BootPack : PREVIEW plan ->" $null = Run-Tool $emit @($Root) } else { Write-Host "[WARN] Emitter introuvable: $emit" } Write-Host "----------------------------------------------------------------" if(-not $NoPrompt){ $resp = Read-Host "Écrire maintenant ? (O/N)" if($resp -notmatch '^[OoYy]'){ Write-Host "[ABORT] Aucun changement écrit."; exit 0 } } # -- EXECUTION RÉELLE --------------------------------------------------- if($countInbox -gt 0 -and (Exists $ingest)){ Get-ChildItem -LiteralPath $inbox -Filter 'AUTO_*.txt' | ForEach-Object { $f = $_.FullName Write-Host ("[INGEST] {0}" -f $f) $rc = Run-Tool $ingest @('-FromFile',$f,'-Execute') if($rc -ne 0){ Write-Host ("[FAIL] ingest rc={0} : {1}" -f $rc,$f); exit $rc } } } else{ Write-Host "[INFO] Rien à ingérer (inbox vide ou outil manquant)." } if(Exists $emit){ Write-Host "[EMIT] BootPack (écriture réelle)..." $rc = Run-Tool-Write $emit @($Root) if($rc -ne 0){ Write-Host ("[FAIL] emit rc={0}" -f $rc); exit $rc } } if(Exists $posthk){ Write-Host "[POST] Pointer refresh + acceptance... $null = SelfHeal-Pointer $Root $rc = Run-Tool-Write $posthk @('-Root',$Root,'-NoPrompt','-Verify') if($rc -ne 0){ Write-Host ("[WARN] post-hook rc={0}. Tentative de self-heal + retry..." -f $rc) $null = SelfHeal-Pointer $Root $rc = Run-Tool-Write $posthk @('-Root',$Root,'-NoPrompt','-Verify') if($rc -ne 0){ Write-Host ("[FAIL] post-hook rc={0}" -f $rc); exit $rc } }" -f $rc); exit $rc } } else { Write-Host "[WARN] post-hook introuvable: $posthk" } if(Exists $accept){ Write-Host "[CHECK] Acceptance PTR finale..." $rc = Run-Tool $accept @($Root) if($rc -ne 0){ Write-Host ("[FAIL] acceptance rc={0}" -f $rc); exit $rc } } Write-Host "[DONE] Interractif : opérations appliquées avec succès."