Skip to content

Commit

Permalink
Refactor the design of 'Clean' block implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
daxian-dbw committed Apr 7, 2021
1 parent fdb2f83 commit 0e28c4a
Show file tree
Hide file tree
Showing 13 changed files with 486 additions and 835 deletions.
7 changes: 7 additions & 0 deletions src/System.Management.Automation/engine/CommandBase.cs
Expand Up @@ -227,6 +227,13 @@ internal virtual void DoStopProcessing()
{
}

/// <summary>
/// When overridden in the derived class, performs clean-up after the command execution.
/// </summary>
internal virtual void DoCleanResource()
{
}

#endregion Override

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/System.Management.Automation/engine/CommandInfo.cs
Expand Up @@ -526,7 +526,7 @@ private void GetMergedCommandParameterMetadata(out MergedCommandParameterMetadat
processor = scriptCommand != null
? new CommandProcessor(scriptCommand, _context, useLocalScope: true, fromScriptFile: false,
sessionState: scriptCommand.ScriptBlock.SessionStateInternal ?? Context.EngineSessionState)
: new CommandProcessor((CmdletInfo)this, _context) { UseLocalScope = true };
: new CommandProcessor((CmdletInfo)this, _context);

ParameterBinderController.AddArgumentsToCommandProcessor(processor, Arguments);
CommandProcessorBase oldCurrentCommandProcessor = Context.CurrentCommandProcessor;
Expand Down
21 changes: 5 additions & 16 deletions src/System.Management.Automation/engine/CommandProcessor.cs
Expand Up @@ -309,13 +309,11 @@ internal override void DoBegin()
internal override void ProcessRecord()
{
// Invoke the Command method with the request object

if (!this.RanBeginAlready)
{
RanBeginAlready = true;
try
{
// NOTICE-2004/06/08-JonN 959638
using (commandRuntime.AllowThisCommandToWrite(true))
{
if (Context._debuggingMode > 0 && Command is not PSScriptCmdlet)
Expand All @@ -326,12 +324,9 @@ internal override void ProcessRecord()
Command.DoBeginProcessing();
}
}
// 2004/03/18-JonN This is understood to be
// an FXCOP violation, cleared by KCwalina.
catch (Exception e) // Catch-all OK, 3rd party callout.
catch (Exception e) when (ShallWrapBeforeBubbleUp(e))
{
// This cmdlet threw an exception, so
// wrap it and bubble it up.
// This cmdlet threw an exception, so wrap it and bubble it up.
throw ManageInvocationException(e);
}
}
Expand Down Expand Up @@ -366,6 +361,7 @@ internal override void ProcessRecord()

// NOTICE-2004/06/08-JonN 959638
using (commandRuntime.AllowThisCommandToWrite(true))
using (ParameterBinderBase.bindingTracer.TraceScope("CALLING ProcessRecord"))
{
if (CmdletParameterBinderController.ObsoleteParameterWarningList != null &&
CmdletParameterBinderController.ObsoleteParameterWarningList.Count > 0)
Expand Down Expand Up @@ -398,16 +394,9 @@ internal override void ProcessRecord()

exceptionToThrow = rte;
}
catch (LoopFlowException)
{
// Win8:84066 - Don't wrap LoopFlowException, we incorrectly raise a PipelineStoppedException
// which gets caught by a script try/catch if we wrap here.
throw;
}
// 2004/03/18-JonN This is understood to be
// an FXCOP violation, cleared by KCwalina.
catch (Exception e) // Catch-all OK, 3rd party callout.
catch (Exception e) when (ShallWrapBeforeBubbleUp(e))
{
// Catch-all OK, 3rd party callout.
exceptionToThrow = e;
}
finally
Expand Down

0 comments on commit 0e28c4a

Please sign in to comment.