Skip to content

Commit

Permalink
use MatchesSimpleExpression instead of janky regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellpeck committed Aug 31, 2024
1 parent 22730c7 commit b410b81
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
12 changes: 5 additions & 7 deletions Contentless/OverrideInfo.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using System.Text.RegularExpressions;

namespace Contentless;
namespace Contentless;

public class OverrideInfo {

public readonly Regex Regex;
public readonly string Expression;
public readonly Override Override;

public OverrideInfo(Regex regex, Override over) {
this.Regex = regex;
public OverrideInfo(string expression, Override over) {
this.Expression = expression;
this.Override = over;
}

}
}
13 changes: 4 additions & 9 deletions Contentless/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Enumeration;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.Build.Construction;
using Microsoft.Xna.Framework.Content.Pipeline;
using Newtonsoft.Json;
Expand Down Expand Up @@ -143,7 +143,6 @@ private static int ProcessContentFile(FileInfo contentFile, string packagesFolde
Console.WriteLine($"Found possible importer types {string.Join(", ", importers)}");
Console.WriteLine($"Found possible processor types {string.Join(", ", processors)}");

var excluded = config.ExcludedFiles.Select(Program.GlobbyRegex).ToArray();
var overrides = Program.GetOverrides(config.Overrides).ToArray();
foreach (var file in contentFile.Directory.EnumerateFiles("*", SearchOption.AllDirectories)) {
// is the file the content or config file?
Expand All @@ -152,7 +151,7 @@ private static int ProcessContentFile(FileInfo contentFile, string packagesFolde
var relative = Path.GetRelativePath(contentFile.DirectoryName, file.FullName).Replace('\\', '/');

// is the file in an excluded directory?
if (excluded.Any(e => e.IsMatch(relative))) {
if (config.ExcludedFiles.Any(e => FileSystemName.MatchesSimpleExpression(e, relative))) {
if (config.LogSkipped)
Console.WriteLine($"Skipping excluded file {relative}");
continue;
Expand Down Expand Up @@ -271,12 +270,12 @@ private static (List<ImporterInfo>, List<string>) GetContentData() {

private static IEnumerable<OverrideInfo> GetOverrides(Dictionary<string, Override> config) {
foreach (var entry in config)
yield return new OverrideInfo(Program.GlobbyRegex(entry.Key), entry.Value);
yield return new OverrideInfo(entry.Key, entry.Value);
}

private static OverrideInfo GetOverrideFor(string file, IEnumerable<OverrideInfo> overrides) {
foreach (var over in overrides) {
if (over.Regex.IsMatch(file))
if (FileSystemName.MatchesSimpleExpression(over.Expression, file))
return over;
}
return null;
Expand Down Expand Up @@ -327,8 +326,4 @@ private static void CopyFile(ICollection<string> content, string relative) {
Console.WriteLine($"Adding file {relative} with the Copy build action");
}

private static Regex GlobbyRegex(string s) {
return new Regex(s.Replace(".", "[.]").Replace("*", ".*").Replace("?", "."));
}

}

0 comments on commit b410b81

Please sign in to comment.