File size: 13,257 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Provider;
using Dbg = System.Management.Automation;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// This provider is the data accessor for shell functions. It uses
/// the SessionStateProviderBase as the base class to produce a view on
/// session state data.
/// </summary>
[CmdletProvider(FunctionProvider.ProviderName, ProviderCapabilities.ShouldProcess)]
[OutputType(typeof(FunctionInfo), ProviderCmdlet = ProviderCmdlet.SetItem)]
[OutputType(typeof(FunctionInfo), ProviderCmdlet = ProviderCmdlet.RenameItem)]
[OutputType(typeof(FunctionInfo), ProviderCmdlet = ProviderCmdlet.CopyItem)]
[OutputType(typeof(FunctionInfo), ProviderCmdlet = ProviderCmdlet.GetChildItem)]
[OutputType(typeof(FunctionInfo), ProviderCmdlet = ProviderCmdlet.GetItem)]
[OutputType(typeof(FunctionInfo), ProviderCmdlet = ProviderCmdlet.NewItem)]
public sealed class FunctionProvider : SessionStateProviderBase
{
/// <summary>
/// Gets the name of the provider.
/// </summary>
public const string ProviderName = "Function";
#region Constructor
/// <summary>
/// The constructor for the provider that exposes variables to the user
/// as drives.
/// </summary>
public FunctionProvider()
{
}
#endregion Constructor
#region DriveCmdletProvider overrides
/// <summary>
/// Initializes the function drive.
/// </summary>
/// <returns>
/// An array of a single PSDriveInfo object representing the functions drive.
/// </returns>
protected override Collection<PSDriveInfo> InitializeDefaultDrives()
{
string description = SessionStateStrings.FunctionDriveDescription;
PSDriveInfo functionDrive =
new PSDriveInfo(
DriveNames.FunctionDrive,
ProviderInfo,
string.Empty,
description,
null);
Collection<PSDriveInfo> drives = new Collection<PSDriveInfo>();
drives.Add(functionDrive);
return drives;
}
#endregion DriveCmdletProvider overrides
#region Dynamic Parameters
/// <summary>
/// Gets the dynamic parameters for the NewItem cmdlet.
/// </summary>
/// <param name="path">
/// Ignored.
/// </param>
/// <param name="type">
/// Ignored.
/// </param>
/// <param name="newItemValue">
/// Ignored.
/// </param>
/// <returns>
/// An instance of FunctionProviderDynamicParameters which is the dynamic parameters for
/// NewItem.
/// </returns>
protected override object NewItemDynamicParameters(string path, string type, object newItemValue)
{
return new FunctionProviderDynamicParameters();
}
/// <summary>
/// Gets the dynamic parameters for the NewItem cmdlet.
/// </summary>
/// <param name="path">
/// Ignored.
/// </param>
/// <param name="value">
/// Ignored.
/// </param>
/// <returns>
/// An instance of FunctionProviderDynamicParameters which is the dynamic parameters for
/// SetItem.
/// </returns>
protected override object SetItemDynamicParameters(string path, object value)
{
return new FunctionProviderDynamicParameters();
}
#endregion Dynamic Parameters
#region protected members
/// <summary>
/// Gets a function from session state.
/// </summary>
/// <param name="name">
/// The name of the function to retrieve.
/// </param>
/// <returns>
/// A ScriptBlock that represents the function.
/// </returns>
internal override object GetSessionStateItem(string name)
{
Dbg.Diagnostics.Assert(
!string.IsNullOrEmpty(name),
"The caller should verify this parameter");
CommandInfo function = SessionState.Internal.GetFunction(name, Context.Origin);
return function;
}
/// <summary>
/// Sets the function of the specified name to the specified value.
/// </summary>
/// <param name="name">
/// The name of the function to set.
/// </param>
/// <param name="value">
/// The new value for the function.
/// </param>
/// <param name="writeItem">
/// If true, the item that was set should be written to WriteItemObject.
/// </param>
#pragma warning disable 0162
internal override void SetSessionStateItem(string name, object value, bool writeItem)
{
Dbg.Diagnostics.Assert(
!string.IsNullOrEmpty(name),
"The caller should verify this parameter");
FunctionProviderDynamicParameters dynamicParameters =
DynamicParameters as FunctionProviderDynamicParameters;
CommandInfo modifiedItem = null;
bool dynamicParametersSpecified = dynamicParameters != null && dynamicParameters.OptionsSet;
if (value == null)
{
// If the value wasn't specified but the options were, just set the
// options on the existing function.
// If the options weren't specified, then remove the function
if (dynamicParametersSpecified)
{
modifiedItem = (CommandInfo)GetSessionStateItem(name);
if (modifiedItem != null)
{
SetOptions(modifiedItem, dynamicParameters.Options);
}
}
else
{
RemoveSessionStateItem(name);
}
}
else
{
do // false loop
{
// Unwrap the PSObject before binding it as a scriptblock...
PSObject pso = value as PSObject;
if (pso != null)
{
value = pso.BaseObject;
}
ScriptBlock scriptBlockValue = value as ScriptBlock;
if (scriptBlockValue != null)
{
if (dynamicParametersSpecified)
{
modifiedItem = SessionState.Internal.SetFunction(name, scriptBlockValue,
null, dynamicParameters.Options, Force, Context.Origin);
}
else
{
modifiedItem = SessionState.Internal.SetFunction(name, scriptBlockValue, null, Force, Context.Origin);
}
break;
}
FunctionInfo function = value as FunctionInfo;
if (function != null)
{
ScopedItemOptions options = function.Options;
if (dynamicParametersSpecified)
{
options = dynamicParameters.Options;
}
modifiedItem = SessionState.Internal.SetFunction(name, function.ScriptBlock, function, options, Force, Context.Origin);
break;
}
string stringValue = value as string;
if (stringValue != null)
{
ScriptBlock scriptBlock = ScriptBlock.Create(Context.ExecutionContext, stringValue);
if (dynamicParametersSpecified)
{
modifiedItem = SessionState.Internal.SetFunction(name, scriptBlock, null, dynamicParameters.Options, Force, Context.Origin);
}
else
{
modifiedItem = SessionState.Internal.SetFunction(name, scriptBlock, null, Force, Context.Origin);
}
break;
}
throw PSTraceSource.NewArgumentException(nameof(value));
} while (false);
if (writeItem && modifiedItem != null)
{
WriteItemObject(modifiedItem, modifiedItem.Name, false);
}
}
}
#pragma warning restore 0162
private static void SetOptions(CommandInfo function, ScopedItemOptions options)
{
((FunctionInfo)function).Options = options;
}
/// <summary>
/// Removes the specified function from session state.
/// </summary>
/// <param name="name">
/// The name of the function to remove from session state.
/// </param>
internal override void RemoveSessionStateItem(string name)
{
Dbg.Diagnostics.Assert(
!string.IsNullOrEmpty(name),
"The caller should verify this parameter");
SessionState.Internal.RemoveFunction(name, Force);
}
/// <summary>
/// Since items are often more than their value, this method should
/// be overridden to provide the value for an item.
/// </summary>
/// <param name="item">
/// The item to extract the value from.
/// </param>
/// <returns>
/// The value of the specified item.
/// </returns>
/// <remarks>
/// The default implementation will get
/// the Value property of a DictionaryEntry
/// </remarks>
internal override object GetValueOfItem(object item)
{
Dbg.Diagnostics.Assert(
item != null,
"Caller should verify the item parameter");
object value = item;
FunctionInfo function = item as FunctionInfo;
if (function != null)
{
value = function.ScriptBlock;
}
return value;
}
/// <summary>
/// Gets a flattened view of the functions in session state.
/// </summary>
/// <returns>
/// An IDictionary representing the flattened view of the functions in
/// session state.
/// </returns>
internal override IDictionary GetSessionStateTable()
{
return (IDictionary)SessionState.Internal.GetFunctionTable();
}
/// <summary>
/// Determines if the item can be renamed. Derived classes that need
/// to perform a check should override this method.
/// </summary>
/// <param name="item">
/// The item to verify if it can be renamed.
/// </param>
/// <returns>
/// true if the item can be renamed or false otherwise.
/// </returns>
internal override bool CanRenameItem(object item)
{
bool result = false;
FunctionInfo functionInfo = item as FunctionInfo;
if (functionInfo != null)
{
if ((functionInfo.Options & ScopedItemOptions.Constant) != 0 ||
((functionInfo.Options & ScopedItemOptions.ReadOnly) != 0 && !Force))
{
SessionStateUnauthorizedAccessException e =
new SessionStateUnauthorizedAccessException(
functionInfo.Name,
SessionStateCategory.Function,
"CannotRenameFunction",
SessionStateStrings.CannotRenameFunction);
throw e;
}
result = true;
}
return result;
}
#endregion protected members
}
/// <summary>
/// The dynamic parameter object for the FunctionProvider SetItem and NewItem commands.
/// </summary>
public class FunctionProviderDynamicParameters
{
/// <summary>
/// Gets or sets the option parameter for the function.
/// </summary>
[Parameter]
public ScopedItemOptions Options
{
get
{
return _options;
}
set
{
_optionsSet = true;
_options = value;
}
}
private ScopedItemOptions _options = ScopedItemOptions.None;
/// <summary>
/// Determines if the Options parameter was set.
/// </summary>
/// <value></value>
internal bool OptionsSet
{
get { return _optionsSet; }
}
private bool _optionsSet;
}
}
|