| | 1 | | // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT |
| | 2 | | using System.Text.Json.Serialization; |
| | 3 | | using System.Text.RegularExpressions; |
| | 4 | |
|
| | 5 | | namespace DotNetApiDiff.Models.Configuration; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Configuration for excluding types and members from API comparison |
| | 9 | | /// </summary> |
| | 10 | | public class ExclusionConfiguration |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// List of fully qualified type names to exclude |
| | 14 | | /// </summary> |
| | 15 | | [JsonPropertyName("excludedTypes")] |
| 405 | 16 | | public List<string> ExcludedTypes { get; set; } = new List<string>(); |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// List of fully qualified member names to exclude |
| | 20 | | /// </summary> |
| | 21 | | [JsonPropertyName("excludedMembers")] |
| 394 | 22 | | public List<string> ExcludedMembers { get; set; } = new List<string>(); |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// List of type name patterns to exclude (supports wildcards * and ?) |
| | 26 | | /// </summary> |
| | 27 | | [JsonPropertyName("excludedTypePatterns")] |
| 437 | 28 | | public List<string> ExcludedTypePatterns { get; set; } = new List<string>(); |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// List of member name patterns to exclude (supports wildcards * and ?) |
| | 32 | | /// </summary> |
| | 33 | | [JsonPropertyName("excludedMemberPatterns")] |
| 432 | 34 | | public List<string> ExcludedMemberPatterns { get; set; } = new List<string>(); |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Whether to exclude compiler-generated types and members |
| | 38 | | /// </summary> |
| | 39 | | [JsonPropertyName("excludeCompilerGenerated")] |
| 326 | 40 | | public bool ExcludeCompilerGenerated { get; set; } = true; |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Whether to exclude obsolete types and members |
| | 44 | | /// </summary> |
| | 45 | | [JsonPropertyName("excludeObsolete")] |
| 326 | 46 | | public bool ExcludeObsolete { get; set; } = false; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Creates a default exclusion configuration |
| | 50 | | /// </summary> |
| | 51 | | /// <returns>A default exclusion configuration</returns> |
| | 52 | | public static ExclusionConfiguration CreateDefault() |
| 82 | 53 | | { |
| 82 | 54 | | return new ExclusionConfiguration |
| 82 | 55 | | { |
| 82 | 56 | | ExcludedTypes = new List<string>(), |
| 82 | 57 | | ExcludedMembers = new List<string>(), |
| 82 | 58 | | ExcludedTypePatterns = new List<string>(), |
| 82 | 59 | | ExcludedMemberPatterns = new List<string>(), |
| 82 | 60 | | ExcludeCompilerGenerated = true, |
| 82 | 61 | | ExcludeObsolete = false |
| 82 | 62 | | }; |
| 82 | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Validates the exclusion configuration |
| | 67 | | /// </summary> |
| | 68 | | /// <returns>True if valid, false otherwise</returns> |
| | 69 | | public bool IsValid() |
| 41 | 70 | | { |
| | 71 | | // Check for empty entries |
| 41 | 72 | | if (ExcludedTypes.Any(string.IsNullOrWhiteSpace) || |
| 41 | 73 | | ExcludedMembers.Any(string.IsNullOrWhiteSpace) || |
| 41 | 74 | | ExcludedTypePatterns.Any(string.IsNullOrWhiteSpace) || |
| 41 | 75 | | ExcludedMemberPatterns.Any(string.IsNullOrWhiteSpace)) |
| 12 | 76 | | { |
| 12 | 77 | | return false; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | // Validate patterns (should be valid wildcards) |
| | 81 | | try |
| 29 | 82 | | { |
| 191 | 83 | | foreach (var pattern in ExcludedTypePatterns.Concat(ExcludedMemberPatterns)) |
| 52 | 84 | | { |
| | 85 | | // Convert wildcard pattern to regex for validation |
| 52 | 86 | | WildcardToRegex(pattern); |
| 52 | 87 | | } |
| 29 | 88 | | } |
| 0 | 89 | | catch (Exception ex) |
| 0 | 90 | | { |
| | 91 | | // Log the exception details for debugging purposes |
| 0 | 92 | | Console.WriteLine($"Validation failed due to an exception: {ex.Message}"); |
| 0 | 93 | | return false; |
| | 94 | | } |
| | 95 | |
|
| 29 | 96 | | return true; |
| 41 | 97 | | } |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Converts a wildcard pattern to a regular expression |
| | 101 | | /// </summary> |
| | 102 | | /// <param name="pattern">The wildcard pattern</param> |
| | 103 | | /// <returns>A regular expression pattern</returns> |
| | 104 | | private static string WildcardToRegex(string pattern) |
| 52 | 105 | | { |
| 52 | 106 | | return "^" + Regex.Escape(pattern) |
| 52 | 107 | | .Replace("\\*", ".*") |
| 52 | 108 | | .Replace("\\?", ".") + "$"; |
| 52 | 109 | | } |
| | 110 | | } |