From 36913d0c9e890bcdaa9f916dc3a1bcbe419d6413 Mon Sep 17 00:00:00 2001 From: Bernd Baumanns Date: Sun, 11 Apr 2021 23:17:41 +0200 Subject: [PATCH] "ReadyToRun"- template added (similar to LinqPad-extension) --- .../Controllers/QuoterController.cs | 7 ++- src/Quoter.Web/Dtos/QuoterRequestArgument.cs | 1 + src/Quoter.Web/ReadyToRunHelper.cs | 43 +++++++++++++++++++ src/Quoter.Web/wwwroot/index.html | 5 +++ src/Quoter.Web/wwwroot/scripts.js | 5 +++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/Quoter.Web/ReadyToRunHelper.cs diff --git a/src/Quoter.Web/Controllers/QuoterController.cs b/src/Quoter.Web/Controllers/QuoterController.cs index d6547ec..f853ec2 100644 --- a/src/Quoter.Web/Controllers/QuoterController.cs +++ b/src/Quoter.Web/Controllers/QuoterController.cs @@ -42,6 +42,11 @@ namespace QuoterService.Controllers }; responseText = quoter.QuoteText(arguments.SourceText, arguments.NodeKind); + + if (arguments.ReadyToRun) + { + responseText = ReadyToRunHelper.CreateReadyToRunCode(arguments, responseText); + } } catch (Exception ex) { @@ -50,7 +55,7 @@ namespace QuoterService.Controllers prefix = "Congratulations! You've found a bug in Quoter! Please open an issue at https://github.com/KirillOsenkov/RoslynQuoter/issues/new and paste the code you've typed above and this stack:"; } } - + responseText = HttpUtility.HtmlEncode(responseText); if (prefix != null) diff --git a/src/Quoter.Web/Dtos/QuoterRequestArgument.cs b/src/Quoter.Web/Dtos/QuoterRequestArgument.cs index 481cae5..082394c 100644 --- a/src/Quoter.Web/Dtos/QuoterRequestArgument.cs +++ b/src/Quoter.Web/Dtos/QuoterRequestArgument.cs @@ -12,5 +12,6 @@ namespace QuoterWeb public bool KeepRedundantApiCalls { get; set; } public bool AvoidUsingStatic { get; set; } public bool GenerateLinqPad { get; set; } + public bool ReadyToRun { get; set; } } } \ No newline at end of file diff --git a/src/Quoter.Web/ReadyToRunHelper.cs b/src/Quoter.Web/ReadyToRunHelper.cs new file mode 100644 index 0000000..4ee3115 --- /dev/null +++ b/src/Quoter.Web/ReadyToRunHelper.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace QuoterWeb +{ + public static class ReadyToRunHelper + { + public static string CreateReadyToRunCode(QuoterRequestArgument arguments, string roslynCode) + { + return @$"using System; +using System.Linq; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax;{(arguments.AvoidUsingStatic ? string.Empty : @" + +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;")} + +/* +{arguments.SourceText} +*/ + +var tree = SyntaxTree( +//CODE FROM ROSLYN QUOTER: +{roslynCode} +//END +); + +var refApis = AppDomain.CurrentDomain.GetAssemblies() + .Where(a => !a.IsDynamic) + .Select(a => MetadataReference.CreateFromFile(a.Location)); + +var compilation = CSharpCompilation.Create(""something"", new[] {{ tree }}, refApis); +var diag = compilation.GetDiagnostics().Where(e => e.Severity == DiagnosticSeverity.Error).ToList(); + +foreach(var d in diag) +{{ + Console.WriteLine(d); +}}"; + } + } +} diff --git a/src/Quoter.Web/wwwroot/index.html b/src/Quoter.Web/wwwroot/index.html index 2dcd5f7..6bb208d 100644 --- a/src/Quoter.Web/wwwroot/index.html +++ b/src/Quoter.Web/wwwroot/index.html @@ -55,6 +55,11 @@  Do not require 'using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;' +
+ +
 
diff --git a/src/Quoter.Web/wwwroot/scripts.js b/src/Quoter.Web/wwwroot/scripts.js index ad137ba..3883fe8 100644 --- a/src/Quoter.Web/wwwroot/scripts.js +++ b/src/Quoter.Web/wwwroot/scripts.js @@ -27,6 +27,7 @@ function generateArguments() { var preserveOriginalWhitespace = getCheckboxValue("preserveOriginalWhitespace"); var keepRedundantApiCalls = getCheckboxValue("keepRedundantApiCalls"); var avoidUsingStatic = getCheckboxValue("avoidUsingStatic"); + var readyToRun = getCheckboxValue("readyToRun"); var arguments = new Object(); arguments.sourceText = editor.getValue(); arguments.nodeKind = nodeKind; @@ -51,6 +52,10 @@ function generateArguments() { arguments.avoidUsingStatic = true; } + if (readyToRun) { + arguments.readyToRun = true; + } + return arguments; }