Functions/GenXdev.Software/EnsurePlaywright.ps1

<##############################################################################
Part of PowerShell module : GenXdev.Software
Original cmdlet filename : EnsurePlaywright.ps1
Original author : René Vaessen / GenXdev
Version : 3.28.2026
################################################################################
Copyright (c) 2026 René Vaessen / GenXdev

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
################################################################################>


###############################################################################
<#
.SYNOPSIS
Ensures the Playwright .NET assembly and browser binaries are available.

.DESCRIPTION
Internal helper that downloads and loads the Microsoft.Playwright NuGet
assembly and then installs the actual browser binaries (Chromium, Firefox,
WebKit) via Playwright's built-in CLI. Idempotent — skips steps that are
already completed.

Called by Connect-PlaywrightViaDebuggingPort, Open-PlayWrightBrowser,
and explicitly from EnsureGenXdev during environment setup.
#>

function EnsurePlaywright {

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')]

    param(
        ########################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Automatically consent to Playwright browser installation and set persistent flag.'
        )]
        [switch] $AutoConsent,
        ########################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Automatically consent to ALL third-party software installations and set persistent flag for all packages.'
        )]
        [switch] $AutoConsentAllPackages,
        ########################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = ('Only auto consent during session')
        )]
        [switch]$SessionOnly
        ########################################################################
    )

    begin {
        Microsoft.PowerShell.Utility\Write-Verbose 'Ensuring Playwright environment is ready...'

        # track whether browser binaries need to be installed
        $script:needBrowserInstall = $false
    }

    process {


        ########################################################################
        # Step 1: Ensure Microsoft.Playwright .NET assembly is loaded
        ########################################################################

        $assemblyAlreadyLoaded = $null -ne (
            [System.AppDomain]::CurrentDomain.GetAssemblies() |
                Microsoft.PowerShell.Core\Where-Object {
                    $PSItem.GetName().Name -eq 'Microsoft.Playwright'
                }
        )

        if (-not $assemblyAlreadyLoaded) {

            Microsoft.PowerShell.Utility\Write-Verbose (
                'Microsoft.Playwright assembly not loaded. Downloading via EnsureNuGetAssembly...'
            )

            $ensureParams = GenXdev\Copy-IdenticalParamValues `
                -BoundParameters $PSBoundParameters `
                -FunctionName 'GenXdev\EnsureNuGetAssembly'
            $null = GenXdev\EnsureNuGetAssembly @ensureParams -PackageKey 'Microsoft.Playwright' -TypeName 'Microsoft.Playwright.Playwright'

            Microsoft.PowerShell.Utility\Write-Verbose (
                'Microsoft.Playwright assembly loaded successfully.'
            )
            $script:needBrowserInstall = $true
        }
        else {

            Microsoft.PowerShell.Utility\Write-Verbose (
                'Microsoft.Playwright assembly already loaded.'
            )
        }

        ########################################################################
        # Step 2: Ensure browser binaries are installed
        ########################################################################

        # determine the Playwright cache directory for browser binaries
        $playwrightCacheDir = Microsoft.PowerShell.Management\Join-Path `
            -Path $env:LOCALAPPDATA `
            -ChildPath 'ms-playwright'

        # check if Chromium binary exists as a proxy for all browsers
        $browsersAlreadyInstalled = $false

        if (Microsoft.PowerShell.Management\Test-Path -LiteralPath $playwrightCacheDir) {

            $existingChromium = Microsoft.PowerShell.Management\Get-ChildItem `
                -LiteralPath $playwrightCacheDir `
                -Directory `
                -Filter 'chromium-*' `
                -ErrorAction SilentlyContinue

            if ($existingChromium -and $existingChromium.Count -gt 0) {

                $browsersAlreadyInstalled = $true
                Microsoft.PowerShell.Utility\Write-Verbose (
                    "Playwright browser binaries found at: ${playwrightCacheDir}"
                )
            }
        }

        if ((-not $browsersAlreadyInstalled) -or $script:needBrowserInstall) {

            Microsoft.PowerShell.Utility\Write-Verbose (
                'Installing Playwright browser binaries (Chromium, Firefox, WebKit)...'
            )

            Microsoft.PowerShell.Utility\Write-Verbose (
                'This may take several minutes on first run. Subsequent runs will skip this step.'
            )

            try {

                # invoke Playwright's built-in CLI to download browser binaries
                $exitCode = [Microsoft.Playwright.Program]::Main(@('install'))

                if ($exitCode -ne 0) {

                    Microsoft.PowerShell.Utility\Write-Warning (
                        "Playwright browser installation exited with code ${exitCode}. " +
                        'Some browsers may not be available.'
                    )
                }
                else {

                    Microsoft.PowerShell.Utility\Write-Verbose (
                        'Playwright browser binaries installed successfully.'
                    )
                }

                # verify browser binaries exist on disk
                if (Microsoft.PowerShell.Management\Test-Path -LiteralPath $playwrightCacheDir) {
                    $existingChromium = Microsoft.PowerShell.Management\Get-ChildItem `
                        -LiteralPath $playwrightCacheDir `
                        -Directory `
                        -Filter 'chromium-*' `
                        -ErrorAction SilentlyContinue

                    if (-not $existingChromium -or $existingChromium.Count -eq 0) {
                        Microsoft.PowerShell.Utility\Write-Warning (
                            'Playwright browser installation verification — ' +
                            'no Chromium binaries found on disk. ' +
                            'Web automation may not be fully available.'
                        )
                    }
                }
                else {
                    Microsoft.PowerShell.Utility\Write-Warning (
                        'Playwright browser installation verification — ' +
                        'cache directory not found.'
                    )
                }
            }
            catch {

                Microsoft.PowerShell.Utility\Write-Warning (
                    "Failed to install Playwright browser binaries: $($_.Exception.Message)"
                )
            }
        }
        else {

            Microsoft.PowerShell.Utility\Write-Verbose (
                'Playwright browser binaries already installed. Skipping download.'
            )
        }
    }

    end {

        Microsoft.PowerShell.Utility\Write-Verbose (
            'Playwright environment check complete.'
        )
    }
}