functions/serialization/Register-PSFTypeSerializationData.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function Register-PSFTypeSerializationData
{
<#
 .SYNOPSIS
  Registers serialization xml Typedata.
  
 .DESCRIPTION
  Registers serialization xml Typedata.
  Use Get-PSFTypeSerializationData to generate such a string.
  When building a module, consider shipping that xml type extension in a dedicated file as part of the module and import it as part of the manifest's 'TypesToProcess' node.
  
 .PARAMETER TypeData
  The data to register.
  Generate with Get-PSFTypeSerializationData.
  
 .PARAMETER Path
  Where the file should be stored before appending.
  While type extensions can be added at runtime directly from memory, from file is more reliable.
  By default, a PSFramework path is chosen.
  The default path can be configured under the 'PSFramework.Serialization.WorkingDirectory' confguration setting.
  
 .EXAMPLE
  PS C:\> Get-PSFTypeSerializationData -InputObject 'My.Custom.Type' | Register-PSFTypeSerializationData
  
  Generates a custom type serialization xml and registers it.
#>

    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline = $true, Mandatory = $true)]
        [string[]]
        $TypeData,
        
        [string]
        $Path = (Get-PSFConfigValue -FullName 'PSFramework.Serialization.WorkingDirectory' -Fallback $script:path_typedata)
    )
    
    begin
    {
        if (-not (Test-Path $Path -PathType Container))
        {
            $null = New-Item -Path $Path -ItemType Directory -Force
        }
    }
    process
    {
        foreach ($item in $TypeData)
        {
            $name = $item -split "`n" | Select-String "<Name>(.*?)</Name>" | Where-Object { $_ -notmatch "<Name>Deserialized.|<Name>PSStandardMembers</Name>|<Name>SerializationData</Name>" } | Select-Object -First 1 | ForEach-Object { $_.Matches[0].Groups[1].Value }
            $fullName = Join-Path $Path.Trim "$($name).Types.ps1xml"
            
            $item | Set-Content -Path $fullName -Force -Encoding UTF8
            Update-TypeData -AppendPath $fullName
        }
    }
    end
    {
    
    }
}