Skip to content

Commit 0bbf54f

Browse files
committed
Address @JamesWTruher's feedback
1 parent 69c483c commit 0bbf54f

File tree

5 files changed

+54
-56
lines changed

5 files changed

+54
-56
lines changed

PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Collection/CompatibilityProfileCollector.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,27 @@ public Builder()
4242
}
4343

4444
/// <summary>
45-
/// Add path prefixes of modules to exclude.
45+
/// Modules on paths starting with these prefixes will excluded from profile collection.
4646
/// </summary>
47-
/// <param name="modulePrefixes">Path prefixes of modules to exclude.</param>
48-
public Builder ExcludedModulePathPrefixes(IReadOnlyCollection<string> modulePrefixes)
47+
public IReadOnlyCollection<string> ExcludedModulePathPrefixes
4948
{
50-
_pwshDataCollectorBuilder.ExcludedModulePathPrefixes(modulePrefixes);
51-
return this;
49+
get => _pwshDataCollectorBuilder.ExcludedModulePathPrefixes;
50+
set
51+
{
52+
_pwshDataCollectorBuilder.ExcludedModulePathPrefixes = value;
53+
}
5254
}
5355

5456
/// <summary>
55-
/// Add path prefixes of assemblies to exclude.
57+
/// .NET assemblies on paths starting with these prefixes will excluded from profile collection.
5658
/// </summary>
57-
/// <param name="assemblyPrefixes">Path prefixes of assemblies to exclude.</param>
58-
public Builder ExcludeAssemblyPathPrefixes(IReadOnlyCollection<string> assemblyPrefixes)
59+
public IReadOnlyCollection<string> ExcludedAssemblyPathPrefixes
5960
{
60-
_typeDataColletorBuilder.ExcludedAssemblyPathPrefixes(assemblyPrefixes);
61-
return this;
61+
get => _typeDataColletorBuilder.ExcludedAssemblyPathPrefixes;
62+
set
63+
{
64+
_typeDataColletorBuilder.ExcludedAssemblyPathPrefixes = value;
65+
}
6266
}
6367

