From 95a61c3b2891caf01da507039ee39d5a54026f79 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 22 Mar 2021 09:55:29 -0700 Subject: [PATCH] Add path matching to util --- BurnOutSharp/Matching/MatchUtil.cs | 108 ++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-) diff --git a/BurnOutSharp/Matching/MatchUtil.cs b/BurnOutSharp/Matching/MatchUtil.cs index d60847f4..d2cbafda 100644 --- a/BurnOutSharp/Matching/MatchUtil.cs +++ b/BurnOutSharp/Matching/MatchUtil.cs @@ -4,10 +4,12 @@ using System.Linq; namespace BurnOutSharp.Matching { /// - /// Helper class for content matching + /// Helper class for matching /// internal static class MatchUtil { + #region Content Matching + /// /// Get all content matches for a given list of matchers /// @@ -101,5 +103,109 @@ namespace BurnOutSharp.Matching return matchedProtections; } + + #endregion + + #region Path Matching + + /// + /// Get all path matches for a given list of matchers + /// + /// File path to check for matches + /// Enumerable of matchers to be run on the file + /// True if any path match is a success, false if all have to match + /// List of strings representing the matched protections, null or empty otherwise + public static List GetAllPathMatches(string file, IEnumerable matchers, bool any = false) + { + return FindAllPathMatches(new List { file }, matchers, any, false); + } + + // + /// Get all path matches for a given list of matchers + /// + /// File paths to check for matches + /// Enumerable of matchers to be run on the file + /// True if any path match is a success, false if all have to match + /// List of strings representing the matched protections, null or empty otherwise + public static List GetAllPathMatches(List files, IEnumerable matchers, bool any = false) + { + return FindAllPathMatches(files, matchers, any, false); + } + + /// + /// Get first path match for a given list of matchers + /// + /// File path to check for matches + /// Enumerable of matchers to be run on the file + /// True if any path match is a success, false if all have to match + /// String representing the matched protection, null otherwise + public static string GetFirstPathMatch(string file, IEnumerable matchers, bool any = false) + { + var contentMatches = FindAllPathMatches(new List { file }, matchers, any, true); + if (contentMatches == null || !contentMatches.Any()) + return null; + + return contentMatches.First(); + } + + /// + /// Get first path match for a given list of matchers + /// + /// File paths to check for matches + /// Enumerable of matchers to be run on the file + /// True if any path match is a success, false if all have to match + /// String representing the matched protection, null otherwise + public static string GetFirstPathMatch(List files, IEnumerable matchers, bool any = false) + { + var contentMatches = FindAllPathMatches(files, matchers, any, true); + if (contentMatches == null || !contentMatches.Any()) + return null; + + return contentMatches.First(); + } + + /// + /// Get the required set of path matches on a per Matcher basis + /// + /// File paths to check for matches + /// Enumerable of matchers to be run on the file + /// True if any path match is a success, false if all have to match + /// True to stop after the first match, false otherwise + /// List of strings representing the matched protections, null or empty otherwise + private static List FindAllPathMatches(List files, IEnumerable matchers, bool any, bool stopAfterFirst) + { + // If there's no mappings, we can't match + if (matchers == null || !matchers.Any()) + return null; + + // Initialize the list of matched protections + List matchedProtections = new List(); + + // Loop through and try everything otherwise + foreach (var matcher in matchers) + { + // Determine if the matcher passes + bool passes; + if (any) + (passes, _) = matcher.MatchesAny(files); + else + (passes, _) = matcher.MatchesAll(files); + + // If we don't have a pass, just continue + if (!passes) + continue; + + //Add the matched protection + matchedProtections.Add(matcher.ProtectionName ?? "Unknown Protection"); + + // If we're stopping after the first protection, bail out here + if (stopAfterFirst) + return matchedProtections; + } + + return matchedProtections; + } + + #endregion } } \ No newline at end of file