Skip to content

Commit abe7ce2

Browse files
committed
Add compress support to PSAppDeployToolkit.CodeGen.PowerShellSyntaxSerializer.
1 parent 3921aff commit abe7ce2

2 files changed

Lines changed: 74 additions & 6 deletions

File tree

src/PSADT/PSAppDeployToolkit/CodeGen/PowerShellSyntaxSerializer.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ namespace PSAppDeployToolkit.CodeGen
3434
/// It is typically used to generate PowerShell code or scripts that recreate the serialized objects.</remarks>
3535
public static class PowerShellSyntaxSerializer
3636
{
37+
/// <summary>
38+
/// Serializes the specified object to a PowerShell-compatible string representation.
39+
/// </summary>
40+
/// <remarks>The returned string can be used in PowerShell scripts to recreate the original object
41+
/// structure, where supported.</remarks>
42+
/// <param name="value">The object to serialize. Can be null.</param>
43+
/// <returns>A string containing the PowerShell syntax representation of the specified object. Returns an empty string if
44+
/// the value is null.</returns>
45+
public static string Serialize(object? value)
46+
{
47+
PowerShellSyntaxWriter writer = new();
48+
WriteValue(value, writer);
49+
return writer.ToString();
50+
}
51+
3752
/// <summary>
3853
/// Serializes the specified value to a PowerShell-formatted string.
3954
/// </summary>
@@ -47,6 +62,19 @@ public static string Serialize(object? value, string indentChars = " ")
4762
return writer.ToString();
4863
}
4964

65+
/// <summary>
66+
/// Serializes the specified object to a PowerShell syntax string, with optional compression.
67+
/// </summary>
68+
/// <param name="value">The object to serialize. Can be null.</param>
69+
/// <param name="compress">true to compress the output; otherwise, false.</param>
70+
/// <returns>A string containing the PowerShell syntax representation of the serialized object.</returns>
71+
public static string Serialize(object? value, bool compress)
72+
{
73+
PowerShellSyntaxWriter writer = new(compress: compress);
74+
WriteValue(value, writer);
75+
return writer.ToString();
76+
}
77+
5078
/// <summary>
5179
/// Writes the specified value into the given <see cref="PowerShellSyntaxWriter"/>,
5280
/// checking registered converters first, then falling back to built-in handling.

src/PSADT/PSAppDeployToolkit/CodeGen/PowerShellSyntaxWriter.cs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
using System;
22+
using System.Collections.Generic;
2223
using System.Management.Automation.Language;
2324
using System.Text;
2425

@@ -32,7 +33,8 @@ namespace PSAppDeployToolkit.CodeGen
3233
/// such as for code generation or serialization scenarios. It manages indentation and escaping automatically to
3334
/// produce syntactically correct output.</remarks>
3435
/// <param name="indentChars">The string used for each indentation level. Defaults to four spaces. Cannot be null.</param>
35-
internal sealed class PowerShellSyntaxWriter(string indentChars = " ")
36+
/// <param name="compress">When <see langword="true"/>, hashtables are emitted on a single line with semicolon separators.</param>
37+
internal sealed class PowerShellSyntaxWriter(string indentChars = " ", bool compress = false)
3638
{
3739
/// <summary>
3840
/// Writes the opening token of a PowerShell hashtable literal (<c>@{</c>)
@@ -46,6 +48,11 @@ public void WriteStartHashtable(bool ordered = false)
4648
{
4749
_ = _buffer.Append(ordered ? "[ordered]@{" : "@{");
4850
_depth++;
51+
if (_compress)
52+
{
53+
_propertyWrittenStack.Push(_propertyWritten);
54+
_propertyWritten = false;
55+
}
4956
}
5057

5158
/// <summary>
@@ -55,9 +62,17 @@ public void WriteStartHashtable(bool ordered = false)
5562
public void WriteEndHashtable()
5663
{
5764
_depth--;
58-
_ = _buffer.AppendLine();
59-
WriteIndent();
60-
_ = _buffer.Append('}');
65+
if (_compress)
66+
{
67+
_ = _buffer.Append(" }");
68+
_propertyWritten = _propertyWrittenStack.Pop();
69+
}
70+
else
71+
{
72+
_ = _buffer.AppendLine();
73+
WriteIndent();
74+
_ = _buffer.Append('}');
75+
}
6176
}
6277

6378
/// <summary>
@@ -68,8 +83,16 @@ public void WriteEndHashtable()
6883
public void WritePropertyName(string name)
6984
{
7085
ArgumentNullException.ThrowIfNull(name);
71-
_ = _buffer.AppendLine();
72-
WriteIndent();
86+
if (_compress)
87+
{
88+
_ = _buffer.Append(_propertyWritten ? "; " : " ");
89+
_propertyWritten = true;
90+
}
91+
else
92+
{
93+
_ = _buffer.AppendLine();
94+
WriteIndent();
95+
}
7396
_ = _buffer.Append(name);
7497
_ = _buffer.Append(" = ");
7598
}
@@ -155,6 +178,8 @@ public void Reset()
155178
{
156179
_ = _buffer.Clear();
157180
_depth = 0;
181+
_propertyWritten = false;
182+
_propertyWrittenStack.Clear();
158183
}
159184

160185
private void WriteIndent()
@@ -170,6 +195,21 @@ private void WriteIndent()
170195
/// </summary>
171196
private int _depth;
172197

198+
/// <summary>
199+
/// Tracks whether a property has been written at the current hashtable depth (compressed mode).
200+
/// </summary>
201+
private bool _propertyWritten;
202+
203+
/// <summary>
204+
/// When <see langword="true"/>, hashtables are emitted on a single line with semicolons.
205+
/// </summary>
206+
private readonly bool _compress = compress;
207+
208+
/// <summary>
209+
/// Saves <see cref="_propertyWritten"/> state when entering nested hashtables.
210+
/// </summary>
211+
private readonly Stack<bool> _propertyWrittenStack = new();
212+
173213
/// <summary>
174214
/// Stores the string used for indentation in formatted output.
175215
/// </summary>

0 commit comments

Comments
 (0)