File size: 5,677 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Configuration;
using System.Management.Automation.Internal;
using System.Management.Automation.Language;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Base class for Enable/Disable-ExperimentalFeature cmdlet.
/// </summary>
public class EnableDisableExperimentalFeatureCommandBase : PSCmdlet
{
/// <summary>
/// Gets or sets the feature names.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true, Position = 0, Mandatory = true)]
[ArgumentCompleter(typeof(ExperimentalFeatureNameCompleter))]
public string[] Name { get; set; }
/// <summary>
/// Gets or sets the scope of persistence of updating the PowerShell configuration json.
/// </summary>
[Parameter]
public ConfigScope Scope { get; set; } = ConfigScope.CurrentUser;
/// <summary>
/// EndProcessing method.
/// </summary>
protected override void EndProcessing()
{
WriteWarning(ExperimentalFeatureStrings.ExperimentalFeaturePending);
}
}
/// <summary>
/// Implements Enable-ExperimentalFeature cmdlet.
/// </summary>
[Cmdlet(VerbsLifecycle.Enable, "ExperimentalFeature", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2046964")]
public class EnableExperimentalFeatureCommand : EnableDisableExperimentalFeatureCommandBase
{
/// <summary>
/// ProcessRecord method of this cmdlet.
/// </summary>
protected override void ProcessRecord()
{
ExperimentalFeatureConfigHelper.UpdateConfig(this, Name, Scope, enable: true);
}
}
/// <summary>
/// Implements Enable-ExperimentalFeature cmdlet.
/// </summary>
[Cmdlet(VerbsLifecycle.Disable, "ExperimentalFeature", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2046963")]
public class DisableExperimentalFeatureCommand : EnableDisableExperimentalFeatureCommandBase
{
/// <summary>
/// ProcessRecord method of this cmdlet.
/// </summary>
protected override void ProcessRecord()
{
ExperimentalFeatureConfigHelper.UpdateConfig(this, Name, Scope, enable: false);
}
}
internal static class ExperimentalFeatureConfigHelper
{
internal static void UpdateConfig(PSCmdlet cmdlet, string[] name, ConfigScope scope, bool enable)
{
IEnumerable<WildcardPattern> namePatterns = SessionStateUtilities.CreateWildcardsFromStrings(name, WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant);
GetExperimentalFeatureCommand getExperimentalFeatureCommand = new GetExperimentalFeatureCommand();
getExperimentalFeatureCommand.Context = cmdlet.Context;
bool foundFeature = false;
foreach (ExperimentalFeature feature in getExperimentalFeatureCommand.GetAvailableExperimentalFeatures(namePatterns))
{
foundFeature = true;
if (!cmdlet.ShouldProcess(feature.Name))
{
return;
}
PowerShellConfig.Instance.SetExperimentalFeatures(scope, feature.Name, enable);
}
if (!foundFeature)
{
string errMsg = string.Format(CultureInfo.InvariantCulture, ExperimentalFeatureStrings.ExperimentalFeatureNameNotFound, name);
cmdlet.WriteError(new ErrorRecord(new ItemNotFoundException(errMsg), "ItemNotFoundException", ErrorCategory.ObjectNotFound, name));
return;
}
}
}
/// <summary>
/// Provides argument completion for ExperimentalFeature names.
/// </summary>
public class ExperimentalFeatureNameCompleter : IArgumentCompleter
{
/// <summary>
/// Returns completion results for experimental feature names used as arguments to experimental feature cmdlets.
/// </summary>
/// <param name="commandName">The command name.</param>
/// <param name="parameterName">The parameter name.</param>
/// <param name="wordToComplete">The word to complete.</param>
/// <param name="commandAst">The command AST.</param>
/// <param name="fakeBoundParameters">The fake bound parameters.</param>
/// <returns>List of Completion Results.</returns>
public IEnumerable<CompletionResult> CompleteArgument(
string commandName,
string parameterName,
string wordToComplete,
CommandAst commandAst,
IDictionary fakeBoundParameters)
{
SortedSet<string> expirmentalFeatures = new(StringComparer.OrdinalIgnoreCase);
foreach (ExperimentalFeature feature in GetExperimentalFeatures())
{
expirmentalFeatures.Add(feature.Name);
}
return CompletionHelpers.GetMatchingResults(wordToComplete, expirmentalFeatures);
}
private static Collection<ExperimentalFeature> GetExperimentalFeatures()
{
using var ps = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace);
ps.AddCommand("Get-ExperimentalFeature");
return ps.Invoke<ExperimentalFeature>();
}
}
}
|