Skip to content

Commit ae88875

Browse files
committed
Added templates for Blazor Server Interop Generation
1 parent 1e08568 commit ae88875

40 files changed

+777
-2
lines changed

EventHorizon.Blazor.TypeScript.Interop.Generator.sln

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Microsoft Visual Studio Solution File, Format Version 12.00
1+
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio Version 16
33
VisualStudioVersion = 16.0.30204.135
44
MinimumVisualStudioVersion = 10.0.40219.1
@@ -31,6 +31,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tool", "Tool", "{C261DE2D-6
3131
EndProject
3232
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventHorizon.Blazor.TypeScript.Interop.Tool", "Tool\EventHorizon.Blazor.TypeScript.Interop.Tool\EventHorizon.Blazor.TypeScript.Interop.Tool.csproj", "{5893EDD4-24E7-4260-9A43-EFF21EE23F97}"
3333
EndProject
34+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Source", "Source", "{88BB5182-7755-4777-A45F-7BC667CAF330}"
35+
EndProject
36+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Server", "Server", "{0FD80298-8614-4419-8D86-250A9AE740AD}"
37+
EndProject
38+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventHorizon.Blazor.Interop.Generator", "Source\Server\EventHorizon.Blazor.Interop.Generator\EventHorizon.Blazor.Interop.Generator.csproj", "{1284B89B-86BB-4566-A74B-E915771974A8}"
39+
EndProject
3440
Global
3541
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3642
Debug|Any CPU = Debug|Any CPU
@@ -61,6 +67,10 @@ Global
6167
{5893EDD4-24E7-4260-9A43-EFF21EE23F97}.Debug|Any CPU.Build.0 = Debug|Any CPU
6268
{5893EDD4-24E7-4260-9A43-EFF21EE23F97}.Release|Any CPU.ActiveCfg = Release|Any CPU
6369
{5893EDD4-24E7-4260-9A43-EFF21EE23F97}.Release|Any CPU.Build.0 = Release|Any CPU
70+
{1284B89B-86BB-4566-A74B-E915771974A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
71+
{1284B89B-86BB-4566-A74B-E915771974A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
72+
{1284B89B-86BB-4566-A74B-E915771974A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
73+
{1284B89B-86BB-4566-A74B-E915771974A8}.Release|Any CPU.Build.0 = Release|Any CPU
6474
EndGlobalSection
6575
GlobalSection(SolutionProperties) = preSolution
6676
HideSolutionNode = FALSE
@@ -69,6 +79,8 @@ Global
6979
{CAEED771-5D2A-48F8-9DF5-8E60FA569FA0} = {932AD335-24DD-419D-9F45-93720F37D7DE}
7080
{2D1BE986-B7C9-457F-8F09-E7D71EEA0D8F} = {66BA240A-9512-4874-A05A-FE86C72DAA5B}
7181
{5893EDD4-24E7-4260-9A43-EFF21EE23F97} = {C261DE2D-65FB-4616-B02A-5A7461C6D767}
82+
{0FD80298-8614-4419-8D86-250A9AE740AD} = {88BB5182-7755-4777-A45F-7BC667CAF330}
83+
{1284B89B-86BB-4566-A74B-E915771974A8} = {0FD80298-8614-4419-8D86-250A9AE740AD}
7284
EndGlobalSection
7385
GlobalSection(ExtensibilityGlobals) = postSolution
7486
SolutionGuid = {DB1241E0-8802-4F96-9FD7-B938FE890192}

Source/EventHorizon.Blazor.TypeScript.Interop.Generator/Templates/ReadTemplates.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static ClassGenerationTemplates Read()
2828
$"{templatesPath}.CachedEntityObjectTemplate.txt"
2929
);
3030
// Accessor/Property Template
31-
var accessorTemplate = ReadAllText($"{templatesPath}.AccessorTemplate.txt"
31+
var accessorTemplate = ReadAllText(
32+
$"{templatesPath}.AccessorTemplate.txt"
3233
);
3334
// Accessor/Property With Setter Template
3435
var accessorWithSetterTemplate = ReadAllText(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
6+
<AssemblyTitle>EventHorizon.Blazor.Interop.Generator.Writers.Project</AssemblyTitle>
7+
<AssemblyName>EventHorizon.Blazor.Interop.Generator.Writers.Project</AssemblyName>
8+
<PackageId>EventHorizon.Blazor.Interop.Generator.Writers.Project</PackageId>
9+
10+
<Description>Includes a Writer implementation that will generate a C# Project. This should be used with the EventHorizon.Blazor.Interop.Generator project to write out the generated statements to a C# project.</Description>
11+
<PackageTags>Blazor, WASM, Interop</PackageTags>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<EmbeddedResource Include="Templates\*" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="EventHorizon.Blazor.TypeScript.Interop.Generator" Version="0.1.9" />
20+
<PackageReference Include="EventHorizon.Blazor.TypeScript.Interop.Generator.Model" Version="0.1.9" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
namespace EventHorizon.Blazor.Interop.Generator.Writers.Project
2+
{
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Text;
9+
using EventHorizon.Blazor.TypeScript.Interop.Generator.Model;
10+
using EventHorizon.Blazor.TypeScript.Interop.Generator.Model.Writer;
11+
using EventHorizon.Blazor.TypeScript.Interop.Generator.Writers;
12+
using Microsoft.CodeAnalysis;
13+
14+
public class ProjectWriter : IWriter
15+
{
16+
private readonly string _projectPath;
17+
private readonly string _projectName;
18+
private readonly string _projectDirectory;
19+
20+
public ProjectWriter(
21+
string path,
22+
string name
23+
)
24+
{
25+
_projectPath = path;
26+
_projectName = name;
27+
_projectDirectory = Path.Combine(
28+
_projectPath,
29+
_projectName
30+
);
31+
}
32+
33+
public void Write(
34+
IList<GeneratedStatement> generatedStatementList
35+
)
36+
{
37+
// Generate Directory with ProjectName in ProjectPath
38+
GenerateProjectDirectory();
39+
40+
// Create Project File, with ProjectName
41+
GenerateProjectFile();
42+
43+
// Create Files from GeneratedStatementList
44+
foreach (var generatedStatement in generatedStatementList)
45+
{
46+
GeneratedStatementFile(
47+
generatedStatement
48+
);
49+
}
50+
}
51+
52+
private void GeneratedStatementFile(
53+
GeneratedStatement generatedStatement
54+
)
55+
{
56+
// Get Path/File
57+
var namespaceToPath = Path.Combine(
58+
generatedStatement.ClassStatement.Namespace.Split(
59+
'.'
60+
).Skip(1).ToArray()
61+
);
62+
var namepacePath = Path.Combine(
63+
_projectDirectory,
64+
namespaceToPath
65+
);
66+
var filePath = Path.Combine(
67+
namepacePath,
68+
$"{generatedStatement.ClassStatement.Name}.cs"
69+
);
70+
if (!Directory.Exists(
71+
namepacePath
72+
))
73+
{
74+
Directory.CreateDirectory(
75+
namepacePath
76+
);
77+
}
78+
File.WriteAllText(
79+
filePath,
80+
CleanupGeneratedStatementString(
81+
generatedStatement
82+
)
83+
);
84+
}
85+
86+
private string CleanupGeneratedStatementString(
87+
GeneratedStatement generatedStatement
88+
)
89+
{
90+
return generatedStatement.GeneratedString.Replace(
91+
"ValueTask<void>",
92+
"ValueTask"
93+
).Replace(
94+
"set_@",
95+
"set_"
96+
).Replace(
97+
"get_@",
98+
"get_"
99+
).Replace(
100+
"[[CLASS_GENERICS]]",
101+
BuildClassGenerics(
102+
generatedStatement
103+
)
104+
);
105+
}
106+
107+
private static string BuildClassGenerics(
108+
GeneratedStatement generatedStatement
109+
)
110+
{
111+
var template = "<[[TYPE]]>";
112+
113+
if (!generatedStatement.ClassStatement.GenericTypes.Any())
114+
{
115+
return string.Empty;
116+
}
117+
118+
return template.Replace(
119+
"[[TYPE]]",
120+
string.Join(
121+
", ",
122+
generatedStatement.ClassStatement.GenericTypes.Select(
123+
genericType => TypeStatementWriter.Write(
124+
genericType
125+
)
126+
)
127+
)
128+
);
129+
}
130+
131+
private void GenerateProjectFile()
132+
{
133+
var templatesPath = "EventHorizon.Blazor.Interop.Generator.Writers.Project.Templates";
134+
var template = ReadAllText(
135+
$"{templatesPath}.ProjectFileTemplate.txt"
136+
);
137+
File.WriteAllText(Path.Combine(
138+
_projectDirectory,
139+
$"{_projectName}.csproj"
140+
), template);
141+
}
142+
143+
public static string ReadAllText(
144+
string templatePath
145+
)
146+
{
147+
var assembly = typeof(ProjectWriter).Assembly;
148+
var resourceStream = assembly.GetManifestResourceStream(
149+
templatePath
150+
);
151+
using var reader = new StreamReader(
152+
resourceStream,
153+
Encoding.UTF8
154+
);
155+
return reader.ReadToEnd();
156+
}
157+
158+
private void GenerateProjectDirectory()
159+
{
160+
if (Directory.Exists(_projectDirectory)
161+
&& (Directory.GetFiles(_projectDirectory).Any()
162+
|| Directory.GetDirectories(_projectDirectory).Any()))
163+
{
164+
throw new Exception($"{_projectDirectory} already exists.");
165+
}
166+
Directory.CreateDirectory(_projectDirectory);
167+
}
168+
}
169+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using System.Reflection;
2+
3+
[assembly: AssemblyVersion("0.1.0.0")]
4+
[assembly: AssemblyFileVersion("0.1.0.0")]
5+
[assembly: AssemblyInformationalVersion("0.1.0.0")]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="EventHorizon.Blazor.Server.Interop" Version="0.1.0-preview0002" />
9+
</ItemGroup>
10+
11+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
6+
<AssemblyTitle>EventHorizon.Blazor.Interop.Generator</AssemblyTitle>
7+
<AssemblyName>EventHorizon.Blazor.Interop.Generator</AssemblyName>
8+
<PackageId>EventHorizon.Blazor.Interop.Generator</PackageId>
9+
10+
<Description>Parser for TypeScript definitions to create C# representations. This packages builds on top of the Wasm implementation by replacing the templates with ones that are Async first..</Description>
11+
<PackageTags>Blazor, Server, Interop, Generation</PackageTags>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\EventHorizon.Blazor.TypeScript.Interop.Generator\EventHorizon.Blazor.TypeScript.Interop.Generator.csproj" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<EmbeddedResource Include="Templates\*" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace EventHorizon.Blazor.Interop.Generator
2+
{
3+
using System.Collections.Generic;
4+
using EventHorizon.Blazor.Interop.Generator.Templates;
5+
using EventHorizon.Blazor.TypeScript.Interop.Generator;
6+
using EventHorizon.Blazor.TypeScript.Interop.Generator.AstParser.Model;
7+
using EventHorizon.Blazor.TypeScript.Interop.Generator.Model.Formatter;
8+
using EventHorizon.Blazor.TypeScript.Interop.Generator.Model.Writer;
9+
10+
public class GenerateInteropSource
11+
{
12+
public bool Run(
13+
string projectAssembly,
14+
string sourceDirectory,
15+
IList<string> sourceFiles,
16+
IList<string> generationList,
17+
IWriter writer,
18+
TextFormatter textFormatter,
19+
IDictionary<string, string> typeOverrideMap,
20+
ASTParserType parserType = ASTParserType.Sdcb
21+
)
22+
{
23+
ReadInteropTemplates.SetReadTemplates();
24+
25+
return new GenerateSource()
26+
.Run(
27+
projectAssembly,
28+
sourceDirectory,
29+
sourceFiles,
30+
generationList,
31+
writer,
32+
textFormatter,
33+
typeOverrideMap,
34+
parserType
35+
);
36+
}
37+
}
38+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using System.Reflection;
2+
3+
[assembly: AssemblyVersion("0.1.0.0")]
4+
[assembly: AssemblyFileVersion("0.1.0.0")]
5+
[assembly: AssemblyInformationalVersion("0.1.0.0")]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[[CACHE_SECTION]]
2+
public [[STATIC]]async ValueTask<[[TYPE]]> get_[[NAME]]()
3+
{
4+
[[PROPERTY_GETTER]]
5+
}

0 commit comments

Comments
 (0)