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

loresoft / EntityFrameworkCore.Generator / 15403562343

02 Jun 2025 09:51PM UTC coverage: 54.917% (+0.08%) from 54.841%
15403562343

push

github

pwelter34
test fix

643 of 1333 branches covered (48.24%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

5 existing lines in 3 files now uncovered.

1881 of 3263 relevant lines covered (57.65%)

61.27 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;
×
51
        var fileName = TemplateOptions.FileName;
×
52

UNCOV
53
        if (directory.IsNullOrEmpty() || fileName.IsNullOrEmpty())
×
54
        {
55
            Logger.LogWarning("Template '{template}' could not resolve output file.", templatePath);
×
56
            return;
×
57
        }
58

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

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

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

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

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

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

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

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

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

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

106
        return scriptState.ReturnValue;
×
107
    }
108

109
    protected abstract TVariable CreateVariables();
110

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

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

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

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

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

138
        if (diagnostics.Length == 0)
×
139
            return _scriptTemplate;
×
140

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

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