77
88namespace IISParser . PowerShell ;
99
10+ /// <summary>
11+ /// Base class for asynchronous PowerShell cmdlets that exposes asynchronous equivalents
12+ /// of the standard <see cref="PSCmdlet"/> lifecycle methods.
13+ /// </summary>
1014public abstract class AsyncPSCmdlet : PSCmdlet , IDisposable {
1115 private readonly CancellationTokenSource _cancelSource = new ( ) ;
1216
1317 private BlockingCollection < ( object ? , PipelineType ) > ? _currentOutPipe ;
1418 private BlockingCollection < object ? > ? _currentReplyPipe ;
1519
20+ /// <summary>
21+ /// Gets a cancellation token that is triggered when the cmdlet is stopped.
22+ /// </summary>
1623 protected internal CancellationToken CancelToken => _cancelSource . Token ;
1724
25+ /// <inheritdoc />
1826 public void Dispose ( ) {
1927 _cancelSource ? . Dispose ( ) ;
2028 }
2129
30+ /// <inheritdoc />
2231 protected override void BeginProcessing ( ) {
2332 RunBlockInAsync ( BeginProcessingAsync ) ;
2433 }
2534
35+ /// <summary>
36+ /// Asynchronously performs initialization before record processing begins.
37+ /// </summary>
38+ /// <returns>A task representing the asynchronous operation.</returns>
2639 protected virtual Task BeginProcessingAsync ( ) {
2740 return Task . CompletedTask ;
2841 }
2942
43+ /// <inheritdoc />
3044 protected override void ProcessRecord ( ) {
3145 RunBlockInAsync ( ProcessRecordAsync ) ;
3246 }
3347
48+ /// <summary>
49+ /// Asynchronously processes each record.
50+ /// </summary>
51+ /// <returns>A task representing the asynchronous operation.</returns>
3452 protected virtual Task ProcessRecordAsync ( ) {
3553 return Task . CompletedTask ;
3654 }
3755
56+ /// <inheritdoc />
3857 protected override void EndProcessing ( ) {
3958 RunBlockInAsync ( EndProcessingAsync ) ;
4059 }
4160
61+ /// <summary>
62+ /// Asynchronously performs cleanup after processing is complete.
63+ /// </summary>
64+ /// <returns>A task representing the asynchronous operation.</returns>
4265 protected virtual Task EndProcessingAsync ( ) {
4366 return Task . CompletedTask ;
4467 }
4568
69+ /// <inheritdoc />
4670 protected override void StopProcessing ( ) {
4771 _cancelSource ? . Cancel ( ) ;
4872 }
@@ -100,46 +124,85 @@ private void RunBlockInAsync(Func<Task> task) {
100124 blockTask . GetAwaiter ( ) . GetResult ( ) ;
101125 }
102126
127+ /// <summary>
128+ /// Determines whether the cmdlet should continue processing.
129+ /// </summary>
130+ /// <param name="target">The target of the operation.</param>
131+ /// <param name="action">The action to be performed.</param>
132+ /// <returns><c>true</c> if processing should continue; otherwise, <c>false</c>.</returns>
103133 public new bool ShouldProcess ( string target , string action ) {
104134 ThrowIfStopped ( ) ;
105135 _currentOutPipe ? . Add ( ( ( target , action ) , PipelineType . ShouldProcess ) ) ;
106136 return ( bool ) _currentReplyPipe ? . Take ( CancelToken ) ! ;
107137 }
108138
139+ /// <summary>
140+ /// Writes an object to the output pipeline.
141+ /// </summary>
142+ /// <param name="sendToPipeline">The object to write.</param>
109143 public new void WriteObject ( object ? sendToPipeline ) {
110144 WriteObject ( sendToPipeline , false ) ;
111145 }
112146
147+ /// <summary>
148+ /// Writes an object to the output pipeline with optional enumeration of collections.
149+ /// </summary>
150+ /// <param name="sendToPipeline">The object to write.</param>
151+ /// <param name="enumerateCollection">Whether to enumerate the object if it is a collection.</param>
113152 public new void WriteObject ( object ? sendToPipeline , bool enumerateCollection ) {
114153 ThrowIfStopped ( ) ;
115154 _currentOutPipe ? . Add ( ( sendToPipeline , enumerateCollection ? PipelineType . OutputEnumerate : PipelineType . Output ) ) ;
116155 }
117156
157+ /// <summary>
158+ /// Writes an error record to the error pipeline.
159+ /// </summary>
160+ /// <param name="errorRecord">The error to write.</param>
118161 public new void WriteError ( ErrorRecord errorRecord ) {
119162 ThrowIfStopped ( ) ;
120163 _currentOutPipe ? . Add ( ( errorRecord , PipelineType . Error ) ) ;
121164 }
122165
166+ /// <summary>
167+ /// Writes a warning message to the warning pipeline.
168+ /// </summary>
169+ /// <param name="message">The warning message.</param>
123170 public new void WriteWarning ( string message ) {
124171 ThrowIfStopped ( ) ;
125172 _currentOutPipe ? . Add ( ( message , PipelineType . Warning ) ) ;
126173 }
127174
175+ /// <summary>
176+ /// Writes a verbose message to the verbose pipeline.
177+ /// </summary>
178+ /// <param name="message">The verbose message.</param>
128179 public new void WriteVerbose ( string message ) {
129180 ThrowIfStopped ( ) ;
130181 _currentOutPipe ? . Add ( ( message , PipelineType . Verbose ) ) ;
131182 }
132183
184+ /// <summary>
185+ /// Writes a debug message to the debug pipeline.
186+ /// </summary>
187+ /// <param name="message">The debug message.</param>
133188 public new void WriteDebug ( string message ) {
134189 ThrowIfStopped ( ) ;
135190 _currentOutPipe ? . Add ( ( message , PipelineType . Debug ) ) ;
136191 }
137192
193+ /// <summary>
194+ /// Writes information to the information pipeline.
195+ /// </summary>
196+ /// <param name="informationRecord">The information record.</param>
138197 public new void WriteInformation ( InformationRecord informationRecord ) {
139198 ThrowIfStopped ( ) ;
140199 _currentOutPipe ? . Add ( ( informationRecord , PipelineType . Information ) ) ;
141200 }
142201
202+ /// <summary>
203+ /// Writes a progress record to the progress pipeline or directly if asynchronous output is not active.
204+ /// </summary>
205+ /// <param name="progressRecord">The progress record.</param>
143206 public new void WriteProgress ( ProgressRecord progressRecord ) {
144207 ThrowIfStopped ( ) ;
145208 if ( _currentOutPipe != null ) {
@@ -155,6 +218,10 @@ internal void ThrowIfStopped() {
155218 }
156219 }
157220
221+ /// <summary>
222+ /// Retrieves the effective <see cref="ActionPreference"/> for error handling.
223+ /// </summary>
224+ /// <returns>The resolved <see cref="ActionPreference"/>.</returns>
158225 protected ActionPreference GetErrorActionPreference ( ) {
159226 if ( MyInvocation . BoundParameters . ContainsKey ( "ErrorAction" ) ) {
160227 string ? errorActionString = MyInvocation . BoundParameters [ "ErrorAction" ] ? . ToString ( ) ;
@@ -175,6 +242,12 @@ protected ActionPreference GetErrorActionPreference() {
175242 return ActionPreference . Continue ;
176243 }
177244
245+ /// <summary>
246+ /// Ensures that the specified file exists, writing a warning or terminating error as appropriate.
247+ /// </summary>
248+ /// <param name="path">The file path to check.</param>
249+ /// <param name="errorAction">The action preference determining error handling.</param>
250+ /// <returns><c>true</c> if the file exists; otherwise, <c>false</c>.</returns>
178251 protected bool EnsureFileExists ( string path , ActionPreference errorAction ) {
179252 if ( File . Exists ( path ) ) {
180253 return true ;
0 commit comments