Set-StrictMode -Version Latest function Assert-JsonHasNoDuplicateKeys { param([Parameter(Mandatory)][string]$Path) $document = [System.Text.Json.JsonDocument]::Parse([System.IO.File]::ReadAllText($Path)) try { function Test-Element([System.Text.Json.JsonElement]$Element, [string]$Location) { if ($Element.ValueKind -eq [System.Text.Json.JsonValueKind]::Object) { $seen = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::Ordinal) foreach ($property in $Element.EnumerateObject()) { if (-not $seen.Add($property.Name)) { throw "Duplicate JSON key '$($property.Name)' at $Location in '$Path'." } Test-Element $property.Value "$Location.$($property.Name)" } } elseif ($Element.ValueKind -eq [System.Text.Json.JsonValueKind]::Array) { $index = 0 foreach ($item in $Element.EnumerateArray()) { Test-Element $item "$Location[$index]" $index++ } } } Test-Element $document.RootElement '$' } finally { $document.Dispose() } } function Assert-AllowedProperties { param($Object, [string[]]$Allowed, [string]$Location) foreach ($property in $Object.PSObject.Properties.Name) { if ($property -eq '$schema') { continue } if ($property -notin $Allowed) { throw "Unknown field '$property' at $Location." } } } function Get-OptionalProperty { param($Object, [string]$Name, $Default = $null) $property = $Object.PSObject.Properties[$Name] if ($null -eq $property) { return $Default } return $property.Value } function Assert-ItemList { param($Items, [ValidateSet('package', 'config')][string]$Kind, [string]$Location) if ($null -eq $Items) { return } if ($Items -isnot [System.Array]) { throw "$Location must be an array." } $keys = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase) $index = 0 foreach ($item in $Items) { if ($Kind -eq 'package') { Assert-AllowedProperties $item @('id', 'source', 'scope', 'version', 'update', 'enabled') "$Location[$index]" if ([string]::IsNullOrWhiteSpace([string](Get-OptionalProperty $item 'id'))) { throw "$Location[$index].id is required." } $source = [string](Get-OptionalProperty $item 'source' 'winget') $key = "$source/$($item.id)" $scope = Get-OptionalProperty $item 'scope' if ($null -ne $scope -and $scope -notin @('user', 'machine')) { throw "Invalid package scope at $Location[$index]." } $update = [string](Get-OptionalProperty $item 'update' 'latest') if ($update -notin @('latest', 'pinned', 'install-only')) { throw "Invalid update policy at $Location[$index]." } if ($update -eq 'pinned' -and [string]::IsNullOrWhiteSpace([string](Get-OptionalProperty $item 'version'))) { throw "Pinned package at $Location[$index] requires a version." } } else { Assert-AllowedProperties $item @('name', 'source', 'target', 'mode', 'requiresPackage', 'enabled') "$Location[$index]" if ([string]::IsNullOrWhiteSpace([string](Get-OptionalProperty $item 'name'))) { throw "$Location[$index].name is required." } $key = [string]$item.name $enabled = [bool](Get-OptionalProperty $item 'enabled' $true) if ($enabled) { if ([string]::IsNullOrWhiteSpace([string](Get-OptionalProperty $item 'source'))) { throw "$Location[$index].source is required." } if ([string]::IsNullOrWhiteSpace([string](Get-OptionalProperty $item 'target'))) { throw "$Location[$index].target is required." } if ((Get-OptionalProperty $item 'mode' 'copy') -notin @('copy', 'ini-overlay', 'registry', 'hosts-block')) { throw "Unsupported config mode at $Location[$index]." } } } if (-not $keys.Add($key)) { throw "Duplicate $Kind key '$key' in $Location." } $index++ } } function Read-ManifestFile { param([Parameter(Mandatory)][string]$Path, [ValidateSet('profile', 'machine')][string]$Kind) if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { throw "Missing $Kind manifest '$Path'." } Assert-JsonHasNoDuplicateKeys -Path $Path try { $manifest = Get-Content -LiteralPath $Path -Raw | ConvertFrom-Json -Depth 32 } catch { throw "Invalid JSON in '$Path': $($_.Exception.Message)" } Assert-AllowedProperties $manifest @('schemaVersion', 'name', 'description', 'profiles', 'packages', 'configs') $Path if ((Get-OptionalProperty $manifest 'schemaVersion') -ne 1) { throw "Unsupported schemaVersion in '$Path'." } if ([string]::IsNullOrWhiteSpace([string](Get-OptionalProperty $manifest 'name'))) { throw "Manifest name is required in '$Path'." } if ($manifest.name -notmatch '^[a-z0-9][a-z0-9-]*$') { throw "Invalid manifest name '$($manifest.name)'." } if ($Kind -eq 'machine') { $profilesProperty = $manifest.PSObject.Properties['profiles'] $profiles = if ($null -ne $profilesProperty) { $profilesProperty.Value } else { $null } if ($profiles -isnot [System.Array] -or $profiles.Count -eq 0) { throw "Machine '$($manifest.name)' requires at least one profile." } $uniqueProfiles = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase) foreach ($profile in $profiles) { if ($profile -notmatch '^[a-z0-9][a-z0-9-]*$' -or -not $uniqueProfiles.Add([string]$profile)) { throw "Invalid or duplicate profile '$profile' in '$Path'." } } } foreach ($listName in @('packages', 'configs')) { $listProperty = $manifest.PSObject.Properties[$listName] if ($null -ne $listProperty -and $listProperty.Value -isnot [System.Array]) { throw "$Path.$listName must be an array." } } Assert-ItemList -Items @(Get-OptionalProperty $manifest 'packages' @()) -Kind package -Location "$Path.packages" Assert-ItemList -Items @(Get-OptionalProperty $manifest 'configs' @()) -Kind config -Location "$Path.configs" return $manifest } function Add-ManifestItems { param($Target, $Items, [ValidateSet('package', 'config')][string]$Kind) foreach ($item in @($Items)) { if ($Kind -eq 'package') { $source = [string](Get-OptionalProperty $item 'source' 'winget') $key = "$source/$($item.id)".ToLowerInvariant() } else { $key = ([string]$item.name).ToLowerInvariant() } $enabled = [bool](Get-OptionalProperty $item 'enabled' $true) if (-not $enabled) { $Target.Remove($key); continue } if ($Kind -eq 'package') { $normalized = [ordered]@{ id = [string]$item.id; source = $source; update = [string](Get-OptionalProperty $item 'update' 'latest') } foreach ($field in @('scope', 'version')) { $value = Get-OptionalProperty $item $field if ($null -ne $value) { $normalized[$field] = [string]$value } } } else { $normalized = [ordered]@{ name = [string]$item.name; source = [string]$item.source; target = [string]$item.target; mode = [string](Get-OptionalProperty $item 'mode' 'copy') } $requires = Get-OptionalProperty $item 'requiresPackage' if ($null -ne $requires) { $normalized.requiresPackage = [string]$requires } } $Target[$key] = [pscustomobject]$normalized } } function Resolve-MachineManifest { param([Parameter(Mandatory)][string]$RepositoryRoot, [Parameter(Mandatory)][string]$Machine) if ($Machine -notmatch '^[a-z0-9][a-z0-9-]*$') { throw "Invalid machine name '$Machine'." } $machinePath = Join-Path $RepositoryRoot "machines/$Machine.json" $machineManifest = Read-ManifestFile $machinePath machine if ($machineManifest.name -ne $Machine) { throw "Machine filename and manifest name do not match." } $packages = [ordered]@{} $configs = [ordered]@{} foreach ($profileName in $machineManifest.profiles) { $profilePath = Join-Path $RepositoryRoot "profiles/$profileName.json" $profile = Read-ManifestFile $profilePath profile if ($profile.name -ne $profileName) { throw "Profile filename and manifest name do not match for '$profileName'." } Add-ManifestItems -Target $packages -Items @(Get-OptionalProperty $profile 'packages' @()) -Kind package Add-ManifestItems -Target $configs -Items @(Get-OptionalProperty $profile 'configs' @()) -Kind config } Add-ManifestItems -Target $packages -Items @(Get-OptionalProperty $machineManifest 'packages' @()) -Kind package Add-ManifestItems -Target $configs -Items @(Get-OptionalProperty $machineManifest 'configs' @()) -Kind config $destinations = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase) foreach ($config in $configs.Values) { if (-not $destinations.Add($config.target)) { throw "Config destination collision at '$($config.target)'." } } [pscustomobject]@{ schemaVersion = 1; machine = $Machine; packages = @($packages.Values); configs = @($configs.Values) } } Export-ModuleMember -Function Read-ManifestFile, Resolve-MachineManifest