| | | 1 | | // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT |
| | | 2 | | using System.ComponentModel.DataAnnotations; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | |
| | | 6 | | namespace DotNetApiDiff.Models.Configuration; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Main configuration for API comparison |
| | | 10 | | /// </summary> |
| | | 11 | | public class ComparisonConfiguration |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Configuration for namespace and type mappings |
| | | 15 | | /// </summary> |
| | | 16 | | [JsonPropertyName("mappings")] |
| | 280 | 17 | | public MappingConfiguration Mappings { get; set; } = new MappingConfiguration(); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Configuration for excluding types and members |
| | | 21 | | /// </summary> |
| | | 22 | | [JsonPropertyName("exclusions")] |
| | 298 | 23 | | public ExclusionConfiguration Exclusions { get; set; } = new ExclusionConfiguration(); |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Configuration for breaking change rules |
| | | 27 | | /// </summary> |
| | | 28 | | [JsonPropertyName("breakingChangeRules")] |
| | 333 | 29 | | public BreakingChangeRules BreakingChangeRules { get; set; } = new BreakingChangeRules(); |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Configuration for filtering types and namespaces |
| | | 33 | | /// </summary> |
| | | 34 | | [JsonPropertyName("filters")] |
| | 323 | 35 | | public FilterConfiguration Filters { get; set; } = new FilterConfiguration(); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Output format for the comparison results |
| | | 39 | | /// </summary> |
| | | 40 | | [JsonPropertyName("outputFormat")] |
| | 245 | 41 | | public ReportFormat OutputFormat { get; set; } = ReportFormat.Console; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Path to the output file (if not specified, output is written to console) |
| | | 45 | | /// </summary> |
| | | 46 | | [JsonPropertyName("outputPath")] |
| | 99 | 47 | | public string? OutputPath { get; set; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Whether to fail on breaking changes |
| | | 51 | | /// </summary> |
| | | 52 | | [JsonPropertyName("failOnBreakingChanges")] |
| | 190 | 53 | | public bool FailOnBreakingChanges { get; set; } = true; |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Creates a default configuration |
| | | 57 | | /// </summary> |
| | | 58 | | /// <returns>A default configuration</returns> |
| | | 59 | | public static ComparisonConfiguration CreateDefault() |
| | 64 | 60 | | { |
| | 64 | 61 | | return new ComparisonConfiguration |
| | 64 | 62 | | { |
| | 64 | 63 | | Mappings = MappingConfiguration.CreateDefault(), |
| | 64 | 64 | | Exclusions = ExclusionConfiguration.CreateDefault(), |
| | 64 | 65 | | BreakingChangeRules = BreakingChangeRules.CreateDefault(), |
| | 64 | 66 | | Filters = FilterConfiguration.CreateDefault(), |
| | 64 | 67 | | OutputFormat = ReportFormat.Console, |
| | 64 | 68 | | OutputPath = null, |
| | 64 | 69 | | FailOnBreakingChanges = true |
| | 64 | 70 | | }; |
| | 64 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Loads configuration from a JSON file |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="path">Path to the JSON file</param> |
| | | 77 | | /// <returns>The loaded configuration</returns> |
| | | 78 | | /// <exception cref="FileNotFoundException">Thrown when the file is not found</exception> |
| | | 79 | | /// <exception cref="JsonException">Thrown when the JSON is invalid</exception> |
| | | 80 | | public static ComparisonConfiguration LoadFromJsonFile(string path) |
| | 16 | 81 | | { |
| | 16 | 82 | | if (!File.Exists(path)) |
| | 2 | 83 | | { |
| | 2 | 84 | | throw new FileNotFoundException($"Configuration file not found: {path}"); |
| | | 85 | | } |
| | | 86 | | |
| | 14 | 87 | | var json = File.ReadAllText(path); |
| | 14 | 88 | | var options = new JsonSerializerOptions |
| | 14 | 89 | | { |
| | 14 | 90 | | PropertyNameCaseInsensitive = true, |
| | 14 | 91 | | ReadCommentHandling = JsonCommentHandling.Skip, |
| | 14 | 92 | | AllowTrailingCommas = true, |
| | 14 | 93 | | Converters = { new JsonStringEnumConverter(null, false) } |
| | 14 | 94 | | }; |
| | | 95 | | |
| | 14 | 96 | | var config = JsonSerializer.Deserialize<ComparisonConfiguration>(json, options); |
| | 12 | 97 | | if (config == null) |
| | 0 | 98 | | { |
| | 0 | 99 | | throw new JsonException("Failed to deserialize configuration"); |
| | | 100 | | } |
| | | 101 | | |
| | 12 | 102 | | if (!config.IsValid()) |
| | 0 | 103 | | { |
| | 0 | 104 | | throw new ValidationException("Configuration validation failed"); |
| | | 105 | | } |
| | | 106 | | |
| | 12 | 107 | | return config; |
| | 12 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Validates the configuration |
| | | 112 | | /// </summary> |
| | | 113 | | /// <returns>True if valid, false otherwise</returns> |
| | | 114 | | public bool IsValid() |
| | 26 | 115 | | { |
| | 26 | 116 | | return Mappings.IsValid() && |
| | 26 | 117 | | Exclusions.IsValid() && |
| | 26 | 118 | | Filters.IsValid() && |
| | 26 | 119 | | Enum.IsDefined(typeof(ReportFormat), OutputFormat); |
| | 26 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Saves configuration to a JSON file |
| | | 124 | | /// </summary> |
| | | 125 | | /// <param name="path">Path to the JSON file</param> |
| | | 126 | | /// <exception cref="IOException">Thrown when the file cannot be written</exception> |
| | | 127 | | public void SaveToJsonFile(string path) |
| | 3 | 128 | | { |
| | 3 | 129 | | var options = new JsonSerializerOptions |
| | 3 | 130 | | { |
| | 3 | 131 | | WriteIndented = true, |
| | 3 | 132 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 3 | 133 | | Converters = { new JsonStringEnumConverter(null, false) } |
| | 3 | 134 | | }; |
| | | 135 | | |
| | 3 | 136 | | var json = JsonSerializer.Serialize(this, options); |
| | 3 | 137 | | File.WriteAllText(path, json); |
| | 3 | 138 | | } |
| | | 139 | | } |