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

loresoft / EntityFrameworkCore.Generator / 15072048022

16 May 2025 03:31PM UTC coverage: 55.392% (-1.4%) from 56.772%
15072048022

push

github

pwelter34
enable nullable support

616 of 1271 branches covered (48.47%)

Branch coverage included in aggregate %.

233 of 397 new or added lines in 61 files covered. (58.69%)

17 existing lines in 11 files now uncovered.

1824 of 3134 relevant lines covered (58.2%)

88.56 hits per line

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

16.36
/src/EntityFrameworkCore.Generator.Core/ConfigurationSerializer.cs
1
using System;
2
using System.IO;
3

4
using EntityFrameworkCore.Generator.Serialization;
5

6
using Microsoft.Extensions.Logging;
7

8
using YamlDotNet.Serialization;
9
using YamlDotNet.Serialization.NamingConventions;
10

11
namespace EntityFrameworkCore.Generator;
12

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

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

29
    /// <summary>
30
    /// The options file name. Default 'generation.yml'
31
    /// </summary>
32
    public const string OptionsFileName = "generation.yml";
33

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

NEW
49
        _logger.LogInformation("Loading options file: {file}", file);
×
50
        using var reader = File.OpenText(path);
×
51

52
        return Load(reader);
×
53
    }
×
54

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

67
        var deserializer = new DeserializerBuilder()
1✔
68
            .WithNamingConvention(CamelCaseNamingConvention.Instance)
1✔
69
            .Build();
1✔
70

71
        // use Serialization model for better yaml support
72
        return deserializer.Deserialize<GeneratorModel>(reader);
1✔
73
    }
74

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

87
        if (string.IsNullOrWhiteSpace(file))
×
88
            file = OptionsFileName;
×
89

90
        if (!Directory.Exists(directory))
×
91
        {
92
            _logger.LogTrace($"Creating Directory: {directory}");
×
93
            Directory.CreateDirectory(directory);
×
94
        }
95

96
        _logger.LogInformation($"Saving options file: {file}");
×
97

98
        var path = Path.Combine(directory, file);
×
99

100
        var serializer = new SerializerBuilder()
×
101
            .ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitDefaults)
×
102
            .WithNamingConvention(CamelCaseNamingConvention.Instance)
×
103
            .Build();
×
104

105
        using (var streamWriter = File.CreateText(path))
×
106
            serializer.Serialize(streamWriter, generatorOptions);
×
107

108
        return path;
×
109
    }
110

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

123

124
    private static string GetPath(string? directory, string? file)
125
    {
126
        if (string.IsNullOrWhiteSpace(directory))
×
127
            directory = Environment.CurrentDirectory;
×
128

129
        if (string.IsNullOrWhiteSpace(file))
×
130
            file = OptionsFileName;
×
131

132
        var path = Path.Combine(directory, file);
×
133
        return path;
×
134
    }
135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc