Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
DefaultCommandRuntime.cs247 linesDownload Raw Back to engine
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#pragma warning disable 1634, 16915 6using System.Collections;7using System.Collections.Generic;8using System.Management.Automation.Host;9 10namespace System.Management.Automation11{12    /// <summary>13    /// Default implementation of ICommandRuntime for running Cmdlets standalone.14    /// </summary>15    internal class DefaultCommandRuntime : ICommandRuntime216    {17        private readonly List<object> _output;18        /// <summary>19        /// Constructs an instance of the default ICommandRuntime object20        /// that will write objects into the list that was passed.21        /// </summary>22        public DefaultCommandRuntime(List<object> outputList)23        {24            ArgumentNullException.ThrowIfNull(outputList);25 26            _output = outputList;27        }28 29        /// <summary>30        /// Return the instance of PSHost - null by default.31        /// </summary>32        public PSHost Host { get; set; }33 34        #region Write35        /// <summary>36        /// Implementation of WriteDebug - just discards the input.37        /// </summary>38        /// <param name="text">Text to write.</param>39        public void WriteDebug(string text) { }40 41        /// <summary>42        /// Default implementation of WriteError - if the error record contains43        /// an exception then that exception will be thrown. If not, then an44        /// InvalidOperationException will be constructed and thrown.45        /// </summary>46        /// <param name="errorRecord">Error record instance to process.</param>47        public void WriteError(ErrorRecord errorRecord)48        {49            if (errorRecord.Exception != null)50                throw errorRecord.Exception;51            else52                throw new InvalidOperationException(errorRecord.ToString());53        }54 55        /// <summary>56        /// Default implementation of WriteObject - adds the object to the list57        /// passed to the objects constructor.58        /// </summary>59        /// <param name="sendToPipeline">Object to write.</param>60        public void WriteObject(object sendToPipeline)61        {62            _output.Add(sendToPipeline);63        }64 65        /// <summary>66        /// Default implementation of the enumerated WriteObject. Either way, the67        /// objects are added to the list passed to this object in the constructor.68        /// </summary>69        /// <param name="sendToPipeline">Object to write.</param>70        /// <param name="enumerateCollection">If true, the collection is enumerated, otherwise71        /// it's written as a scalar.72        /// </param>73        public void WriteObject(object sendToPipeline, bool enumerateCollection)74        {75            if (enumerateCollection)76            {77                IEnumerator e = LanguagePrimitives.GetEnumerator(sendToPipeline);78                if (e == null)79                {80                    _output.Add(sendToPipeline);81                }82                else83                {84                    while (e.MoveNext())85                    {86                        _output.Add(e.Current);87                    }88                }89            }90            else91            {92                _output.Add(sendToPipeline);93            }94        }95 96        /// <summary>97        /// Default implementation - just discards it's arguments.98        /// </summary>99        /// <param name="progressRecord">Progress record to write.</param>100        public void WriteProgress(ProgressRecord progressRecord) { }101 102        /// <summary>103        /// Default implementation - just discards it's arguments.104        /// </summary>105        /// <param name="sourceId">Source ID to write for.</param>106        /// <param name="progressRecord">Record to write.</param>107        public void WriteProgress(Int64 sourceId, ProgressRecord progressRecord) { }108 109        /// <summary>110        /// Default implementation - just discards it's arguments.111        /// </summary>112        /// <param name="text">Text to write.</param>113        public void WriteVerbose(string text) { }114 115        /// <summary>116        /// Default implementation - just discards it's arguments.117        /// </summary>118        /// <param name="text">Text to write.</param>119        public void WriteWarning(string text) { }120 121        /// <summary>122        /// Default implementation - just discards it's arguments.123        /// </summary>124        /// <param name="text">Text to write.</param>125        public void WriteCommandDetail(string text) { }126 127        /// <summary>128        /// Default implementation - just discards it's arguments.129        /// </summary>130        /// <param name="informationRecord">Record to write.</param>131        public void WriteInformation(InformationRecord informationRecord) { }132 133        #endregion Write134 135        #region Should136        /// <summary>137        /// Default implementation - always returns true.138        /// </summary>139        /// <param name="target">Ignored.</param>140        /// <returns>True.</returns>141        public bool ShouldProcess(string target) { return true; }142 143        /// <summary>144        /// Default implementation - always returns true.145        /// </summary>146        /// <param name="target">Ignored.</param>147        /// <param name="action">Ignored.</param>148        /// <returns>True.</returns>149        public bool ShouldProcess(string target, string action) { return true; }150 151        /// <summary>152        /// Default implementation - always returns true.153        /// </summary>154        /// <param name="verboseDescription">Ignored.</param>155        /// <param name="verboseWarning">Ignored.</param>156        /// <param name="caption">Ignored.</param>157        /// <returns>True.</returns>158        public bool ShouldProcess(string verboseDescription, string verboseWarning, string caption) { return true; }159 160        /// <summary>161        /// Default implementation - always returns true.162        /// </summary>163        /// <param name="verboseDescription">Ignored.</param>164        /// <param name="verboseWarning">Ignored.</param>165        /// <param name="caption">Ignored.</param>166        /// <param name="shouldProcessReason">Ignored.</param>167        /// <returns>True.</returns>168        public bool ShouldProcess(string verboseDescription, string verboseWarning, string caption, out ShouldProcessReason shouldProcessReason) { shouldProcessReason = ShouldProcessReason.None; return true; }169 170        /// <summary>171        /// Default implementation - always returns true.172        /// </summary>173        /// <param name="query">Ignored.</param>174        /// <param name="caption">Ignored.</param>175        /// <returns>True.</returns>176        public bool ShouldContinue(string query, string caption) { return true; }177 178        /// <summary>179        /// Default implementation - always returns true.180        /// </summary>181        /// <param name="query">Ignored.</param>182        /// <param name="caption">Ignored.</param>183        /// <param name="yesToAll">Ignored.</param>184        /// <param name="noToAll">Ignored.</param>185        /// <returns>True.</returns>186        public bool ShouldContinue(string query, string caption, ref bool yesToAll, ref bool noToAll) { return true; }187 188        /// <summary>189        /// Default implementation - always returns true.190        /// </summary>191        /// <param name="query">Ignored.</param>192        /// <param name="caption">Ignored.</param>193        /// <param name="hasSecurityImpact">Ignored.</param>194        /// <param name="yesToAll">Ignored.</param>195        /// <param name="noToAll">Ignored.</param>196        /// <returns>True.</returns>197        public bool ShouldContinue(string query, string caption, bool hasSecurityImpact, ref bool yesToAll, ref bool noToAll) { return true; }198 199        #endregion Should200 201        #region Transaction Support202        /// <summary>203        /// Returns true if a transaction is available and active.204        /// </summary>205        public bool TransactionAvailable() { return false; }206 207        /// <summary>208        /// Gets an object that surfaces the current PowerShell transaction.209        /// When this object is disposed, PowerShell resets the active transaction.210        /// </summary>211        public PSTransactionContext CurrentPSTransaction212        {213            get214            {215                string error = TransactionStrings.CmdletRequiresUseTx;216 217                // We want to throw in this situation, and want to use a218                // property because it mimics the C# using(TransactionScope ...) syntax219#pragma warning suppress 56503220                throw new InvalidOperationException(error);221            }222        }223        #endregion Transaction Support224 225        #region Misc226        /// <summary>227        /// Implementation of the dummy default ThrowTerminatingError API - it just228        /// does what the base implementation does anyway - rethrow the exception229        /// if it exists, otherwise throw an invalid operation exception.230        /// </summary>231        /// <param name="errorRecord">The error record to throw.</param>232        [System.Diagnostics.CodeAnalysis.DoesNotReturn]233        public void ThrowTerminatingError(ErrorRecord errorRecord)234        {235            if (errorRecord.Exception != null)236            {237                throw errorRecord.Exception;238            }239            else240            {241                throw new System.InvalidOperationException(errorRecord.ToString());242            }243        }244        #endregion245    }246}247