• 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

0.0
/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)
×
20
    {
21
        Logger = loggerFactory.CreateLogger(this.GetType());
×
22

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

27
    }
×
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;
×
42

43
        if (!File.Exists(templatePath))
×
44
        {
45
            Logger.LogWarning("Template '{template}' could not be found.", templatePath);
×
46
            return;
×
47
        }
48

49
        // save file
50
        var directory = TemplateOptions.Directory;
×
NEW
51
        if (directory.HasValue() && !Directory.Exists(directory))
×
52
            Directory.CreateDirectory(directory);
×
53

54
        var fileName = TemplateOptions.FileName;
×
NEW
55
        if (directory.IsNullOrEmpty() || fileName.IsNullOrEmpty())
×
56
        {
NEW
57
            Logger.LogWarning("Template '{template}' could not resolve output file.", templatePath);
×
NEW
58
            return;
×
59
        }
60

NEW
61
        var path = Path.Combine(directory, fileName);
×
UNCOV
62
        var exists = File.Exists(path);
×
63

64
        if (exists && !(TemplateOptions.Merge || TemplateOptions.Overwrite))
×
65
        {
66
            Logger.LogDebug("Skipping template '{template}' because output '{fileName}' already exists.", templatePath, fileName);
×
67
            return;
×
68
        }
69

NEW
70
        if (File.Exists(path))
×
NEW
71
            Logger.LogInformation("Updating template script file: {fileName}", fileName);
×
72
        else
NEW
73
            Logger.LogInformation("Creating template script file: {fileName}", fileName);
×
74

75
        // get content
76
        var content = ExecuteScript();
×
77

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

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

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

99
        var script = LoadScript(templatePath);
×
100
        var variables = CreateVariables();
×
101

102
        var scriptTask = script.RunAsync(variables);
×
103
        var scriptState = scriptTask.Result;
×
104

105
        return scriptState.ReturnValue;
×
106
    }
107

108
    protected abstract TVariable CreateVariables();
109

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

115
        Logger.LogDebug("Loading template script: {script}", scriptPath);
×
116

117
        var scriptContent = File.ReadAllText(scriptPath);
×
118

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

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

137
        if (diagnostics.Length == 0)
×
138
            return _scriptTemplate;
×
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

© 2025 Coveralls, Inc