public/Build-SfGlobalValueSetMetadata.ps1

function Build-SfGlobalValueSetMetadata {

    <#
        .SYNOPSIS
        Builds the *.globalValueSet-meta.xml files from the metadata model.

        .DESCRIPTION
        Processes the global value sets as given in table [_GlobalValueSets] in the metadata model.
        The picklist values will be collected from table [_PicklistValues] in the metadata model.

        Manifest accepts bare GlobalValueSet names (e.g. 'MyValueSet') as well as fully
        qualified 'Type:Name' entries (e.g. 'GlobalValueSet:MyValueSet'), and can be piped in.
        Each entry is parsed via ConvertTo-SfMetadataComponent with DefaultComponentType
        'GlobalValueSet'; entries with a format error, or whose Type is not 'GlobalValueSet'
        (e.g. from a combined manifest list shared across multiple Build-Sf*Metadata functions),
        are silently ignored. A 'GlobalValueSet' entry whose Name does not exist in table
        [_GlobalValueSets] produces a warning and is skipped.

        If Manifest is left empty (no pipeline input and no explicit value), all global
        value sets in the metadata model are built.

        .LINK
        Build-SfGlobalValueSetMetadataTranslation

        .LINK
        Import-SfMetadataModel

        .LINK
        https://codeberg.org/uwe-braig/sf-metadata-toolkit
    #>


    [CmdletBinding()]
    param (
        # The metadata model as imported from *.csv and/or *.xlsx with Import-SfMetadataModel
        [Parameter(Mandatory, Position = 0)]
        [PSCustomObject] $MetadataModel,

        # Manifest entries in 'Type:Name' notation. Accepts 'GlobalValueSet:Name' as well as bare
        # names (treated as GlobalValueSet). Other Types are ignored. If left empty, all will be
        # built. Can be supplied via the pipeline.
        [Parameter(ValueFromPipeline, Position = 1)]
        [string[]] $Manifest,

        # Target path for the resulting *.xml files. Default is [./force-app/main/default].
        [Parameter()]
        [string] $TargetPath = './force-app/main/default'
    )

    begin {
        $ErrorActionPreference = 'Stop'
        [bool] $IsDebug   = $DebugPreference   -ne 'SilentlyContinue'
        [bool] $IsVerbose = $VerbosePreference -ne 'SilentlyContinue'

        [int] $ReceivedCount = 0
        [Collections.Generic.List[PSCustomObject]] $ValueSetsToBuild = [Collections.Generic.List[PSCustomObject]]::new()

        $GlobalValueSetPath = Join-Path $TargetPath 'globalValueSets'

        [hashtable] $GlobalValueSetsByName = @{}
        foreach ($ValueSet in $MetadataModel.Tables._GlobalValueSets) {
            $GlobalValueSetsByName[$ValueSet._Name] = $ValueSet
        }
    }

    process {
        foreach ($RawEntry in $Manifest) {
            $ReceivedCount++

            $Component = ConvertTo-SfMetadataComponent -ComponentName $RawEntry -DefaultComponentType 'GlobalValueSet'

            if ($null -eq $Component) {
                continue
            }
            if ($Component.Type -ne 'GlobalValueSet') {
                continue
            }
            if (-not $GlobalValueSetsByName.ContainsKey($Component.Name)) {
                Write-Warning "[$($Component.Key)] Not found in metadata model."
                continue
            }
            $ValueSetsToBuild.Add($GlobalValueSetsByName[$Component.Name])
        }
    }

    end {
        if ($ReceivedCount -eq 0) {
            foreach ($ValueSet in $MetadataModel.Tables._GlobalValueSets) {
                $ValueSetsToBuild.Add($ValueSet)
            }
        }

        foreach ($ValueSet in $ValueSetsToBuild) {
            $xmlString = ConvertTo-SfGlobalValueSetXml -ValueSet $ValueSet -PicklistValues $MetadataModel.Tables._PicklistValues
            Set-MetadataXmlFile 'GlobalValueSet' $ValueSet._Name '.globalValueSet-meta.xml' $GlobalValueSetPath $xmlString
        }
    }
}