Skip to content

Commit 6a79745

Browse files
committed
Updated Tool CLI Options
Can now generate for Blazor Server or Wasm hosting models.
1 parent c39374b commit 6a79745

File tree

5 files changed

+90
-29
lines changed

5 files changed

+90
-29
lines changed

Sample/EventHorizon.BabylonJS.Interop.Generator.ConsoleApp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace EventHorizon.Blazor.TypeScript.Interop.Generator.ConsoleApp
77
using EventHorizon.Blazor.TypeScript.Interop.Generator.Logging;
88

99
using ServerGenerator = Blazor.Interop.Generator.GenerateInteropSource;
10-
using ServerProjectWriter = Blazor.Interop.Generator.Writers.Project.ProjectWriter;
10+
using ServerProjectWriter = Blazor.Interop.Generator.Writers.Project.ServerProjectWriter;
1111
using WasmGenerator = GenerateSource;
1212
using WasmProjectWriter = Writers.Project.ProjectWriter;
1313

Source/Server/EventHorizon.Blazor.Interop.Generator.Writers.Project/ProjectWriter.cs renamed to Source/Server/EventHorizon.Blazor.Interop.Generator.Writers.Project/ServerProjectWriter.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace EventHorizon.Blazor.Interop.Generator.Writers.Project
22
{
33
using System;
4-
using System.Collections;
54
using System.Collections.Generic;
65
using System.IO;
76
using System.Linq;
@@ -11,13 +10,13 @@ namespace EventHorizon.Blazor.Interop.Generator.Writers.Project
1110
using EventHorizon.Blazor.TypeScript.Interop.Generator.Writers;
1211
using Microsoft.CodeAnalysis;
1312

14-
public class ProjectWriter : IWriter
13+
public class ServerProjectWriter : IWriter
1514
{
1615
private readonly string _projectPath;
1716
private readonly string _projectName;
1817
private readonly string _projectDirectory;
1918

20-
public ProjectWriter(
19+
public ServerProjectWriter(
2120
string path,
2221
string name
2322
)
@@ -156,7 +155,7 @@ public static string ReadAllText(
156155
string templatePath
157156
)
158157
{
159-
var assembly = typeof(ProjectWriter).Assembly;
158+
var assembly = typeof(ServerProjectWriter).Assembly;
160159
var resourceStream = assembly.GetManifestResourceStream(
161160
templatePath
162161
);

Tool/EventHorizon.Blazor.TypeScript.Interop.Tool/EventHorizon.Blazor.TypeScript.Interop.Tool.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
<ItemGroup>
2727
<ProjectReference Include="..\..\Source\EventHorizon.Blazor.TypeScript.Interop.Generator.Writers.Project\EventHorizon.Blazor.TypeScript.Interop.Generator.Writers.Project.csproj" />
2828
<ProjectReference Include="..\..\Source\EventHorizon.Blazor.TypeScript.Interop.Generator\EventHorizon.Blazor.TypeScript.Interop.Generator.csproj" />
29+
<ProjectReference Include="..\..\Source\Server\EventHorizon.Blazor.Interop.Generator.Writers.Project\EventHorizon.Blazor.Interop.Generator.Writers.Project.csproj" />
30+
<ProjectReference Include="..\..\Source\Server\EventHorizon.Blazor.Interop.Generator\EventHorizon.Blazor.Interop.Generator.csproj" />
2931
</ItemGroup>
3032

3133
</Project>

Tool/EventHorizon.Blazor.TypeScript.Interop.Tool/Program.cs

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace EventHorizon.Blazor.TypeScript.Interop.Tool
77
using System.Diagnostics;
88
using System.IO;
99
using System.Net;
10+
using EventHorizon.Blazor.Interop.Generator;
11+
using EventHorizon.Blazor.Interop.Generator.Writers.Project;
1012
using EventHorizon.Blazor.TypeScript.Interop.Generator;
1113
using EventHorizon.Blazor.TypeScript.Interop.Generator.AstParser.Model;
1214
using EventHorizon.Blazor.TypeScript.Interop.Generator.Formatter;
@@ -39,7 +41,7 @@ static int Main(string[] args)
3941
},
4042
new Option<string>(
4143
new string[] { "--project-assembly", "-a" },
42-
getDefaultValue: () => "Generated.WASM",
44+
getDefaultValue: () => "Generated.Code",
4345
description: "The project name of the Assembly that will be generated"
4446
),
4547
new Option<string>(
@@ -57,12 +59,17 @@ static int Main(string[] args)
5759
getDefaultValue: () => "dotnet",
5860
description: "The type of TypeScript parser to use, 'dotnet' will use the embedded .NET parser. 'nodejs' will use the TypeScript compiler through NodeJS (requires NodeJS installed)."
5961
),
62+
new Option<string>(
63+
new string[] { "--host-type", "-h" },
64+
getDefaultValue: () => "wasm",
65+
description: "The host type the source should be generator for. Is 'wasm' by default, will only work for Wasm hosted Blazor projects. Supports 'server' to generate a async first interop layer that works in both Blazor Server and Wasm hosted models."
66+
),
6067
};
6168

62-
rootCommand.Description = "Generate a Blazor Wasm project from a TypeScript definition file.";
69+
rootCommand.Description = "Generate a Blazor Wasm or Server project from TypeScript definition files.";
6370

6471
// Note that the parameters of the handler method are matched according to the names of the options
65-
rootCommand.Handler = CommandHandler.Create<IList<string>, IList<string>, string, string, bool, string>(
72+
rootCommand.Handler = CommandHandler.Create<IList<string>, IList<string>, string, string, bool, string, string>(
6673
GenerateSources
6774
);
6875

@@ -73,10 +80,11 @@ static int Main(string[] args)
7380
private static int GenerateSources(
7481
IList<string> source,
7582
IList<string> classToGenerate,
76-
string projectAssembly = "Generated.Wasm",
83+
string projectAssembly = "Generated.Code",
7784
string projectGenerationLocation = "_generated",
7885
bool force = false,
79-
string parser = "dotnet"
86+
string parser = "dotnet",
87+
string hostType = "wasm"
8088
)
8189
{
8290
try
@@ -87,7 +95,8 @@ private static int GenerateSources(
8795
classToGenerate,
8896
projectAssembly,
8997
projectGenerationLocation,
90-
parser
98+
parser,
99+
hostType
91100
);
92101
GlobalLogger.Info($"projectAssembly: {projectAssembly}");
93102
GlobalLogger.Info($"projectGenerationLocation: {projectGenerationLocation}");
@@ -149,23 +158,46 @@ private static int GenerateSources(
149158
}
150159

151160
var textFormatter = new NoFormattingTextFormatter();
152-
var writer = new ProjectWriter(
153-
projectGenerationLocation,
154-
projectAssembly
155-
);
156161

157-
new GenerateSource().Run(
158-
projectAssembly,
159-
sourceDirectory,
160-
sourceFiles,
161-
generationList,
162-
writer,
163-
textFormatter,
164-
new Dictionary<string, string>
165-
{
166-
},
167-
GetParserType(parser)
168-
);
162+
if (hostType == "server")
163+
{
164+
var writer = new ServerProjectWriter(
165+
projectGenerationLocation,
166+
projectAssembly
167+
);
168+
new GenerateInteropSource().Run(
169+
projectAssembly,
170+
sourceDirectory,
171+
sourceFiles,
172+
generationList,
173+
writer,
174+
textFormatter,
175+
new Dictionary<string, string>
176+
{
177+
},
178+
GetParserType(parser)
179+
);
180+
}
181+
else if (hostType == "wasm")
182+
{
183+
var writer = new ProjectWriter(
184+
projectGenerationLocation,
185+
projectAssembly
186+
);
187+
new GenerateSource().Run(
188+
projectAssembly,
189+
sourceDirectory,
190+
sourceFiles,
191+
generationList,
192+
writer,
193+
textFormatter,
194+
new Dictionary<string, string>
195+
{
196+
},
197+
GetParserType(parser)
198+
);
199+
}
200+
169201
stopwatch.Stop();
170202
GlobalLogger.Success($"Took {stopwatch.ElapsedMilliseconds}ms to Generate Source Project.");
171203

@@ -192,7 +224,8 @@ private static void ValidateArguments(
192224
IList<string> classToGenerate,
193225
string projectAssembly,
194226
string projectGenerationLocation,
195-
string parser
227+
string parser,
228+
string hostType
196229
)
197230
{
198231
if (sources == null || sources.Count == 0)
@@ -236,6 +269,15 @@ string parser
236269
"Invalid parser found. Valid Parsers: [dotnet, nodejs]"
237270
);
238271
}
272+
if (string.IsNullOrWhiteSpace(
273+
hostType
274+
) || (hostType.ToLower() != "server" && hostType.ToLower() != "wasm"))
275+
{
276+
throw new ToolArgumentException(
277+
"--host-type",
278+
"Invalid host type found. Valid Host Types: [wasm, server]"
279+
);
280+
}
239281
}
240282

241283
/// <summary>

Tool/EventHorizon.Blazor.TypeScript.Interop.Tool/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Identifier | Details | Required/Default
2020
-l, --project-generation-location &lt;project-generation-location&gt; | The directory where the Generated Project assembly will be saved | Default: "_generated"
2121
-f, --force | This will force generation, by deleting --project-generation-location | Default: (False)
2222
-p, --parser | The type of TypeScript parser to use, Supported values: ("dotnet","nodejs") | Default: ("dotnet")
23+
-h, --host-type | The host type the source should be generator for, Supported values: ("wasm","server"). | Default: ("wasm")
2324

2425
## Parsers
2526

@@ -32,6 +33,17 @@ Type | Details
3233
dotnet | Has no external dependencies, and on average 3x faster than the nodejs parser. A con is that it does not support modern TypeScript syntax, but should coverage 90% of use-cases.
3334
nodejs | Requires NodeJS to function, supports modern TypeScript syntax. A con is that it is very slow relative to the dotnet parser.
3435

36+
## Host Types
37+
38+
This tool supports two types of Hosting models, the standard Blazor Server and Wasm hosting models. The main reason for this is the Wasm generated source will create an interop layer that has a speed benefit but does not work outside of the Wasm hosting model.
39+
40+
Below is a table listing the pro/con of each hosting type.
41+
42+
Type | Details
43+
--- | ---
44+
wasm | Uses the unmarshaller to move data between the .NET and JavaScript layers. The con is that it only works in a Blazor WebAssembly hosted model, and will not function when used from Blazor Server.
45+
server | Uses the IJSRuntime for moving data between the .NET and JavaScript layers. A pro is that it allows it to work in both Blazor Server and WebAssembly, but with a con that it has to go through the async layer adding a performance overhead to each request.
46+
3547
## Usage
3648

3749
~~~ bash
@@ -54,4 +66,10 @@ ehz-generate -c Vector3 -c Mesh -c Engine -c Scene -s https://raw.githubusercont
5466
~~~ bash
5567
# Generated BabylonJS project interop with multiple sources.
5668
ehz-generate -c Button -s https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/babylon.d.ts -s https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/gui/babylon.gui.d.ts
57-
~~~
69+
~~~
70+
71+
~~~ bash
72+
# Generate a BabylonJS project that work in both Blazor Server and Wasm projects
73+
ehz-generate --host-type server -a Blazor.BabylonJS -c Vector3 -s https://raw.githubusercontent.com/BabylonJS/Babylon.js/master/dist/babylon.d.ts
74+
75+
~~~

0 commit comments

Comments
 (0)