Volver al índice

publicar-servidor-raps.ps1

param([switch]$SinPruebas, [switch]$SoloJar)
$ErrorActionPreference = 'Stop'

# VALIDACIÓN: exigir el mismo código compilado y probado antes de tocar Exaroton.
$pythonPath = 'C:\Users\toto83571\AppData\Local\Programs\Python\Python311\python.exe'
if (-not $SinPruebas) {
    & $pythonPath (Join-Path $PSScriptRoot 'docs/publicar-raps.py') --check-server-ready
    if ($LASTEXITCODE -ne 0) { throw 'La entrega actual no tiene todas las comprobaciones requeridas.' }
}

# PARCHE EXCLUSIVO DE SERVIDOR: conservar el contrato del cliente actualmente instalado.
if ($SoloJar) {
    & $pythonPath (Join-Path $PSScriptRoot 'docs/pruebas/comprobar-cliente-estable.py') `
        (Join-Path $PSScriptRoot '../mods/Raps.jar') (Join-Path $PSScriptRoot 'build/libs/Raps.jar') `
        --report (Join-Path $PSScriptRoot 'build/publicacion/cliente-estable-solo-jar.json')
    if ($LASTEXITCODE -ne 0) { throw 'El parche requiere cambiar el cliente; no se publicó.' }
}

# PUBLICACIÓN: el puente sólo escribe con el servidor apagado y conserva respaldo.
$bridgePath = Join-Path $env:LOCALAPPDATA 'CodexTools/ExarotonBridge/ExarotonBridge.ps1'
. $bridgePath -Command help
Assert-ServerOffline
$serverId = (Get-Settings).serverId

# BIBLIOTECA GUARDADA: verificar copias privadas antes de reemplazar el mod.
# Este control también se ejecuta con -SinPruebas; no consulta modelos ni inicia un mundo.
$librarySnapshot = Join-Path $env:LOCALAPPDATA ('Raps/validacion/Tools-publicacion-' + [Guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $librarySnapshot | Out-Null
try {
    $learningInfo = Get-RemoteInfoOrNull 'config/raps/Tools/learning.json'
    if ($null -ne $learningInfo) {
        Invoke-ExarotonApi -Method GET -Route "servers/$serverId/files/data/config/raps/Tools/learning.json/" -DownloadFile (Join-Path $librarySnapshot 'learning.json') | Out-Null
        Invoke-ExarotonApi -Method GET -Route "servers/$serverId/files/data/config/raps/Tools/certification.key/" -DownloadFile (Join-Path $librarySnapshot 'certification.key') | Out-Null
        & (Join-Path $PSScriptRoot 'verificar-biblioteca-raps.ps1') -ToolSnapshot $librarySnapshot
    } else {
        & (Join-Path $PSScriptRoot 'verificar-biblioteca-raps.ps1')
    }
    if (-not $?) { throw 'La biblioteca persistente no es compatible; no se reemplazó el JAR.' }
} finally {
    # Sólo archivos de nombres fijos dentro de la carpeta privada creada por esta publicación.
    foreach ($snapshotName in @('learning.json', 'certification.key')) {
        $snapshotFile = Join-Path $librarySnapshot $snapshotName
        if (Test-Path -LiteralPath $snapshotFile) { Remove-Item -LiteralPath $snapshotFile -Force }
    }
    Remove-Item -LiteralPath $librarySnapshot
}

$jarPath = Join-Path $PSScriptRoot 'build/libs/Raps.jar'
$expected = (Get-FileHash -LiteralPath $jarPath -Algorithm SHA256).Hash
$libraryEvidence = Get-Content -LiteralPath (Join-Path $PSScriptRoot 'build/publicacion/biblioteca-verificada.json') -Raw | ConvertFrom-Json
if ($libraryEvidence.status -ne 'passed' -or $libraryEvidence.sha256 -ne $expected.ToLowerInvariant()) {
    throw 'El JAR a subir no coincide con el que pasó la prueba de biblioteca.'
}
& $bridgePath -Command push -Arguments @($jarPath, 'mods/Raps.jar')
if (-not $?) { throw 'El puente no confirmó la subida del JAR.' }

# VERIFICACIÓN: descargar la copia real, comprobar bytes y conservar evidencia.
$verifiedPath = Join-Path $PSScriptRoot 'dist/Raps-verificado-servidor.jar'
New-Item -ItemType Directory -Path (Split-Path $verifiedPath -Parent) -Force | Out-Null
Invoke-ExarotonApi -Method GET -Route "servers/$serverId/files/data/mods/Raps.jar/" -DownloadFile $verifiedPath | Out-Null
$actual = (Get-FileHash -LiteralPath $verifiedPath -Algorithm SHA256).Hash
if ($actual -ne $expected) { throw 'La copia de Exaroton no coincide con el JAR validado.' }
$evidence = @{ status = 'verified'; sha256 = $expected.ToLowerInvariant(); server = 'dontstarve.exaroton.me'; verified_at = (Get-Date -Format o) }
$evidence.server_tests = $(if ($SinPruebas) { 'skipped' } else { 'passed' })
[IO.File]::WriteAllText((Join-Path $PSScriptRoot 'build/publicacion/exaroton.json'), ($evidence | ConvertTo-Json), [Text.UTF8Encoding]::new($false))

# CONFIGURACIÓN: publicar instrucciones y permisos, conservando el presupuesto consumido.
if (-not $SoloJar) {
    & (Join-Path $PSScriptRoot 'publicar-config-raps.ps1')
    if (-not $?) { throw 'La configuración no quedó verificada; revisar el registro antes de iniciar el servidor.' }
}
Write-Output "Exaroton actualizado y verificado: $expected"