Modules/businessdev.ALbuild.Core/Public/Test-BcPlatform.ps1
|
function Test-BcPlatform { <# .SYNOPSIS Reports (or asserts) the host's capability to run Business Central container operations. .DESCRIPTION Business Central container images are Windows-only, so container operations require a Windows host with a reachable Docker CLI. This function detects the platform and Docker availability and returns a capability object. With -Require it throws a clear, actionable terminating error when the prerequisites are not met (used by container cmdlets to gate execution off-platform). .PARAMETER Require Throw if the host cannot run BC containers (not Windows, or Docker CLI unavailable). .PARAMETER Quiet Return a single boolean (CanRunContainers) instead of the capability object. .EXAMPLE if ((Test-BcPlatform).CanRunContainers) { New-BcContainer ... } .EXAMPLE Test-BcPlatform -Require # throws off Windows/Docker .OUTPUTS PSCustomObject with IsWindows, HasDocker, CanRunContainers, PSEdition, OSDescription; or System.Boolean when -Quiet is used. #> [CmdletBinding()] [OutputType([bool], [PSCustomObject])] param( [switch] $Require, [switch] $Quiet ) # $IsWindows only exists on PowerShell 6+. On Windows PowerShell 5.1 the host is always Windows. $isWindowsHost = if ($PSVersionTable.PSVersion.Major -lt 6) { $true } else { [bool]$IsWindows } $hasDocker = [bool](Get-Command -Name 'docker' -CommandType Application -ErrorAction SilentlyContinue) $canRun = $isWindowsHost -and $hasDocker $result = [PSCustomObject]@{ IsWindows = $isWindowsHost HasDocker = $hasDocker CanRunContainers = $canRun PSEdition = $PSVersionTable.PSEdition OSDescription = [System.Runtime.InteropServices.RuntimeInformation]::OSDescription } if ($Require -and -not $canRun) { $reasons = @() if (-not $isWindowsHost) { $reasons += 'the host is not Windows (Business Central container images are Windows-only)' } if (-not $hasDocker) { $reasons += 'the Docker CLI was not found on PATH' } throw "Business Central container operations are not available because $($reasons -join ' and '). Run on a Windows host with Docker, or use a remote Docker host / containerless compile (Invoke-BcCompiler -Engine AlTool)." } if ($Quiet) { return $canRun } return $result } |