using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Scripting; using Microsoft.CodeAnalysis.Scripting; namespace Cauldron.Core; public class RoslynHost { /// /// Run the provided C# script /// /// The script to run /// Namespace imports for the script /// Values that will be made available to the script /// public static async Task RunScript(string code, string[] imports, RoslynHostGlobals globals, CancellationToken cancellationToken = default) { ScriptOptions options = ScriptOptions.Default .AddImports(imports); await CSharpScript.RunAsync(code, options, globals, cancellationToken: cancellationToken); } public static ImmutableArray BuildScript(string code, string[] imports, RoslynHostGlobals globals) { ScriptOptions options = ScriptOptions.Default .AddImports(imports); Script script = CSharpScript.Create(code, options, globals.GetType()); return script.GetCompilation().GetDiagnostics(); } } public class RoslynHostGlobals { public CauldronWriter Cauldron { get; set; } public RoslynHostGlobals(CauldronWriter writer) { this.Cauldron = writer; } }