-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutableCodeRenderer.cs
More file actions
53 lines (49 loc) · 2.45 KB
/
ExecutableCodeRenderer.cs
File metadata and controls
53 lines (49 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using Microsoft.CodeAnalysis;
using System.IO;
using Dotnet.Script.DependencyModel.Logging;
using Dotnet.Script.Core;
using Microsoft.CodeAnalysis.Scripting.Hosting;
using Microsoft.CodeAnalysis.CSharp.Scripting.Hosting;
using Dotnet.Script.DependencyModel.Context;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
using Markdig.Extensions.Xmdl.ExecutableCode;
namespace Markdig.Extensions.Xmdl.CSharp;
internal class ExecutableCodeRenderer(IExecutableCodeOptions options, MarkdownPipeline pipeline) : Xmdl.ExecutableCode.ExecutableCodeRenderer(options, pipeline)
{
private readonly IExecutableCodeOptions _options = options;
private readonly MarkdownPipeline _pipeline = pipeline;
public override async Task<(ExecutableCodeState, ExecutableCodeResult, List<string>)> ExecuteAsync(string script, MarkdownParserContext context, string previousScript = null)
{
List<string> errors = new();
ScriptContext scriptContext = new(SourceText.From((previousScript ?? Globals.GlobalScript) ?? script), _options.WorkingDirectory ?? Directory.GetCurrentDirectory(), _options.Arguments, null, ((ExecutableCodeOptions)_options).OptimizationLevel ?? OptimizationLevel.Debug, ScriptMode.Eval);
ScriptCompilationContext<object> compilationContext = Globals.ScriptCompiler.CreateCompilationContext<object, CommandLineScriptGlobals>(scriptContext);
var globals = new CommandLineScriptGlobals(Globals.ScriptConsole.Out, CSharpObjectFormatter.Instance);
ScriptState scriptState = null;
try
{
scriptState = await compilationContext.Script.ContinueWith(script).RunAsync(globals);
}
catch (Exception ex)
{
errors.Add(Markdown.ToHtml(BuildMarkdownExceptionMessage("C#", ex, _options.IsDebugMode), _pipeline));
}
var result = new ExecutableCodeResult()
{
Errors = compilationContext.Errors.Select(e => new ExecutableCodeError(e.Severity.ToString(), "C#", e.ToString())).ToList(),
};
var state = new ExecutableCodeState()
{
Exception = scriptState.Exception,
ReturnValue = scriptState.ReturnValue,
//Script = scriptState.Script,
//Variables = scriptState.Variables,
};
return (state, result, errors);
}
}