diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..e1a05e1
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,7 @@
+# build
+[Oo]bj/
+[Bb]in/
+
+# globs
+*.DS_Store
+.git
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c02e110
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,7 @@
+# build
+[Oo]bj/
+[Bb]in/
+
+# globs
+*.DS_Store
+launchSettings.json
\ No newline at end of file
diff --git a/.vs/MonacoEditorCSharp/DesignTimeBuild/.dtbcache.v2 b/.vs/MonacoEditorCSharp/DesignTimeBuild/.dtbcache.v2
new file mode 100644
index 0000000..2aa8b47
Binary files /dev/null and b/.vs/MonacoEditorCSharp/DesignTimeBuild/.dtbcache.v2 differ
diff --git a/.vs/MonacoEditorCSharp/config/applicationhost.config b/.vs/MonacoEditorCSharp/config/applicationhost.config
new file mode 100644
index 0000000..4c40a9d
--- /dev/null
+++ b/.vs/MonacoEditorCSharp/config/applicationhost.config
@@ -0,0 +1,990 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/.vs/MonacoEditorCSharp/v16/.suo b/.vs/MonacoEditorCSharp/v16/.suo
new file mode 100644
index 0000000..79f337e
Binary files /dev/null and b/.vs/MonacoEditorCSharp/v16/.suo differ
diff --git a/Controllers/CompilerController.cs b/Controllers/CompilerController.cs
new file mode 100644
index 0000000..47edb9b
--- /dev/null
+++ b/Controllers/CompilerController.cs
@@ -0,0 +1,73 @@
+using Microsoft.AspNetCore.Mvc;
+using monacoEditorCSharp.Models;
+
+using System;
+
+namespace monacoEditorCSharp.Controllers
+{
+ [Route("api/compiler")]
+ public class CompilerController : Controller
+ {
+ [HttpGet("get")]
+ public dynamic Get()
+ {
+
+ var source = new SourceInfo();
+ return source;
+
+ }
+
+ [HttpPost("resolve")]
+ public dynamic Resolve([FromBody]SourceInfo source)
+ {
+
+ try
+ {
+
+ return CSharpScriptCompiler.CompileRos(source);
+
+ }
+ catch (Exception ex)
+ {
+ return ex.Message;
+ //throw;
+ }
+ }
+
+ [HttpPost("compile")]
+ public dynamic Compile([FromBody]SourceInfo source)
+ {
+
+ try
+ {
+
+ return CSharpScriptCompiler.Compile(source);
+
+ }
+ catch (Exception ex)
+ {
+ return ex.Message;
+ //throw;
+ }
+ }
+
+ [HttpPost("formatcode")]
+ public dynamic formatcode([FromBody]SourceInfo source)
+ {
+
+ try
+ {
+
+ return CSharpScriptCompiler.FormatCode(source);
+
+ }
+ catch (Exception ex)
+ {
+ return ex.Message;
+ //throw;
+ }
+ }
+
+
+ }
+}
diff --git a/DataHelpers/DownloadNugetPackages.cs b/DataHelpers/DownloadNugetPackages.cs
new file mode 100644
index 0000000..cc65efa
--- /dev/null
+++ b/DataHelpers/DownloadNugetPackages.cs
@@ -0,0 +1,115 @@
+//using NuGet;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Reflection;
+
+namespace monacoEditorCSharp.DataHelpers
+{
+ public static class DownloadNugetPackages
+ {
+ private static string installationDirectory = Directory.GetCurrentDirectory() + "//NugetPackages//packages";
+
+ public static List LoadPackages(string packages)
+ {
+ List assemblies = new List();
+ if (!String.IsNullOrWhiteSpace(packages))
+ {
+
+ string[] npackages = packages.Split(';');
+ foreach (var item in npackages)
+ {
+ string downloadItem = "";
+ string version = "";
+ if (item.Contains(','))
+ {
+ downloadItem = item.Split(',')[0];
+ version = item.Split(',')[1];
+ }
+ else
+ {
+ downloadItem = item;
+
+ }
+ var path = $"{installationDirectory}//{downloadItem}";
+
+ var files = System.IO.Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories);
+ foreach (var file in files)
+ {
+ try
+ {
+ var assembly = Assembly.LoadFile(file);
+ assemblies.Add(assembly);
+ }
+ catch (Exception)
+ {
+ // throw
+ }
+ }
+
+ }
+ }
+ return assemblies;
+ }
+ public static void DownloadAllPackages(string packages)
+ {
+ if (!String.IsNullOrWhiteSpace(packages))
+ {
+ string[] npackages = packages.Split(';');
+ foreach (var item in npackages)
+ {
+ if (!String.IsNullOrWhiteSpace(item))
+ {
+ string downloadItem = "";
+ string version = "";
+ if (item.Contains(','))
+ {
+ downloadItem = item.Split(',')[0];
+ version = item.Split(',')[1];
+ }
+ else
+ {
+ downloadItem = item;
+
+ }
+ if (!String.IsNullOrWhiteSpace(version))
+ {
+ DownloadPackage(downloadItem, version);
+ }
+ else
+ {
+ DownloadPackage(downloadItem, null);
+ }
+ }
+ }
+ }
+ }
+ public static void DownloadPackage(string packageName, string version)
+ {
+ string packageInstallationDirectory = installationDirectory + $"//{packageName}";
+ if (!System.IO.File.Exists($"{packageInstallationDirectory}//{packageName}.nuget"))
+ {
+ if (!System.IO.Directory.Exists(packageInstallationDirectory))
+ {
+ System.IO.Directory.CreateDirectory(packageInstallationDirectory);
+ }
+ //https://www.nuget.org/api/v2/package/NuGet.Core/2.14.0
+ string url = "https://packages.nuget.org/api/v2/package/" + $"{packageName}";
+ if (!String.IsNullOrWhiteSpace(version))
+ {
+ url += $"/{version}";
+ }
+
+
+ var wc = new System.Net.WebClient();
+ wc.DownloadFile(url, $"{packageInstallationDirectory}//{packageName}.nuget");
+
+ System.IO.Compression.ZipFile.ExtractToDirectory($"{packageInstallationDirectory}//{packageName}.nuget", packageInstallationDirectory, true);
+
+ }
+
+ }
+
+
+ }
+}
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..c3a6e95
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,21 @@
+#
+# A simple CSharpCompiler
+#
+#
+
+FROM microsoft/aspnetcore-build:latest
+LABEL author="Karay Akar "
+LABEL version=1.0
+
+
+# Web API listens at http://localhost:5000 in container
+EXPOSE 5000
+
+# Copy web app directory to image
+RUN mkdir -pv $HOMEDIR
+WORKDIR $HOMEDIR
+COPY . $HOMEDIR
+
+RUN ["dotnet", "restore"]
+RUN ["dotnet", "build"]
+CMD ["dotnet", "run"]
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..6c2f9b3
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2019 Karay Akar
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
\ No newline at end of file
diff --git a/Models/CodeCompiler.cs b/Models/CodeCompiler.cs
new file mode 100644
index 0000000..ebd6614
--- /dev/null
+++ b/Models/CodeCompiler.cs
@@ -0,0 +1,567 @@
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.Completion;
+using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.CodeAnalysis.CSharp.Scripting;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+using Microsoft.CodeAnalysis.Formatting;
+using Microsoft.CodeAnalysis.Host;
+using Microsoft.CodeAnalysis.Host.Mef;
+using Microsoft.CodeAnalysis.Scripting;
+using Microsoft.CodeAnalysis.Text;
+using Microsoft.Extensions.DependencyModel;
+using monacoEditorCSharp.DataHelpers;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Collections.Immutable;
+using System.Composition.Hosting;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+
+namespace monacoEditorCSharp.Models
+{
+ public static class Extension
+ {
+ public static T[] Concatenate(this T[] first, T[] second)
+ {
+ if (first == null)
+ {
+ return second;
+ }
+ if (second == null)
+ {
+ return first;
+ }
+
+ return first.Concat(second).ToArray();
+ }
+ }
+ public static class CSharpScriptCompiler
+ {
+ private static MetadataReference[] _ref;
+
+
+ public static object Compile(SourceInfo sourceInfo)
+ {
+ var usings = new List();
+ var allusingsInCode = sourceInfo.SourceCode.Split(new string[] { "using " }, StringSplitOptions.None);
+ foreach (var item in allusingsInCode)
+ {
+ if (!String.IsNullOrWhiteSpace(item))
+ {
+ usings.Add(item.Split(';')[0]);
+ }
+ }
+ //var scriptOptions = ScriptOptions.Default.AddImports(usings);
+ DownloadNugetPackages.DownloadAllPackages(sourceInfo.Nuget);
+ var assemblies = DownloadNugetPackages.LoadPackages(sourceInfo.Nuget);
+ ScriptOptions.Default.AddReferences(assemblies);
+ var scriptOptions = ScriptOptions.Default.AddReferences(usings).AddReferences(assemblies);
+
+ string result = "";
+ try
+ {
+ result = JsonConvert.SerializeObject(CSharpScript.EvaluateAsync(sourceInfo.SourceCode, scriptOptions).Result);
+ var r = JsonConvert.SerializeObject(CSharpScript.RunAsync(sourceInfo.SourceCode, scriptOptions).Result);
+ if (String.IsNullOrWhiteSpace(result) || result == "null")
+ {
+ result = r;
+ }
+ }
+ catch (Exception ex)
+ {
+
+ result = ex.Message;
+ }
+ return result;
+
+
+ }
+
+ public static dynamic CompileRos(SourceInfo sourceInfo)
+ {
+ var usings = new List();
+ var allusingsInCode = sourceInfo.SourceCode.Split(new string[] { "using " }, StringSplitOptions.None);
+ foreach (var item in allusingsInCode)
+ {
+ if (!String.IsNullOrWhiteSpace(item))
+ {
+ usings.Add(item.Split(';')[0]);
+ }
+ }
+
+ List assemblies = new List
+ {
+ Assembly.Load("Microsoft.CodeAnalysis"),
+ Assembly.Load("Microsoft.CodeAnalysis.CSharp"),
+ Assembly.Load("Microsoft.CodeAnalysis.Features"),
+ Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features"),
+ };
+
+ var assembliesList = DownloadNugetPackages.LoadPackages(sourceInfo.Nuget);
+ foreach (var item in assembliesList)
+ {
+ var fname = item.FullName.Split(',')[0];
+ if (assemblies.Where(x => x.FullName.Split(',')[0] == fname).FirstOrDefault() == null)
+ {
+ try
+ {
+
+ var asm = item.FullName.Split(',')[0];
+ var loadAssembly = Assembly.Load(asm);
+ assemblies.Add(loadAssembly);
+ }
+ catch (Exception)
+ {
+ //assemblies.Add(item);
+ }
+ }
+ }
+
+ var assembliest = AppDomain.CurrentDomain.GetAssemblies()
+ .Where(a => !a.IsDynamic && File.Exists(a.Location));
+ //MefHostServices.DefaultAssemblies.AddRange (assembliest);
+
+ var partTypes = MefHostServices.DefaultAssemblies.Concat(assemblies)
+ .Distinct()?
+ .SelectMany(x => x?.GetTypes())?
+ .ToArray();
+
+ var compositionContext = new ContainerConfiguration()
+ .WithParts(partTypes)
+ //.WithAssemblies(assemblies)
+ //.WithAssemblies(assembliest)
+ .CreateContainer();
+
+ var host = MefHostServices.Create(compositionContext);
+
+ var workspace = new AdhocWorkspace(host);
+
+ var scriptCode = sourceInfo.SourceCode;// "Guid.N";
+
+ var _ = typeof(Microsoft.CodeAnalysis.CSharp.Formatting.CSharpFormattingOptions);
+
+ var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, usings: usings);
+
+ if (_ref == null || _ref.Length == 0)
+ {
+
+
+
+ //.GroupBy(a => a.GetName().Name)
+ //.Select(grp =>
+ // grp.First(y => y.GetName().Version == grp.Max(x => x.GetName().Version)))
+ //.Select(a => MetadataReference.CreateFromFile(a.Location));
+
+
+ MetadataReference[] _refs2 = assembliest
+ .Select(asm => MetadataReference.CreateFromFile(asm.Location))
+ .ToArray();
+
+ MetadataReference[] _refs = assembliesList
+ .Select(asm => MetadataReference.CreateFromFile(asm.Location))
+ .ToArray();
+
+
+ _ref = new MetadataReference[0];
+
+ try
+ {
+
+ try
+ {
+ foreach (var item in DependencyContext.Default.CompileLibraries)
+ {
+ try
+ {
+ var arr = item.ResolveReferencePaths().Select(asm => MetadataReference.CreateFromFile(asm))?
+ .ToArray();
+ _ref = _ref.Concatenate(arr);
+ }
+ catch (Exception)
+ {
+
+
+ }
+
+ }
+
+ }
+ catch (Exception)
+ {
+
+ }
+
+
+
+ }
+ catch (Exception)
+ {
+
+ }
+ _ref = _ref.Concatenate(_refs);
+ _ref = _ref.Concatenate(_refs2);
+
+ }
+ if (_ref != null && _ref.Length > 0)
+ {
+ _ref = _ref.Concatenate(new[]
+ {
+ MetadataReference.CreateFromFile(typeof(object).Assembly.Location)
+ });
+ var scriptProjectInfo = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(), "Script", "Script", LanguageNames.CSharp, isSubmission: true)
+ .WithMetadataReferences(_ref)
+ .WithCompilationOptions(compilationOptions);
+
+
+ var scriptProject = workspace.AddProject(scriptProjectInfo);
+ var scriptDocumentInfo = DocumentInfo.Create(
+ DocumentId.CreateNewId(scriptProject.Id), "Script",
+ sourceCodeKind: SourceCodeKind.Script,
+ loader: TextLoader.From(TextAndVersion.Create(SourceText.From(scriptCode), VersionStamp.Create())));
+ var scriptDocument = workspace.AddDocument(scriptDocumentInfo);
+
+ // cursor position is at the end
+ var position = sourceInfo.LineNumberOffsetFromTemplate;// scriptCode.Length - 1;
+
+ var completionService = CompletionService.GetService(scriptDocument);
+
+ var results = completionService.GetCompletionsAsync(scriptDocument, position).Result;
+ if (results == null && sourceInfo.LineNumberOffsetFromTemplate < sourceInfo.SourceCode.Length)
+ {
+
+ sourceInfo.LineNumberOffsetFromTemplate++;
+ CompileRos(sourceInfo);
+ }
+
+ if (sourceInfo.SourceCode[sourceInfo.LineNumberOffsetFromTemplate - 1].ToString() == "(")
+ {
+ sourceInfo.LineNumberOffsetFromTemplate--;
+ //CompileRos(sourceInfo);
+ results = completionService.GetCompletionsAsync(scriptDocument, sourceInfo.LineNumberOffsetFromTemplate).Result;
+
+
+ }
+ //Method parameters
+
+ List overloads = GetMethodParams(scriptCode, position);
+ if (overloads != null && overloads.Count > 0)
+ {
+
+ ImmutableArray.Builder builder = ImmutableArray.CreateBuilder();
+ foreach (var item in overloads)
+ {
+ string DisplayText = item;
+ string insertText = item.Split('(')[1].Split(')')[0];
+ CompletionItem ci = CompletionItem.Create(insertText, insertText, insertText);
+ builder.Add(ci);
+
+ }
+
+ if (builder.Count > 0)
+ {
+ ImmutableArray itemlist = builder.ToImmutable();
+ return CompletionList.Create(new Microsoft.CodeAnalysis.Text.TextSpan(), itemlist);
+
+ }
+ }
+
+ return results;
+ }
+ return null;
+ }
+
+ public static dynamic CompileRos2(SourceInfo sourceInfo)
+ {
+
+ List assemblies = new List
+ {
+ Assembly.Load("Microsoft.CodeAnalysis"),
+ Assembly.Load("Microsoft.CodeAnalysis.CSharp"),
+ Assembly.Load("Microsoft.CodeAnalysis.Features"),
+ Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features"),
+ };
+
+ var assembliesList = DownloadNugetPackages.LoadPackages(sourceInfo.Nuget);
+ foreach (var item in assembliesList)
+ {
+ var fname = item.FullName.Split(',')[0];
+ if (assemblies.Where(x => x.FullName.Split(',')[0] == fname).FirstOrDefault() == null)
+ {
+ var loadAssembly = Assembly.Load(item.FullName.Split(',')[0]);
+ assemblies.Add(loadAssembly);
+ }
+ }
+
+ var partTypes = MefHostServices.DefaultAssemblies.Concat(assemblies)
+ .Distinct()
+ .SelectMany(x => x.GetTypes())
+ .ToArray();
+
+ var compositionContext = new ContainerConfiguration()
+ .WithParts(partTypes)
+ //.WithAssemblies(assemblies)
+ .CreateContainer();
+
+ var host = MefHostServices.Create(compositionContext);
+
+ var workspace = new AdhocWorkspace(host);
+
+ var scriptCode = sourceInfo.SourceCode;// "Guid.N";
+
+ var _ = typeof(Microsoft.CodeAnalysis.CSharp.Formatting.CSharpFormattingOptions);
+
+ var usings = new List();
+ var allusingsInCode = sourceInfo.SourceCode.Split(new string[] { "using " }, StringSplitOptions.None);
+ foreach (var item in allusingsInCode)
+ {
+ if (!String.IsNullOrWhiteSpace(item))
+ {
+ usings.Add(item.Split(';')[0]);
+ }
+ }
+
+
+
+
+ var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, usings: usings);
+
+ MetadataReference[] _ref =
+ DependencyContext.Default.CompileLibraries
+ .First(cl => cl.Name == "mssqlrestapi")
+ .ResolveReferencePaths()
+ .Select(asm => MetadataReference.CreateFromFile(asm))
+ .ToArray();
+
+ MetadataReference[] _refs = assembliesList
+
+ .Select(asm => MetadataReference.CreateFromFile(asm.Location))
+ .ToArray();
+
+ MetadataReference[] newArray = new MetadataReference[_ref.Length + _refs.Length];
+ Array.Copy(_ref, newArray, _ref.Length);
+ Array.Copy(_refs, 0, newArray, _ref.Length, _refs.Length);
+ //assembliesList
+ //IMethodSymbol methodWithGenericTypeArgsSymbol = simpleClassToAnalyze.GetMembers("MySimpleMethod").FirstOrDefault() as IMethodSymbol;
+ //var genericMethodSignature = methodWithGenericTypeArgsSymbol.Parameters;
+
+ var scriptProjectInfo = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(), "Script", "Script", LanguageNames.CSharp, isSubmission: true)
+ .WithMetadataReferences(newArray)
+ .WithCompilationOptions(compilationOptions);
+
+ // without .net asseblies
+ // .WithMetadataReferences(new[]
+ // {
+ // MetadataReference.CreateFromFile(typeof(object).Assembly.Location)
+ // })
+
+ var scriptProject = workspace.AddProject(scriptProjectInfo);
+ var scriptDocumentInfo = DocumentInfo.Create(
+ DocumentId.CreateNewId(scriptProject.Id), "Script",
+ sourceCodeKind: SourceCodeKind.Script,
+ loader: TextLoader.From(TextAndVersion.Create(SourceText.From(scriptCode), VersionStamp.Create())));
+ var scriptDocument = workspace.AddDocument(scriptDocumentInfo);
+
+ // cursor position is at the end
+ var position = sourceInfo.LineNumberOffsetFromTemplate;// scriptCode.Length - 1;
+
+ var completionService = CompletionService.GetService(scriptDocument);
+
+ var results = completionService.GetCompletionsAsync(scriptDocument, position).Result;
+ if (results == null && sourceInfo.LineNumberOffsetFromTemplate < sourceInfo.SourceCode.Length)
+ {
+ sourceInfo.LineNumberOffsetFromTemplate++;
+ CompileRos2(sourceInfo);
+ }
+
+ //Method parameters
+
+ List overloads = GetMethodParams(scriptCode, position);
+ if (overloads != null && overloads.Count > 0)
+ {
+
+ ImmutableArray.Builder builder = ImmutableArray.CreateBuilder();
+ foreach (var item in overloads)
+ {
+ string DisplayText = item;
+ string insertText = item.Split('(')[1].Split(')')[0];
+ CompletionItem ci = CompletionItem.Create(insertText, insertText, insertText);
+ builder.Add(ci);
+
+ }
+
+ if (builder.Count > 0)
+ {
+ ImmutableArray itemlist = builder.ToImmutable();
+ return CompletionList.Create(new Microsoft.CodeAnalysis.Text.TextSpan(), itemlist);
+
+ }
+ }
+
+ return results;// JsonConvert.SerializeObject(results);
+ }
+
+ public static dynamic FormatCode(SourceInfo sourceInfo)
+ {
+
+ var assemblies = new[]
+ {
+ Assembly.Load("Microsoft.CodeAnalysis"),
+ Assembly.Load("Microsoft.CodeAnalysis.CSharp"),
+ Assembly.Load("Microsoft.CodeAnalysis.Features"),
+ Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features"),
+ };
+
+ var partTypes = MefHostServices.DefaultAssemblies.Concat(assemblies)
+ .Distinct()
+ .SelectMany(x => x.GetTypes())
+ .ToArray();
+
+ var compositionContext = new ContainerConfiguration()
+ .WithParts(partTypes)
+ .CreateContainer();
+
+ var host = MefHostServices.Create(compositionContext);
+
+ var workspace = new AdhocWorkspace(host);
+ var sourceLanguage = new CSharpLanguage();
+
+ SyntaxTree syntaxTree = sourceLanguage.ParseText(sourceInfo.SourceCode, SourceCodeKind.Script);
+ var root = (CompilationUnitSyntax)syntaxTree.GetRoot();
+ return Formatter.Format(root, workspace).ToFullString();
+ }
+
+ public static List GetMethodParams(string scriptCode, int position)
+ {
+ //position = position - 2;
+ List overloads = new List();
+ var meta = AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES");
+ string[] assemblies = meta.ToString().Split(';', StringSplitOptions.None);
+ var sourceLanguage = new CSharpLanguage();
+ //SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(scriptCode);
+ SyntaxTree syntaxTree = sourceLanguage.ParseText(scriptCode, SourceCodeKind.Script);
+ var root = (CompilationUnitSyntax)syntaxTree.GetRoot();
+
+ try
+ {
+ if (_ref == null)
+ {
+ _ref = _ref = new MetadataReference[0];
+
+
+ var assembliest = AppDomain.CurrentDomain.GetAssemblies()
+ .Where(a => !a.IsDynamic && File.Exists(a.Location));
+
+
+ MetadataReference[] _refs2 = assembliest
+ .Select(asm => MetadataReference.CreateFromFile(asm.Location))
+ .ToArray();
+
+
+
+
+ try
+ {
+ foreach (var item in DependencyContext.Default.CompileLibraries)
+ {
+ try
+ {
+ var arr = item.ResolveReferencePaths().Select(asm => MetadataReference.CreateFromFile(asm))?
+ .ToArray();
+ _ref = _ref.Concatenate(arr);
+ }
+ catch (Exception)
+ {
+
+
+ }
+
+ }
+
+ }
+ catch (Exception)
+ {
+
+ }
+
+
+
+ _ref = _ref.Concatenate(_refs2);
+ }
+
+ var Mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
+ var compilation = CSharpCompilation.Create("MyCompilation",
+ syntaxTrees: new[] { syntaxTree }, references: _ref);
+
+ var model = compilation.GetSemanticModel(syntaxTree);
+
+ var theToken = syntaxTree.GetRoot().FindToken(position);
+ var theNode = theToken.Parent;
+ while (!theNode.IsKind(Microsoft.CodeAnalysis.CSharp.SyntaxKind.InvocationExpression))
+ {
+ theNode = theNode.Parent;
+ if (theNode == null) break; // There isn't an InvocationExpression in this branch of the tree
+ }
+
+ if (theNode == null)
+ {
+ overloads = null;
+ }
+ else
+ {
+ var symbolInfo = model.GetSymbolInfo(theNode);
+ var symbol = symbolInfo.Symbol;
+ var containingType = symbol?.ContainingType;
+
+ //overloads = containingType?.GetMembers(symbol.Name);
+ if (symbolInfo.CandidateSymbols != null)
+ {
+ if (symbolInfo.CandidateSymbols.Length > 0)
+ {
+ foreach (var parameters in symbolInfo.CandidateSymbols)
+ {
+ var i = parameters.ToMinimalDisplayParts(model, position);
+ if (parameters.Kind == SymbolKind.Method)
+ {
+ var mp = parameters.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
+ //var smpl = parameters.ToMinimalDisplayString();
+ overloads.Add(mp);
+
+ }
+
+ }
+
+ }
+ }
+ }
+
+ return overloads;
+ }
+ catch (Exception)
+ {
+ return null;
+ }
+ }
+ public class CSharpLanguage : ILanguageService
+ {
+ private static readonly LanguageVersion MaxLanguageVersion = Enum
+ .GetValues(typeof(LanguageVersion))
+ .Cast()
+ .Max();
+
+ public SyntaxTree ParseText(string sourceCode, SourceCodeKind kind)
+ {
+ var options = new CSharpParseOptions(kind: kind, languageVersion: MaxLanguageVersion);
+
+ // Return a syntax tree of our source code
+ return CSharpSyntaxTree.ParseText(sourceCode, options);
+ }
+
+ public Compilation CreateLibraryCompilation(string assemblyName, bool enableOptimisations)
+ {
+ throw new NotImplementedException();
+ }
+ }
+ }
+}
diff --git a/Models/SourceInfo.cs b/Models/SourceInfo.cs
new file mode 100644
index 0000000..19f019c
--- /dev/null
+++ b/Models/SourceInfo.cs
@@ -0,0 +1,24 @@
+using System.Collections.Generic;
+
+namespace monacoEditorCSharp.Models
+{
+ public class SourceInfo
+ {
+ public SourceInfo()
+ {
+ Usings = new List();
+ References = new List();
+ }
+
+ public string SourceCode { get; set; }
+ public string Nuget { get; set; }
+
+ public List Usings { get; set; }
+
+ public List References { get; set; }
+
+ public int LineNumberOffsetFromTemplate { get; set; }
+
+ internal int CalculateVisibleLineNumber(int compilerLineError) => compilerLineError - LineNumberOffsetFromTemplate;
+ }
+}
diff --git a/MonacoEditorCSharp.csproj b/MonacoEditorCSharp.csproj
new file mode 100644
index 0000000..7e23f55
--- /dev/null
+++ b/MonacoEditorCSharp.csproj
@@ -0,0 +1,59 @@
+
+
+ netcoreapp3.1
+ Exe
+ false
+ win-x64
+ 3.0
+
+
+
+
+
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ true
+
+
+ true
+ true
+
+
+ AnyCPU
+
+
+
+
+
+
+
+
diff --git a/MonacoEditorCSharp.csproj.user b/MonacoEditorCSharp.csproj.user
new file mode 100644
index 0000000..cbc5ee3
--- /dev/null
+++ b/MonacoEditorCSharp.csproj.user
@@ -0,0 +1,11 @@
+
+
+
+ IIS Express
+ FolderProfile1
+ true
+
+
+ ProjectDebugger
+
+
\ No newline at end of file
diff --git a/MonacoEditorCSharp.sln b/MonacoEditorCSharp.sln
new file mode 100644
index 0000000..bfd1712
--- /dev/null
+++ b/MonacoEditorCSharp.sln
@@ -0,0 +1,25 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 16
+VisualStudioVersion = 16.0.30104.148
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MonacoEditorCSharp", "MonacoEditorCSharp.csproj", "{83C7EC0F-399D-423D-A37E-08F08BF79C74}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {83C7EC0F-399D-423D-A37E-08F08BF79C74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {83C7EC0F-399D-423D-A37E-08F08BF79C74}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {83C7EC0F-399D-423D-A37E-08F08BF79C74}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {83C7EC0F-399D-423D-A37E-08F08BF79C74}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {6971475F-29FA-4149-B0A4-D1C5F823960B}
+ EndGlobalSection
+EndGlobal
diff --git a/Program.cs b/Program.cs
new file mode 100644
index 0000000..d97dc56
--- /dev/null
+++ b/Program.cs
@@ -0,0 +1,41 @@
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.Extensions.Hosting;
+
+using System;
+using System.IO;
+
+namespace monacoEditorCSharp
+{
+ public class Program
+ {
+ public static int Main(string[] args)
+ {
+
+ try
+ {
+ StartWebHost();
+ return 0;
+ }
+ catch (Exception)
+ {
+ return 1;
+ }
+ finally
+ {
+
+ }
+ }
+
+
+ private static void StartWebHost()
+ {
+
+ var host = new WebHostBuilder()
+ .UseKestrel()
+ .UseContentRoot(Directory.GetCurrentDirectory())
+ .UseStartup()
+ .Build();
+ host.Run();
+ }
+ }
+}
diff --git a/Properties/PublishProfiles/FolderProfile.pubxml b/Properties/PublishProfiles/FolderProfile.pubxml
new file mode 100644
index 0000000..d6effc9
--- /dev/null
+++ b/Properties/PublishProfiles/FolderProfile.pubxml
@@ -0,0 +1,22 @@
+
+
+
+
+ FileSystem
+ FileSystem
+ Debug
+ Any CPU
+
+ True
+ False
+ 604e1831-3b8d-4c63-9dc0-133744a34254
+ C:\inetpub\monaco
+ True
+ netcoreapp2.0
+ true
+ win-x64
+
+
diff --git a/Properties/PublishProfiles/FolderProfile.pubxml.user b/Properties/PublishProfiles/FolderProfile.pubxml.user
new file mode 100644
index 0000000..ad4c135
--- /dev/null
+++ b/Properties/PublishProfiles/FolderProfile.pubxml.user
@@ -0,0 +1,11 @@
+
+
+
+
+
+ <_PublishTargetUrl>C:\inetpub\monaco
+
+
diff --git a/Properties/PublishProfiles/FolderProfile1.pubxml b/Properties/PublishProfiles/FolderProfile1.pubxml
new file mode 100644
index 0000000..ec0d560
--- /dev/null
+++ b/Properties/PublishProfiles/FolderProfile1.pubxml
@@ -0,0 +1,22 @@
+
+
+
+
+ FileSystem
+ FileSystem
+ Debug
+ Any CPU
+
+ True
+ False
+ 604e1831-3b8d-4c63-9dc0-133744a34254
+ F:\NETSample\monaco
+ False
+ netcoreapp3.1
+ win-x86
+ false
+
+
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..32c19ab
--- /dev/null
+++ b/README.md
@@ -0,0 +1,7 @@
+# What's here?
+
+This is a *prototype* of a simple Monaco Editor CSharp Backend Compiler
+
+## Try it out!
+
+Happy coding
diff --git a/RoslynUtilities/ReferenceEqualityComparer.cs b/RoslynUtilities/ReferenceEqualityComparer.cs
new file mode 100644
index 0000000..283550b
--- /dev/null
+++ b/RoslynUtilities/ReferenceEqualityComparer.cs
@@ -0,0 +1,18 @@
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+
+namespace monacoEditorCSharp.Utilities
+{
+ internal class ReferenceEqualityComparer : IEqualityComparer