File size: 11,566 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 | // 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 aliases. It uses
/// the SessionStateProviderBase as the base class to produce a view on
/// session state data.
/// </summary>
[CmdletProvider(AliasProvider.ProviderName, ProviderCapabilities.ShouldProcess)]
[OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.SetItem)]
[OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.RenameItem)]
[OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.CopyItem)]
[OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.GetChildItem)]
[OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.NewItem)]
public sealed class AliasProvider : SessionStateProviderBase
{
/// <summary>
/// Gets the name of the provider.
/// </summary>
public const string ProviderName = "Alias";
#region Constructor
/// <summary>
/// The constructor for the provider that exposes variables to the user
/// as drives.
/// </summary>
public AliasProvider()
{
}
#endregion Constructor
#region DriveCmdletProvider overrides
/// <summary>
/// Initializes the alias drive.
/// </summary>
/// <returns>
/// An array of a single PSDriveInfo object representing the alias drive.
/// </returns>
protected override Collection<PSDriveInfo> InitializeDefaultDrives()
{
string description = SessionStateStrings.AliasDriveDescription;
PSDriveInfo aliasDrive =
new PSDriveInfo(
DriveNames.AliasDrive,
ProviderInfo,
string.Empty,
description,
null);
Collection<PSDriveInfo> drives = new Collection<PSDriveInfo>();
drives.Add(aliasDrive);
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 AliasProviderDynamicParameters which is the dynamic parameters for
/// NewItem.
/// </returns>
protected override object NewItemDynamicParameters(string path, string type, object newItemValue)
{
return new AliasProviderDynamicParameters();
}
/// <summary>
/// Gets the dynamic parameters for the NewItem cmdlet.
/// </summary>
/// <param name="path">
/// Ignored.
/// </param>
/// <param name="value">
/// Ignored.
/// </param>
/// <returns>
/// An instance of AliasProviderDynamicParameters which is the dynamic parameters for
/// SetItem.
/// </returns>
protected override object SetItemDynamicParameters(string path, object value)
{
return new AliasProviderDynamicParameters();
}
#endregion Dynamic Parameters
#region protected members
/// <summary>
/// Gets a alias from session state.
/// </summary>
/// <param name="name">
/// The name of the alias to retrieve.
/// </param>
/// <returns>
/// A DictionaryEntry that represents the value of the alias.
/// </returns>
internal override object GetSessionStateItem(string name)
{
Dbg.Diagnostics.Assert(
!string.IsNullOrEmpty(name),
"The caller should verify this parameter");
AliasInfo value = SessionState.Internal.GetAlias(name, Context.Origin);
return value;
}
/// <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;
AliasInfo aliasInfo = item as AliasInfo;
if (aliasInfo != null)
{
value = aliasInfo.Definition;
}
return value;
}
/// <summary>
/// Sets the alias of the specified name to the specified value.
/// </summary>
/// <param name="name">
/// The name of the alias to set.
/// </param>
/// <param name="value">
/// The new value for the alias.
/// </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");
AliasProviderDynamicParameters dynamicParameters =
DynamicParameters as AliasProviderDynamicParameters;
AliasInfo item = null;
bool dynamicParametersSpecified = dynamicParameters != null && dynamicParameters.OptionsSet;
if (value == null)
{
if (dynamicParametersSpecified)
{
item = (AliasInfo)GetSessionStateItem(name);
item?.SetOptions(dynamicParameters.Options, Force);
}
else
{
RemoveSessionStateItem(name);
}
}
else
{
do // false loop
{
string stringValue = value as string;
if (stringValue != null)
{
if (dynamicParametersSpecified)
{
item = SessionState.Internal.SetAliasValue(name, stringValue, dynamicParameters.Options, Force, Context.Origin);
}
else
{
item = SessionState.Internal.SetAliasValue(name, stringValue, Force, Context.Origin);
}
break;
}
AliasInfo alias = value as AliasInfo;
if (alias != null)
{
AliasInfo newAliasInfo =
new AliasInfo(
name,
alias.Definition,
this.Context.ExecutionContext,
alias.Options);
if (dynamicParametersSpecified)
{
newAliasInfo.SetOptions(dynamicParameters.Options, Force);
}
item = SessionState.Internal.SetAliasItem(newAliasInfo, Force, Context.Origin);
break;
}
throw PSTraceSource.NewArgumentException(nameof(value));
} while (false);
}
if (writeItem && item != null)
{
WriteItemObject(item, item.Name, false);
}
}
#pragma warning restore 0162
/// <summary>
/// Removes the specified alias from session state.
/// </summary>
/// <param name="name">
/// The name of the alias 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.RemoveAlias(name, Force);
}
/// <summary>
/// Gets a flattened view of the alias in session state.
/// </summary>
/// <returns>
/// An IDictionary representing the flattened view of the aliases in
/// session state.
/// </returns>
internal override IDictionary GetSessionStateTable()
{
return (IDictionary)SessionState.Internal.GetAliasTable();
}
/// <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;
AliasInfo aliasInfo = item as AliasInfo;
if (aliasInfo != null)
{
if ((aliasInfo.Options & ScopedItemOptions.Constant) != 0 ||
((aliasInfo.Options & ScopedItemOptions.ReadOnly) != 0 && !Force))
{
SessionStateUnauthorizedAccessException e =
new SessionStateUnauthorizedAccessException(
aliasInfo.Name,
SessionStateCategory.Alias,
"CannotRenameAlias",
SessionStateStrings.CannotRenameAlias);
throw e;
}
result = true;
}
return result;
}
#endregion protected members
}
/// <summary>
/// The dynamic parameter object for the AliasProvider SetItem and NewItem commands.
/// </summary>
public class AliasProviderDynamicParameters
{
/// <summary>
/// Gets or sets the option parameter for the alias.
/// </summary>
[Parameter]
public ScopedItemOptions Options
{
get
{
return _options;
}
set
{
_optionsSet = true;
_options = value;
}
}
private ScopedItemOptions _options;
/// <summary>
/// Determines if the Options parameter was set.
/// </summary>
/// <value></value>
internal bool OptionsSet
{
get { return _optionsSet; }
}
private bool _optionsSet = false;
}
}
|