Private/Library/Get-EwLibraryInventory.ps1

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

function Get-EwLibraryInventory {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$Path
    )

    $library = Get-EwLibraryFileSnapshot -Root $Path -Path 'EvergreenLibrary.json'
    if ($null -eq $library.Data.PSObject.Properties['Applications']) {
        throw "Library manifest has no Applications property: $($library.Path)"
    }
    $inventory = @(
        foreach ($application in @($library.Data.Applications)) {
            $name = [string]$application.Name
            if ([string]::IsNullOrWhiteSpace($name) -or $name -in @('.', '..') -or
                $name.IndexOfAny([IO.Path]::GetInvalidFileNameChars()) -ge 0 -or $name.TrimEnd(' ', '.') -ne $name) {
                throw "Invalid application folder name in library manifest: '$name'"
            }
            $manifestPath = Get-EwLibrarySafePath -Root $Path -Path "$name\$name.json"
            $manifest = if (Test-Path -LiteralPath $manifestPath -PathType Leaf) {
                Get-EwLibraryFileSnapshot -Root $Path -Path $manifestPath
            } else {
                Write-Verbose -Message "EvergreenUI: Library registration $name is awaiting a library update"
                $null
            }
            [PSCustomObject]@{
                ApplicationName = $name
                ApplicationPath = [IO.Path]::GetDirectoryName($manifestPath)
                Versions = if ($null -ne $manifest) { @($manifest.Data) } else { @() }
                ManifestHash = if ($null -ne $manifest) { $manifest.Hash } else { '' }
                Status = if ($null -ne $manifest) { 'Available' } else { 'Awaiting library update' }
                LibraryHash = $library.Hash
            }
        }
    )
    if ((Get-EwLibraryFileSnapshot -Root $Path -Path 'EvergreenLibrary.json').Hash -ne $library.Hash) {
        throw 'Library changed while loading inventory. Reload the library.'
    }
    Write-Verbose -Message "EvergreenUI: Read $($inventory.Count) library applications from $Path"
    [PSCustomObject]@{ Library = $library.Data; Inventory = $inventory }
}