Skip to content

Commit c6f74a9

Browse files
feat: support wildcard executable paths (#63)
Support case-insensitive * and ? matching for persistent process rules and power-plan associations while preserving exact-path behavior.
1 parent a26c792 commit c6f74a9

5 files changed

Lines changed: 178 additions & 8 deletions

File tree

‎Helpers/PathPatternMatcher.cs‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* ThreadPilot - path pattern matcher with wildcard support.
3+
*/
4+
namespace ThreadPilot.Helpers
5+
{
6+
using System;
7+
using System.IO;
8+
9+
public static class PathPatternMatcher
10+
{
11+
public static bool HasWildcard(string? pattern) =>
12+
!string.IsNullOrEmpty(pattern) && (pattern.Contains('*') || pattern.Contains('?'));
13+
14+
public static bool IsPathMatch(string? pattern, string? path)
15+
{
16+
if (string.IsNullOrWhiteSpace(pattern) || string.IsNullOrWhiteSpace(path))
17+
{
18+
return false;
19+
}
20+
21+
var normalizedPattern = Normalize(pattern);
22+
var normalizedPath = Normalize(path);
23+
24+
return HasWildcard(normalizedPattern)
25+
? MatchesWildcard(normalizedPattern, normalizedPath)
26+
: string.Equals(normalizedPattern, normalizedPath, StringComparison.OrdinalIgnoreCase);
27+
}
28+
29+
private static bool MatchesWildcard(string pattern, string path)
30+
{
31+
var patternIndex = 0;
32+
var pathIndex = 0;
33+
var starIndex = -1;
34+
var retryPathIndex = -1;
35+
36+
while (pathIndex < path.Length)
37+
{
38+
if (patternIndex < pattern.Length &&
39+
(pattern[patternIndex] == '?' ||
40+
pattern.AsSpan(patternIndex, 1).Equals(
41+
path.AsSpan(pathIndex, 1),
42+
StringComparison.OrdinalIgnoreCase)))
43+
{
44+
patternIndex++;
45+
pathIndex++;
46+
}
47+
else if (patternIndex < pattern.Length && pattern[patternIndex] == '*')
48+
{
49+
starIndex = patternIndex++;
50+
retryPathIndex = pathIndex;
51+
}
52+
else if (starIndex >= 0)
53+
{
54+
patternIndex = starIndex + 1;
55+
pathIndex = ++retryPathIndex;
56+
}
57+
else
58+
{
59+
return false;
60+
}
61+
}
62+
63+
while (patternIndex < pattern.Length && pattern[patternIndex] == '*')
64+
{
65+
patternIndex++;
66+
}
67+
68+
return patternIndex == pattern.Length;
69+
}
70+
71+
private static string Normalize(string path) =>
72+
path.Trim()
73+
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
74+
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
75+
}
76+
}

‎Models/ProcessPowerPlanAssociation.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace ThreadPilot.Models
44
using System.Collections.Generic;
55
using System.IO;
66
using CommunityToolkit.Mvvm.ComponentModel;
7+
using ThreadPilot.Helpers;
78
using ThreadPilot.Models.Core;
89

910
public partial class ProcessPowerPlanAssociation : ObservableObject, IModel
@@ -72,7 +73,7 @@ public bool MatchesProcess(ProcessModel process)
7273

7374
if (this.MatchByPath && !string.IsNullOrEmpty(this.ExecutablePath))
7475
{
75-
return string.Equals(process.ExecutablePath, this.ExecutablePath, StringComparison.OrdinalIgnoreCase);
76+
return PathPatternMatcher.IsPathMatch(this.ExecutablePath, process.ExecutablePath);
7677
}
7778
else
7879
{

‎Services/PersistentProcessRuleMatcher.cs‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
*/
44
namespace ThreadPilot.Services
55
{
6+
using System;
67
using System.IO;
8+
using ThreadPilot.Helpers;
79
using ThreadPilot.Models;
810

911
public interface IPersistentProcessRuleMatcher
@@ -28,7 +30,7 @@ public bool IsMatch(PersistentProcessRule rule, ProcessModel process)
2830
{
2931
var processPath = NormalizePath(process.ExecutablePath);
3032
return !string.IsNullOrWhiteSpace(processPath) &&
31-
string.Equals(rulePath, processPath, StringComparison.OrdinalIgnoreCase);
33+
PathPatternMatcher.IsPathMatch(rulePath, processPath);
3234
}
3335

3436
return !string.IsNullOrWhiteSpace(rule.ProcessName) &&
@@ -43,13 +45,16 @@ public bool IsMatch(PersistentProcessRule rule, ProcessModel process)
4345
}
4446

4547
var trimmed = path.Trim();
46-
try
48+
if (!PathPatternMatcher.HasWildcard(trimmed))
4749
{
48-
trimmed = Path.GetFullPath(trimmed);
49-
}
50-
catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
51-
{
52-
// Keep matching best-effort for inaccessible or malformed process paths.
50+
try
51+
{
52+
trimmed = Path.GetFullPath(trimmed);
53+
}
54+
catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
55+
{
56+
// Keep matching best-effort for inaccessible or malformed process paths.
57+
}
5358
}
5459

5560
return trimmed.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);

‎Tests/ThreadPilot.Core.Tests/PersistentProcessRuleMatcherTests.cs‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,61 @@ public void IsMatch_WithNullPaths_DoesNotThrow()
7676
Assert.Null(exception);
7777
}
7878

