Generic/Versioning.ps1

<#
.SYNOPSIS
    Gets the installed version of winget.
#>

function Get-WingetVersion {
    [CmdletBinding()]
    param()

    try {
        $version = winget --version 2>$null
        if ($LASTEXITCODE -eq 0 -and $version) {
            return $version.Trim()
        }
        return $null
    }
    catch {
        return $null
    }
}

function Get-SingBoxVersionInfo {
    param (
        [string]$ExecutablePath = "sing-box.exe"
    )

    # Run sing-box and capture output
    $output = & $ExecutablePath version 2>&1

    if (-not $output) {
        throw "Failed to retrieve version info from sing-box."
    }

    # Initialize hashtable
    $info = @{}

    foreach ($line in $output) {
        switch -Regex ($line) {
            '^sing-box version ([\d\.]+)' {
                $info["version"] = $matches[1]
                continue
            }
            '^Environment:\s*(.+)' {
                $info["environment"] = $matches[1]
                continue
            }
            '^Tags:\s*(.+)' {
                $info["tags"] = $matches[1] -split ','
                continue
            }
            '^Revision:\s*(.+)' {
                $info["revision"] = $matches[1]
                continue
            }
            '^CGO:\s*(.+)' {
                $info["cgo"] = $matches[1]
                continue
            }
        }
    }

    return $info
}