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