Skip to content

Commit

Permalink
Blazorator updates. Target interface instead of extensions. Append se…
Browse files Browse the repository at this point in the history
…rvice naming.
  • Loading branch information
IEvangelist committed Mar 22, 2022
1 parent b953606 commit f0f2c3e
Show file tree
Hide file tree
Showing 28 changed files with 645 additions and 750 deletions.
759 changes: 402 additions & 357 deletions README.md

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions samples/Blazor.ExampleConsumer/Pages/ClientPosition.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@page "/geolocation"

@inject IGeolocation Geolocation
@inject IGeolocationService Geolocation

<PageTitle>Geolocation</PageTitle>
<h1>
Expand All @@ -15,7 +15,7 @@
{
<span>:
<code>
@(_position.TimestampAsDateTime.ToLocalTime().ToString())
@(_position.TimestampAsUtcDateTime.ToLocalTime().ToString())
</code>
</span>
}
Expand All @@ -37,30 +37,29 @@
}

@code {
const string GeolocationOptionsKey = "geolocation-options";
readonly JsonSerializerOptions _opts = new()
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};

readonly PositionOptions _options = new()
{
EnableHighAccuracy = true,
MaximumAge = 0,
Timeout = 15_000
};

GeolocationPosition? _position;
GeolocationPositionError? _positionError;
bool _isLoading = true;

protected override void OnInitialized() =>
Geolocation.GetCurrentPosition(
this,
nameof(OnPositionRecieved),
nameof(OnPositionError),
_options);
component: this,
onSuccessCallbackMethodName: nameof(OnPositionRecieved),
onErrorCallbackMethodName: nameof(OnPositionError),
options: _options);

