Skip to content

Commit 1e6b7e7

Browse files
authored
consolidate source generation (#140)
1 parent 33e9742 commit 1e6b7e7

File tree

132 files changed

+2808
-3007
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+2808
-3007
lines changed

.github/workflows/deploy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ jobs:
1616
name: github-pages
1717
steps:
1818
- uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
1921
- uses: actions/setup-node@v3
2022
- name: build
2123
run: |

samples/minimal/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ <h1 id="msg">Loading...</h1>
55
<script type="module">
66

77
// Named exports are auto-generated on C# build.
8-
import bootsharp, { Global } from './cs/bin/bootsharp/bootsharp.mjs';
8+
import bootsharp, { Program } from './cs/bin/bootsharp/index.mjs';
99

1010
// Binding 'Program.GetFrontendName' endpoint invoked in C#.
11-
Global.getFrontendName = () => "Browser";
11+
Program.getFrontendName = () => "Browser";
1212

1313
// Subscribing to 'Program.OnMainInvoked' C# event.
14-
Global.onMainInvoked.subscribe(console.log);
14+
Program.onMainInvoked.subscribe(console.log);
1515

1616
// Initializing dotnet runtime and invoking entry point.
1717
await bootsharp.boot();
1818

1919
// Invoking 'Program.GetBackendName' C# method.
20-
document.getElementById("msg").innerHTML = `Hello ${Global.getBackendName()}!`;
20+
document.getElementById("msg").innerHTML = `Hello ${Program.getBackendName()}!`;
2121

2222
</script>

samples/minimal/main.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Named exports are auto-generated on C# build.
2-
import bootsharp, { Global } from "./cs/bin/bootsharp/bootsharp.mjs";
2+
import bootsharp, { Program } from "./cs/bin/bootsharp/index.mjs";
33

44
// Binding 'Program.GetFrontendName' endpoint invoked in C#.
5-
Global.getFrontendName = () =>
5+
Program.getFrontendName = () =>
66
typeof Bun === "object" ? `Bun ${Bun.version}` :
77
typeof Deno === "object" ? `Deno ${Deno.version.deno}` :
88
typeof process === "object" ? `Node ${process.version}` :
99
"Unknown JavaScript Runtime";
1010

1111
// Subscribing to 'Program.OnMainInvoked' C# event.
12-
Global.onMainInvoked.subscribe(console.log);
12+
Program.onMainInvoked.subscribe(console.log);
1313

1414
// Initializing dotnet runtime and invoking entry point.
1515
await bootsharp.boot();
1616

1717
// Invoking 'Program.GetBackendName' C# method.
18-
console.log(`Hello ${Global.getBackendName()}!`);
18+
console.log(`Hello ${Program.getBackendName()}!`);

samples/react/backend/Backend.WASM/Backend.WASM.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
55
<RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
6+
<ImplicitUsings>enable</ImplicitUsings>
67
<!-- Specify custom name for the generated JavaScript module. -->
78
<BootsharpName>backend</BootsharpName>
8-
<!-- Publish module's package.json file under solution folder for accessibility. -->
9+
<!-- Publish module's package.json file under solution folder. -->
910
<BootsharpPackageDirectory>$(SolutionDir)</BootsharpPackageDirectory>
1011
<!-- Don't embed the C# solution binaries to the JavaScript module. -->
1112
<BootsharpEmbedBinaries>false</BootsharpEmbedBinaries>
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using Backend;
2-
using Backend.Prime;
31
using Bootsharp;
42
using Bootsharp.Inject;
53
using Microsoft.Extensions.DependencyInjection;
@@ -10,15 +8,15 @@
108
// and can be shared with other build targets (console, MAUI, etc).
119

1210
// Generate C# -> JavaScript interop handlers for specified contracts.
13-
[assembly: JSExport(typeof(IComputer))]
11+
[assembly: JSExport(typeof(Backend.IComputer))]
1412
// Generate JavaScript -> C# interop handlers for specified contracts.
15-
[assembly: JSImport(typeof(IPrimeUI))]
16-
// Group all generated JavaScript artifacts under 'Computer' namespace.
17-
[assembly: JSNamespace("^.*$", "Computer")]
13+
[assembly: JSImport(typeof(Backend.Prime.IPrimeUI))]
14+
// Group all generated JavaScript APIs under "Computer" namespace.
15+
[assembly: JSPreferences(Space = [".+", "Computer"])]
1816

1917
// Perform dependency injection.
2018
new ServiceCollection()
21-
.AddSingleton<IComputer, Prime>() // use prime computer
19+
.AddSingleton<Backend.IComputer, Backend.Prime.Prime>() // use prime computer
2220
.AddBootsharp() // inject generated interop handlers
2321
.BuildServiceProvider()
2422
.RunBootsharp(); // initialize interop services

samples/react/backend/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "backend",
3-
"main": "Backend.WASM/bin/backend/backend.mjs",
4-
"types": "Backend.WASM/bin/backend/types/backend.d.ts"
3+
"type": "module",
4+
"main": "Backend.WASM/bin/backend/index.mjs",
5+
"types": "Backend.WASM/bin/backend/types/index.d.ts"
56
}

