| | | 1 | | # Portions derived from PowerShell-Yaml (https://github.com/cloudbase/powershell-yaml) |
| | | 2 | | # Copyright (c) 2016–2024 Cloudbase Solutions Srl |
| | | 3 | | # Licensed under the Apache License, Version 2.0 (Apache-2.0). |
| | | 4 | | # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| | | 5 | | # Modifications Copyright (c) 2025 Kestrun Contributors |
| | | 6 | | |
| | | 7 | | <# |
| | | 8 | | .SYNOPSIS |
| | | 9 | | Converts a PowerShell object or hashtable to a YAML string. |
| | | 10 | | .DESCRIPTION |
| | | 11 | | The ConvertTo-KrYaml cmdlet converts a PowerShell object or hashtable to a |
| | | 12 | | YAML string. This is useful for serializing data in a human-readable format. |
| | | 13 | | .PARAMETER InputObject |
| | | 14 | | The PowerShell object or hashtable to convert. This parameter is mandatory and accepts input from the pipeline. |
| | | 15 | | .PARAMETER Options |
| | | 16 | | Specifies serialization options for the YAML output. This parameter is available in the 'Options' parameter set. |
| | | 17 | | .PARAMETER JsonCompatible |
| | | 18 | | If specified, the YAML output will be formatted to be compatible with JSON. This parameter is available in the 'NoOp |
| | | 19 | | .PARAMETER KeepArray |
| | | 20 | | If specified, the output will always be an array, even if there is only a single input object. By default, a single |
| | | 21 | | |
| | | 22 | | .NOTES |
| | | 23 | | This cmdlet requires PowerShell 7.0 or later. |
| | | 24 | | It uses the Kestrun.Utilities.Yaml library for YAML serialization. |
| | | 25 | | .EXAMPLE |
| | | 26 | | $obj = [PSCustomObject]@{ Name = "John"; Age = 30; Skills = @("PowerShell", "YAML") } |
| | | 27 | | $yaml = $obj | ConvertTo-KrYaml |
| | | 28 | | # Outputs the YAML representation of the object to the console. |
| | | 29 | | .EXAMPLE |
| | | 30 | | $obj = [PSCustomObject]@{ Name = "John"; Age = 30; Skills = @("PowerShell", "YAML") } |
| | | 31 | | $obj | ConvertTo-KrYaml -OutFile "output.yaml" -Force |
| | | 32 | | # Saves the YAML representation of the object to 'output.yaml', overwriting the file if it already exists. |
| | | 33 | | .EXAMPLE |
| | | 34 | | $obj = [PSCustomObject]@{ Name = "John"; Age = 30; Skills = @("PowerShell", "YAML") } |
| | | 35 | | $yaml = $obj | ConvertTo-KrYaml -JsonCompatible |
| | | 36 | | # Outputs the YAML representation of the object in a JSON-compatible format to the console. |
| | | 37 | | #> |
| | | 38 | | function ConvertTo-KrYaml { |
| | | 39 | | [KestrunRuntimeApi('Everywhere')] |
| | | 40 | | [OutputType([string])] |
| | | 41 | | [CmdletBinding(DefaultParameterSetName = 'NoOptions')] |
| | | 42 | | param( |
| | | 43 | | [Parameter(ValueFromPipeline = $true, Position = 0)] |
| | | 44 | | [System.Object]$InputObject, |
| | | 45 | | |
| | | 46 | | [Parameter(ParameterSetName = 'Options')] |
| | | 47 | | [Kestrun.Utilities.Yaml.SerializationOptions]$Options = [Kestrun.Utilities.Yaml.SerializationOptions]::Roundtrip |
| | | 48 | | |
| | | 49 | | [Parameter(ParameterSetName = 'NoOptions')] |
| | | 50 | | [switch]$JsonCompatible, |
| | | 51 | | |
| | | 52 | | [switch]$KeepArray |
| | | 53 | | ) |
| | | 54 | | begin { |
| | 1 | 55 | | $d = [System.Collections.Generic.List[object]]::new() |
| | | 56 | | } |
| | | 57 | | process { |
| | 1 | 58 | | if ($null -ne $InputObject) { |
| | 1 | 59 | | $d.Add($InputObject) |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | end { |
| | 1 | 63 | | if ($d -eq $null -or $d.Count -eq 0) { |
| | | 64 | | return |
| | | 65 | | } |
| | 2 | 66 | | if ($d.Count -eq 1 -and !($KeepArray)) { |
| | 1 | 67 | | $d = $d[0] |
| | | 68 | | } |
| | 1 | 69 | | $norm = Convert-PSObjectToGenericObject -InputObject $d |
| | | 70 | | |
| | 1 | 71 | | if ( $JsonCompatible.IsPresent) { |
| | | 72 | | # No indent options :~( |
| | 0 | 73 | | $Options = [Kestrun.Utilities.Yaml.SerializationOptions]::JsonCompatible |
| | | 74 | | } |
| | | 75 | | |
| | 1 | 76 | | $out = [Kestrun.Utilities.Yaml.YamlHelper]::ToYaml($norm, $Options) |
| | 1 | 77 | | return $out |
| | | 78 | | } |
| | | 79 | | } |