Skip to content

Commit

Permalink
fix NTypewriter.Online
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed Feb 12, 2024
1 parent c90998e commit 3e03ee3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
9 changes: 9 additions & 0 deletions NTypewriter.Online/NTypewriter.Online.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup>
<ActiveDebugProfile>http</ActiveDebugProfile>
</PropertyGroup>
</Project>
4 changes: 3 additions & 1 deletion NTypewriter.Online/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using NTypewriter.Online.Adapters;
using NTypewriter.Ports;
using NTypewriter.Runtime;
using NTypewriter.Runtime.Scripting;

namespace NTypewriter.Online
{
Expand Down Expand Up @@ -58,7 +60,7 @@ public async Task<Result> RunAsync(string inputCode, string template, Cancellati

try
{
var cmd = new RenderTemplatesCommand(null, userCodeProvider, generatedFileReaderWriter, userIO, null, null, null, null);
var cmd = new RenderTemplatesCommand(null, userCodeProvider, generatedFileReaderWriter, userIO, null, null, null, null, new ExpressionCompiler(References));
await cmd.Execute(generatorCompilation, new[] { templateToRender });
}
catch (Exception ex)
Expand Down
12 changes: 9 additions & 3 deletions NTypewriter.Runtime/RenderTemplatesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using NTypewriter.Editor.Config;
using NTypewriter.Ports;
using NTypewriter.Runtime.CodeModel;
using NTypewriter.Runtime.CodeModel.Internals;
using NTypewriter.Runtime.Internals;
using NTypewriter.Runtime.Rendering;
using NTypewriter.Runtime.Scripting;
using NTypewriter.Runtime.UserCode;

namespace NTypewriter.Runtime
Expand Down Expand Up @@ -36,7 +38,8 @@ public class RenderTemplatesCommand
private readonly IUserCodeProvider userCodeProvider;
private readonly IUserInterfaceErrorListUpdater uiErrorList;
private readonly IUserInterfaceOutputWriter uiOutput;
private readonly IUserInterfaceStatusUpdater uiStatus;
private readonly IUserInterfaceStatusUpdater uiStatus;
private readonly IExpressionCompiler expressionCompiler;


public RenderTemplatesCommand(ITemplateContentLoader templateContentLoader,
Expand All @@ -46,7 +49,9 @@ public RenderTemplatesCommand(ITemplateContentLoader templateContentLoader,
ISolutionItemsManager solutionItemsManager = null,
ISourceControl sourceControl = null,
IUserInterfaceErrorListUpdater uiErrorList = null,
IUserInterfaceStatusUpdater uiStatus = null)
IUserInterfaceStatusUpdater uiStatus = null,
IExpressionCompiler expressionCompiler = null
)
{
this.templateContentLoader = templateContentLoader;
this.userCodeProvider = userCodeProvider;
Expand All @@ -56,6 +61,7 @@ public RenderTemplatesCommand(ITemplateContentLoader templateContentLoader,
this.solutionItemsManager = solutionItemsManager;
this.sourceControl = sourceControl ?? NullObject.Singleton;
this.uiStatus = uiStatus ?? NullObject.Singleton;
this.expressionCompiler = expressionCompiler ?? ExpressionCompiler.Default;
}


Expand Down Expand Up @@ -93,7 +99,7 @@ private async Task Execute(SolutionOrCompilation roslynInput, IList<TemplateToRe
uiOutput.Info($"Loading template : {template.FilePath}");
var templateContent = template.Content ?? await templateContentLoader.Read(template.FilePath).ConfigureAwait(false);

var templateRenderer = new TemplateRenderer(uiErrorList, uiOutput);
var templateRenderer = new TemplateRenderer(uiErrorList, uiOutput, expressionCompiler);
var renderedItems = await templateRenderer.RenderAsync(template.FilePath, templateContent, codeModel, userInput.TypesThatMayContainCustomFunctions, editorConfig, environmentVariables).ConfigureAwait(false);

await GeneratedFileManager.SaveChanges(renderedItems, generatedFileReaderWriter, uiOutput, sourceControl).ConfigureAwait(false);
Expand Down
8 changes: 4 additions & 4 deletions NTypewriter.Runtime/Rendering/TemplateRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
using NTypewriter.Editor.Config;
using NTypewriter.Ports;
using NTypewriter.Runtime.Rendering.Internals;
using NTypewriter.Runtime.Scripting;

namespace NTypewriter.Runtime.Rendering
{
public class TemplateRenderer
{
private readonly IUserInterfaceErrorListUpdater errorList;
private readonly IUserInterfaceOutputWriter output;
private readonly IExpressionCompiler expressionCompiler;


public TemplateRenderer(IUserInterfaceErrorListUpdater errorList, IUserInterfaceOutputWriter output)
public TemplateRenderer(IUserInterfaceErrorListUpdater errorList, IUserInterfaceOutputWriter output, IExpressionCompiler expressionCompiler)
{
this.errorList = errorList;
this.output = output;
this.expressionCompiler = expressionCompiler;
}


Expand All @@ -40,7 +40,7 @@ public async Task<IEnumerable<RenderingResult>> RenderAsync(string templateFileP
dataModels[VariableNames.Config] = configAdapter;
dataModels[VariableNames.Env] = environmentVariables ?? new EnvironmentVariables();

var result = await NTypeWriter.Render(template, dataModels, configuration, new ExternalOutputAdapter(output), new ExpressionCompiler());
var result = await NTypeWriter.Render(template, dataModels, configuration, new ExternalOutputAdapter(output), expressionCompiler);

output.Info("Used configuration : " + editorConfig.ToString());

Expand Down
13 changes: 10 additions & 3 deletions NTypewriter.Runtime/Scripting/ExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

namespace NTypewriter.Runtime.Scripting
{
internal class ExpressionCompiler : IExpressionCompiler
public class ExpressionCompiler : IExpressionCompiler
{
private static readonly Lazy<ExpressionCompiler> defaultSingleton = new Lazy<ExpressionCompiler>(() => new ExpressionCompiler());
private static readonly Type[] RefTypeMarkers =
[
typeof(NTypewriter.CodeModel.ICodeModel),
Expand All @@ -24,10 +25,12 @@ internal class ExpressionCompiler : IExpressionCompiler
"System",
"NTypewriter.CodeModel",
];
private static readonly IEnumerable<MetadataReference> MetadataReferences;
private readonly IEnumerable<MetadataReference> MetadataReferences;

public static ExpressionCompiler Default => defaultSingleton.Value;

static ExpressionCompiler()

public ExpressionCompiler()
{
var additionalReferences = RefTypeMarkers.Select(x => MetadataReference.CreateFromFile(x.Assembly.Location)).ToList();

Expand All @@ -37,6 +40,10 @@ static ExpressionCompiler()

MetadataReferences = references;
}
public ExpressionCompiler(IEnumerable<MetadataReference> references)
{
MetadataReferences = references;
}


public Func<object, bool> CompilePredicate(string predicate, Type type)
Expand Down

0 comments on commit 3e03ee3

Please sign in to comment.