Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refact CSharpCompilationProvider to compile a Post

  • Loading branch information...
commit 90ffd7e4c55ed67f3e324462c7834b048c236644 1 parent 9fe0ea5
@jrusbatch authored
View
52 Core/Services/CSharpCompilationProvider.cs
@@ -1,5 +1,8 @@
using System;
using System.Reflection;
+using System.Text;
+using Compilify.Extensions;
+using Compilify.Models;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
@@ -7,11 +10,21 @@ namespace Compilify.Services
{
public interface ICSharpCompilationProvider
{
- Compilation Compile(string compilationName, params SyntaxTree[] syntaxTrees);
+ Compilation Compile(Post post);
}
public class CSharpCompilationProvider : ICSharpCompilationProvider
{
+ private const string EntryPoint = @"public class EntryPoint
+ {
+ public static object Result { get; set; }
+
+ public static void Main()
+ {
+ Result = Script.Eval();
+ }
+ }";
+
private static readonly ReadOnlyArray<string> DefaultNamespaces =
ReadOnlyArray<string>.CreateFrom(new[]
{
@@ -24,9 +37,31 @@ public class CSharpCompilationProvider : ICSharpCompilationProvider
"System.Collections.Generic"
});
+ public Compilation Compile(Post post)
+ {
+ if (post == null)
+ {
+ throw new ArgumentNullException("post");
+ }
+
+
+ var entryPoint = SyntaxTree.ParseCompilationUnit(EntryPoint);
+
+ var prompt = SyntaxTree.ParseCompilationUnit(BuildScript(post.Content), fileName: "Prompt",
+ options: new ParseOptions(kind: SourceCodeKind.Interactive))
+ .RewriteWith<MissingSemicolonRewriter>();
+
+ var editor = SyntaxTree.ParseCompilationUnit(post.Classes ?? string.Empty, fileName: "Editor",
+ options: new ParseOptions(kind: SourceCodeKind.Script))
+ .RewriteWith<MissingSemicolonRewriter>();
+
+
+ return Compile(post.Title ?? "Untitled", new[] { entryPoint, prompt, editor });
+ }
+
public Compilation Compile(string compilationName, params SyntaxTree[] syntaxTrees)
{
- if (string.IsNullOrEmpty(compilationName))
+ if (String.IsNullOrEmpty(compilationName))
{
throw new ArgumentNullException("compilationName");
}
@@ -48,5 +83,18 @@ public Compilation Compile(string compilationName, params SyntaxTree[] syntaxTre
return compilation;
}
+
+ private static string BuildScript(string content)
+ {
+ var builder = new StringBuilder();
+
+ builder.AppendLine("public static object Eval() {");
+ builder.AppendLine("#line 1");
+ builder.Append(content);
+ builder.AppendLine("}");
+
+ return builder.ToString();
+ }
+
}
}
View
42 Core/Services/CSharpExecutor.cs
@@ -9,16 +9,6 @@ namespace Compilify.Services
{
public class CSharpExecutor
{
- public const string EntryPoint = @"public class EntryPoint
- {
- public static object Result { get; set; }
-
- public static void Main()
- {
- Result = Script.Eval();
- }
- }";
-
public CSharpExecutor()
: this(new CSharpCompilationProvider()) { }
@@ -31,43 +21,19 @@ public CSharpExecutor(ICSharpCompilationProvider compilationProvider)
public object Execute(Post post)
{
- if (post == null)
- {
- throw new ArgumentNullException("post");
- }
-
- return Execute(post.Content, post.Classes);
- }
-
- public object Execute(string command, string classes)
- {
-
- var script = "public static object Eval() {" + command + "}";
-
- var name = "_" + Guid.NewGuid().ToString("N");
-
- var rewriter = new MissingSemicolonRewriter();
- var compilation = compiler.Compile(name,
- SyntaxTree.ParseCompilationUnit(EntryPoint),
-
- SyntaxTree.ParseCompilationUnit(script, options: new ParseOptions(kind: SourceCodeKind.Interactive))
- .RewriteWith(rewriter),
-
- SyntaxTree.ParseCompilationUnit(classes ?? string.Empty, options: new ParseOptions(kind: SourceCodeKind.Script))
- .RewriteWith(rewriter)
- );
+ var compilation = compiler.Compile(post);
byte[] compiledAssembly;
- using (var output = new MemoryStream())
+ using (var stream = new MemoryStream())
{
- var emitResult = compilation.Emit(output);
+ var emitResult = compilation.Emit(stream);
if (!emitResult.Success)
{
return "[Compilation failed]";
}
- compiledAssembly = output.ToArray();
+ compiledAssembly = stream.ToArray();
}
object result;
View
26 Core/Services/CSharpValidator.cs
@@ -2,8 +2,6 @@
using System.Linq;
using System.Text;
using Compilify.Models;
-using Roslyn.Compilers;
-using Roslyn.Compilers.CSharp;
using Roslyn.Compilers.Common;
using Compilify.Extensions;
@@ -20,31 +18,9 @@ public CSharpValidator(ICSharpCompilationProvider compilationProvider)
public IEnumerable<IDiagnostic> GetCompilationErrors(Post post)
{
- var entryPoint = SyntaxTree.ParseCompilationUnit(CSharpExecutor.EntryPoint);
-
- var prompt = SyntaxTree.ParseCompilationUnit(BuildScript(post.Content),
- options: new ParseOptions(kind: SourceCodeKind.Interactive))
- .RewriteWith<MissingSemicolonRewriter>();
-
- var editor = SyntaxTree.ParseCompilationUnit(post.Classes ?? string.Empty, fileName: "Editor",
- options: new ParseOptions(kind: SourceCodeKind.Script))
- .RewriteWith<MissingSemicolonRewriter>();
-
- var result = compiler.Compile("foo", new[] { entryPoint, prompt, editor }).Emit();
+ var result = compiler.Compile(post).Emit();
return result.Diagnostics;
}
-
- private static string BuildScript(string content)
- {
- var builder = new StringBuilder();
-
- builder.AppendLine("public static object Eval() {");
- builder.AppendLine("#line 1");
- builder.Append(content);
- builder.AppendLine("}");
-
- return builder.ToString();
- }
}
}
View
26 Worker/Program.cs
@@ -5,6 +5,7 @@
using System.Text;
using System.Threading.Tasks;
using BookSleeve;
+using Compilify.Models;
using Compilify.Services;
using Newtonsoft.Json;
using Roslyn.Scripting;
@@ -50,26 +51,28 @@ private static void ProcessQueue(string[] queues)
while (true)
{
- var command = WaitForCommandFromQueue(queues);
+ var cmd = WaitForCommandFromQueue(queues);
- if (command == null)
+ if (cmd == null)
{
continue;
}
- var timeInQueue = DateTime.UtcNow - command.Submitted;
+ var timeInQueue = DateTime.UtcNow - cmd.Submitted;
Logger.Info("Job received after {0:N3} seconds in queue.", timeInQueue.TotalSeconds);
- if (timeInQueue > command.TimeoutPeriod)
+ if (timeInQueue > cmd.TimeoutPeriod)
{
- Logger.Warn("Job was in queue for longer than {0} seconds, skipping!", command.TimeoutPeriod.Seconds);
+ Logger.Warn("Job was in queue for longer than {0} seconds, skipping!", cmd.TimeoutPeriod.Seconds);
continue;
}
+ var post = new Post { Content = cmd.Code, Classes = cmd.Classes };
+
stopWatch.Start();
- var result = Executer.Execute(command.Code, command.Classes);
+ var result = Executer.Execute(post);
stopWatch.Stop();
@@ -79,15 +82,16 @@ private static void ProcessQueue(string[] queues)
{
var response = JsonConvert.SerializeObject(new
{
- code = command.Code,
- classes = command.Classes,
- result = formatter.FormatObject(result, quoteStrings: false, memberFormat: MemberDisplayFormat.Inline),
+ code = cmd.Code,
+ classes = cmd.Classes,
time = DateTime.UtcNow,
- duration = stopWatch.ElapsedMilliseconds
+ duration = stopWatch.ElapsedMilliseconds,
+ result = formatter.FormatObject(result, quoteStrings: false,
+ memberFormat: MemberDisplayFormat.Inline),
});
var bytes = Encoding.UTF8.GetBytes(response);
- var listeners = PublishToClient(command.ClientId, bytes);
+ var listeners = PublishToClient(cmd.ClientId, bytes);
Logger.Info("Work results published to {0} listeners.", listeners.Result);
}
Please sign in to comment.
Something went wrong with that request. Please try again.