[JSInvokable]
public void OnPositionRecieved(GeolocationPosition position)
Expand Down
2 changes: 1 addition & 1 deletion samples/Blazor.ExampleConsumer/Pages/Counter.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@page "/counter"
@inject IStorage LocalStorage
@inject IStorageService LocalStorage
@inject NavigationManager Nav
@implements IDisposable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Microsoft.JSInterop;
Implementation = "window.navigator.geolocation",
HostingModel = BlazorHostingModel.Server,
Url = "https://developer.mozilla.org/docs/Web/API/Geolocation")]
internal static partial class AsynchronousGeolocationExtensions
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public partial interface IGeolocationService
#pragma warning restore CS1591 // The XML comments are source generated
{
}
6 changes: 3 additions & 3 deletions src/Blazor.Geolocation.Server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The [`Blazor.Geolocation.Server`](https://www.nuget.org/packages/Blazor.Geolocat

## Get started

After the NuGet package is added as a reference, call the `AddGeolocationServices` method to register the `IGeolocation` service type.
After the NuGet package is added as a reference, call the `AddGeolocationServices` method to register the `IGeolocationService` service type.

```csharp
var builder = WebApplication.CreateBuilder(args);
Expand All @@ -27,7 +27,7 @@ app.MapFallbackToPage("/_Host");
app.Run();
```

Anywhere needed within your Razor component, or Blazor client code — either `@inject` or `[Inject]` the `IGeolocation` type. The interface takes the following shape:
Anywhere needed within your Razor component, or Blazor client code — either `@inject` or `[Inject]` the `IGeolocationService` type. The interface takes the following shape:

```csharp
#nullable enable
Expand All @@ -36,7 +36,7 @@ namespace Microsoft.JSInterop;
/// <summary>
/// Source generated interface definition of the <c>Geolocation</c> type.
/// </summary>
public interface IGeolocation
public interface IGeolocationService
{
/// <summary>
/// Source generated implementation of <c>window.navigator.geolocation.clearWatch</c>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Microsoft.JSInterop;
TypeName = "Geolocation",
Implementation = "window.navigator.geolocation",
Url = "https://developer.mozilla.org/docs/Web/API/Geolocation")]
internal static partial class SynchronousGeolocationExtensions
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public partial interface IGeolocationService
#pragma warning restore CS1591 // The XML comments are source generated
{
}
6 changes: 3 additions & 3 deletions src/Blazor.Geolocation.WebAssembly/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The [`Blazor.Geolocation.WebAssembly`](https://www.nuget.org/packages/Blazor.Geo

## Get started

After the NuGet package is added as a reference, call the `AddGeolocationServices` method to register the `IGeolocation` service type.
After the NuGet package is added as a reference, call the `AddGeolocationServices` method to register the `IGeolocationService` service type.

```csharp
using Microsoft.AspNetCore.Components.Web;
Expand All @@ -25,7 +25,7 @@ builder.Services.AddGeolocationServices();
await builder.Build().RunAsync();
```

Anywhere needed within your Razor component, or Blazor client code — either `@inject` or `[Inject]` the `IGeolocation` type. The interface takes the following shape:
Anywhere needed within your Razor component, or Blazor client code — either `@inject` or `[Inject]` the `IGeolocationService` type. The interface takes the following shape:

```csharp
#nullable enable
Expand All @@ -34,7 +34,7 @@ namespace Microsoft.JSInterop;
/// <summary>
/// Source generated interface definition of the <c>Geolocation</c> type.
/// </summary>
public interface IGeolocation
public interface IGeolocationService
{
/// <summary>
/// Source generated implementation of <c>window.navigator.geolocation.clearWatch</c>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

namespace Microsoft.JSInterop;

/// <summary>
/// Source generated extension methods on the <see cref="IJSRuntime"/> implementation.
/// </summary>
[JSAutoInterop(
TypeName = "Storage",
Implementation = "window.localStorage",
HostingModel = BlazorHostingModel.Server,
OnlyGeneratePureJS = true,
Url = "https://developer.mozilla.org/docs/Web/API/Window/localStorage")]
internal static partial class AsynchronousLocalStorageExtensions
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public partial interface IStorageService
#pragma warning restore CS1591 // The XML comments are source generated
{
}
6 changes: 3 additions & 3 deletions src/Blazor.LocalStorage.Server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The [`Blazor.LocalStorage.Server`](https://www.nuget.org/packages/Blazor.LocalSt

## Get started

After the NuGet package is added as a reference, call the `AddLocalStorageServices` method to register the `ILocalStorage` service type.
After the NuGet package is added as a reference, call the `AddLocalStorageServices` method to register the `IStorageService` service type.

```csharp
var builder = WebApplication.CreateBuilder(args);
Expand All @@ -27,7 +27,7 @@ app.MapFallbackToPage("/_Host");
app.Run();
```

Anywhere needed within your Razor component, or Blazor client code — either `@inject` or `[Inject]` the `ILocalStorage` type. The interface takes the following shape:
Anywhere needed within your Razor component, or Blazor client code — either `@inject` or `[Inject]` the `IStorageService` type. The interface takes the following shape:

```csharp
using Blazor.Serialization.Extensions;
Expand All @@ -39,7 +39,7 @@ namespace Microsoft.JSInterop;
/// <summary>
/// Source generated interface definition of the <c>Storage</c> type.
/// </summary>
public interface IStorage
public interface IStorageService
{
/// <summary>
/// Source generated implementation of <c>window.localStorage.clear</c>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

namespace Microsoft.JSInterop;

/// <summary>
/// Source generated extension methods on the <see cref="IJSInProcessRuntime"/> implementation.
/// </summary>
[JSAutoGenericInterop(
TypeName = "Storage",
Implementation = "window.localStorage",
Expand All @@ -15,6 +12,8 @@ namespace Microsoft.JSInterop;
"getItem",
"setItem:value"
})]
internal static partial class SynchronousLocalStorageExtensions
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public partial interface IStorageService
#pragma warning restore CS1591 // The XML comments are source generated
{
}
6 changes: 3 additions & 3 deletions src/Blazor.LocalStorage.WebAssembly/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The [`Blazor.LocalStorage.WebAssembly`](https://www.nuget.org/packages/Blazor.Lo

## Get started

After the NuGet package is added as a reference, call the `AddLocalStorageServices` method to register the `ILocalStorage` service type.
After the NuGet package is added as a reference, call the `AddLocalStorageServices` method to register the `IStorageService` service type.

```csharp
using Microsoft.AspNetCore.Components.Web;
Expand All @@ -25,7 +25,7 @@ builder.Services.AddLocalStorageServices();
await builder.Build().RunAsync();
```

Anywhere needed within your Razor component, or Blazor client code — either `@inject` or `[Inject]` the `ILocalStorage` type. The interface takes the following shape:
Anywhere needed within your Razor component, or Blazor client code — either `@inject` or `[Inject]` the `IStorageService` type. The interface takes the following shape:

```csharp
using Blazor.Serialization.Extensions;
Expand All @@ -37,7 +37,7 @@ namespace Microsoft.JSInterop;
/// <summary>
/// Source generated interface definition of the <c>Storage</c> type.
/// </summary>
public interface IStorage
public interface IStorageService
{
/// <summary>
/// Source generated implementation of <c>window.localStorage.clear</c>.
Expand Down
13 changes: 4 additions & 9 deletions src/Blazor.SourceGenerators/Builders/MethodBuilderDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ internal readonly record struct MethodBuilderDetails(
string? GenericTypeArgs)
{
/// <summary>
/// A value representing the generic return type, <c>"TResult"</c>.
/// A value representing the generic return type, <c>"TValue"</c>.
/// </summary>
internal const string GenericReturnType = "TResult";

/// <summary>
/// A value representing the generic return type, <c>"TArg"</c>.
/// </summary>
internal const string GenericArgumentType = "TArg";
internal static readonly string GenericTypeValue = "TValue";

/// <summary>
/// A value representing the generic component type, <c>"TComponent"</c>.
Expand All @@ -44,8 +39,8 @@ internal static MethodBuilderDetails Create(CSharpMethod method, GeneratorOption
var containsGenericParameters =
method.ParameterDefinitions.Any(p => p.IsGenericParameter(method.RawName, options));
var genericTypeArgs = isGenericReturnType
? ToGenericTypeArgument(GenericReturnType)
: containsGenericParameters ? ToGenericTypeArgument(GenericArgumentType) : null;
? ToGenericTypeArgument(GenericTypeValue)
: containsGenericParameters ? ToGenericTypeArgument(GenericTypeValue) : null;
var fullyQualifiedJavaScriptIdentifier = method.JavaScriptMethodDependency?.InvokableMethodName;
fullyQualifiedJavaScriptIdentifier ??=
options.Implementation is not null
Expand Down
22 changes: 9 additions & 13 deletions src/Blazor.SourceGenerators/Builders/SourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ internal sealed class SourceBuilder
private Indentation _indentation = new(0);
private string? _implementationName;
private string? _interfaceName;
private bool _isService;

internal int IndentationLevel => _indentation.Level;
internal string ImplementationName => _implementationName ??= _options.Implementation!.ToImplementationName();
internal string InterfaceName => _interfaceName ??= $"I{_options.TypeName}";
internal string ImplementationName => _implementationName ??=
$"{_options.Implementation.ToImplementationName(_isService)}";
internal string InterfaceName => _interfaceName ??=
_options.TypeName.ToInterfaceName(_isService);

internal SourceBuilder(GeneratorOptions options) => _options = options;
internal SourceBuilder(GeneratorOptions options, bool isService = true) =>
(_options, _isService) = (options, isService);

internal SourceBuilder AppendCopyRightHeader()
{
Expand Down Expand Up @@ -64,28 +68,20 @@ internal SourceBuilder AppendNamespace(string namespaceString, bool isNullableCo
return this;
}

internal SourceBuilder AppendStaticPartialClassDeclaration(
string className, string? accessModifier)
{
_builder.Append($"{accessModifier ?? "internal"} static partial class {className}{_newLine}");

return this;
}

internal SourceBuilder AppendPublicInterfaceDeclaration()
{
_builder.Append($"/// <summary>{_newLine}");
_builder.Append($"/// Source generated interface definition of the <c>{_options.TypeName}</c> type.{_newLine}");
_builder.Append($"/// </summary>{_newLine}");
_builder.Append($"public interface I{_options.TypeName}{_newLine}");
_builder.Append($"public partial interface {InterfaceName}{_newLine}");

return this;
}

internal SourceBuilder AppendInternalImplementationDeclaration()
{
_builder.Append($"/// <inheritdoc />{_newLine}");
_builder.Append($"internal class {ImplementationName} : {InterfaceName}{_newLine}");
_builder.Append($"internal sealed class {ImplementationName} : {InterfaceName}{_newLine}");

return this;
}
Expand Down
26 changes: 25 additions & 1 deletion src/Blazor.SourceGenerators/CSharp/CSharpObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ internal string ToClassString()

builder.Append("using System.Text.Json.Serialization;\r\n\r\n");
builder.Append("namespace Microsoft.JSInterop;\r\n\r\n");

builder.Append(
$"/// <summary>\r\n");
builder.Append(
$"/// Source-generated object representing an ideally immutable <c>{TypeName}</c> value.\r\n");
builder.Append(
$"/// </summary>\r\n");

builder.Append($"public class {TypeName}\r\n{{\r\n");

var memberCount = Properties.Count;
Expand All @@ -73,6 +81,12 @@ in Properties.Select((kvp, index) => (index, kvp)))
var statementTerminator = member.IsNullable ? " = default!;" : "";
var csharpMemberName = memberName.CapitalizeFirstLetter();

builder.Append(
$" /// <summary>\r\n");
builder.Append(
$" /// Source-generated property representing the <c>{TypeName}.{memberName}</c> value.\r\n");
builder.Append(
$" /// </summary>\r\n");
builder.Append(
$" [JsonPropertyName(\"{memberName}\")]\r\n");
builder.Append(
Expand All @@ -81,11 +95,21 @@ in Properties.Select((kvp, index) => (index, kvp)))
// Add readonly property for converting DOMTimeStamp (long) to DateTime.
if (member.RawTypeName is "DOMTimeStamp" or "DOMTimeStamp | null")
{
builder.Append(
$" /// <summary>\r\n");
builder.Append(
$" /// Source-generated property representing the <c>{TypeName}.{memberName}</c> value, \r\n");

builder.Append(
$" /// converted as a <see cref=\"System.DateTime\" /> in UTC.\r\n");
builder.Append(
$" /// </summary>\r\n");

var nullable = member.IsNullable ? "?" : "";
builder.Append(
$" [JsonIgnore]\r\n");
builder.Append(
$" public DateTime{nullable} {csharpMemberName}AsDateTime => {csharpMemberName}.ToDateTimeFromUnix();\r\n");
$" public DateTime{nullable} {csharpMemberName}AsUtcDateTime => {csharpMemberName}.ToDateTimeFromUnix();\r\n");
}
}

Expand Down
Loading

0 comments on commit f0f2c3e

Please sign in to comment.