Skip to content

Commit c6bc4a4

Browse files
committed
emit is failing
1 parent 62a3484 commit c6bc4a4

File tree

12 files changed

+181
-90
lines changed

12 files changed

+181
-90
lines changed

Diff for: LearnJsonEverything.Template/ILessonRunner.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace LearnJsonEverything.Template;
2+
3+
public interface ILessonRunner<out T>
4+
{
5+
T Run();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

Diff for: LearnJsonEverything.sln

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 17.9.34622.214
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LearnJsonEverything", "LearnJsonEverything\LearnJsonEverything.csproj", "{82FEC514-66E5-4034-9845-7C66B753D6FD}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LearnJsonEverything.Template", "LearnJsonEverything.Template\LearnJsonEverything.Template.csproj", "{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{82FEC514-66E5-4034-9845-7C66B753D6FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{82FEC514-66E5-4034-9845-7C66B753D6FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{82FEC514-66E5-4034-9845-7C66B753D6FD}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

Diff for: LearnJsonEverything/LearnJsonEverything.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
<NoWarn>NU1701</NoWarn>
88
<PublishTrimmed>false</PublishTrimmed>
9+
<LangVersion>latest</LangVersion>
910
</PropertyGroup>
1011

1112
<ItemGroup>
@@ -20,6 +21,10 @@
2021
<PackageReference Include="Yaml2JsonNode" Version="2.1.0" />
2122
</ItemGroup>
2223

24+
<ItemGroup>
25+
<ProjectReference Include="..\LearnJsonEverything.Template\LearnJsonEverything.Template.csproj" />
26+
</ItemGroup>
27+
2328
<ItemGroup>
2429
<Content Update="wwwroot\favicon.ico">
2530
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>

Diff for: LearnJsonEverything/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
builder.RootComponents.Add<App>("#app");
99
builder.RootComponents.Add<HeadOutlet>("head::after");
1010

11-
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
11+
builder.Services.AddScoped(_ => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
1212
builder.Services.AddBlazoredLocalStorageAsSingleton();
1313
builder.Services.AddSingleton<DataManager>();
1414

1515
var host = builder.Build();
16-
var client = host.Services.GetService<HttpClient>();
16+
_ = host.Services.GetService<HttpClient>();
1717

1818
await host.RunAsync();

Diff for: LearnJsonEverything/Services/EditorOptions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ public static StandaloneEditorConstructionOptions Basic() =>
88
new()
99
{
1010
AutomaticLayout = true,
11-
Language = "json",
11+
Language = "csharp",
1212
Theme = "vs-dark",
1313
SelectOnLineNumbers = true,
1414
Scrollbar = new EditorScrollbarOptions
1515
{
1616
AlwaysConsumeMouseWheel = false
1717
},
1818
ScrollBeyondLastLine = false,
19-
TabSize = 2
19+
TabSize = 4
2020
};
2121

2222
public static StandaloneEditorConstructionOptions Readonly()

Diff for: LearnJsonEverything/Services/HelpContent.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,4 @@ It may also be beneficial to compare your results with the [original JavaScript
200200
Patch generation follows an iterative strategy, preferring multiple small changes. This may result in
201201
larger patches than expected.
202202
""";
203-
}
203+
}

Diff for: LearnJsonEverything/Services/LessonData.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
namespace LearnJsonEverything.Services;
1+
using System.Text.Json.Nodes;
2+
3+
namespace LearnJsonEverything.Services;
24

35
public class LessonData
46
{
57
public int Index { get; set; }
68
public Guid Id { get; set; }
79
public string Title { get; set; }
810
public string Instructions { get; set; }
9-
public string CodeTemplate { get; set; }
10-
}
11+
public JsonObject? Data { get; set; }
12+
}

Diff for: LearnJsonEverything/Services/LessonPlan.cs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public class LessonPlan
88
private readonly LessonData[] _lessonData;
99
private readonly Dictionary<Guid, int> _indexLookup;
1010

11+
public LessonData this[int i] => _lessonData[i];
12+
1113
public LessonPlan(LessonData[] lessonData)
1214
{
1315
_lessonData = lessonData;

Diff for: LearnJsonEverything/Services/Runners/SchemaRunner.cs

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System.Reflection;
2+
using System.Text.Json;
3+
using Json.Schema;
4+
using LearnJsonEverything.Template;
5+
using Microsoft.CodeAnalysis;
6+
using Microsoft.CodeAnalysis.CSharp;
7+
8+
namespace LearnJsonEverything.Services.Runners;
9+
10+
public static class SchemaRunner
11+
{
12+
private const string ContextCode =
13+
"""
14+
using System;
15+
using System.Collections.Generic;
16+
using System.Text.Json;
17+
using System.Text.Json.Nodes;
18+
using System.Text.Json.Serialization;
19+
using Json.Schema;
20+
21+
namespace JsonEverythingTemp;
22+
23+
public class Lesson : ILessonRunner<EvaluationResults>
24+
{
25+
public EvaluationResults Run()
26+
{
27+
var instance = JsonNode.Parse("/* INSTANCE */");
28+
29+
/* USER CODE */
30+
}
31+
}
32+
""";
33+
34+
private const string Instructions =
35+
$"""
36+
### /* TITLE */
37+
38+
/* INSTRUCTIONS */
39+
40+
### Code template
41+
42+
```csharp
43+
{ContextCode}
44+
```
45+
""";
46+
47+
public static string BuildInstructions(LessonData lesson) => Instructions
48+
.Replace("/* TITLE */", lesson.Title)
49+
.Replace("/* INSTRUCTIONS */", lesson.Instructions)
50+
.Replace("/* INSTANCE */", JsonSerializer.Serialize(lesson.Data?["instance"]));
51+
52+
public static string Run(string userCode, LessonData lesson, MetadataReference[] references)
53+
{
54+
var fullSource = ContextCode
55+
.Replace("/* USER CODE */", userCode)
56+
.Replace("/* INSTANCE */", JsonSerializer.Serialize(lesson.Data?["instance"]));
57+
58+
var syntaxTree = CSharpSyntaxTree.ParseText(fullSource);
59+
var assemblyPath = Path.ChangeExtension(Path.GetTempFileName(), "dll");
60+
61+
var compilation = CSharpCompilation.Create(Path.GetFileName(assemblyPath))
62+
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
63+
.AddReferences(references)
64+
.AddSyntaxTrees(syntaxTree);
65+
66+
using var dllStream = new MemoryStream();
67+
using var pdbStream = new MemoryStream();
68+
using var xmlStream = new MemoryStream();
69+
var emitResult = compilation.Emit(dllStream, pdbStream, xmlStream);
70+
if (!emitResult.Success)
71+
{
72+
Console.WriteLine("You may expect a list of what compilation errors there are, but unfortunately " +
73+
"Roslyn doesn't seem to be giving that information out (or I don't know how to " +
74+
"interpret it). So instead, here are the errors in raw form. Good luck. If you " +
75+
"know what these mean, please drop a line in a GitHub issue.");
76+
//var errors = string.Join("\n", emitResult.Diagnostics.Where(x => x.Severity == DiagnosticSeverity.Error)
77+
// .Select(x => GetErrorDetails(source, x)));
78+
//await _outputEditor.SetValue(errors);
79+
return "Compilation error";
80+
}
81+
82+
var assembly = Assembly.Load(dllStream.ToArray());
83+
84+
var type = assembly.DefinedTypes.Single(x => !x.IsInterface && x.ImplementedInterfaces.Contains(typeof(ILessonRunner<EvaluationResults>)));
85+
var runner = (ILessonRunner<EvaluationResults>) Activator.CreateInstance(type)!;
86+
87+
var results = runner.Run();
88+
// run the code
89+
90+
return JsonSerializer.Serialize(results);
91+
}
92+
}

0 commit comments

Comments
 (0)