# kb_bootpack_inline_json_patch_v1.1.ps1 # Adds/refreshes [BUG_KB_JSON] fenced JSON inside bootpack.txt, keeping the pointer block intact. param([string]$Root="\\DS-918\chatgpt\ChatGPT-Gouvernance-Projets\_registry",[ValidateSet("paste","kb")] [string]$Source="paste",[switch]$Utf8Console) if($Utf8Console){ try{ [Console]::OutputEncoding = [Text.Encoding]::UTF8 }catch{} } function Exists([string]$p){ (-not [string]::IsNullOrWhiteSpace($p)) -and (Test-Path -LiteralPath $p) } function Read-Text([string]$p){ if(-not (Exists $p)){ return "" } try{ Get-Content -LiteralPath $p -Raw -Encoding UTF8 }catch{ Get-Content -LiteralPath $p -Raw } } Write-Host "== KB INLINE JSON PATCH v1.1 ==" $boot = Join-Path $Root "bootpack\bootpack.txt" $paste= Join-Path $Root "bootpack\bootpack_paste.txt" $kb = Join-Path $Root "bug_kb\BUG_KB.json.txt" if(-not (Exists $boot)){ Write-Host ("[FATAL] missing bootpack.txt: {0}" -f $boot); exit 3 } if($Source -eq "paste"){ if(-not (Exists $paste)){ Write-Host ("[FATAL] missing paste: {0}" -f $paste); exit 3 } $jsonRaw = Read-Text $paste } else { if(-not (Exists $kb)){ Write-Host ("[FATAL] missing kb: {0}" -f $kb); exit 3 } $jsonRaw = Read-Text $kb } if([string]::IsNullOrWhiteSpace($jsonRaw)){ Write-Host "[FATAL] empty JSON source"; exit 3 } if($jsonRaw.Length -gt 0 -and ([int]$jsonRaw[0] -eq 0xFEFF)){ $jsonRaw = $jsonRaw.Substring(1) } $bootRaw = Read-Text $boot $norm = $bootRaw -replace "`r`n","`n" $lines = $norm -split "`n" # remove existing [BUG_KB_JSON] block $out = New-Object System.Collections.Generic.List[string] $inBlk = $false for($i=0;$i -lt $lines.Length;$i++){ $t = $lines[$i] $trim = $t.Trim() if(-not $inBlk -and $trim -ceq "[BUG_KB_JSON]"){ $inBlk = $true; continue } if($inBlk){ if($trim.StartsWith("[")){ $inBlk=$false; $out.Add($t); continue } else { continue } } $out.Add($t) } # find insertion point: after pointer block if present, else end $insIdx = $out.Count for($i=0;$i -lt $out.Count;$i++){ if($out[$i].Trim() -ceq "[BUG_KB_JSON_POINTER]"){ for($j=$i+1;$j -lt $out.Count;$j++){ if($out[$j].Trim().StartsWith("[")){ $insIdx=$j; break } } break } } if($insIdx -gt $out.Count){ $insIdx = $out.Count } # build fenced block WITHOUT literal backticks in strings $bt = [char]96 $fxOpen = ([string]$bt) + ([string]$bt) + ([string]$bt) + "json" $fxClose = ([string]$bt) + ([string]$bt) + ([string]$bt) $jsonNorm = $jsonRaw -replace "`r`n","`n" $jLines = $jsonNorm -split "`n" $blk = New-Object System.Collections.Generic.List[string] $blk.Add("[BUG_KB_JSON]") | Out-Null $blk.Add($fxOpen) | Out-Null foreach($l in $jLines){ $blk.Add($l) | Out-Null } $blk.Add($fxClose) | Out-Null # insert if($insIdx -lt $out.Count){ $head = @(); for($x=0;$x -lt $insIdx;$x++){ $head += $out[$x] } $tail = @(); for($x=$insIdx;$x -lt $out.Count;$x++){ $tail += $out[$x] } $new = @(); $new += $head; $new += $blk; $new += $tail } else { $new = @(); foreach($x in $out){ $new += $x }; foreach($x in $blk){ $new += $x } } $txt = ($new -join "`r`n") $enc = New-Object System.Text.UTF8Encoding($false) if(Test-Path -LiteralPath $boot){ Copy-Item -LiteralPath $boot -Destination ($boot + "." + (Get-Date).ToString("yyyyMMdd_HHmmss") + ".bak") -Force } [IO.File]::WriteAllText($boot,$txt,$enc) try{ $sha=(Get-FileHash -Algorithm SHA256 -LiteralPath $boot).Hash }catch{ $sha="" } Write-Host ("[PATCH-OK] bootpack.txt updated sha256={0}" -f $sha) exit 0