private/Select-OSDeployCoreBuildScript.ps1

#Requires -PSEdition Core

function Select-OSDeployCoreBuildScript {
    <#
    .SYNOPSIS
        Selects scripts for an OSDeploy Boot build
 
    .DESCRIPTION
        Enumerates PowerShell files from boot-winpescript and boot-mediascript directories
        in OSDeploy Boot-Assets and available OSDeploy, OSDCloud, and OSD module roots.
        Discovery includes files one directory below each script-type root. Results are
        displayed in an Out-GridView multi-selection picker.
 
        When Architecture is specified, filenames containing the opposite architecture are
        excluded. Canceling the picker returns no object. This function does not execute or
        modify selected scripts.
 
    .PARAMETER Architecture
        Limits results to amd64 or arm64 by excluding filenames that contain the opposite
        architecture. When omitted, no filename architecture filter is applied.
 
    .EXAMPLE
        PS> Select-OSDeployCoreBuildScript -Architecture arm64
 
        Displays eligible arm64 app, WinPE, and media build scripts for multi-selection.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        System.Management.Automation.PSCustomObject[]. Returns selected objects with Type,
        Name, FullName, and LastWriteTime properties, or no object when none are selected.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
        Version: 1.0.0
        Date: 2026-08-28
 
        OSDCloud and OSD roots are searched only when their module path commands are available.
 
    .LINK
        Build-OSDeployBoot
    #>

    [CmdletBinding()]
    param (
        [ValidateSet('amd64', 'arm64')]
        [System.String]
        $Architecture
    )

    $moduleSearchRoots = @($Script:OSDeployModuleBase)

    $osdCloudBase = if (Get-Command -Name 'Get-OSDCloudModulePath' -ErrorAction SilentlyContinue) { Get-OSDCloudModulePath }
    if ($osdCloudBase) {
        $moduleSearchRoots += $osdCloudBase
    }

    $osdBase = if (Get-Command -Name 'Get-OSDModulePath' -ErrorAction SilentlyContinue) { Get-OSDModulePath }
    if ($osdBase) {
        $moduleSearchRoots += $osdBase
    }

    $scriptTypes = @(
        [PSCustomObject]@{ Type = 'boot-winpescript'; BootAssetsFolder = 'boot-winpescript'; ModuleFolder = (Join-Path 'core' 'boot-winpescript') }
        [PSCustomObject]@{ Type = 'boot-mediascript'; BootAssetsFolder = 'boot-mediascript'; ModuleFolder = (Join-Path 'core' 'boot-mediascript') }
    )
    $scriptItems = @()

    foreach ($scriptType in $scriptTypes) {
        $typePaths = @(
            Join-Path $Script:OSDeployBootAssetsPath $scriptType.BootAssetsFolder
            foreach ($root in $moduleSearchRoots) {
                Join-Path $root $scriptType.ModuleFolder
            }
        )

        foreach ($typePath in $typePaths) {
            if (Test-Path -LiteralPath $typePath) {
                $scriptItems += Get-ChildItem -LiteralPath $typePath -Filter '*.ps1' -File -Recurse -Depth 1 -ErrorAction SilentlyContinue |
                    Select-Object @{Name = 'Type'; Expression = { $scriptType.Type } },
                        Name,
                        FullName,
                        LastWriteTime
            }
        }
    }

    if ($Architecture) {
        $oppositeArch = if ($Architecture -eq 'amd64') { 'arm64' } else { 'amd64' }
        $scriptItems = $scriptItems | Where-Object { $_.Name -notmatch $oppositeArch }
    }

    if ($scriptItems -and $scriptItems.Count -gt 0) {
        Write-HostDateTimeDarkGray 'Select WinPE Scripts to execute during the build (Cancel to skip)'
        $selected = $scriptItems |
            Out-GridView -PassThru -Title 'Select WinPE Scripts to execute during the build (Cancel to skip)'

        return $selected
    }

    return $null
}