Private/Get-BinaryArchitecture.ps1

function Get-BinaryArchitecture {
    param([string]$Path)

    if (-not (Test-Path -LiteralPath $Path)) {
        return [pscustomobject]@{
            Type          = "Missing"
            Architectures = ""
            RosettaNeeded = $false
            Status        = "missing"
            Reason        = "Datei nicht vorhanden"
        }
    }

    $item = Get-Item -LiteralPath $Path -Force

    if ($item.PSIsContainer) {
        return [pscustomobject]@{
            Type          = "Directory"
            Architectures = ""
            RosettaNeeded = $false
            Status        = "unknown"
            Reason        = "Verzeichnis"
        }
    }

    $fileInfo = /usr/bin/file -b "$Path" 2>$null

    if ($fileInfo -notmatch "Mach-O") {
        return [pscustomobject]@{
            Type          = "Non-Mach-O"
            Architectures = ""
            RosettaNeeded = $false
            Status        = "unknown"
            Reason        = "Keine native macOS-Binary"
        }
    }

    # Optimierung: Architektur direkt aus "file -b" lesen.
    # lipo wird nur noch fuer Universal-Binaries benoetigt (~20% der Faelle).
    if ($fileInfo -match "universal binary") {
        # Fat binary — lipo liefert die vollstaendige Arch-Liste
        $archs     = /usr/bin/lipo -archs "$Path" 2>$null
        $archsText = ($archs -join " ").Trim()
    }
    elseif ($fileInfo -match "\bx86_64\b") {
        $archsText = "x86_64"
    }
    elseif ($fileInfo -match "\barm64\b") {
        $archsText = "arm64"
    }
    else {
        # Unbekannte Arch im file-Output — lipo als Fallback
        $archs     = /usr/bin/lipo -archs "$Path" 2>$null
        $archsText = ($archs -join " ").Trim()
    }

    $hasX64 = $archsText -match "(^|\s)x86_64(\s|$)"
    $hasArm = $archsText -match "(^|\s)arm64(e)?(\s|$)"

    if ($hasX64 -and -not $hasArm) {
        return [pscustomobject]@{
            Type          = "Mach-O"
            Architectures = $archsText
            RosettaNeeded = $true
            Status        = "rosetta"
            Reason        = "Intel-only Binary"
        }
    }

    if ($hasX64 -and $hasArm) {
        return [pscustomobject]@{
            Type          = "Mach-O"
            Architectures = $archsText
            RosettaNeeded = $false
            Status        = "universal"
            Reason        = "Universal Binary"
        }
    }

    if ($hasArm -and -not $hasX64) {
        return [pscustomobject]@{
            Type          = "Mach-O"
            Architectures = $archsText
            RosettaNeeded = $false
            Status        = "native"
            Reason        = "Native Apple-Silicon-Binary"
        }
    }

    return [pscustomobject]@{
        Type          = "Mach-O"
        Architectures = $archsText
        RosettaNeeded = $false
        Status        = "unknown"
        Reason        = "Architektur nicht eindeutig"
    }
}