Private/check-prerequisites.ps1

<#
.SYNOPSIS
    Checks if the Azure CLI (az) and GitHub Copilot CLI are installed.

.DESCRIPTION
    Checks if 'az' and 'copilot' commands are available in the current path.
    Returns $true if installed, throws an error otherwise.

.EXAMPLE
    Invoke-CheckPrerequisites
#>

function Invoke-CheckPrerequisites {
    [CmdletBinding()]
    param()
    
    if (-not (Get-Command -Name 'az' -ErrorAction SilentlyContinue)) {
        throw "Azure CLI (az) is not installed or not in the PATH."
    }

    if (-not (Get-Command -Name 'copilot' -ErrorAction SilentlyContinue)) {
        throw "GitHub Copilot CLI (copilot) is not installed or not in the PATH."
    }

    if (-not (Get-Command -Name 'git' -ErrorAction SilentlyContinue)) {
        throw "Git is not installed or not in the PATH."
    }

    # Check if az is logged in
    try {
        $null = az account show 2>&1
    } catch {
        throw "Azure CLI (az) is not logged in. Please run 'az login'."
    }

    # Check if Repo Root exists
    if (-not $env:AzDevOpsRepoRoot -or -not (Test-Path $env:AzDevOpsRepoRoot)) {
        throw "Source code repository not found at '$($env:AzDevOpsRepoRoot)'. Please ensure the repository is checked out and AzDevOpsRepoRoot environment variable is set correctly."
    }

    return $true
}

# Entry point
if ($MyInvocation.InvocationName -eq $PSCommandPath) {
    # Called directly as a script
    try {
        $null = Invoke-CheckPrerequisites
        Write-Verbose "Prerequisites (az, copilot) are installed."
    } catch {
        Write-Error $_
        exit 1
    }
}