Modules/businessdev.ALbuild.Core/Public/Assert-ALbuildLicensed.ps1

function Assert-ALbuildLicensed {
    <#
    .SYNOPSIS
        Throws unless a valid ALbuild license is present for the given feature.
 
    .DESCRIPTION
        The single licensing gate that licensed cmdlets call on entry (e.g. Marketplace, OnPrem,
        the runtime-package engine, cross-feed transitive resolution and AL validation). Free
        features never call it. If the license is invalid - or a trial has expired - it throws a
        terminating error with remediation guidance. An imminent trial expiry logs a warning but
        does not block.
 
    .PARAMETER Feature
        Name of the licensed feature requesting access (used in messages/telemetry).
 
    .PARAMETER TenantId
        Optional tenant override (defaults as Test-ALbuildLicense).
 
    .PARAMETER TenantName
        Optional tenant name override.
 
    .EXAMPLE
        Assert-ALbuildLicensed -Feature 'RuntimePackages'
 
    .OUTPUTS
        None. Throws on failure.
    #>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseApprovedVerbs', '',
        Justification = 'Assert- is an established guard-clause convention; the cmdlet validates and throws.')]
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string] $Feature,

        [string] $TenantId = $env:System_CollectionId,
        [string] $TenantName = $env:System_CollectionUri
    )

    $license = Test-ALbuildLicense -TenantId $TenantId -TenantName $TenantName

    if (-not $license.IsValid) {
        throw "The ALbuild feature '$Feature' requires a valid license. $($license.Reason) Acquire a license at https://365businessdev.com or set the tenant explicitly."
    }

    if ($license.IsTrial -and $license.ExpiresOn) {
        $now = Get-Date
        if ($license.ExpiresOn -lt $now) {
            throw "The ALbuild trial license for tenant '$($license.TenantId)' expired on $($license.ExpiresOn.ToString('u')). Feature '$Feature' is unavailable. Acquire a license at https://365businessdev.com."
        }
        if ($license.ExpiresOn -lt $now.AddDays(10)) {
            Write-ALbuildLog -Level Warning "ALbuild trial license expires on $($license.ExpiresOn.ToString('d')) (feature '$Feature'). Acquire a license at https://365businessdev.com."
        }
    }
}