"Fossies" - the Fresh Open Source Software Archive 
Member "PowerShell-7.2.6/src/System.Management.Automation/namespaces/AliasProvider.cs" (11 Aug 2022, 11652 Bytes) of package /linux/misc/PowerShell-7.2.6.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C# source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "AliasProvider.cs" see the
Fossies "Dox" file reference documentation.
1 // Copyright (c) Microsoft Corporation.
2 // Licensed under the MIT License.
3
4 using System.Collections;
5 using System.Collections.ObjectModel;
6 using System.Management.Automation;
7 using System.Management.Automation.Provider;
8
9 using Dbg = System.Management.Automation;
10
11 namespace Microsoft.PowerShell.Commands
12 {
13 /// <summary>
14 /// This provider is the data accessor for shell aliases. It uses
15 /// the SessionStateProviderBase as the base class to produce a view on
16 /// session state data.
17 /// </summary>
18 [CmdletProvider(AliasProvider.ProviderName, ProviderCapabilities.ShouldProcess)]
19 [OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.SetItem)]
20 [OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.RenameItem)]
21 [OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.CopyItem)]
22 [OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.GetChildItem)]
23 [OutputType(typeof(AliasInfo), ProviderCmdlet = ProviderCmdlet.NewItem)]
24 public sealed class AliasProvider : SessionStateProviderBase
25 {
26 /// <summary>
27 /// Gets the name of the provider.
28 /// </summary>
29 public const string ProviderName = "Alias";
30
31 #region Constructor
32
33 /// <summary>
34 /// The constructor for the provider that exposes variables to the user
35 /// as drives.
36 /// </summary>
37 public AliasProvider()
38 {
39 }
40
41 #endregion Constructor
42
43 #region DriveCmdletProvider overrides
44
45 /// <summary>
46 /// Initializes the alias drive.
47 /// </summary>
48 /// <returns>
49 /// An array of a single PSDriveInfo object representing the alias drive.
50 /// </returns>
51 protected override Collection<PSDriveInfo> InitializeDefaultDrives()
52 {
53 string description = SessionStateStrings.AliasDriveDescription;
54
55 PSDriveInfo aliasDrive =
56 new PSDriveInfo(
57 DriveNames.AliasDrive,
58 ProviderInfo,
59 string.Empty,
60 description,
61 null);
62
63 Collection<PSDriveInfo> drives = new Collection<PSDriveInfo>();
64 drives.Add(aliasDrive);
65 return drives;
66 }
67
68 #endregion DriveCmdletProvider overrides
69
70 #region Dynamic Parameters
71
72 /// <summary>
73 /// Gets the dynamic parameters for the NewItem cmdlet.
74 /// </summary>
75 /// <param name="path">
76 /// Ignored.
77 /// </param>
78 /// <param name="type">
79 /// Ignored.
80 /// </param>
81 /// <param name="newItemValue">
82 /// Ignored.
83 /// </param>
84 /// <returns>
85 /// An instance of AliasProviderDynamicParameters which is the dynamic parameters for
86 /// NewItem.
87 /// </returns>
88 protected override object NewItemDynamicParameters(string path, string type, object newItemValue)
89 {
90 return new AliasProviderDynamicParameters();
91 }
92
93 /// <summary>
94 /// Gets the dynamic parameters for the NewItem cmdlet.
95 /// </summary>
96 /// <param name="path">
97 /// Ignored.
98 /// </param>
99 /// <param name="value">
100 /// Ignored.
101 /// </param>
102 /// <returns>
103 /// An instance of AliasProviderDynamicParameters which is the dynamic parameters for
104 /// SetItem.
105 /// </returns>
106 protected override object SetItemDynamicParameters(string path, object value)
107 {
108 return new AliasProviderDynamicParameters();
109 }
110
111 #endregion Dynamic Parameters
112
113 #region protected members
114
115 /// <summary>
116 /// Gets a alias from session state.
117 /// </summary>
118 /// <param name="name">
119 /// The name of the alias to retrieve.
120 /// </param>
121 /// <returns>
122 /// A DictionaryEntry that represents the value of the alias.
123 /// </returns>
124 internal override object GetSessionStateItem(string name)
125 {
126 Dbg.Diagnostics.Assert(
127 !string.IsNullOrEmpty(name),
128 "The caller should verify this parameter");
129
130 AliasInfo value = SessionState.Internal.GetAlias(name, Context.Origin);
131
132 return value;
133 }
134
135 /// <summary>
136 /// Since items are often more than their value, this method should
137 /// be overridden to provide the value for an item.
138 /// </summary>
139 /// <param name="item">
140 /// The item to extract the value from.
141 /// </param>
142 /// <returns>
143 /// The value of the specified item.
144 /// </returns>
145 /// <remarks>
146 /// The default implementation will get
147 /// the Value property of a DictionaryEntry
148 /// </remarks>
149 internal override object GetValueOfItem(object item)
150 {
151 Dbg.Diagnostics.Assert(
152 item != null,
153 "Caller should verify the item parameter");
154
155 object value = item;
156
157 AliasInfo aliasInfo = item as AliasInfo;
158 if (aliasInfo != null)
159 {
160 value = aliasInfo.Definition;
161 }
162
163 return value;
164 }
165
166 /// <summary>
167 /// Sets the alias of the specified name to the specified value.
168 /// </summary>
169 /// <param name="name">
170 /// The name of the alias to set.
171 /// </param>
172 /// <param name="value">
173 /// The new value for the alias.
174 /// </param>
175 /// <param name="writeItem">
176 /// If true, the item that was set should be written to WriteItemObject.
177 /// </param>
178 #pragma warning disable 0162
179 internal override void SetSessionStateItem(string name, object value, bool writeItem)
180 {
181 Dbg.Diagnostics.Assert(
182 !string.IsNullOrEmpty(name),
183 "The caller should verify this parameter");
184
185 AliasProviderDynamicParameters dynamicParameters =
186 DynamicParameters as AliasProviderDynamicParameters;
187
188 AliasInfo item = null;
189
190 bool dynamicParametersSpecified = dynamicParameters != null && dynamicParameters.OptionsSet;
191
192 if (value == null)
193 {
194 if (dynamicParametersSpecified)
195 {
196 item = (AliasInfo)GetSessionStateItem(name);
197
198 if (item != null)
199 {
200 item.SetOptions(dynamicParameters.Options, Force);
201 }
202 }
203 else
204 {
205 RemoveSessionStateItem(name);
206 }
207 }
208 else
209 {
210 do // false loop
211 {
212 string stringValue = value as string;
213 if (stringValue != null)
214 {
215 if (dynamicParametersSpecified)
216 {
217 item = SessionState.Internal.SetAliasValue(name, stringValue, dynamicParameters.Options, Force, Context.Origin);
218 }
219 else
220 {
221 item = SessionState.Internal.SetAliasValue(name, stringValue, Force, Context.Origin);
222 }
223
224 break;
225 }
226
227 AliasInfo alias = value as AliasInfo;
228 if (alias != null)
229 {
230 AliasInfo newAliasInfo =
231 new AliasInfo(
232 name,
233 alias.Definition,
234 this.Context.ExecutionContext,
235 alias.Options);
236
237 if (dynamicParametersSpecified)
238 {
239 newAliasInfo.SetOptions(dynamicParameters.Options, Force);
240 }
241
242 item = SessionState.Internal.SetAliasItem(newAliasInfo, Force, Context.Origin);
243 break;
244 }
245
246 throw PSTraceSource.NewArgumentException(nameof(value));
247 } while (false);
248 }
249
250 if (writeItem && item != null)
251 {
252 WriteItemObject(item, item.Name, false);
253 }
254 }
255 #pragma warning restore 0162
256
257 /// <summary>
258 /// Removes the specified alias from session state.
259 /// </summary>
260 /// <param name="name">
261 /// The name of the alias to remove from session state.
262 /// </param>
263 internal override void RemoveSessionStateItem(string name)
264 {
265 Dbg.Diagnostics.Assert(
266 !string.IsNullOrEmpty(name),
267 "The caller should verify this parameter");
268
269 SessionState.Internal.RemoveAlias(name, Force);
270 }
271
272 /// <summary>
273 /// Gets a flattened view of the alias in session state.
274 /// </summary>
275 /// <returns>
276 /// An IDictionary representing the flattened view of the aliases in
277 /// session state.
278 /// </returns>
279 internal override IDictionary GetSessionStateTable()
280 {
281 return (IDictionary)SessionState.Internal.GetAliasTable();
282 }
283
284 /// <summary>
285 /// Determines if the item can be renamed. Derived classes that need
286 /// to perform a check should override this method.
287 /// </summary>
288 /// <param name="item">
289 /// The item to verify if it can be renamed.
290 /// </param>
291 /// <returns>
292 /// true if the item can be renamed or false otherwise.
293 /// </returns>
294 internal override bool CanRenameItem(object item)
295 {
296 bool result = false;
297
298 AliasInfo aliasInfo = item as AliasInfo;
299 if (aliasInfo != null)
300 {
301 if ((aliasInfo.Options & ScopedItemOptions.Constant) != 0 ||
302 ((aliasInfo.Options & ScopedItemOptions.ReadOnly) != 0 && !Force))
303 {
304 SessionStateUnauthorizedAccessException e =
305 new SessionStateUnauthorizedAccessException(
306 aliasInfo.Name,
307 SessionStateCategory.Alias,
308 "CannotRenameAlias",
309 SessionStateStrings.CannotRenameAlias);
310
311 throw e;
312 }
313
314 result = true;
315 }
316
317 return result;
318 }
319
320 #endregion protected members
321 }
322
323 /// <summary>
324 /// The dynamic parameter object for the AliasProvider SetItem and NewItem commands.
325 /// </summary>
326 public class AliasProviderDynamicParameters
327 {
328 /// <summary>
329 /// Gets or sets the option parameter for the alias.
330 /// </summary>
331 [Parameter]
332 public ScopedItemOptions Options
333 {
334 get
335 {
336 return _options;
337 }
338
339 set
340 {
341 _optionsSet = true;
342 _options = value;
343 }
344 }
345
346 private ScopedItemOptions _options;
347
348 /// <summary>
349 /// Determines if the Options parameter was set.
350 /// </summary>
351 /// <value></value>
352 internal bool OptionsSet
353 {
354 get { return _optionsSet; }
355 }
356
357 private bool _optionsSet = false;
358 }
359 }