• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / EntityFrameworkCore.Generator / 27828904503

19 Jun 2026 01:34PM UTC coverage: 71.825% (+0.1%) from 71.692%
27828904503

push

github

pwelter34
Use LoggerMessage partials

985 of 1863 branches covered (52.87%)

Branch coverage included in aggregate %.

22 of 63 new or added lines in 10 files covered. (34.92%)

4195 of 5349 relevant lines covered (78.43%)

1286.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

40.0
/src/EntityFrameworkCore.Generator.Core/ConfigurationSerializer.cs
1
using EntityFrameworkCore.Generator.Serialization;
2

3
using Microsoft.Extensions.Logging;
4

5
using YamlDotNet.Serialization;
6
using YamlDotNet.Serialization.NamingConventions;
7

8
namespace EntityFrameworkCore.Generator;
9

10
/// <summary>
11
/// Serialization and Deserialization for the <see cref="Generator"/> class
12
/// </summary>
13
public partial class ConfigurationSerializer : IConfigurationSerializer
14
{
15
    private readonly ILogger<ConfigurationSerializer> _logger;
16

17
    /// <summary>
18
    /// Initializes a new instance of the <see cref="ConfigurationSerializer"/> class.
19
    /// </summary>
20
    /// <param name="logger">The logger.</param>
21
    public ConfigurationSerializer(ILogger<ConfigurationSerializer> logger)
6 ✔
22
    {
23
        _logger = logger;
6 ✔
24
    }
6 ✔
25

26
    /// <summary>
27
    /// The options file name. Default 'generation.yml'
28
    /// </summary>
29
    public const string OptionsFileName = "generation.yml";
30

31
    /// <summary>
32
    /// Loads the options file using the specified <paramref name="directory"/> and <paramref name="file"/>.
33
    /// </summary>
34
    /// <param name="directory">The directory where the file is located.</param>
35
    /// <param name="file">The name of the options file.</param>
36
    /// <returns>An instance of <see cref="Generator"/> if the file exists; otherwise <c>null</c>.</returns>
37
    public GeneratorModel? Load(string? directory = null, string file = OptionsFileName)
38
    {
39
        var path = GetPath(directory, file);
4 ✔
40
        if (!File.Exists(path))
4 !
41
        {
NEW
42
            LogOptionFileNotFound(_logger, file);
×
43
            return null;
×
44
        }
45

46
        LogLoadingOptionsFile(_logger, file);
4 ✔
47
        using var reader = File.OpenText(path);
4 ✔
48

49
        return Load(reader);
4 ✔
50
    }
4 ✔
51

52
    /// <summary>
53
    /// Loads the options using the specified <paramref name="reader" />
54
    /// </summary>
55
    /// <param name="reader">The reader.</param>
56
    /// <returns>
57
    /// An instance of <see cref="Generator" />.
58
    /// </returns>
59
    public GeneratorModel? Load(TextReader reader)
60
    {
61
        if (reader == null)
6 !
62
            return null;
×
63

64
        var deserializer = new DeserializerBuilder()
6 ✔
65
            .WithNamingConvention(CamelCaseNamingConvention.Instance)
6 ✔
66
            .Build();
6 ✔
67

68
        // use Serialization model for better yaml support
69
        return deserializer.Deserialize<GeneratorModel>(reader);
6 ✔
70
    }
71

72
    /// <summary>
73
    /// Saves the generator options to the specified <paramref name="directory"/> and <paramref name="file"/>.
74
    /// </summary>
75
    /// <param name="generatorOptions">The generator options to save.</param>
76
    /// <param name="directory">The directory where the file is located.</param>
77
    /// <param name="file">The name of the options file.</param>
78
    /// <returns>The full path of the options file.</returns>
79
    public string Save(GeneratorModel generatorOptions, string? directory = null, string file = OptionsFileName)
80
    {
81
        if (string.IsNullOrWhiteSpace(directory))
×
82
            directory = Environment.CurrentDirectory;
×
83

84
        if (string.IsNullOrWhiteSpace(file))
×
85
            file = OptionsFileName;
×
86

87
        if (!Directory.Exists(directory))
×
88
        {
NEW
89
            LogCreatingDirectory(_logger, directory);
×
90
            Directory.CreateDirectory(directory);
×
91
        }
92

NEW
93
        LogSavingOptionsFile(_logger, file);
×
94

95
        var path = Path.Combine(directory, file);
×
96

97
        var serializer = new SerializerBuilder()
×
98
            .ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitDefaults)
×
99
            .WithNamingConvention(CamelCaseNamingConvention.Instance)
×
100
            .Build();
×
101

102
        using (var streamWriter = File.CreateText(path))
×
103
            serializer.Serialize(streamWriter, generatorOptions);
×
104

105
        return path;
×
106
    }
107

108
    /// <summary>
109
    /// Determines if the specified options file exists.
110
    /// </summary>
111
    /// <param name="directory">The directory where the file is located.</param>
112
    /// <param name="file">The name of the options file.</param>
113
    /// <returns><c>true</c> if options file exits; otherwise <c>false</c>.</returns>
114
    public bool Exists(string? directory = null, string file = OptionsFileName)
115
    {
116
        var path = GetPath(directory, file);
×
117
        return File.Exists(path);
×
118
    }
119

120

121
    private static string GetPath(string? directory, string? file)
122
    {
123
        if (string.IsNullOrWhiteSpace(directory))
4 !
124
            directory = Environment.CurrentDirectory;
×
125

126
        if (string.IsNullOrWhiteSpace(file))
4 !
127
            file = OptionsFileName;
×
128

129
        var path = Path.Combine(directory, file);
4 ✔
130
        return path;
4 ✔
131
    }
132

133
    [LoggerMessage(EventId = 1, Level = LogLevel.Warning, Message = "Option file not found: {file}")]
134
    private static partial void LogOptionFileNotFound(ILogger logger, string file);
135

136
    [LoggerMessage(EventId = 2, Level = LogLevel.Information, Message = "Loading options file: {file}")]
137
    private static partial void LogLoadingOptionsFile(ILogger logger, string file);
138

139
    [LoggerMessage(EventId = 3, Level = LogLevel.Trace, Message = "Creating Directory: {directory}")]
140
    private static partial void LogCreatingDirectory(ILogger logger, string directory);
141

142
    [LoggerMessage(EventId = 4, Level = LogLevel.Information, Message = "Saving options file: {file}")]
143
    private static partial void LogSavingOptionsFile(ILogger logger, string file);
144
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc