1919 */
2020
2121using System ;
22+ using System . Collections . Generic ;
2223using System . Management . Automation . Language ;
2324using 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