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

loresoft / EntityFrameworkCore.Generator / 27799729863

19 Jun 2026 01:26AM UTC coverage: 71.762% (+1.3%) from 70.481%
27799729863

push

github

pwelter34
Adjust README and fix SQL type mapping

985 of 1863 branches covered (52.87%)

Branch coverage included in aggregate %.

4217 of 5386 relevant lines covered (78.3%)

1278.11 hits per line

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

57.52
/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 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
        {
45
            Logger.LogWarning("Template '{template}' could not be found.", 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
        {
55
            Logger.LogWarning("Template '{template}' could not resolve output file.", 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
        {
64
            Logger.LogDebug("Skipping template '{template}' because output '{fileName}' already exists.", templatePath, fileName);
×
65
            return;
×
66
        }
67

68
        if (File.Exists(path))
1!
69
            Logger.LogInformation("Updating template script file: {fileName}", fileName);
×
70
        else
71
            Logger.LogInformation("Creating template script file: {fileName}", fileName);
1✔
72

73
        // get content
74
        var content = ExecuteScript();
1✔
75

76
        if (content.IsNullOrWhiteSpace())
1!
77
        {
78
            Logger.LogDebug("Skipping template '{template}' because it didn't return any text.", templatePath);
×
79
            return;
×
80
        }
81

82
        if (directory.HasValue() && !Directory.Exists(directory))
1!
83
            Directory.CreateDirectory(directory);
×
84

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

91
    protected virtual string ExecuteScript()
92
    {
93
        var templatePath = TemplateOptions.TemplatePath;
1✔
94
        if (!File.Exists(templatePath))
1!
95
        {
96
            Logger.LogWarning("Template '{template}' could not be found.", templatePath);
×
97
            return string.Empty;
×
98
        }
99

100
        var script = LoadScript(templatePath);
1✔
101
        var variables = CreateVariables();
1✔
102

103
        var scriptTask = script.RunAsync(variables);
1✔
104
        var scriptState = scriptTask.Result;
1✔
105

106
        return scriptState.ReturnValue;
1✔
107
    }
108

109
    protected abstract TVariable CreateVariables();
110

111
    protected Script<string> LoadScript(string scriptPath)
112
    {
113
        if (_scriptTemplate != null)
1!
114
            return _scriptTemplate;
×
115

116
        Logger.LogDebug("Loading template script: {script}", scriptPath);
1✔
117

118
        var scriptContent = File.ReadAllText(scriptPath);
1✔
119

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

134
        _scriptTemplate = CSharpScript.Create<string>(scriptContent, scriptOptions, typeof(TVariable));
1✔
135
        var diagnostics = _scriptTemplate.Compile();
1✔
136

137
        if (diagnostics.Length == 0)
1!
138
            return _scriptTemplate;
1✔
139

140
        Logger.LogInformation("Template Compile Diagnostics: ");
×
141
        foreach (var diagnostic in diagnostics)
×
142
        {
143
            var message = diagnostic.GetMessage();
×
144
            switch (diagnostic.Severity)
×
145
            {
146
                case DiagnosticSeverity.Info:
147
                    Logger.LogDebug(message);
×
148
                    break;
×
149
                case DiagnosticSeverity.Warning:
150
                    Logger.LogWarning(message);
×
151
                    break;
×
152
                case DiagnosticSeverity.Error:
153
                    Logger.LogError(message);
×
154
                    break;
×
155
                default:
156
                    Logger.LogDebug(message);
×
157
                    break;
158
            }
159
        }
160

161
        return _scriptTemplate;
×
162
    }
163
}
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

© 2026 Coveralls, Inc