6468
/// <summary>
@@ -269,9 +273,9 @@ protected virtual void Dispose(bool disposing)
269273
{
270274
if (disposing)
271275
{
272-
_pwsh.Dispose();
273-
_platformInfoCollector.Dispose();
274276
_pwshDataCollector.Dispose();
277+
_platformInfoCollector.Dispose();
278+
_pwsh.Dispose();
275279
}
276280

277281
_pwsh = null;

PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Collection/PlatformInformationCollector.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public OperatingSystemData GetOperatingSystemData()
143143
{
144144
osData.ServicePack = Environment.OSVersion.ServicePack;
145145
}
146-
osData.SkuId = GetSkuId();
146+
osData.SkuId = GetWinSkuId();
147147
break;
148148

149149
case OSFamily.Linux:
@@ -265,8 +265,11 @@ private DotnetRuntime GetDotnetRuntime()
265265
/// Get the Windows SKU ID of the current PowerShell session.
266266
/// </summary>
267267
/// <returns>An unsigned 32-bit integer representing the SKU of the current Windows OS.</returns>
268-
private uint GetSkuId()
268+
private uint GetWinSkuId()
269269
{
270+
// There are a few ways to get this, with varying support on different systems.
271+
// So we try them in order from least to most expensive.
272+
270273
// If we have a cached value here, try this first
271274
if (_lazyWin32OperatingSystemInfo.IsValueCreated
272275
&& _lazyWin32OperatingSystemInfo.Value != null

PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Collection/PowerShellDataCollector.cs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,10 @@ public class PowerShellDataCollector : IDisposable
2727
/// </summary>
2828
public class Builder
2929
{
30-
private IReadOnlyCollection<string> _modulePrefixes;
31-
3230
/// <summary>
33-
/// Add path prefixes that modules should be excluded with.
31+
/// Modules on paths underneath any of these will be excluded.
3432
/// </summary>
35-
/// <param name="modulePrefixes">The path prefixes of modules to exclude.</param>
36-
public Builder ExcludedModulePathPrefixes(IReadOnlyCollection<string> modulePrefixes)
37-
{
38-
_modulePrefixes = modulePrefixes;
39-
return this;
40-
}
33+
public IReadOnlyCollection<string> ExcludedModulePathPrefixes { get; set; }
4134

4235
/// <summary>
4336
/// Build a new PowerShellDataCollector with the given configuration.
@@ -47,7 +40,7 @@ public Builder ExcludedModulePathPrefixes(IReadOnlyCollection<string> modulePref
4740
/// <returns>The constructed PowerShell data collector object.</returns>
4841
public PowerShellDataCollector Build(SMA.PowerShell pwsh, PowerShellVersion psVersion)
4942
{
50-
return new PowerShellDataCollector(pwsh, psVersion, _modulePrefixes);
43+
return new PowerShellDataCollector(pwsh, psVersion, ExcludedModulePathPrefixes);
5144
}
5245
}
5346

@@ -57,7 +50,7 @@ public PowerShellDataCollector Build(SMA.PowerShell pwsh, PowerShellVersion psVe
5750

5851
private const string THIS_MODULE_NAME = "PSCompatibilityCollector";
5952

60-
private static readonly Regex s_typeDataRegex = new Regex("Error in TypeData \"([A-Za-z.]+)\"", RegexOptions.Compiled);
53+
private static readonly Regex s_typeDataRegex = new Regex("Error in TypeData \"([A-Za-z\\.]+)\"", RegexOptions.Compiled);
6154

6255
private static readonly CmdletInfo s_gmoInfo = new CmdletInfo("Get-Module", typeof(GetModuleCommand));
6356

@@ -151,7 +144,7 @@ public IEnumerable<Tuple<string, Version, ModuleData>> GetModulesData(out IEnume
151144

152145
Tuple<string, Version, ModuleData> moduleData = LoadAndGetModuleData(module, out Exception error);
153146

154-
if (moduleData == null)
147+
if (moduleData == null && error != null)
155148
{
156149
errs.Add(error);
157150
continue;
@@ -306,26 +299,30 @@ public Tuple<string, Version, ModuleData> GetCoreModuleData()
306299
// Get default variables and core aliases out of a fresh runspace
307300
using (SMA.PowerShell freshPwsh = SMA.PowerShell.Create(RunspaceMode.NewRunspace))
308301
{
309-
Collection<PSVariable> defaultVariables = freshPwsh.AddCommand("Get-ChildItem")
310-
.AddParameter("Path", "variable:")
311-
.InvokeAndClear<PSVariable>();
302+
Collection<PSObject> varsAndAliases = freshPwsh.AddCommand("Get-ChildItem")
303+
.AddParameter("Path", "variable:,alias:")
304+
.InvokeAndClear();
305+
306+
var variables = new List<string>();
307+
var aliases = new JsonCaseInsensitiveStringDictionary<string>();
312308

313-
var variableArray = new string[defaultVariables.Count];
314-
for (int i = 0; i < variableArray.Length; i++)
309+
foreach (PSObject returnedObject in varsAndAliases)
315310
{
316-
variableArray[i] = defaultVariables[i].Name;
317-
}
318-
moduleData.Variables = variableArray;
311+
switch (returnedObject.BaseObject)
312+
{
313+
case PSVariable variable:
314+
variables.Add(variable.Name);
315+
continue;
319316

320-
IEnumerable<AliasInfo> coreAliases = freshPwsh.AddCommand("Get-ChildItem")
321-
.AddParameter("Path", "alias:")
322-
.InvokeAndClear<AliasInfo>();
317+
case AliasInfo alias:
318+
aliases.Add(alias.Name, GetSingleAliasData(alias));
319+
continue;
323320

324-
var aliases = new JsonCaseInsensitiveStringDictionary<string>();
325-
foreach (AliasInfo aliasInfo in coreAliases)
326-
{
327-
aliases.Add(aliasInfo.Name, GetSingleAliasData(aliasInfo));
321+
// Skip over other objects we get back, since there's no reason to throw
322+
}
328323
}
324+
325+
moduleData.Variables = variables.ToArray();
329326
moduleData.Aliases = aliases;
330327
}
331328

@@ -676,8 +673,9 @@ private bool IsExcludedPath(string path)
676673

677674
private static ReadOnlySet<string> GetPowerShellCommonParameterNames()
678675
{
676+
const BindingFlags propertyBindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
679677
var set = new List<string>();
680-
foreach (PropertyInfo property in typeof(CommonParameters).GetProperties())
678+
foreach (PropertyInfo property in typeof(CommonParameters).GetProperties(propertyBindingFlags))
681679
{
682680
set.Add(property.Name);
683681
}

PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Collection/TypeDataCollector.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,19 @@ public class TypeDataCollector
2727
/// </summary>
2828
public class Builder
2929
{
30-
private IReadOnlyCollection<string> _excludedAssemblyPathPrefixes;
31-
3230
/// <summary>
33-
/// Exclude assemblies on paths starting with the given prefixes when collecting
34-
/// .NET type information.
31+
/// Assemblies on paths starting with these prefixes will be excluded
32+
/// when collecting .NET type information.
3533
/// </summary>
36-
/// <param name="assemblyPrefixes">The path prefixes to exclude assemblies with.</param>
37-
public Builder ExcludedAssemblyPathPrefixes(IReadOnlyCollection<string> assemblyPrefixes)
38-
{
39-
_excludedAssemblyPathPrefixes = assemblyPrefixes;
40-
return this;
41-
}
34+
public IReadOnlyCollection<string> ExcludedAssemblyPathPrefixes { get; set; }
4235

4336
/// <summary>
4437
/// Build the configured TypeDataCollector object.
4538
/// </summary>
4639
/// <returns>The constructed TypeDataCollector object.</returns>
4740
public TypeDataCollector Build()
4841
{
49-
return new TypeDataCollector(_excludedAssemblyPathPrefixes);
42+
return new TypeDataCollector(ExcludedAssemblyPathPrefixes);
5043
}
5144
}
5245

@@ -190,7 +183,7 @@ public KeyValuePair<string, AssemblyData> AssembleAssembly(Assembly asm)
190183
if (types.Length > 0)
191184
{
192185
namespacedTypes = new JsonDictionary<string, JsonDictionary<string, TypeData>>();
193-
foreach (Type type in asm.GetTypes())
186+
foreach (Type type in types)
194187
{
195188
if (!type.IsPublic)
196189
{

PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Commands/NewPSCompatibilityProfileCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ protected override void EndProcessing()
9696

9797
if (ExcludeModulePathPrefix != null && ExcludeModulePathPrefix.Length > 0)
9898
{
99-
collectorBuilder.ExcludedModulePathPrefixes(ExcludeModulePathPrefix);
99+
collectorBuilder.ExcludedModulePathPrefixes = ExcludeModulePathPrefix;
100100
}
101101

102102
if (ExcludeAssemblyPathPrefix != null && ExcludeAssemblyPathPrefix.Length > 0)
103103
{
104-
collectorBuilder.ExcludeAssemblyPathPrefixes(ExcludeAssemblyPathPrefix);
104+
collectorBuilder.ExcludedAssemblyPathPrefixes = ExcludeAssemblyPathPrefix;
105105
}
106106

107107
using (var profileCollector = collectorBuilder.Build(pwsh))

0 commit comments

Comments
 (0)