From 128ed038c058c3ccbcacbe34abd463c08960030a Mon Sep 17 00:00:00 2001 From: Andrey Shchekin Date: Fri, 23 Dec 2016 23:39:56 +1300 Subject: [PATCH] wip-6: removed some of the unused parts. --- source/Core/AgentHeisenbug.Annotations.cs | 4 - source/Core/Core.csproj | 5 - source/Core/CoreModule.cs | 2 - source/Core/ICodeProcessor.cs | 10 -- source/Core/Processing/CodeProcessor.cs | 67 ---------- source/Core/ProcessingResult.cs | 25 ---- source/Core/ProcessingResultDiagnostic.cs | 25 ---- .../ProcessingResultDiagnosticLocation.cs | 17 --- source/Tests/CodeProcessorTests.cs | 124 +++++++++--------- source/TryRoslyn.sln.DotSettings | 2 + source/Web.Api/App_Start/Startup.cs | 6 +- source/Web.Api/Integration/SlowUpdate.cs | 1 - source/Web/Web.csproj | 6 +- source/Web/index.html | 6 +- source/Web/js/app.js | 2 - source/Web/js/server/send-code-async.js | 36 ----- source/Web/js/ui/components/app-codemirror.js | 13 +- 17 files changed, 74 insertions(+), 277 deletions(-) delete mode 100644 source/Core/ICodeProcessor.cs delete mode 100644 source/Core/Processing/CodeProcessor.cs delete mode 100644 source/Core/ProcessingResult.cs delete mode 100644 source/Core/ProcessingResultDiagnostic.cs delete mode 100644 source/Core/ProcessingResultDiagnosticLocation.cs delete mode 100644 source/Web/js/server/send-code-async.js diff --git a/source/Core/AgentHeisenbug.Annotations.cs b/source/Core/AgentHeisenbug.Annotations.cs index 310967c..aa21502 100644 --- a/source/Core/AgentHeisenbug.Annotations.cs +++ b/source/Core/AgentHeisenbug.Annotations.cs @@ -9,8 +9,4 @@ namespace JetBrains.Annotations { [ThreadSafe] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method | AttributeTargets.Struct | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] public class ThreadSafeAttribute : Attribute { } - - [ThreadSafe] - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method | AttributeTargets.Struct | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] - public class ReadOnlyAttribute : Attribute { } } diff --git a/source/Core/Core.csproj b/source/Core/Core.csproj index cd06166..47be092 100644 --- a/source/Core/Core.csproj +++ b/source/Core/Core.csproj @@ -102,7 +102,6 @@ - @@ -113,17 +112,13 @@ - - - - diff --git a/source/Core/CoreModule.cs b/source/Core/CoreModule.cs index cf92627..5155562 100644 --- a/source/Core/CoreModule.cs +++ b/source/Core/CoreModule.cs @@ -36,8 +36,6 @@ namespace TryRoslyn.Core { builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); - - builder.RegisterType().As().SingleInstance(); } } } \ No newline at end of file diff --git a/source/Core/ICodeProcessor.cs b/source/Core/ICodeProcessor.cs deleted file mode 100644 index cd99fc9..0000000 --- a/source/Core/ICodeProcessor.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using JetBrains.Annotations; - -namespace TryRoslyn.Core { - [ThreadSafe] - public interface ICodeProcessor : IDisposable { - [NotNull] - ProcessingResult Process([NotNull] string code, [CanBeNull] ProcessingOptions options = null); - } -} \ No newline at end of file diff --git a/source/Core/Processing/CodeProcessor.cs b/source/Core/Processing/CodeProcessor.cs deleted file mode 100644 index c4fb635..0000000 --- a/source/Core/Processing/CodeProcessor.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using JetBrains.Annotations; -using Microsoft.CodeAnalysis; -using TryRoslyn.Core.Decompilation; -using TryRoslyn.Core.Processing.Languages; - -namespace TryRoslyn.Core.Processing { - [ThreadSafe] - public class CodeProcessor : ICodeProcessor { - // ReSharper disable once AgentHeisenbug.FieldOfNonThreadSafeTypeInThreadSafeType - private readonly MetadataReference[] _references = { - MetadataReference.CreateFromFile(typeof(object).Assembly.Location), - MetadataReference.CreateFromFile(typeof(Uri).Assembly.Location), - MetadataReference.CreateFromFile(typeof(DynamicAttribute).Assembly.Location) - }; - private readonly IReadOnlyCollection _decompilers; - private readonly IReadOnlyCollection _languages; - - public CodeProcessor( - IReadOnlyCollection languages, - IReadOnlyCollection decompilers - ) { - _languages = languages; - _decompilers = decompilers; - } - - public ProcessingResult Process(string code, ProcessingOptions options) { - options = options ?? new ProcessingOptions(); - var kind = options.ScriptMode ? SourceCodeKind.Script : SourceCodeKind.Regular; - var sourceLanguage = _languages.Single(l => l.Identifier == options.SourceLanguage); - - var syntaxTree = sourceLanguage.ParseText(code, kind); - - var stream = new MemoryStream(); - var compilation = sourceLanguage - .CreateLibraryCompilation("Test", options.OptimizationsEnabled) - .AddReferences(_references) - .AddSyntaxTrees(syntaxTree); - - var emitResult = compilation.Emit(stream); - - if (!emitResult.Success) - return new ProcessingResult(null, emitResult.Diagnostics.Select(d => new ProcessingResultDiagnostic(d))); - - stream.Seek(0, SeekOrigin.Begin); - - var resultWriter = new StringWriter(); - var decompiler = _decompilers.Single(d => d.Language == options.TargetLanguage); - decompiler.Decompile(stream, resultWriter); - return new ProcessingResult( - resultWriter.ToString(), - emitResult.Diagnostics.Select(d => new ProcessingResultDiagnostic(d)) - ); - } - - #region IDisposable Members - - void IDisposable.Dispose() { - } - - #endregion - } -} \ No newline at end of file diff --git a/source/Core/ProcessingResult.cs b/source/Core/ProcessingResult.cs deleted file mode 100644 index 86c8a1c..0000000 --- a/source/Core/ProcessingResult.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Microsoft.CodeAnalysis; - -namespace TryRoslyn.Core { - [Serializable] - public class ProcessingResult { - public string Decompiled { get; private set; } - public IReadOnlyList Diagnostics { get; private set; } - - public bool IsSuccess { - get { return Decompiled != null; } - } - - public ProcessingResult(string decompiled, IEnumerable diagnostics) { - Decompiled = decompiled; - Diagnostics = diagnostics.ToArray(); // can't use immutables here, as they are non-serializable - } - - public IEnumerable GetDiagnostics(DiagnosticSeverity severity) { - return Diagnostics.Where(d => d.Severity == severity); - } - } -} \ No newline at end of file diff --git a/source/Core/ProcessingResultDiagnostic.cs b/source/Core/ProcessingResultDiagnostic.cs deleted file mode 100644 index bd9973b..0000000 --- a/source/Core/ProcessingResultDiagnostic.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Microsoft.CodeAnalysis; - -namespace TryRoslyn.Core { - [Serializable] - public class ProcessingResultDiagnostic { - public ProcessingResultDiagnostic(Diagnostic diagnostic) { - Id = diagnostic.Id; - Severity = diagnostic.Severity; - Message = diagnostic.GetMessage(); - - var lineSpan = diagnostic.Location.GetMappedLineSpan(); - Start = new ProcessingResultDiagnosticLocation(lineSpan.StartLinePosition); - End = new ProcessingResultDiagnosticLocation(lineSpan.EndLinePosition); - } - - public string Id { get; private set; } - public DiagnosticSeverity Severity { get; private set; } - public string Message { get; private set; } - public ProcessingResultDiagnosticLocation Start { get; private set; } - public ProcessingResultDiagnosticLocation End { get; private set; } - } -} diff --git a/source/Core/ProcessingResultDiagnosticLocation.cs b/source/Core/ProcessingResultDiagnosticLocation.cs deleted file mode 100644 index cfabaae..0000000 --- a/source/Core/ProcessingResultDiagnosticLocation.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Microsoft.CodeAnalysis.Text; - -namespace TryRoslyn.Core { - [Serializable] - public class ProcessingResultDiagnosticLocation { - public ProcessingResultDiagnosticLocation(LinePosition position) { - Line = position.Line; - Column = position.Character; - } - - public int Line { get; private set; } - public int Column { get; private set; } - } -} diff --git a/source/Tests/CodeProcessorTests.cs b/source/Tests/CodeProcessorTests.cs index e328bbe..2d4c4e9 100644 --- a/source/Tests/CodeProcessorTests.cs +++ b/source/Tests/CodeProcessorTests.cs @@ -12,79 +12,79 @@ using Xunit; namespace TryRoslyn.Tests { public class CodeProcessorTests { - private static readonly IDictionary LanguageMap = new Dictionary { - { "cs", LanguageIdentifier.CSharp }, - { "vb", LanguageIdentifier.VBNet }, - { "il", LanguageIdentifier.IL } - }; + //private static readonly IDictionary LanguageMap = new Dictionary { + // { "cs", LanguageIdentifier.CSharp }, + // { "vb", LanguageIdentifier.VBNet }, + // { "il", LanguageIdentifier.IL } + //}; - [Theory] - [InlineData("Constructor.BaseCall.cs2cs")] - [InlineData("NullPropagation.ToTernary.cs2cs")] - [InlineData("Script.cs2cs")] - [InlineData("Simple.cs2il")] - [InlineData("Simple.vb2vb")] - [InlineData("Module.vb2vb")] - public void Process_ReturnsExpectedCode(string resourceName) { - var content = GetResourceContent(resourceName); - var parts = content.Split("?=>"); - var code = parts[0].Trim(); - var expected = parts[1].Trim(); + //[Theory] + //[InlineData("Constructor.BaseCall.cs2cs")] + //[InlineData("NullPropagation.ToTernary.cs2cs")] + //[InlineData("Script.cs2cs")] + //[InlineData("Simple.cs2il")] + //[InlineData("Simple.vb2vb")] + //[InlineData("Module.vb2vb")] + //public void Process_ReturnsExpectedCode(string resourceName) { + // var content = GetResourceContent(resourceName); + // var parts = content.Split("?=>"); + // var code = parts[0].Trim(); + // var expected = parts[1].Trim(); - var result = CreateProcessor().Process(code, GetProcessingOptions(resourceName, content)); + // var result = CreateProcessor().Process(code, GetProcessingOptions(resourceName, content)); - Assert.True(result.IsSuccess, GetErrorString(result)); - AssertGold.Equal(expected, result.Decompiled.Trim()); - } + // Assert.True(result.IsSuccess, GetErrorString(result)); + // AssertGold.Equal(expected, result.Decompiled.Trim()); + //} - [Fact] - public void Process_CanHandleFormattableString() { - var result = CreateProcessor().Process("using System; public class C { public void M() { IFormattable f = $\"{42}\"; } }", new ProcessingOptions { - SourceLanguage = LanguageIdentifier.CSharp - }); + //[Fact] + //public void Process_CanHandleFormattableString() { + // var result = CreateProcessor().Process("using System; public class C { public void M() { IFormattable f = $\"{42}\"; } }", new ProcessingOptions { + // SourceLanguage = LanguageIdentifier.CSharp + // }); - Assert.NotNull(result); - Assert.True(result.IsSuccess, GetErrorString(result)); - } + // Assert.NotNull(result); + // Assert.True(result.IsSuccess, GetErrorString(result)); + //} - private static string GetErrorString(ProcessingResult result) { - return result.Diagnostics - .Aggregate(new StringBuilder("Errors:"), (builder, d) => builder.AppendLine().Append(d)) - .ToString(); - } + //private static string GetErrorString(ProcessingResult result) { + // return result.Diagnostics + // .Aggregate(new StringBuilder("Errors:"), (builder, d) => builder.AppendLine().Append(d)) + // .ToString(); + //} - private string GetResourceContent(string name) { - var fullName = GetType().Namespace + ".TestCode." + name; - // ReSharper disable once AssignNullToNotNullAttribute - using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(fullName)) { - if (stream == null) - throw new FileNotFoundException("Resource was not found.", fullName); + //private string GetResourceContent(string name) { + // var fullName = GetType().Namespace + ".TestCode." + name; + // // ReSharper disable once AssignNullToNotNullAttribute + // using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(fullName)) { + // if (stream == null) + // throw new FileNotFoundException("Resource was not found.", fullName); - using (var reader = new StreamReader(stream)) { - return reader.ReadToEnd(); - } - } - } + // using (var reader = new StreamReader(stream)) { + // return reader.ReadToEnd(); + // } + // } + //} - private ProcessingOptions GetProcessingOptions(string resourceName, string content) { - var scriptMode = content.StartsWith("// script mode") || content.StartsWith("' Script Mode"); - var fromTo = Path.GetExtension(resourceName).TrimStart('.').Split('2'); + //private ProcessingOptions GetProcessingOptions(string resourceName, string content) { + // var scriptMode = content.StartsWith("// script mode") || content.StartsWith("' Script Mode"); + // var fromTo = Path.GetExtension(resourceName).TrimStart('.').Split('2'); - return new ProcessingOptions { - SourceLanguage = LanguageMap[fromTo[0]], - TargetLanguage = LanguageMap[fromTo[1]], - OptimizationsEnabled = true, - ScriptMode = scriptMode - }; - } + // return new ProcessingOptions { + // SourceLanguage = LanguageMap[fromTo[0]], + // TargetLanguage = LanguageMap[fromTo[1]], + // OptimizationsEnabled = true, + // ScriptMode = scriptMode + // }; + //} - private static CodeProcessor CreateProcessor() { - var builder = new ContainerBuilder(); - builder.RegisterModule(); - builder.RegisterType().AsSelf(); - var container = builder.Build(); + //private static CodeProcessor CreateProcessor() { + // var builder = new ContainerBuilder(); + // builder.RegisterModule(); + // builder.RegisterType().AsSelf(); + // var container = builder.Build(); - return container.Resolve(); - } + // return container.Resolve(); + //} } } diff --git a/source/TryRoslyn.sln.DotSettings b/source/TryRoslyn.sln.DotSettings index 67224ba..fc7ad99 100644 --- a/source/TryRoslyn.sln.DotSettings +++ b/source/TryRoslyn.sln.DotSettings @@ -1,4 +1,6 @@  + DO_NOT_SHOW + HINT Experimental END_OF_LINE END_OF_LINE diff --git a/source/Web.Api/App_Start/Startup.cs b/source/Web.Api/App_Start/Startup.cs index de39330..f916712 100644 --- a/source/Web.Api/App_Start/Startup.cs +++ b/source/Web.Api/App_Start/Startup.cs @@ -8,7 +8,7 @@ using MirrorSharp; using MirrorSharp.Advanced; using MirrorSharp.Owin; using Owin; -using TryRoslyn.Core; +using TryRoslyn.Core.Decompilation; using TryRoslyn.Web.Api; [assembly: OwinStartup(typeof(Startup), nameof(Startup.Configuration))] @@ -38,8 +38,8 @@ namespace TryRoslyn.Web.Api { private static IContainer CreateContainer() { var builder = new ContainerBuilder(); var apiAssembly = Assembly.GetExecutingAssembly(); - - builder.RegisterAssemblyModules(typeof(ICodeProcessor).Assembly); + + builder.RegisterAssemblyModules(typeof(IDecompiler).Assembly); builder.RegisterAssemblyModules(apiAssembly); return builder.Build(); diff --git a/source/Web.Api/Integration/SlowUpdate.cs b/source/Web.Api/Integration/SlowUpdate.cs index e39e93c..b652ad4 100644 --- a/source/Web.Api/Integration/SlowUpdate.cs +++ b/source/Web.Api/Integration/SlowUpdate.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Collections.Immutable; using System.IO; using System.Linq; using System.Threading; diff --git a/source/Web/Web.csproj b/source/Web/Web.csproj index ac06f79..38a1a44 100644 --- a/source/Web/Web.csproj +++ b/source/Web/Web.csproj @@ -181,8 +181,11 @@ + + + @@ -191,13 +194,10 @@ - - - diff --git a/source/Web/index.html b/source/Web/index.html index 1de91db..c3a678f 100644 --- a/source/Web/index.html +++ b/source/Web/index.html @@ -1,4 +1,4 @@ - + @@ -131,7 +131,7 @@
  • - +
@@ -144,7 +144,7 @@
  • - +
diff --git a/source/Web/js/app.js b/source/Web/js/app.js index 459042b..da3957b 100644 --- a/source/Web/js/app.js +++ b/source/Web/js/app.js @@ -1,5 +1,3 @@ -import CodeMirror from 'codemirror'; - import getBranchesAsync from './server/get-branches-async'; import state from './state'; diff --git a/source/Web/js/server/send-code-async.js b/source/Web/js/server/send-code-async.js deleted file mode 100644 index 076da04..0000000 --- a/source/Web/js/server/send-code-async.js +++ /dev/null @@ -1,36 +0,0 @@ -import $ from 'jquery'; - -export default function sendCodeAsync(code, options, branchUrl) { - let url = 'api/compilation'; - if (branchUrl) - url = branchUrl.replace(/\/?$/, '/') + url; - - const data = Object.assign({ code: code }, options); - const xhr = $.ajax({ - type: 'POST', - url: url, - datatype: 'application/json', - contentType: 'application/json', - data: JSON.stringify(data) - }); - const promise = xhr.then( - data => data, - response => { - let error; - if (response.statusText === 'abort') { - error = Error('Request aborted.'); - error.reason = 'abort'; - return error; - } - - error = Error('Request failed.'); - error.reason = 'response'; - error.response = { - data: JSON.parse(response.responseText) - }; - return error; - } - ); - promise.abort = () => xhr.abort(); - return promise; -} \ No newline at end of file diff --git a/source/Web/js/ui/components/app-codemirror.js b/source/Web/js/ui/components/app-codemirror.js index 448fbc5..857c6c6 100644 --- a/source/Web/js/ui/components/app-codemirror.js +++ b/source/Web/js/ui/components/app-codemirror.js @@ -1,20 +1,12 @@ import Vue from 'vue'; import CodeMirror from 'codemirror'; -import 'codemirror/addon/lint/lint'; import 'codemirror/mode/clike/clike'; import 'codemirror/mode/vb/vb'; -function buildGetAnnotations(data) { - return (cm, updateLinting) => { - data.lint(data.value, updateLinting); - }; -} - Vue.component('app-codemirror', { props: { value: String, mode: String, - lint: Function, options: Object }, ready: function() { @@ -23,10 +15,7 @@ Vue.component('app-codemirror', { const options = Object.assign( {}, this.options, - this.mode !== undefined ? { mode: this.mode } : {}, - this.lint !== undefined ? { - lint: { async: true, getAnnotations: buildGetAnnotations(this) } - } : {} + this.mode !== undefined ? { mode: this.mode } : {} ); const instance = CodeMirror.fromTextArea(textarea, options); this.$watch('mode', value => instance.setOption('mode', value));