Public/Library/Add-EwLibraryApp.ps1

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

function Add-EwLibraryApp {
    <#
    .SYNOPSIS
        Saves an application and reusable filters in an initialized Evergreen library.
    .DESCRIPTION
        Registers an application without downloading artifacts. Filter values within
        a property are OR'd; properties are AND'd. Existing custom registration names,
        other settings and downloaded artifacts are preserved. Multiple named
        registrations can reference one Evergreen application. Identical filters
        return the existing registration instead of adding another copy.
    .PARAMETER Path
        Initialized library folder. Defaults to the configured LibraryPath.
    .PARAMETER Name
        Evergreen application identity, not its friendly display name.
    .PARAMETER Filter
        Property names mapped to selected values. At least one value is required.
        _DerivedType matches the file extension of URI, as in the Apps filters.
    .PARAMETER Replace
        Explicitly permits replacing the filter of an existing registration.
    .PARAMETER RegistrationName
        Unique Library registration name for a new filtered variant. With Replace,
        identifies the existing registration to update. Required for replacement
        when multiple registrations reference the Evergreen application.
    .PARAMETER ExpectedLibraryHash
        SHA256 of the reviewed library manifest. Rejects concurrent changes.
    .EXAMPLE
        Add-EwLibraryApp -Path C:\Library -Name MicrosoftEdge -Filter @{ Architecture = 'x64'; Channel = 'Stable' }
    .EXAMPLE
        Add-EwLibraryApp -Path C:\Library -Name MicrosoftEdge -RegistrationName EdgeBeta -Filter @{ Architecture = 'x64'; Channel = 'Beta' }
    .OUTPUTS
        System.Management.Automation.PSCustomObject
    .NOTES
        Supports WhatIf and Confirm. Use Update-EwLibrary later to download artifacts.
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([PSCustomObject])]
    param(
        [Parameter()][string]$Path,
        [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$Name,
        [Parameter(Mandatory)][Collections.IDictionary]$Filter,
        [Parameter()][switch]$Replace,
        [Parameter()][ValidateNotNullOrEmpty()][string]$RegistrationName,
        [Parameter()][string]$ExpectedLibraryHash
    )

    if ([string]::IsNullOrWhiteSpace($Path)) { $Path = [string](Get-EwSetting).LibraryPath }
    if ([string]::IsNullOrWhiteSpace($Path)) { throw 'Library path is not configured. Select an initialized library first.' }
    $parameters = @{ Path = $Path; Name = $Name; Filter = $Filter; Replace = $Replace }
    if ($PSBoundParameters.ContainsKey('RegistrationName')) { $parameters.RegistrationName = $RegistrationName }
    $plan = Get-EwLibraryRegistrationPlan @parameters
    if ($ExpectedLibraryHash -and $ExpectedLibraryHash -cne $plan.LibraryHash) {
        throw 'Library manifest changed. Review the current library and add again.'
    }
    if ($plan.Status -eq 'Updated' -and -not $Replace) {
        throw "Application '$($plan.Name)' already has different library filters. Use -Replace to replace them or -RegistrationName to add a new filtered variant."
    }
    if ($plan.Status -eq 'ChoiceRequired') {
        throw 'Specify -RegistrationName to add a uniquely named variant, or combine -RegistrationName and -Replace to select an existing registration.'
    }
    if ($PSCmdlet.ShouldProcess("$($plan.Name) in $($plan.Path)", "Save library registration ($($plan.Status))")) {
        Invoke-EwLibraryRegistration -Plan $plan
    }
}