• 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

57.66
/src/EntityFrameworkCore.Generator.Core/Scripts/ScriptTemplateBase.cs
1
using EntityFrameworkCore.Generator.Extensions;
2
using EntityFrameworkCore.Generator.Options;
3
using EntityFrameworkCore.Generator.Parsing;
4

5
using Microsoft.CodeAnalysis;
6
using Microsoft.CodeAnalysis.CSharp.Scripting;
7
using Microsoft.CodeAnalysis.Scripting;
8
using Microsoft.Extensions.Logging;
9

10
using ScriptOptions = Microsoft.CodeAnalysis.Scripting.ScriptOptions;
11

12
namespace EntityFrameworkCore.Generator.Scripts;
13

14
public abstract partial class ScriptTemplateBase<TVariable>
15
    where TVariable : ScriptVariablesBase
16
{
17
    private Script<string>? _scriptTemplate;
18

19
    protected ScriptTemplateBase(ILoggerFactory loggerFactory, GeneratorOptions generatorOptions, TemplateOptions templateOptions)
1 ✔
20
    {
21
        Logger = loggerFactory.CreateLogger(this.GetType());
1 ✔
22

23
        TemplateOptions = templateOptions;
1 ✔
24
        GeneratorOptions = generatorOptions;
1 ✔
25
        RegionReplace = new RegionReplace();
1 ✔
26

27
    }
1 ✔
28

29
    protected ILogger Logger { get; }
30

31
    protected RegionReplace RegionReplace { get; }
32

33

34
    public TemplateOptions TemplateOptions { get; }
35

36
    public GeneratorOptions GeneratorOptions { get; }
37

38

39
    protected virtual void WriteCode()
40
    {
41
        var templatePath = TemplateOptions.TemplatePath;
1 ✔
42

43
        if (!File.Exists(templatePath))
1 !
44
        {
NEW
45
            LogTemplateNotFound(Logger, templatePath);
×
46
            return;
×
47
        }
48

49
        // save file
50
        var directory = TemplateOptions.Directory;
1 ✔
51
        var fileName = TemplateOptions.FileName;
1 ✔
52

53
        if (directory.IsNullOrEmpty() || fileName.IsNullOrEmpty())
1 !
54
        {
NEW
55
            LogTemplateCouldNotResolveOutputFile(Logger, templatePath);
×
56
            return;
×
57
        }
58

59
        var path = Path.Combine(directory, fileName);
1 ✔
60
        var exists = File.Exists(path);
1 ✔
61

62
        if (exists && !(TemplateOptions.Merge || TemplateOptions.Overwrite))
1 !
63
        {
NEW
64
            LogSkippingTemplateBecauseOutputExists(Logger, templatePath, fileName);
×
65
            return;
×
66
        }
67

68
        LogTemplateScriptFileAction(Logger, File.Exists(path) ? "Updating" : "Creating", fileName);
1 !
69

70
        // get content
71
        var content = ExecuteScript();
1 ✔
72

73
        if (content.IsNullOrWhiteSpace())
1 !
74
        {
NEW
75
            LogSkippingTemplateBecauseNoText(Logger, templatePath);
×
76
            return;
×
77
        }
78

79
        if (directory.HasValue() && !Directory.Exists(directory))
1 !
80
            Directory.CreateDirectory(directory);
×
81

82
        if (exists && TemplateOptions.Merge && !TemplateOptions.Overwrite)
1 !
83
            RegionReplace.MergeFile(path, content);
×
84
        else
85
            File.WriteAllText(path, content);
1 ✔
86
    }
1 ✔
87

88
    protected virtual string ExecuteScript()
89
    {
90
        var templatePath = TemplateOptions.TemplatePath;
1 ✔
91
        if (!File.Exists(templatePath))
1 !
92
        {
NEW
93
            LogTemplateNotFound(Logger, templatePath);
×
94
            return string.Empty;
×
95
        }
96

97
        var script = LoadScript(templatePath);
1 ✔
98
        var variables = CreateVariables();
1 ✔
99

100
        var scriptTask = script.RunAsync(variables);
1 ✔
101
        var scriptState = scriptTask.Result;
1 ✔
102

103
        return scriptState.ReturnValue;
1 ✔
104
    }
105

106
    protected abstract TVariable CreateVariables();
107

108
    protected Script<string> LoadScript(string scriptPath)
109
    {
110
        if (_scriptTemplate != null)
1 !
111
            return _scriptTemplate;
×
112

113
        LogLoadingTemplateScript(Logger, scriptPath);
1 ✔
114

115
        var scriptContent = File.ReadAllText(scriptPath);
1 ✔
116

117
        var scriptOptions = ScriptOptions.Default
1 ✔
118
            .WithReferences(
1 ✔
119
                typeof(ScriptVariablesBase).Assembly
1 ✔
120
            )
1 ✔
121
            .WithImports(
1 ✔
122
                "System",
1 ✔
123
                "System.Collections.Generic",
1 ✔
124
                "System.Linq",
1 ✔
125
                "System.Text",
1 ✔
126
                "EntityFrameworkCore.Generator.Extensions",
1 ✔
127
                "EntityFrameworkCore.Generator.Metadata.Generation",
1 ✔
128
                "EntityFrameworkCore.Generator.Options"
1 ✔
129
            );
1 ✔
130

131
        _scriptTemplate = CSharpScript.Create<string>(scriptContent, scriptOptions, typeof(TVariable));
1 ✔
132
        var diagnostics = _scriptTemplate.Compile();
1 ✔
133

134
        if (diagnostics.Length == 0)
1 !
135
            return _scriptTemplate;
1 ✔
136

NEW
137
        LogTemplateCompileDiagnostics(Logger);
×
138
        foreach (var diagnostic in diagnostics)
×
139
        {
140
            var message = diagnostic.GetMessage();
×
141
            switch (diagnostic.Severity)
×
142
            {
143
                case DiagnosticSeverity.Info:
NEW
144
                    LogTemplateDiagnosticDebug(Logger, message);
×
145
                    break;
×
146
                case DiagnosticSeverity.Warning:
NEW
147
                    LogTemplateDiagnosticWarning(Logger, message);
×
148
                    break;
×
149
                case DiagnosticSeverity.Error:
NEW
150
                    LogTemplateDiagnosticError(Logger, message);
×
151
                    break;
×
152
                default:
NEW
153
                    LogTemplateDiagnosticDebug(Logger, message);
×
154
                    break;
155
            }
156
        }
157

158
        return _scriptTemplate;
×
159
    }
160

161
    [LoggerMessage(EventId = 1, Level = LogLevel.Warning, Message = "Template '{template}' could not be found.")]
162
    private static partial void LogTemplateNotFound(ILogger logger, string? template);
163

164
    [LoggerMessage(EventId = 2, Level = LogLevel.Warning, Message = "Template '{template}' could not resolve output file.")]
165
    private static partial void LogTemplateCouldNotResolveOutputFile(ILogger logger, string? template);
166

167
    [LoggerMessage(EventId = 3, Level = LogLevel.Debug, Message = "Skipping template '{template}' because output '{fileName}' already exists.")]
168
    private static partial void LogSkippingTemplateBecauseOutputExists(ILogger logger, string? template, string fileName);
169

170
    [LoggerMessage(EventId = 4, Level = LogLevel.Information, Message = "{action} template script file: {fileName}")]
171
    private static partial void LogTemplateScriptFileAction(ILogger logger, string action, string fileName);
172

173
    [LoggerMessage(EventId = 5, Level = LogLevel.Debug, Message = "Skipping template '{template}' because it didn't return any text.")]
174
    private static partial void LogSkippingTemplateBecauseNoText(ILogger logger, string? template);
175

176
    [LoggerMessage(EventId = 6, Level = LogLevel.Debug, Message = "Loading template script: {script}")]
177
    private static partial void LogLoadingTemplateScript(ILogger logger, string script);
178

179
    [LoggerMessage(EventId = 7, Level = LogLevel.Information, Message = "Template Compile Diagnostics: ")]
180
    private static partial void LogTemplateCompileDiagnostics(ILogger logger);
181

182
    [LoggerMessage(EventId = 8, Level = LogLevel.Debug, Message = "{message}")]
183
    private static partial void LogTemplateDiagnosticDebug(ILogger logger, string message);
184

185
    [LoggerMessage(EventId = 9, Level = LogLevel.Warning, Message = "{message}")]
186
    private static partial void LogTemplateDiagnosticWarning(ILogger logger, string message);
187

188
    [LoggerMessage(EventId = 10, Level = LogLevel.Error, Message = "{message}")]
189
    private static partial void LogTemplateDiagnosticError(ILogger logger, string message);
190
}
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