Files
TDC_R2C_Drawing/launch-viewer.ps1
T

42 lines
1.8 KiB
PowerShell

$ErrorActionPreference = 'Stop'
$url = 'http://127.0.0.1:8000/'
$serverOutput = Join-Path $env:TEMP 'r2c-viewer-server.log'
$serverError = Join-Path $env:TEMP 'r2c-viewer-server-error.log'
function Test-Viewer {
try {
$model = Invoke-RestMethod -Uri ($url + 'model.json') -TimeoutSec 10
return ($null -ne $model.meshes -and $null -ne $model.variants)
} catch { return $false }
}
try {
if (-not (Test-Viewer)) {
# Explorer exposes WSL folders as either of these UNC paths.
if ($PSScriptRoot -match '^\\\\(?:wsl\$|wsl\.localhost)\\([^\\]+)\\(.*)$') {
$distro = $Matches[1]
$linuxPath = '/' + $Matches[2].Replace('\', '/')
$wslArgs = @('-d', $distro, '--cd', ('"' + $linuxPath + '"'), '--', 'python3', 'r2c.py', 'serve')
} else {
$linuxPath = & wsl.exe -- wslpath -u $PSScriptRoot
if ($LASTEXITCODE -ne 0) { throw 'Cannot locate the project in WSL.' }
$wslArgs = @('--cd', ('"' + $linuxPath.Trim() + '"'), '--', 'python3', 'r2c.py', 'serve')
}
$server = Start-Process -FilePath 'wsl.exe' -ArgumentList $wslArgs -PassThru -RedirectStandardOutput $serverOutput -RedirectStandardError $serverError
$ready = $false
for ($attempt = 0; $attempt -lt 30; $attempt++) {
if (Test-Viewer) { $ready = $true; break }
if ($server.HasExited) {
$details = Get-Content $serverError -Raw -ErrorAction SilentlyContinue
throw ('The viewer server exited: ' + $details)
}
Start-Sleep -Milliseconds 500
}
if (-not $ready) { throw 'The viewer did not respond at http://localhost:8000/. Check WSL and port 8000.' }
}
Start-Process $url
} catch {
Write-Host $_.Exception.Message -ForegroundColor Red
exit 1
}