File size: 9,488 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma warning disable 1634, 1691
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation.Host;
namespace System.Management.Automation
{
/// <summary>
/// Default implementation of ICommandRuntime for running Cmdlets standalone.
/// </summary>
internal class DefaultCommandRuntime : ICommandRuntime2
{
private readonly 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)
{
ArgumentNullException.ThrowIfNull(outputList);
_output = outputList;
}
/// <summary>
/// Return the instance of PSHost - null by default.
/// </summary>
public PSHost Host { get; set; }
#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 exception 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 constructor.
/// </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>
[System.Diagnostics.CodeAnalysis.DoesNotReturn]
public void ThrowTerminatingError(ErrorRecord errorRecord)
{
if (errorRecord.Exception != null)
{
throw errorRecord.Exception;
}
else
{
throw new System.InvalidOperationException(errorRecord.ToString());
}
}
#endregion
}
}
|