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

loresoft / EntityFrameworkCore.Generator / 14749418583

30 Apr 2025 07:44AM CUT coverage: 56.772%. First build
14749418583

Pull #666

github

web-flow
Merge d89b6a9a9 into 5ddfdd103
Pull Request #666: Bump Injectio from 4.1.0 to 5.0.0

566 of 1125 branches covered (50.31%)

Branch coverage included in aggregate %.

1815 of 3069 relevant lines covered (59.14%)

83.9 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 System.IO;
2
using System.Text;
3

4
using EntityFrameworkCore.Generator.Extensions;
5
using EntityFrameworkCore.Generator.Options;
6
using EntityFrameworkCore.Generator.Parsing;
7

8
using Microsoft.CodeAnalysis;
9
using Microsoft.CodeAnalysis.CSharp.Scripting;
10
using Microsoft.CodeAnalysis.Scripting;
11
using Microsoft.Extensions.Logging;
12

13
using ScriptOptions = Microsoft.CodeAnalysis.Scripting.ScriptOptions;
14

15
namespace EntityFrameworkCore.Generator.Scripts;
16

17
public abstract class ScriptTemplateBase<TVariable>
18
    where TVariable : ScriptVariablesBase
19
{
20
    private Script<string> _scriptTemplate;
21

22
    protected ScriptTemplateBase(ILoggerFactory loggerFactory, GeneratorOptions generatorOptions, TemplateOptions templateOptions)
×
23
    {
24
        Logger = loggerFactory.CreateLogger(this.GetType());
×
25

26
        TemplateOptions = templateOptions;
×
27
        GeneratorOptions = generatorOptions;
×
28
        RegionReplace = new RegionReplace();
×
29

30
    }
×
31

32
    protected ILogger Logger { get; }
×
33

34
    protected RegionReplace RegionReplace { get; }
×
35

36

37
    public TemplateOptions TemplateOptions { get; }
×
38

39
    public GeneratorOptions GeneratorOptions { get; }
×
40

41

42
    protected virtual void WriteCode()
43
    {
44
        var templatePath = TemplateOptions.TemplatePath;
×
45

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

52
        // save file
53
        var directory = TemplateOptions.Directory;
×
54
        if (!Directory.Exists(directory))
×
55
            Directory.CreateDirectory(directory);
×
56

57
        var fileName = TemplateOptions.FileName;
×
58
        var path = Path.Combine(directory, fileName);
×
59

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
        Logger.LogInformation(exists
×
69
            ? $"Updating template script file: {fileName}"
×
70
            : $"Creating template script file: {fileName}");
×
71

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

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

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

87
    protected virtual string ExecuteScript()
88
    {
89
        var templatePath = TemplateOptions.TemplatePath;
×
90
        var script = LoadScript(templatePath);
×
91
        var variables = CreateVariables();
×
92

93
        var scriptTask = script.RunAsync(variables);
×
94
        var scriptState = scriptTask.Result;
×
95

96
        return scriptState.ReturnValue;
×
97
    }
98

99
    protected abstract TVariable CreateVariables();
100

101
    protected Script<string> LoadScript(string scriptPath)
102
    {
103
        if (_scriptTemplate != null)
×
104
            return _scriptTemplate;
×
105

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

108
        var scriptContent = File.ReadAllText(scriptPath);
×
109

110
        var scriptOptions = ScriptOptions.Default
×
111
            .WithReferences(
×
112
                typeof(ScriptVariablesBase).Assembly
×
113
            )
×
114
            .WithImports(
×
115
                "System",
×
116
                "System.Collections.Generic",
×
117
                "System.Linq",
×
118
                "System.Text",
×
119
                "EntityFrameworkCore.Generator.Extensions",
×
120
                "EntityFrameworkCore.Generator.Metadata.Generation",
×
121
                "EntityFrameworkCore.Generator.Options",
×
122
                "Microsoft.EntityFrameworkCore.Internal"
×
123
            );
×
124

125
        _scriptTemplate = CSharpScript.Create<string>(scriptContent, scriptOptions, typeof(TVariable));
×
126
        var diagnostics = _scriptTemplate.Compile();
×
127

128
        if (diagnostics.Length == 0)
×
129
            return _scriptTemplate;
×
130

131
        Logger.LogInformation("Template Compile Diagnostics: ");
×
132
        foreach (var diagnostic in diagnostics)
×
133
        {
134
            var message = diagnostic.GetMessage();
×
135
            switch (diagnostic.Severity)
×
136
            {
137
                case DiagnosticSeverity.Info:
138
                    Logger.LogDebug(message);
×
139
                    break;
×
140
                case DiagnosticSeverity.Warning:
141
                    Logger.LogWarning(message);
×
142
                    break;
×
143
                case DiagnosticSeverity.Error:
144
                    Logger.LogError(message);
×
145
                    break;
×
146
                default:
147
                    Logger.LogDebug(message);
×
148
                    break;
149
            }
150
        }
151

152
        return _scriptTemplate;
×
153
    }
154
}
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