-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3f8704
commit 07bde0f
Showing
16 changed files
with
214 additions
and
155 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
@using Bicep.Core.Syntax | ||
@using Bicep.Core.SemanticModel | ||
@using Bicep.Core.Emit | ||
@using Bicep.Core.Text | ||
|
||
@inject IJSRuntime JsRuntime; | ||
|
||
<div class="container"> | ||
<div id="editor_lhs" class="left-half" /> | ||
<div id="editor_rhs" class="right-half" /> | ||
</div> | ||
|
||
@code { | ||
protected override async Task OnAfterRenderAsync(bool firstRender) | ||
{ | ||
await base.OnAfterRenderAsync(firstRender); | ||
|
||
if (firstRender) | ||
{ | ||
await JsRuntime.InvokeAsync<object>("CreateDemoEditors", DotNetObjectReference.Create(this)); | ||
|
||
await JS_SetLhsContent(GetStartingTemplate()); | ||
await JS_OnContentChanged(); | ||
} | ||
} | ||
|
||
[JSInvokable] | ||
public async Task JS_OnContentChanged() | ||
=> await Compile(); | ||
|
||
public async Task<string> JS_GetLhsContent() | ||
=> await JsRuntime.InvokeAsync<string>("GetLhsContent", new object[] { }); | ||
|
||
public async Task JS_SetLhsContent(string value) | ||
=> await JsRuntime.InvokeVoidAsync("SetLhsContent", new object[] { value }); | ||
|
||
public async Task JS_SetRhsContent(string value) | ||
=> await JsRuntime.InvokeVoidAsync("SetRhsContent", new object[] { value }); | ||
|
||
public async Task JS_SetLhsDiagnostics(IEnumerable<dynamic> diagnostics) | ||
=> await JsRuntime.InvokeVoidAsync("SetLhsDiagnostics", new object[] { diagnostics }); | ||
|
||
private static dynamic ToMonacoDiagnostic(Error error,IReadOnlyList<int> lineStarts) | ||
{ | ||
var (startLine, startChar) = TextCoordinateConverter.GetPosition(lineStarts, error.Span.Position); | ||
var (endLine, endChar) = TextCoordinateConverter.GetPosition(lineStarts, error.Span.Position + error.Span.Length); | ||
|
||
return new { | ||
code = "BCP001", | ||
message = error.Message, | ||
startLineNumber = startLine + 1, | ||
startColumn = startChar + 1, | ||
endLineNumber = endLine + 1, | ||
endColumn = endChar + 1, | ||
}; | ||
} | ||
|
||
private static string GetStartingTemplate() | ||
{ | ||
return ReadStreamToEnd(Assembly.GetExecutingAssembly().GetManifestResourceStream("Bicep.WebDemo.basic.arm")); | ||
} | ||
|
||
private static string ReadStreamToEnd(Stream stream) | ||
{ | ||
using var reader = new StreamReader(stream); | ||
return reader.ReadToEnd(); | ||
} | ||
|
||
private async Task Compile() | ||
{ | ||
try | ||
{ | ||
var content = await JS_GetLhsContent(); | ||
|
||
var lineStarts = TextCoordinateConverter.GetLineStarts(content); | ||
var compilation = new Compilation(SyntaxFactory.CreateFromText(content)); | ||
|
||
var emitter = new TemplateEmitter(compilation.GetSemanticModel()); | ||
|
||
// memory stream is not ideal for frequent large allocations | ||
using var stream = new MemoryStream(); | ||
var emitResult = emitter.Emit(stream); | ||
|
||
await JS_SetLhsDiagnostics(emitResult.Diagnostics.Select(e => ToMonacoDiagnostic(e, lineStarts))); | ||
|
||
if (emitResult.Status != EmitStatus.Failed) | ||
{ | ||
// compilation was successful or had warnings - return the compiled template | ||
stream.Position = 0; | ||
var template = ReadStreamToEnd(stream); | ||
await JS_SetRhsContent(template); | ||
|
||
return; | ||
} | ||
|
||
// compilation failed | ||
var buffer = new StringBuilder(); | ||
foreach (Error diagnostic in emitResult.Diagnostics) | ||
{ | ||
var (line, character) = TextCoordinateConverter.GetPosition(lineStarts, diagnostic.Span.Position); | ||
|
||
buffer.AppendLine($"({line + 1},{character + 1}): error BCP001: {diagnostic.Message}"); | ||
} | ||
|
||
await JS_SetRhsContent(buffer.ToString()); | ||
} | ||
catch (Exception exception) | ||
{ | ||
await JS_SetRhsContent(exception.ToString()); | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
html, body { | ||
height: 100vh; | ||
border: 0; | ||
margin: 0; | ||
overflow: hidden; | ||
} | ||
|
||
.container { | ||
height: 100vh; | ||
display: grid; | ||
grid-auto-columns: 1fr; | ||
grid-auto-flow: column; | ||
} | ||
|
||
.left-half { | ||
grid-column: 1; | ||
} | ||
|
||
.right-half { | ||
grid-column: 2; | ||
} |
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> | ||
<title>Bicep Web Demo</title> | ||
<base href="/" /> | ||
<link href="css/app.css" rel="stylesheet" /> | ||
<link rel="stylesheet" data-name="vs/editor/editor.main" href="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs/editor/editor.main.min.css" integrity="sha512-XjO5XYWl8u7tYF3CItEHgoWh8rtQmDeSzPJq4+4PIrbak7TiqeKbWv/4ZwHZ1HAf5dIWHXoguA/jobn2psKZ7w==" crossorigin="anonymous" /> | ||
</head> | ||
|
||
<body> | ||
<app></app> | ||
<script src="_framework/blazor.webassembly.js"></script> | ||
<script>var require = { paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs' } };</script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs/loader.min.js" integrity="sha512-gWqaPfGsZiCkYB6e5mNN4SMRpEvLGItLBqgOAoyLSKtDxWS2O1YPcOeGTy8d8l00gqc8rVue9ae/ncZqhmfj4g==" crossorigin="anonymous"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs/editor/editor.main.nls.min.js" integrity="sha512-4XY3/LL6Plkq2tXMnatY+se07ZhwFMkZehD5Z68HrKoSL3BTyLDjwFomhUWixKu8VUw6jWe/8RXT1WtH95lLFg==" crossorigin="anonymous"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs/editor/editor.main.min.js" integrity="sha512-CK3czr9gLG2z/ygdSM+gNenkN9AShfpW4gMjVyY06VgFHLoc4OCoO92Z8UkcBwVX0ufoYbJw3IVs1mHfXQeI3g==" crossorigin="anonymous"></script> | ||
<script src="js/app.js"></script> | ||
</body> | ||
|
||
</html> |
Oops, something went wrong.