Last active
September 11, 2020 13:50
-
-
Save pwhe23/85ea64f16388b7af0e8a77fb82ca00a0 to your computer and use it in GitHub Desktop.
ExecuteCSX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Dotnet.Script.Core; | |
| using Dotnet.Script.DependencyModel.Context; | |
| using Microsoft.CodeAnalysis; | |
| using Microsoft.CodeAnalysis.Text; | |
| public static T ExecuteCsx<T>(string scriptPath, params string[] args) | |
| { | |
| using var scriptStream = File.OpenRead(scriptPath); | |
| var compiler = new ScriptCompiler(LogFactory, true); | |
| var runner = new ScriptRunner(compiler, LogFactory, ScriptConsole.Default); | |
| var sourceText = SourceText.From(scriptStream); | |
| var context = new ScriptContext(sourceText, Directory.GetCurrentDirectory(), args, null, OptimizationLevel.Release, ScriptMode.Eval); | |
| var result = runner.Execute<T>(context).GetAwaiter().GetResult(); | |
| return result; | |
| Dotnet.Script.DependencyModel.Logging.Logger LogFactory(Type type) | |
| { | |
| return (level, message, exception) => | |
| { | |
| Console.WriteLine($"{level} {message} {exception}"); | |
| }; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Args is a global variable | |
| var result = Args.Count; | |
| // Return results to caller | |
| return result; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment