Skip to content

Commit

Permalink
Code
Browse files Browse the repository at this point in the history
  • Loading branch information
Suchiman committed May 24, 2018
1 parent 28c729b commit e3a7c2d
Show file tree
Hide file tree
Showing 26 changed files with 1,217 additions and 0 deletions.
63 changes: 63 additions & 0 deletions .gitattributes
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
25 changes: 25 additions & 0 deletions Runny.sln
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27705.2000
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Runny", "Runny\Runny.csproj", "{2BBD4894-B2B0-4E5B-A226-92A71334CC5D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2BBD4894-B2B0-4E5B-A226-92A71334CC5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2BBD4894-B2B0-4E5B-A226-92A71334CC5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2BBD4894-B2B0-4E5B-A226-92A71334CC5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2BBD4894-B2B0-4E5B-A226-92A71334CC5D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {934861F0-0D4F-43E7-B119-048325D872D6}
EndGlobalSection
EndGlobal
5 changes: 5 additions & 0 deletions Runny/App.cshtml
@@ -0,0 +1,5 @@
<!--
Configuring this here is temporary. Later we'll move the app config
into Program.cs, and it won't be necessary to specify AppAssembly.
-->
<Router AppAssembly=typeof(Program).Assembly />
64 changes: 64 additions & 0 deletions Runny/Compiler.cs
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace Runny
{
public static class Compiler
{
public static Assembly LoadSource(string source)
{
var references = new List<MetadataReference>();
references.Add(MetadataReference.CreateFromFile(typeof(object).Assembly.Location));

var compilation = CSharpCompilation.Create("DynamicCode")
.WithOptions(new CSharpCompilationOptions(OutputKind.ConsoleApplication))
.AddReferences(references)
.AddSyntaxTrees(CSharpSyntaxTree.ParseText(source));

ImmutableArray<Diagnostic> diagnostics = compilation.GetDiagnostics();

bool error = false;
foreach (Diagnostic diag in diagnostics)
{
switch (diag.Severity)
{
case DiagnosticSeverity.Info:
Console.WriteLine(diag.ToString());
break;
case DiagnosticSeverity.Warning:
Console.WriteLine(diag.ToString());
break;
case DiagnosticSeverity.Error:
error = true;
Console.Error.WriteLine(diag.ToString());
break;
}
}

if (error)
{
throw new Exception("Compilation failed");
}

using (var outputAssembly = new MemoryStream())
{
compilation.Emit(outputAssembly);

return Assembly.Load(outputAssembly.ToArray());
}
}

public static string Format(string source)
{
var tree = CSharpSyntaxTree.ParseText(source);
var root = tree.GetRoot();
var normalized = root.NormalizeWhitespace();
return normalized.ToString();
}
}
}
6 changes: 6 additions & 0 deletions Runny/Pages/Index.cshtml
@@ -0,0 +1,6 @@
@page "/"
@inherits IndexModel

<textarea bind="@Code" rows="20" cols="100"></textarea>
<button class="btn" onclick="@Run">Run</button>
<textarea bind="@Output" rows="20" cols="100"></textarea>
47 changes: 47 additions & 0 deletions Runny/Pages/Index.cshtml.cs
@@ -0,0 +1,47 @@
using System;
using System.IO;
using Microsoft.AspNetCore.Blazor.Components;

namespace Runny.Pages
{
public class IndexModel : BlazorComponent
{
public string Output = "";
public string Code = @"using System;
class Program
{
public static void Main()
{
Console.WriteLine(""Hello World"");
}
}";

public void Run()
{
Output = "";
var asm = Compiler.LoadSource(Code);
var hasArgs = asm.EntryPoint.GetParameters().Length > 0;

var currentOut = Console.Out;
var writer = new StringWriter();
Console.SetOut(writer);

Exception exception = null;
try
{
asm.EntryPoint.Invoke(null, hasArgs ? new string[][] { null } : null);
}
catch (Exception ex)
{
exception = ex;
}
Output = writer.ToString();
if (exception != null)
{
Output += "\r\n" + exception.ToString();
}
Console.SetOut(currentOut);
}
}
}
1 change: 1 addition & 0 deletions Runny/Pages/_ViewImports.cshtml
@@ -0,0 +1 @@
@layout MainLayout
20 changes: 20 additions & 0 deletions Runny/Program.cs
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Blazor.Browser.Rendering;
using Microsoft.AspNetCore.Blazor.Browser.Services;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace Runny
{
public class Program
{
static void Main(string[] args)
{
var serviceProvider = new BrowserServiceProvider(services =>
{
// Add any custom services here
});

new BrowserRenderer(serviceProvider).AddComponent<App>("app");
}
}
}
24 changes: 24 additions & 0 deletions Runny/Runny.csproj
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RunCommand>dotnet</RunCommand>
<RunArguments>blazor serve</RunArguments>
<RestoreSources>
$(RestoreSources);
https://api.nuget.org/v3/index.json;
https://dotnet.myget.org/f/blazor-dev/api/v3/index.json;
</RestoreSources>
<LangVersion>7.3</LangVersion>
<BlazorLinkOnBuild>false</BlazorLinkOnBuild>
<!--<AdditionalMonoLinkerOptions>- -skip-unresolved true</AdditionalMonoLinkerOptions>-->
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.7.0" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Browser" Version="0.4.0-preview1-10298" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="0.4.0-preview1-10298" />
<DotNetCliToolReference Include="Microsoft.AspNetCore.Blazor.Cli" Version="0.4.0-preview1-10298" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions Runny/Shared/MainLayout.cshtml
@@ -0,0 +1,15 @@
@inherits BlazorLayoutComponent

<div class="sidebar">
<NavMenu />
</div>

<div class="main">
<div class="top-row px-4">
<a href="http://blazor.net" target="_blank" class="ml-md-auto">About</a>
</div>

<div class="content px-4">
@Body
</div>
</div>
25 changes: 25 additions & 0 deletions Runny/Shared/NavMenu.cshtml
@@ -0,0 +1,25 @@
<div class="top-row pl-4 navbar navbar-dark">
<a class="navbar-brand" href="">Runny</a>
<button class="navbar-toggler" onclick=@ToggleNavMenu>
<span class="navbar-toggler-icon"></span>
</button>
</div>

<div class=@(collapseNavMenu ? "collapse" : null) onclick=@ToggleNavMenu>
<ul class="nav flex-column">
<li class="nav-item px-3">
<NavLink class="nav-link" href="" Match=NavLinkMatch.All>
<span class="oi oi-home" aria-hidden="true"></span> Home
</NavLink>
</li>
</ul>
</div>

@functions {
bool collapseNavMenu = true;

void ToggleNavMenu()
{
collapseNavMenu = !collapseNavMenu;
}
}
5 changes: 5 additions & 0 deletions Runny/_ViewImports.cshtml
@@ -0,0 +1,5 @@
@using System.Net.Http
@using Microsoft.AspNetCore.Blazor.Layouts
@using Microsoft.AspNetCore.Blazor.Routing
@using Runny
@using Runny.Shared
5 changes: 5 additions & 0 deletions Runny/global.json
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "2.1.300-preview2-008533"
}
}
7 changes: 7 additions & 0 deletions Runny/wwwroot/css/bootstrap/bootstrap.min.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Runny/wwwroot/css/bootstrap/bootstrap.min.css.map

Large diffs are not rendered by default.

0 comments on commit e3a7c2d

Please sign in to comment.