Skip to content

Commit c639b39

Browse files
committed
Improve directive matching by using named groups
More resilient and self-documenting than using group indexes.
1 parent 1e76957 commit c639b39

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/SmallSharp/EmitTargets.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ namespace SmallSharp;
1313

1414
public class EmitTargets : Task
1515
{
16-
static readonly Regex sdkExpr = new(@"^#:sdk\s+([^@]+?)(@(.+))?$");
17-
static readonly Regex packageExpr = new(@"^#:package\s+([^@]+)@(.+)$");
18-
static readonly Regex propertyExpr = new(@"^#:property\s+([^=]+)=(.+)$");
16+
static readonly Regex sdkExpr = new(@"^#:sdk\s+(?<sdk>[^@]+?)(@(?<version>.+))?$");
17+
static readonly Regex packageExpr = new(@"^#:package\s+(?<id>[^@]+)@(?<version>.+)$");
18+
static readonly Regex propertyExpr = new(@"^#:property\s+(?<name>[^=]+)=(?<value>.+)$");
1919

2020
[Required]
2121
public required ITaskItem StartupFile { get; set; }
@@ -66,8 +66,8 @@ public override bool Execute()
6666
{
6767
if (packageExpr.Match(line) is { Success: true } match)
6868
{
69-
var id = match.Groups[1].Value.Trim();
70-
var version = match.Groups[2].Value.Trim();
69+
var id = match.Groups["id"].Value.Trim();
70+
var version = match.Groups["version"].Value.Trim();
7171

7272
packages.Add(NewTaskItem(id, [("Version", version)]));
7373

@@ -77,8 +77,8 @@ public override bool Execute()
7777
}
7878
else if (sdkExpr.Match(line) is { Success: true } sdkMatch)
7979
{
80-
var name = sdkMatch.Groups[1].Value.Trim();
81-
var version = sdkMatch.Groups[2].Value.Trim();
80+
var name = sdkMatch.Groups["sdk"].Value.Trim();
81+
var version = sdkMatch.Groups["version"].Value.Trim();
8282
if (!string.IsNullOrEmpty(version))
8383
{
8484
sdkItems.Add(NewTaskItem(name, [("Version", version)]));
@@ -92,8 +92,8 @@ public override bool Execute()
9292
}
9393
else if (propertyExpr.Match(line) is { Success: true } propMatch)
9494
{
95-
var name = propMatch.Groups[1].Value.Trim();
96-
var value = propMatch.Groups[2].Value.Trim();
95+
var name = propMatch.Groups["name"].Value.Trim();
96+
var value = propMatch.Groups["value"].Value.Trim();
9797

9898
propItems.Add(NewTaskItem(name, [("Value", value)]));
9999
properties.Add(new XElement(name, value));

0 commit comments

Comments
 (0)