← Files Codex UsageARCHIVED FILE

scripts/hud.ps1

2.75 KB · Oct 2, 2026 · 00:34 UTC

↓ Download file

param(
    [Parameter(Mandatory)]
    [ValidateSet('Start', 'Stop', 'Status')]
    [string]$Action
)

$ErrorActionPreference = 'Stop'
$processName = 'CodexUsage.Desktop'
$pluginRoot = Split-Path -Parent $PSScriptRoot
$isWindowsHost = ($env:OS -eq 'Windows_NT') -or [bool]$IsWindows
$isWindowsX64 = $isWindowsHost -and (
    [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture -eq
        [System.Runtime.InteropServices.Architecture]::X64)
if (-not $isWindowsX64) {
    throw 'This public Codex plugin release currently supports Windows x64 local Codex desktop hosts only.'
}
$runtimeId = 'win-x64'
$executableName = 'CodexUsage.Desktop.exe'
$outputDirectory = Join-Path $pluginRoot (Join-Path 'bin' $runtimeId)
$executablePath = Join-Path $outputDirectory $executableName

function Get-HudProcess {
    Get-Process -Name $processName -ErrorAction SilentlyContinue | Where-Object {
        try {
            $_.Path -eq $executablePath -or $_.Path.StartsWith($pluginRoot, [StringComparison]::OrdinalIgnoreCase)
        }
        catch {
            $false
        }
    }
}

if ($Action -eq 'Status') {
    $running = @(Get-HudProcess)
    if ($running.Count -eq 0) { Write-Output 'Codex Usage is not running.' }
    else { Write-Output "Codex Usage is running (PID $($running[0].Id))." }
    exit 0
}

if ($Action -eq 'Stop') {
    $running = @(Get-HudProcess)
    if ($running.Count -eq 0) {
        Write-Output 'Codex Usage is already stopped.'
        exit 0
    }

    $processIds = @($running | ForEach-Object { $_.Id })
    foreach ($processId in $processIds) {
        Stop-Process -Id $processId -Force -ErrorAction SilentlyContinue
    }

    $deadline = [DateTime]::UtcNow.AddSeconds(5)
    do {
        $remaining = @($processIds | Where-Object {
            $null -ne (Get-Process -Id $_ -ErrorAction SilentlyContinue)
        })
        if ($remaining.Count -eq 0) { break }
        Start-Sleep -Milliseconds 100
    } while ([DateTime]::UtcNow -lt $deadline)

    if ($remaining.Count -gt 0) {
        throw "Codex Usage did not stop within 5 seconds (PID $($remaining -join ', '))."
    }
    Write-Output 'Codex Usage stopped.'
    exit 0
}

$running = @(Get-HudProcess)
if ($running.Count -gt 0) {
    Write-Output "Codex Usage is already running (PID $($running[0].Id))."
    exit 0
}

if (-not (Test-Path -LiteralPath $executablePath)) {
    throw "Bundled Codex Usage helper was not found at $executablePath. Run scripts/publish-package.ps1 before installation."
}

$startParameters = @{ FilePath = $executablePath; WorkingDirectory = $outputDirectory; PassThru = $true }
if ($isWindowsHost) { $startParameters.WindowStyle = 'Hidden' }
$process = Start-Process @startParameters
Write-Output "Codex Usage started (PID $($process.Id))."

SHA-256: 606a992f90cd341e71b62a5cbcb4e087ee098f753549c0aa3fe861c86f7faa8a