Skip to content

Commit

Permalink
Simplified trimming workaround (#52)
Browse files Browse the repository at this point in the history
* Simplified for the workaround for dotnet/runtime#102796

* Update RazorSlices.Samples.WebApp.csproj
  • Loading branch information
DamianEdwards authored Jun 13, 2024
1 parent 929b800 commit 4b65ac3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<VersionPrefix>0.8.1</VersionPrefix>
<VersionPrefix>0.8.2</VersionPrefix>
<!-- VersionSuffix used for local builds -->
<VersionSuffix>dev</VersionSuffix>
<!-- VersionSuffix to be used for CI builds -->
Expand Down
36 changes: 6 additions & 30 deletions src/RazorSlices.SourceGenerator/RazorSliceProxyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,6 @@ private static void Execute(SourceProductionContext context, string? assemblyNam
codeBuilder.AppendLine("#nullable enable");
codeBuilder.AppendLine();

codeBuilder.AppendLine($$"""
namespace {{rootNamespace}}
{
/// <summary>
/// All calls to create Razor Slices instances via the generated <see cref="global::RazorSlices.IRazorSliceProxy"/> classes
/// go through this factory to ensure that the generated types' Create methods are always invoked via the static abstract
/// methods defined in the <see cref="global::RazorSlices.IRazorSliceProxy"/> interface. This ensures that the interface
/// implementation is never trimmed from the generated types.
/// </summary>
/// <remarks>
/// Workaround for https://github.com/dotnet/runtime/issues/102796
/// </remarks>
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] // Hide from IntelliSense.
internal static class RazorSlicesGenericFactory
{
public static RazorSlice CreateSlice<TProxy>() where TProxy : IRazorSliceProxy => TProxy.CreateSlice();
public static RazorSlice<TModel> CreateSlice<TProxy, TModel>(TModel model) where TProxy : IRazorSliceProxy => TProxy.CreateSlice(model);
}
}
""");

foreach (var file in distinctTexts)
{
var fileName = Path.GetFileNameWithoutExtension(file.Path);
Expand Down Expand Up @@ -158,20 +136,18 @@ public sealed class {{className}} : global::RazorSlices.IRazorSliceProxy
/// <summary>
/// Creates a new instance of the Razor Slice defined in <c>{{relativeFilePath}}</c> .
/// </summary>
public static global::RazorSlices.RazorSlice Create()
=> global::{{rootNamespace}}.RazorSlicesGenericFactory.CreateSlice<global::{{fullNamespace}}.{{className}}>();
public static global::RazorSlices.RazorSlice Create() => _sliceDefinition.CreateSlice();
/// <summary>
/// Creates a new instance of the Razor Slice defined in <c>{{relativeFilePath}}</c> with the given model.
/// </summary>
public static global::RazorSlices.RazorSlice<TModel> Create<TModel>(TModel model)
=> global::{{rootNamespace}}.RazorSlicesGenericFactory.CreateSlice<global::{{fullNamespace}}.{{className}}, TModel>(model);
public static global::RazorSlices.RazorSlice<TModel> Create<TModel>(TModel model) => _sliceDefinition.CreateSlice(model);
// Explicit interface implementation
static global::RazorSlices.RazorSlice global::RazorSlices.IRazorSliceProxy.CreateSlice() => _sliceDefinition.CreateSlice();
// Explicit interface implementation, workaround for https://github.com/dotnet/runtime/issues/102796
static global::RazorSlices.RazorSlice global::RazorSlices.IRazorSliceProxy.CreateSlice() => Create();
// Explicit interface implementation
static global::RazorSlices.RazorSlice<TModel> global::RazorSlices.IRazorSliceProxy.CreateSlice<TModel>(TModel model) => _sliceDefinition.CreateSlice(model);
// Explicit interface implementation, workaround for https://github.com/dotnet/runtime/issues/102796
static global::RazorSlices.RazorSlice<TModel> global::RazorSlices.IRazorSliceProxy.CreateSlice<TModel>(TModel model) => Create(model);
}
""");

Expand Down

0 comments on commit 4b65ac3

Please sign in to comment.