79+
[Fact]
80+
public void IsMatch_WithWildcardExecutablePath_MatchesPattern()
81+
{
82+
var rule = CreateRule(executablePath: @"*Raycast*\backend\node.exe");
83+
var process = CreateProcess(name: "node.exe", executablePath: @"C:\Program Files\WindowsApps\Raycast.Raycast_2.5.3.0_x64__qypenmj9wpt2a\Raycast\backend\node.exe");
84+
85+
var result = this.matcher.IsMatch(rule, process);
86+
87+
Assert.True(result);
88+
}
89+
90+
[Fact]
91+
public void IsMatch_WithWildcardExecutablePath_RejectsNonMatchingPath()
92+
{
93+
var rule = CreateRule(executablePath: @"*Raycast*\backend\node.exe");
94+
var process = CreateProcess(name: "node.exe", executablePath: @"C:\nvm4w\nodejs\node.exe");
95+
96+
var result = this.matcher.IsMatch(rule, process);
97+
98+
Assert.False(result);
99+
}
100+
101+
[Fact]
102+
public void IsMatch_WithWildcardExecutablePath_MatchesVersionGlob()
103+
{
104+
var rule = CreateRule(executablePath: @"C:\Program Files\WindowsApps\Raycast.Raycast_*_x64__*\Raycast\backend\node.exe");
105+
var process = CreateProcess(name: "node.exe", executablePath: @"C:\Program Files\WindowsApps\Raycast.Raycast_2.5.3.0_x64__qypenmj9wpt2a\Raycast\backend\node.exe");
106+
107+
var result = this.matcher.IsMatch(rule, process);
108+
109+
Assert.True(result);
110+
}
111+
112+
[Fact]
113+
public void IsMatch_WithQuestionMarkWildcard_MatchesSingleCharacter()
114+
{
115+
var rule = CreateRule(executablePath: @"C:\Games\App?\Game.exe");
116+
var matchProcess = CreateProcess(name: "game.exe", executablePath: @"C:\Games\App1\Game.exe");
117+
var noMatchProcess = CreateProcess(name: "game.exe", executablePath: @"C:\Games\App12\Game.exe");
118+
119+
Assert.True(this.matcher.IsMatch(rule, matchProcess));
120+
Assert.False(this.matcher.IsMatch(rule, noMatchProcess));
121+
}
122+
123+
[Fact]
124+
public void IsMatch_WithForwardSlashWildcard_MatchesNormalizedBackslashes()
125+
{
126+
var rule = CreateRule(executablePath: @"*/Raycast/backend/node.exe");
127+
var process = CreateProcess(name: "node.exe", executablePath: @"C:\Program Files\WindowsApps\Raycast.Raycast_2.5.3.0_x64__qypenmj9wpt2a\Raycast\backend\node.exe");
128+
129+
var result = this.matcher.IsMatch(rule, process);
130+
131+
Assert.True(result);
132+
}
133+
79134
private static PersistentProcessRule CreateRule(string? processName = null, string? executablePath = null) =>
80135
new()
81136
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* ThreadPilot - process power plan association tests.
3+
*/
4+
namespace ThreadPilot.Core.Tests
5+
{
6+
using ThreadPilot.Models;
7+
8+
public sealed class ProcessPowerPlanAssociationTests
9+
{
10+
[Fact]
11+
public void MatchesProcess_WithWildcardPath_MatchesOnlyTargetPath()
12+
{
13+
var association = new ProcessPowerPlanAssociation
14+
{
15+
IsEnabled = true,
16+
MatchByPath = true,
17+
ExecutableName = "node",
18+
ExecutablePath = @"*Raycast*\backend\node.exe",
19+
};
20+
21+
Assert.True(association.MatchesProcess(new ProcessModel
22+
{
23+
Name = "node.exe",
24+
ExecutablePath = @"C:\Program Files\WindowsApps\Raycast.Raycast_2.5.3.0_x64__publisher\Raycast\backend\NODE.EXE",
25+
}));
26+
Assert.False(association.MatchesProcess(new ProcessModel
27+
{
28+
Name = "node.exe",
29+
ExecutablePath = @"C:\nvm4w\nodejs\node.exe",
30+
}));
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)