mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
This commit uses psl-monad branch source-depot 7209cb9 And corresponds to [SD:688741]
256 lines
9.4 KiB
C#
256 lines
9.4 KiB
C#
#pragma warning disable 1634, 1691
|
|
|
|
/********************************************************************++
|
|
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
--********************************************************************/
|
|
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Management.Automation;
|
|
using System.Management.Automation.Host;
|
|
|
|
namespace System.Management.Automation
|
|
{
|
|
/// <summary>
|
|
/// Default implementation of ICommandRuntime for running Cmdlets standalone.
|
|
/// </summary>
|
|
internal class DefaultCommandRuntime : ICommandRuntime2
|
|
{
|
|
|
|
private List<object> output;
|
|
/// <summary>
|
|
/// Constructs an instance of the default ICommandRuntime object
|
|
/// that will write objects into the list that was passed.
|
|
/// </summary>
|
|
public DefaultCommandRuntime(List<object> outputList)
|
|
{
|
|
if (outputList == null)
|
|
throw new System.ArgumentNullException("outputList");
|
|
|
|
this.output = outputList;
|
|
}
|
|
|
|
private PSHost host;
|
|
|
|
/// <summary>
|
|
/// Return the instance of PSHost - null by default.
|
|
/// </summary>
|
|
public PSHost Host
|
|
{
|
|
set { host = value; }
|
|
get { return host; }
|
|
}
|
|
#region Write
|
|
/// <summary>
|
|
/// Implementation of WriteDebug - just discards the input.
|
|
/// </summary>
|
|
/// <param name="text">Text to write</param>
|
|
public void WriteDebug(string text) { ; }
|
|
|
|
/// <summary>
|
|
/// Default implementation of WriteError - if the error record contains
|
|
/// an exceptin then that exception will be thrown. If not, then an
|
|
/// InvalidOperationException will be constructed and thrown.
|
|
/// </summary>
|
|
/// <param name="errorRecord">Error record instance to process</param>
|
|
public void WriteError(ErrorRecord errorRecord)
|
|
{
|
|
if (errorRecord.Exception != null)
|
|
throw errorRecord.Exception;
|
|
else
|
|
throw new InvalidOperationException(errorRecord.ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Default implementation of WriteObject - adds the object to the list
|
|
/// passed to the objects constructor.
|
|
/// </summary>
|
|
/// <param name="sendToPipeline">Object to write</param>
|
|
public void WriteObject(object sendToPipeline)
|
|
{
|
|
output.Add(sendToPipeline);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Default implementation of the enumerated WriteObject. Either way, the
|
|
/// objects are added to the list passed to this object in the constuctor.
|
|
/// </summary>
|
|
/// <param name="sendToPipeline">Object to write</param>
|
|
/// <param name="enumerateCollection">If true, the collection is enumerated, otherwise
|
|
/// it's written as a scalar.
|
|
/// </param>
|
|
public void WriteObject(object sendToPipeline, bool enumerateCollection)
|
|
{
|
|
if (enumerateCollection)
|
|
{
|
|
IEnumerator e = LanguagePrimitives.GetEnumerator(sendToPipeline);
|
|
if (e == null)
|
|
{
|
|
output.Add(sendToPipeline);
|
|
}
|
|
else
|
|
{
|
|
while (e.MoveNext())
|
|
{
|
|
output.Add(e.Current);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
output.Add(sendToPipeline);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Default implementation - just discards it's arguments
|
|
/// </summary>
|
|
/// <param name="progressRecord">progress record to write.</param>
|
|
public void WriteProgress(ProgressRecord progressRecord) { ; }
|
|
|
|
/// <summary>
|
|
/// Default implementation - just discards it's arguments
|
|
/// </summary>
|
|
/// <param name="sourceId">Source ID to write for</param>
|
|
/// <param name="progressRecord">record to write.</param>
|
|
public void WriteProgress(Int64 sourceId, ProgressRecord progressRecord) { ; }
|
|
|
|
/// <summary>
|
|
/// Default implementation - just discards it's arguments
|
|
/// </summary>
|
|
/// <param name="text">Text to write.</param>
|
|
public void WriteVerbose(string text) { ; }
|
|
|
|
/// <summary>
|
|
/// Default implementation - just discards it's arguments
|
|
/// </summary>
|
|
/// <param name="text">Text to write.</param>
|
|
public void WriteWarning(string text) { ; }
|
|
|
|
/// <summary>
|
|
/// Default implementation - just discards it's arguments
|
|
/// </summary>
|
|
/// <param name="text">Text to write.</param>
|
|
public void WriteCommandDetail(string text) { ; }
|
|
|
|
/// <summary>
|
|
/// Default implementation - just discards it's arguments
|
|
/// </summary>
|
|
/// <param name="informationRecord">Record to write.</param>
|
|
public void WriteInformation(InformationRecord informationRecord) { ; }
|
|
|
|
#endregion Write
|
|
|
|
#region Should
|
|
/// <summary>
|
|
/// Default implementation - always returns true.
|
|
/// </summary>
|
|
/// <param name="target">ignored</param>
|
|
/// <returns>true</returns>
|
|
public bool ShouldProcess( string target ) { return true; }
|
|
|
|
/// <summary>
|
|
/// Default implementation - always returns true.
|
|
/// </summary>
|
|
/// <param name="target">ignored</param>
|
|
/// <param name="action">ignored</param>
|
|
/// <returns>true</returns>
|
|
public bool ShouldProcess( string target, string action ) {return true; }
|
|
|
|
/// <summary>
|
|
/// Default implementation - always returns true.
|
|
/// </summary>
|
|
/// <param name="verboseDescription">ignored</param>
|
|
/// <param name="verboseWarning">ignored</param>
|
|
/// <param name="caption">ignored</param>
|
|
/// <returns>true</returns>
|
|
public bool ShouldProcess(string verboseDescription, string verboseWarning, string caption) { return true; }
|
|
|
|
/// <summary>
|
|
/// Default implementation - always returns true.
|
|
/// </summary>
|
|
/// <param name="verboseDescription">ignored</param>
|
|
/// <param name="verboseWarning">ignored</param>
|
|
/// <param name="caption">ignored</param>
|
|
/// <param name="shouldProcessReason">ignored</param>
|
|
/// <returns>true</returns>
|
|
public bool ShouldProcess(string verboseDescription, string verboseWarning, string caption, out ShouldProcessReason shouldProcessReason) { shouldProcessReason = ShouldProcessReason.None; return true; }
|
|
|
|
/// <summary>
|
|
/// Default implementation - always returns true.
|
|
/// </summary>
|
|
/// <param name="query">ignored</param>
|
|
/// <param name="caption">ignored</param>
|
|
/// <returns>true</returns>
|
|
public bool ShouldContinue(string query, string caption) { return true; }
|
|
|
|
/// <summary>
|
|
/// Default implementation - always returns true.
|
|
/// </summary>
|
|
/// <param name="query">ignored</param>
|
|
/// <param name="caption">ignored</param>
|
|
/// <param name="yesToAll">ignored</param>
|
|
/// <param name="noToAll">ignored</param>
|
|
/// <returns>true</returns>
|
|
public bool ShouldContinue( string query, string caption, ref bool yesToAll, ref bool noToAll ) { return true; }
|
|
|
|
/// <summary>
|
|
/// Default implementation - always returns true.
|
|
/// </summary>
|
|
/// <param name="query">ignored</param>
|
|
/// <param name="caption">ignored</param>
|
|
/// <param name="hasSecurityImpact">ignored</param>
|
|
/// <param name="yesToAll">ignored</param>
|
|
/// <param name="noToAll">ignored</param>
|
|
/// <returns>true</returns>
|
|
public bool ShouldContinue(string query, string caption, bool hasSecurityImpact, ref bool yesToAll, ref bool noToAll) { return true; }
|
|
|
|
#endregion Should
|
|
|
|
#region Transaction Support
|
|
/// <summary>
|
|
/// Returns true if a transaction is available and active.
|
|
/// </summary>
|
|
public bool TransactionAvailable() { return false; }
|
|
|
|
/// <summary>
|
|
/// Gets an object that surfaces the current PowerShell transaction.
|
|
/// When this object is disposed, PowerShell resets the active transaction
|
|
/// </summary>
|
|
public PSTransactionContext CurrentPSTransaction
|
|
{
|
|
get
|
|
{
|
|
string error = TransactionStrings.CmdletRequiresUseTx;
|
|
|
|
// We want to throw in this situation, and want to use a
|
|
// property because it mimics the C# using(TransactionScope ...) syntax
|
|
#pragma warning suppress 56503
|
|
throw new InvalidOperationException(error);
|
|
}
|
|
}
|
|
#endregion Transaction Support
|
|
|
|
#region Misc
|
|
/// <summary>
|
|
/// Implementation of the dummy default ThrowTerminatingError API - it just
|
|
/// does what the base implementation does anyway - rethrow the exception
|
|
/// if it exists, otherwise throw an invalid operation exception.
|
|
/// </summary>
|
|
/// <param name="errorRecord">The error record to throw</param>
|
|
public void ThrowTerminatingError(ErrorRecord errorRecord)
|
|
{
|
|
if (errorRecord.Exception != null)
|
|
{
|
|
throw errorRecord.Exception;
|
|
}
|
|
else
|
|
{
|
|
throw new System.InvalidOperationException(errorRecord.ToString());
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|