samples/react/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
},
88
"devDependencies": {
99
"typescript": "^5.3.3",
10-
"@types/react": "^18.2.46",
10+
"@types/react": "^18.2.48",
1111
"@types/react-dom": "^18.2.18",
12-
"vite": "^5.0.10",
12+
"vite": "^5.0.12",
1313
"@vitejs/plugin-react-swc": "^3.5.0",
14-
"vitest": "^1.1.1",
15-
"@vitest/coverage-v8": "^1.1.1",
16-
"happy-dom": "^12.10.3",
14+
"vitest": "^1.2.1",
15+
"@vitest/coverage-v8": "^1.2.1",
16+
"happy-dom": "^13.2.0",
1717
"@testing-library/react": "^14.1.2",
1818
"@testing-library/user-event": "^14.5.2",
1919
"eslint": "^8.56.0",
2020
"eslint-plugin-react": "^7.33.2",
2121
"eslint-plugin-react-hooks": "^4.6.0",
22-
"@typescript-eslint/eslint-plugin": "^6.16.0",
22+
"@typescript-eslint/eslint-plugin": "^6.19.0",
2323
"npm-check-updates": "^16.14.12"
2424
},
2525
"scripts": {

samples/trimming/main.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import bootsharp, { Global } from "./cs/bin/bootsharp/bootsharp.mjs";
1+
import bootsharp, { Program } from "./cs/bin/bootsharp/index.mjs";
22
import zlib from "node:zlib";
33
import util from "node:util";
44
import fs from "node:fs/promises";
@@ -12,7 +12,7 @@ await Promise.all([
1212
...resources.assemblies.map(fetchBro)
1313
]);
1414

15-
Global.log = console.log;
15+
Program.log = console.log;
1616
await bootsharp.boot({ root: "./bin", resources });
1717

1818
async function measure(dir) {

samples/vscode/src/extension.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as vscode from "vscode";
2-
import bootsharp, { Global } from "../../Minimal/cs/bin/bootsharp/bootsharp.mjs";
2+
import bootsharp, { Program } from "../../Minimal/cs/bin/bootsharp/index.mjs";
33

44
export async function activate(context) {
5-
Global.getFrontendName = () => "VS Code";
5+
Program.getFrontendName = () => "VS Code";
66
try { await bootsharp.boot(); }
77
catch (e) { vscode.window.showErrorMessage(e.message); }
88
const command = vscode.commands.registerCommand("bootsharp.hello", greet);
@@ -14,6 +14,6 @@ export function deactivate() {
1414
}
1515

1616
function greet() {
17-
const message = `Welcome, ${Global.getBackendName()}! Enjoy your VS Code extension space.`;
17+
const message = `Welcome, ${Program.getBackendName()}! Enjoy your VS Code extension space.`;
1818
vscode.window.showInformationMessage(message);
1919
}

src/cs/Bootsharp.Common.Test/BindingTest.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/cs/Bootsharp.Common.Test/Bootsharp.Common.Test.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<ItemGroup>
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
15-
<PackageReference Include="xunit" Version="2.6.4"/>
15+
<PackageReference Include="xunit" Version="2.6.6"/>
1616
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
1717
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1818
<PrivateAssets>all</PrivateAssets>
@@ -29,9 +29,6 @@
2929

3030
<ItemGroup>
3131
<ProjectReference Include="../Bootsharp.Common/Bootsharp.Common.csproj"/>
32-
<ProjectReference Include="../Bootsharp.Generate/Bootsharp.Generate.csproj"
33-
PrivateAssets="all" ReferenceOutputAssembly="false"
34-
OutputItemType="Analyzer" SetTargetFramework="TargetFramework=netstandard2.0"/>
3532
</ItemGroup>
3633

3734
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Bootsharp.Common.Test;
2+
3+
public class InterfacesTest
4+
{
5+
[Fact]
6+
public void Records ()
7+
{
8+
// TODO: Remove once coverlet properly handles record coverage.
9+
_ = new ExportInterface(default, default) with { Interface = typeof(int) };
10+
_ = new ImportInterface(default) with { Instance = "" };
11+
}
12+
13+
[Fact]
14+
public void RegistersExports ()
15+
{
16+
var export = new ExportInterface(typeof(IBackend), default);
17+
Interfaces.Register(typeof(Backend), export);
18+
Assert.Equal(typeof(IBackend), Interfaces.Exports[typeof(Backend)].Interface);
19+
}
20+
21+
[Fact]
22+
public void RegistersImports ()
23+
{
24+
var import = new ImportInterface(new Frontend());
25+
Interfaces.Register(typeof(IFrontend), import);
26+
Assert.IsType<Frontend>(Interfaces.Imports[typeof(IFrontend)].Instance);
27+
}
28+
}

src/cs/Bootsharp.Common.Test/NamespaceTest.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/cs/Bootsharp.Common.Test/FunctionTest.cs renamed to src/cs/Bootsharp.Common.Test/ProxiesTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
using static Bootsharp.Function;
1+
using static Bootsharp.Proxies;
22

33
namespace Bootsharp.Common.Test;
44

5-
public class FunctionTest
5+
public class ProxiesTest
66
{
77
[Fact]
88
public void WhenEndpointNotFoundErrorIsThrown ()
99
{
10-
Assert.Contains("Endpoint 'foo' is not found.",
10+
Assert.Contains("Proxy 'foo' is not found.",
1111
Assert.Throws<Error>(() => Get<Action>("foo")).Message);
1212
}
1313

1414
[Fact]
1515
public void WhenFunctionTypeIsWrongErrorIsThrown ()
1616
{
1717
Set("bar", null);
18-
Assert.Contains("Endpoint 'bar' is not 'System.Action'.",
18+
Assert.Contains("Proxy 'bar' is not 'System.Action'.",
1919
Assert.Throws<Error>(() => Get<Action>("bar")).Message);
2020
}
2121

src/cs/Bootsharp.Common.Test/TypesTest.cs

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,8 @@
22
using Bootsharp;
33
using Bootsharp.Common.Test;
44

5-
[assembly: JSExport(
6-
typeof(IBackend),
7-
NamePattern = "ExportNamePattern",
8-
NameReplacement = "ExportNameReplacement",
9-
InvokePattern = "ExportInvokePattern",
10-
InvokeReplacement = "ExportInvokeReplacement"
11-
)]
12-
13-
[assembly: JSImport(
14-
typeof(IFrontend),
15-
NamePattern = "ImportNamePattern",
16-
NameReplacement = "ImportNameReplacement",
17-
InvokePattern = "ImportInvokePattern",
18-
InvokeReplacement = "ImportInvokeReplacement",
19-
EventPattern = "ImportEventPattern",
20-
EventReplacement = "ImportEventReplacement"
21-
)]
5+
[assembly: JSExport(typeof(IBackend))]
6+
[assembly: JSImport(typeof(IFrontend))]
227

238
namespace Bootsharp.Common.Test;
249

@@ -31,11 +16,6 @@ public class TypesTest
3116
public void Records ()
3217
{
3318
// TODO: Remove when coverlet bug is resolved: https://github.com/coverlet-coverage/coverlet/issues/1561
34-
_ = new SolutionMeta { Assemblies = [], Methods = [], Types = [] } with { Assemblies = default };
35-
_ = new AssemblyMeta { Name = "", Bytes = [] } with { Name = "foo" };
36-
_ = new MethodMeta { Name = "", JSName = "", Arguments = default, Assembly = "", Type = default, Space = "", JSSpace = "", ReturnValue = default } with { Assembly = "foo" };
37-
_ = new ArgumentMeta { Name = "", JSName = "", Value = default } with { Name = "foo" };
38-
_ = new ValueMeta { Type = default, Nullable = true, TypeSyntax = "", Void = true, Serialized = true, Async = true, JSTypeSyntax = "" } with { TypeSyntax = "foo" };
3919
_ = new MockItem("") with { Id = "foo" };
4020
_ = new MockItemWithEnum(default) with { Enum = MockEnum.Bar };
4121
_ = new MockRecord(default) with { Items = new[] { new MockItem("") } };
@@ -46,52 +26,23 @@ public void TypesAreAssigned ()
4626
{
4727
Assert.Equal([typeof(IBackend)], new JSExportAttribute(typeof(IBackend)).Types);
4828
Assert.Equal([typeof(IFrontend)], new JSImportAttribute(typeof(IFrontend)).Types);
49-
}
50-
51-
[Fact]
52-
public void NameAndInvokeParametersAreNullByDefault ()
53-
{
54-
var attribute = new JSExportAttribute(typeof(IBackend));
55-
Assert.Null(attribute.NamePattern);
56-
Assert.Null(attribute.NameReplacement);
57-
Assert.Null(attribute.InvokePattern);
58-
Assert.Null(attribute.InvokeReplacement);
59-
}
60-
61-
[Fact]
62-
public void EventParametersAreNullByDefault () // (defaults are in generator)
63-
{
64-
var attribute = new JSImportAttribute(typeof(IBackend));
65-
Assert.Null(attribute.EventPattern);
66-
Assert.Null(attribute.EventReplacement);
29+
Assert.Equal("Space", (new JSPreferencesAttribute { Space = ["Space"] }).Space[0]);
6730
}
6831

6932
[Fact]
7033
public void ExportParametersEqualArguments ()
7134
{
7235
Assert.Equal([typeof(IBackend)],
7336
(export.ConstructorArguments[0].Value as IReadOnlyCollection<CustomAttributeTypedArgument>).Select(a => a.Value));
74-
Assert.Equal("ExportNamePattern", GetNamedValue(export.NamedArguments, nameof(JSTypeAttribute.NamePattern)));
75-
Assert.Equal("ExportNameReplacement", GetNamedValue(export.NamedArguments, nameof(JSTypeAttribute.NameReplacement)));
76-
Assert.Equal("ExportInvokePattern", GetNamedValue(export.NamedArguments, nameof(JSTypeAttribute.InvokePattern)));
77-
Assert.Equal("ExportInvokeReplacement", GetNamedValue(export.NamedArguments, nameof(JSTypeAttribute.InvokeReplacement)));
7837
}
7938

8039
[Fact]
8140
public void ImportParametersEqualArguments ()
8241
{
8342
Assert.Equal([typeof(IFrontend)],
8443
(import.ConstructorArguments[0].Value as IReadOnlyCollection<CustomAttributeTypedArgument>).Select(a => a.Value));
85-
Assert.Equal("ImportNamePattern", GetNamedValue(import.NamedArguments, nameof(JSTypeAttribute.NamePattern)));
86-
Assert.Equal("ImportNameReplacement", GetNamedValue(import.NamedArguments, nameof(JSTypeAttribute.NameReplacement)));
87-
Assert.Equal("ImportInvokePattern", GetNamedValue(import.NamedArguments, nameof(JSTypeAttribute.InvokePattern)));
88-
Assert.Equal("ImportInvokeReplacement", GetNamedValue(import.NamedArguments, nameof(JSTypeAttribute.InvokeReplacement)));
89-
Assert.Equal("ImportEventPattern", GetNamedValue(import.NamedArguments, nameof(JSImportAttribute.EventPattern)));
90-
Assert.Equal("ImportEventReplacement", GetNamedValue(import.NamedArguments, nameof(JSImportAttribute.EventReplacement)));
9144
}
9245

93-
private static object GetNamedValue (IList<CustomAttributeNamedArgument> args, string key) =>
94-
args.First(a => a.MemberName == key).TypedValue.Value;
9546
private static CustomAttributeData GetMockExportAttribute () =>
9647
typeof(TypesTest).Assembly.CustomAttributes
9748
.First(a => a.AttributeType == typeof(JSExportAttribute));

src/cs/Bootsharp.Common/Attributes/JSEventAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/// <example>
88
/// <code>
99
/// [JSEvent]
10-
/// public static partial string OnSomethingHappened (string payload);
10+
/// public static partial void OnSomethingHappened (string payload);
1111
/// Namespace.onSomethingHappened.subscribe(payload => ...);
1212
/// </code>
1313
/// </example>

0 commit comments

Comments
 (0)