public/Build-SfStandardValueSetMetadata.ps1

function Build-SfStandardValueSetMetadata {

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

        .DESCRIPTION
        Processes the list of standard value sets as given in $MetadataModel.Tables._StandardValueSets.
        The picklist values are collected from $MetadataModel.PicklistValues, filtered by
        [ValueSetType] = 'StandardValueSet' and matching [ValueSetName].

        The following columns are used (as populated by the metadata model import):
        - [ValueSetType] must equal 'StandardValueSet'.
        - [ValueSetName] must match the standard value set name.
        - [Flags] can be either 'X', 'X+D' or '--' for 'active', 'active and default' or 'inactive'
        - [PicklistAPIValue]
        - [PicklistLabel] untranslated label. If no label is given, the value in [PicklistAPIValue] will be used.

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

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

        .EXAMPLE
        Build-SfStandardValueSetMetadata -MetadataModel $MetadataModel

        Builds all StandardValueSets as found in $MetadataModel.Tables._StandardValueSets.

        .EXAMPLE
        Build-SfStandardValueSetMetadata -MetadataModel $MetadataModel -Manifest AccountOwnership, Salutation

        Builds only the 2 given standard value sets, if found in $MetadataModel.Tables._StandardValueSets.

        .LINK
        Build-SfStandardValueSetTranslationMetadata

        .LINK
        Build-SfGlobalValueSetMetadata

        .LINK
        Online version: 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 'StandardValueSet:Name' as well as
        # bare names (case sensitive, treated as StandardValueSet). 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()

        $ValueSetPath = Join-Path $TargetPath 'standardValueSets'

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

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

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

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

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

        foreach ($ValueSet in $ValueSetsToBuild) {
            $xmlString = ConvertTo-SfStandardValueSetXml -ValueSet $ValueSet -MetadataModel $MetadataModel
            Set-MetadataXmlFile 'StandardValueSet' $ValueSet._Name '.standardValueSet-meta.xml' $ValueSetPath $xmlString
        }
    }
}