CmdPal: Linq clean-up (All Apps) (#41551)

Beginning the process of removing System.Linq from CmdPal. This PR
removes it from the All Apps extension.
This commit is contained in:
Michael Jolley
2025-09-26 11:15:34 -05:00
committed by GitHub
parent 7b7bae2889
commit 3bdb5305ba
17 changed files with 380 additions and 165 deletions

View File

@@ -7,7 +7,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Security;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@@ -616,9 +615,24 @@ public class Win32Program : IProgram
}
private static IEnumerable<string> CustomProgramPaths(IEnumerable<ProgramSource> sources, IList<string> suffixes)
=> sources?.Where(programSource => Directory.Exists(programSource.Location) && programSource.Enabled)
.SelectMany(programSource => ProgramPaths(programSource.Location, suffixes))
.ToList() ?? Enumerable.Empty<string>();
{
if (sources is not null)
{
var paths = new List<string>();
foreach (var programSource in sources)
{
if (Directory.Exists(programSource.Location) && programSource.Enabled)
{
paths.AddRange(ProgramPaths(programSource.Location, suffixes));
}
}
return paths;
}
return [];
}
// Function to obtain the list of applications, the locations of which have been added to the env variable PATH
private static List<string> PathEnvironmentProgramPaths(IList<string> suffixes)
@@ -647,9 +661,15 @@ public class Win32Program : IProgram
}
private static List<string> IndexPath(IList<string> suffixes, List<string> indexLocations)
=> indexLocations
.SelectMany(indexLocation => ProgramPaths(indexLocation, suffixes))
.ToList();
{
var paths = new List<string>();
foreach (var indexLocation in indexLocations)
{
paths.AddRange(ProgramPaths(indexLocation, suffixes));
}
return paths;
}
private static List<string> StartMenuProgramPaths(IList<string> suffixes)
{
@@ -691,17 +711,51 @@ public class Win32Program : IProgram
}
}
return paths
.Where(path => suffixes.Any(suffix => path.EndsWith(suffix, StringComparison.InvariantCultureIgnoreCase)))
.Select(ExpandEnvironmentVariables)
.Where(path => path is not null)
.ToList();
var returnedPaths = new List<string>();
foreach (var path in paths)
{
var matchesSuffix = false;
foreach (var suffix in suffixes)
{
if (path.EndsWith(suffix, StringComparison.InvariantCultureIgnoreCase))
{
matchesSuffix = true;
break;
}
}
if (matchesSuffix)
{
var expandedPath = ExpandEnvironmentVariables(path);
if (expandedPath is not null)
{
returnedPaths.Add(expandedPath);
}
}
}
return returnedPaths;
}
private static IEnumerable<string> GetPathsFromRegistry(RegistryKey root)
=> root
.GetSubKeyNames()
.Select(x => GetPathFromRegistrySubkey(root, x));
{
var result = new List<string>();
// Get all subkey names
var subKeyNames = root.GetSubKeyNames();
// Process each subkey to extract the path
foreach (var subkeyName in subKeyNames)
{
var path = GetPathFromRegistrySubkey(root, subkeyName);
if (!string.IsNullOrEmpty(path))
{
result.Add(path);
}
}
return result;
}
private static string GetPathFromRegistrySubkey(RegistryKey root, string subkey)
{
@@ -765,7 +819,28 @@ public class Win32Program : IProgram
}
public static List<Win32Program> DeduplicatePrograms(IEnumerable<Win32Program> programs)
=> new HashSet<Win32Program>(programs, Win32ProgramEqualityComparer.Default).ToList();
{
// Create a HashSet with the custom equality comparer to automatically deduplicate programs
var uniquePrograms = new HashSet<Win32Program>(Win32ProgramEqualityComparer.Default);
// Filter out invalid programs and add valid ones to the HashSet
foreach (var program in programs)
{
if (program?.Valid == true)
{
uniquePrograms.Add(program);
}
}
// Convert the HashSet to a List for return
var result = new List<Win32Program>(uniquePrograms.Count);
foreach (var program in uniquePrograms)
{
result.Add(program);
}
return result;
}
private static Win32Program GetProgramFromPath(string path)
{
@@ -881,8 +956,22 @@ public class Win32Program : IProgram
foreach (var path in source.GetPaths())
{
if (disabledProgramsList.All(x => x.UniqueIdentifier != path) &&
!ExecutableApplicationExtensions.Contains(Extension(path)))
if (ExecutableApplicationExtensions.Contains(Extension(path)))
{
continue;
}
var isDisabled = false;
foreach (var disabledProgram in disabledProgramsList)
{
if (disabledProgram.UniqueIdentifier == path)
{
isDisabled = true;
break;
}
}
if (!isDisabled)
{
pathBag.Add(path);
}
@@ -902,7 +991,17 @@ public class Win32Program : IProgram
foreach (var path in source.GetPaths())
{
if (disabledProgramsList.All(x => x.UniqueIdentifier != path))
var isDisabled = false;
foreach (var disabledProgram in disabledProgramsList)
{
if (disabledProgram.UniqueIdentifier == path)
{
isDisabled = true;
break;
}
}
if (!isDisabled)
{
runCommandPathBag.Add(path);
}
@@ -931,10 +1030,8 @@ public class Win32Program : IProgram
}
});
var programs = programsList.ToList();
var runCommandPrograms = runCommandProgramsList.ToList();
return DeduplicatePrograms(programs.Concat(runCommandPrograms).Where(program => program?.Valid == true));
List<Win32Program> allPrograms = [.. programsList, .. runCommandProgramsList];
return DeduplicatePrograms(allPrograms);
}
catch (Exception e)
{