Modules/businessdev.ALbuild.Apps/Private/Get-BcAlRuntimeVersion.ps1

function Get-BcAlRuntimeVersion {
    <#
    .SYNOPSIS
        Returns the AL runtime version a Business Central version ships with.
 
    .DESCRIPTION
        The ONE place that maps a BC version to its AL runtime. Update-BcAppManifest stamps the result
        into app.json and Install-BcAlTool derives the required AL Tool major from it; the two must agree,
        or the manifest declares a runtime the selected compiler rejects with AL1043.
 
        Microsoft changed the numbering with BC 30:
 
          BC 12 .. 29 runtime = (BC major - 11).0 17 -> 6.0, 28 -> 17.0, 29 -> 18.0
                        BC 18.1 and later minors -> 7.1
          BC 30 and on runtime = BC major 30 -> 30.0, 31 -> 31.0
 
        Runtimes 19.0 .. 29.0 do not exist. The AL Tool package follows the runtime: 18.x for BC 29, then
        30.x for BC 30. Measured against AL Tool 30.0.42.60748-beta: runtime 18.0 and 30.0 compile, 19.0,
        20.0 and 29.0 raise AL1043.
 
        Pure helper, no I/O.
 
    .PARAMETER BcVersion
        The BC version - full ('30.0.55808.0') or major ('30').
 
    .EXAMPLE
        Get-BcAlRuntimeVersion -BcVersion '29.0.53586.0'
        # -> 18.0
 
    .EXAMPLE
        Get-BcAlRuntimeVersion -BcVersion '30'
        # -> 30.0
 
    .OUTPUTS
        System.Version (Major.Minor), or $null for a BC major before 12 (no AL runtime numbering).
    #>

    [CmdletBinding()]
    [OutputType([version])]
    param(
        [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $BcVersion
    )

    $bc = ConvertTo-BcVersion $BcVersion
    if ($bc.Major -lt 12) { return $null }
    if ($bc.Major -ge 30) { return [version]::new($bc.Major, 0) }
    if ($bc.Major -eq 18 -and $bc.Minor -ge 1) { return [version]'7.1' }
    return [version]::new($bc.Major - 11, 0)
}