Skip to content

Commit

Permalink
feat: Add support for generating a LINQPad file
Browse files Browse the repository at this point in the history
  • Loading branch information
andymac4182 committed Jun 27, 2018
1 parent 3a7df64 commit 512d1e4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
26 changes: 23 additions & 3 deletions src/Quoter.Web/Controllers/QuoterController.cs
Expand Up @@ -11,14 +11,15 @@ namespace QuoterService.Controllers
public class QuoterController : Controller
{
[HttpGet]
public string Get(
public IActionResult Get(
string sourceText,
NodeKind nodeKind = NodeKind.CompilationUnit,
bool openCurlyOnNewLine = false,
bool closeCurlyOnNewLine = false,
bool preserveOriginalWhitespace = false,
bool keepRedundantApiCalls = false,
bool avoidUsingStatic = false)
bool avoidUsingStatic = false,
bool generateLINQPad = false)
{
string prefix = null;

Expand Down Expand Up @@ -54,14 +55,33 @@ public class QuoterController : Controller
}
}

if (generateLINQPad)
{
var linqpadFile = $@"<Query Kind=""Expression"">
<NuGetReference>Microsoft.CodeAnalysis.Compilers</NuGetReference>
<NuGetReference>Microsoft.CodeAnalysis.CSharp</NuGetReference>
<Namespace>static Microsoft.CodeAnalysis.CSharp.SyntaxFactory</Namespace>
<Namespace>Microsoft.CodeAnalysis.CSharp.Syntax</Namespace>
<Namespace>Microsoft.CodeAnalysis.CSharp</Namespace>
<Namespace>Microsoft.CodeAnalysis</Namespace>
</Query>
{responseText}
";

var responseBytes = Encoding.UTF8.GetBytes(linqpadFile);

return File(responseBytes, "application/octet-stream", "Quoter.linq");
}

responseText = HttpUtility.HtmlEncode(responseText);

if (prefix != null)
{
responseText = "<div class=\"error\"><p>" + prefix + "</p><p>" + responseText + "</p><p><br/>P.S. Sorry!</p></div>";
}

return responseText;
return Ok(responseText);
}
}
}
3 changes: 3 additions & 0 deletions src/Quoter.Web/wwwroot/index.html
Expand Up @@ -58,6 +58,9 @@
<div>
<button id="submitButton" type="button" onclick="onSubmitClick();">Get Roslyn API calls to generate this code!</button> &nbsp;<span id="working" style="display: none">Working...</span>
</div>
<div>
<button id="LINQPadButton" type="button" onclick="onSubmitLINQPad();">Get Roslyn API calls to generate this code as LINQPad file!</button>
</div>
</div>
<div id="outputDiv" xml:space="preserve">
CompilationUnit()
Expand Down
16 changes: 15 additions & 1 deletion src/Quoter.Web/wwwroot/scripts.js
@@ -1,7 +1,7 @@
function onPageLoad() {
}

function onSubmitClick() {
function generateQuery() {
var nodeKind = document.getElementById("nodeKind").value;
var openCurlyOnNewLine = getCheckboxValue("openCurlyOnNewLine");
var closeCurlyOnNewLine = getCheckboxValue("closeCurlyOnNewLine");
Expand Down Expand Up @@ -32,9 +32,23 @@ function onSubmitClick() {
query = query + "&avoidUsingStatic=true";
}

return query;
}

function onSubmitClick() {
var query = generateQuery();

getUrl(query, loadResults);
}

function onSubmitLINQPad() {
var query = generateQuery();

query = query + "&generateLINQPad=true";

window.location = query;
}

function getCheckboxValue(id) {
return document.getElementById(id).checked;
}
Expand Down

0 comments on commit 512d1e4

Please sign in to comment.