Start-DaggerEngineSession.ps1

function Start-DaggerEngineSession {
    [CmdletBinding()]
    param (
        [Parameter(Position = 0)]
        [int]
        $RetryLimit = 10
    )

    $bit = [System.Environment]::Is64BitOperatingSystem ? "amd64" : "386"

    $platform = switch ([System.Environment]::OSVersion.Platform) {
        "Win32NT" { "windows" }
        "Darwin" { "darwin" }
        "Unix" { "linux" }
    }

    $binaryName = 'dagger-engine-session_{0}_{1}' -f $platform, $bit

    $path = Join-Path -Path "$PSScriptRoot" -ChildPath "bin" -AdditionalChildPath $binaryName

    $startInfo = New-Object System.Diagnostics.ProcessStartInfo
    $startInfo.FileName = $path
    $startInfo.RedirectStandardError = $true
    $startInfo.RedirectStandardOutput = $true
    $startInfo.RedirectStandardInput = $true
    $startInfo.UseShellExecute = $false

    $engineProcess = New-Object System.Diagnostics.Process
    $engineProcess.StartInfo = $startInfo
    $startResult = $engineProcess.Start()

    if ($startResult) {
        $i = 0
        do {
            $output = $engineProcess.StandardOutput.ReadLine()
            Start-Sleep -Milliseconds 250
        } until ($output -match "\d{4,5}" -or $i++ -ge $RetryLimit)
    }
    if ($output -notmatch "\d{4,5}") {
        Write-Error -Message "Failed to start `"$Path`". No TCP Port available." -Category InvalidResult -RecommendedAction "Check that the user has access to run `"$Path`"."
    }
    Write-Output $output
}