File size: 4,721 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Internal;

namespace Microsoft.PowerShell.Commands
{
    /// <summary>
    /// Implements Get-ExperimentalFeature cmdlet.
    /// </summary>
    [Cmdlet(VerbsCommon.Get, "ExperimentalFeature", HelpUri = "https://go.microsoft.com/fwlink/?linkid=2096786")]
    [OutputType(typeof(ExperimentalFeature))]
    public class GetExperimentalFeatureCommand : PSCmdlet
    {
        /// <summary>
        /// Get and set the feature names.
        /// </summary>
        [Parameter(ValueFromPipeline = true, Position = 0)]
        [ArgumentCompleter(typeof(ExperimentalFeatureNameCompleter))]
        [ValidateNotNullOrEmpty]
        public string[] Name { get; set; }

        /// <summary>
        /// ProcessRecord method of this cmdlet.
        /// </summary>
        protected override void ProcessRecord()
        {
            const WildcardOptions wildcardOptions = WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant;
            IEnumerable<WildcardPattern> namePatterns = SessionStateUtilities.CreateWildcardsFromStrings(Name, wildcardOptions);

            foreach (ExperimentalFeature feature in GetAvailableExperimentalFeatures(namePatterns).OrderBy(GetSortingString))
            {
                WriteObject(feature);
            }
        }

        /// <summary>
        /// Construct the string for sorting experimental feature records.
        /// </summary>
        /// <remarks>
        /// Engine features come before module features.
        /// Within engine features and module features, features are ordered by name.
        /// </remarks>
        private static (int, string) GetSortingString(ExperimentalFeature feature)
        {
            return ExperimentalFeature.EngineSource.Equals(feature.Source, StringComparison.OrdinalIgnoreCase)
                        ? (0, feature.Name)
                        : (1, feature.Name);
        }

        /// <summary>
        /// Get available experimental features based on the specified name patterns.
        /// </summary>
        internal IEnumerable<ExperimentalFeature> GetAvailableExperimentalFeatures(IEnumerable<WildcardPattern> namePatterns)
        {
            foreach (ExperimentalFeature feature in ExperimentalFeature.EngineExperimentalFeatures)
            {
                if (SessionStateUtilities.MatchesAnyWildcardPattern(feature.Name, namePatterns, defaultValue: true))
                {
                    yield return feature;
                }
            }

            foreach (string moduleFile in GetValidModuleFiles(moduleNamesToFind: null))
            {
                ExperimentalFeature[] features = ModuleIntrinsics.GetExperimentalFeature(moduleFile);
                foreach (var feature in features)
                {
                    if (SessionStateUtilities.MatchesAnyWildcardPattern(feature.Name, namePatterns, defaultValue: true))
                    {
                        yield return feature;
                    }
                }
            }
        }

        /// <summary>
        /// Get valid module files from module paths.
        /// </summary>
        private IEnumerable<string> GetValidModuleFiles(HashSet<string> moduleNamesToFind)
        {
            var modulePaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
            foreach (string path in ModuleIntrinsics.GetModulePath(includeSystemModulePath: false, Context))
            {
                string uniquePath = path.TrimEnd(Utils.Separators.Directory);
                if (!modulePaths.Add(uniquePath))
                {
                    continue;
                }

                foreach (string moduleFile in ModuleUtils.GetDefaultAvailableModuleFiles(uniquePath))
                {
                    // We only care about module manifest files because that's where experimental features are declared.
                    if (!moduleFile.EndsWith(StringLiterals.PowerShellDataFileExtension, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    if (moduleNamesToFind != null)
                    {
                        string currentModuleName = ModuleIntrinsics.GetModuleName(moduleFile);
                        if (!moduleNamesToFind.Contains(currentModuleName))
                        {
                            continue;
                        }
                    }

                    yield return moduleFile;
                }
            }
        }
    }
}