Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ResourcesTagHelper extensible #16329

Merged
merged 21 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3497188
Add model and interface.
sarahelsaig Jun 16, 2024
6ef6da2
Rename ResourceType enum to ResourceTagType and merge the two separat…
sarahelsaig Jun 16, 2024
c578ee3
Evaluate processors in the tag helper.
sarahelsaig Jun 16, 2024
ddfe4fc
Merge resources tag helper and liquid filter implementations in a sin…
sarahelsaig Jun 16, 2024
7581161
Merge remote-tracking branch 'upstream/main' into task/resource-tag-h…
sarahelsaig Jun 17, 2024
5ec2305
Add change log.
sarahelsaig Jun 17, 2024
7fc1b34
Update src/docs/releases/2.0.0.md
sarahelsaig Jun 17, 2024
132b808
Apply suggestions from code review
sarahelsaig Jul 21, 2024
051f733
Update src/OrchardCore.Modules/OrchardCore.Resources/Liquid/Resources…
hishamco Jul 21, 2024
3cbfe95
Merge branch 'main' into task/resource-tag-helper
hishamco Jul 21, 2024
0f433ca
Update src/OrchardCore/OrchardCore.ResourceManagement/TagHelpers/Reso…
hishamco Jul 21, 2024
e25b6ea
Update src/OrchardCore/OrchardCore.ResourceManagement/TagHelpers/Reso…
hishamco Jul 21, 2024
41b0e4b
Add ResourceTagType docs.
sarahelsaig Jul 21, 2024
d2ee20f
Merge remote-tracking branch 'origin/task/resource-tag-helper' into t…
sarahelsaig Jul 21, 2024
5423000
Add ResourcesTagHelperProcessorContext docs.
sarahelsaig Jul 21, 2024
b9168f6
Move properties after constructor
sarahelsaig Jul 21, 2024
f9e38bd
re-generate asset files
sarahelsaig Jul 22, 2024
4da1a92
Revert "re-generate asset files"
sarahelsaig Jul 22, 2024
6da6f99
Merge branch 'main' into task/resource-tag-helper
hishamco Jul 24, 2024
5c8e29c
Update src/OrchardCore.Modules/OrchardCore.Resources/Services/Resourc…
sarahelsaig Jul 24, 2024
470ba66
Merge branch 'main' into task/resource-tag-helper
hishamco Jul 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,30 @@ public class ResourcesTag
public static async ValueTask<Completion> WriteToAsync(List<FilterArgument> argumentsList, TextWriter writer, TextEncoder _, TemplateContext context)
{
var services = ((LiquidTemplateContext)context).Services;
var resourceManager = services.GetRequiredService<IResourceManager>();
var processors = services.GetRequiredService<IEnumerable<IResourcesTagHelperProcessor>>();

var type = ResourceType.Footer;
var processorContext = new ResourcesTagHelperProcessorContext(writer, ResourceTagType.Footer);
hishamco marked this conversation as resolved.
Show resolved Hide resolved

foreach (var argument in argumentsList)
{
switch (argument.Name)
{
#pragma warning disable CA1806 // Do not ignore method results
case "type": Enum.TryParse((await argument.Expression.EvaluateAsync(context)).ToStringValue(), out type); break;
#pragma warning restore CA1806 // Do not ignore method results
case "type":
var typeString = (await argument.Expression.EvaluateAsync(context)).ToStringValue();
if (Enum.TryParse<ResourceTagType>(typeString, out var type))
{
processorContext = processorContext with { Type = type };
}
break;
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved
}
}

switch (type)
foreach (var processor in processors)
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
case ResourceType.Meta:
resourceManager.RenderMeta(writer);
break;

case ResourceType.HeadLink:
resourceManager.RenderHeadLink(writer);
break;

case ResourceType.Stylesheet:
resourceManager.RenderStylesheet(writer);
break;

case ResourceType.HeadScript:
resourceManager.RenderHeadScript(writer);
break;

case ResourceType.FootScript:
resourceManager.RenderFootScript(writer);
break;

case ResourceType.Header:
resourceManager.RenderMeta(writer);
resourceManager.RenderHeadLink(writer);
resourceManager.RenderStylesheet(writer);
resourceManager.RenderHeadScript(writer);
break;

case ResourceType.Footer:
resourceManager.RenderFootScript(writer);
break;

default:
break;
await processor.ProcessAsync(processorContext);
}

return Completion.Normal;
}

public enum ResourceType
{
Meta,
HeadLink,
Stylesheet,
HeadScript,
FootScript,
Header,
Footer
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using OrchardCore.ResourceManagement;

namespace OrchardCore.Resources.Services;

public class ResourcesTagHelperProcessor : IResourcesTagHelperProcessor
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly IResourceManager _resourceManager;
private readonly ILogger<ResourcesTagHelperProcessor> _logger;

public ResourcesTagHelperProcessor(IResourceManager resourceManager, ILogger<ResourcesTagHelperProcessor> logger)
{
_resourceManager = resourceManager;
_logger = logger;
}

public Task ProcessAsync(ResourcesTagHelperProcessorContext context)
{
switch (context.Type)
{
case ResourceTagType.Meta:
_resourceManager.RenderMeta(context.Writer);
break;

case ResourceTagType.HeadLink:
_resourceManager.RenderHeadLink(context.Writer);
break;

case ResourceTagType.Stylesheet:
_resourceManager.RenderStylesheet(context.Writer);
break;

case ResourceTagType.HeadScript:
_resourceManager.RenderHeadScript(context.Writer);
break;

case ResourceTagType.FootScript:
_resourceManager.RenderFootScript(context.Writer);
break;

case ResourceTagType.Header:
_resourceManager.RenderMeta(context.Writer);
_resourceManager.RenderHeadLink(context.Writer);
_resourceManager.RenderStylesheet(context.Writer);
_resourceManager.RenderHeadScript(context.Writer);
break;

case ResourceTagType.Footer:
_resourceManager.RenderFootScript(context.Writer);
break;

default:
_logger.LogWarning("Unknown {TypeName} value \"{Value}\".", nameof(ResourceTagType), context.Type);
break;
}
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved

return Task.CompletedTask;
}
}
3 changes: 3 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Resources/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using OrchardCore.Modules;
using OrchardCore.ResourceManagement;
using OrchardCore.Resources.Liquid;
using OrchardCore.Resources.Services;

namespace OrchardCore.Resources
{
Expand Down Expand Up @@ -34,6 +35,8 @@ public override void ConfigureServices(IServiceCollection serviceCollection)

var resourceConfiguration = _shellConfiguration.GetSection("OrchardCore_Resources");
serviceCollection.Configure<ResourceOptions>(resourceConfiguration);

serviceCollection.AddScoped<IResourcesTagHelperProcessor, ResourcesTagHelperProcessor>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Threading.Tasks;

namespace OrchardCore.ResourceManagement;

/// <summary>
/// Processes resources in the <c>&lt;resources /&gt;</c> tag helper.
/// </summary>
public interface IResourcesTagHelperProcessor
{
/// <summary>
/// Invoked when rendering registered resources.
/// </summary>
Task ProcessAsync(ResourcesTagHelperProcessorContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace OrchardCore.ResourceManagement;

public enum ResourceTagType
hishamco marked this conversation as resolved.
Show resolved Hide resolved
{
Meta,
HeadLink,
Stylesheet,
HeadScript,
FootScript,
Header,
Footer
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.IO;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace OrchardCore.ResourceManagement;

public record ResourcesTagHelperProcessorContext(
hishamco marked this conversation as resolved.
Show resolved Hide resolved
TextWriter Writer,
ResourceTagType Type);
hishamco marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,78 +1,42 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Cysharp.Text;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Logging;

namespace OrchardCore.ResourceManagement.TagHelpers
{
public enum ResourceType
{
Meta,
HeadLink,
Stylesheet,
HeadScript,
FootScript,
Header,
Footer
}

[HtmlTargetElement("resources", Attributes = nameof(Type))]
public class ResourcesTagHelper : TagHelper
{
public ResourceType Type { get; set; }
public ResourceTagType Type { get; set; }
hishamco marked this conversation as resolved.
Show resolved Hide resolved

private readonly IResourceManager _resourceManager;
private readonly ILogger _logger;
private readonly IEnumerable<IResourcesTagHelperProcessor> _processors;

public ResourcesTagHelper(
IResourceManager resourceManager,
ILogger<ResourcesTagHelper> logger)
ILogger<ResourcesTagHelper> logger,
IEnumerable<IResourcesTagHelperProcessor> processors)
{
_resourceManager = resourceManager;
_logger = logger;
_processors = processors;
}

public override void Process(TagHelperContext tagHelperContext, TagHelperOutput output)
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
try
{
using var sw = new ZStringWriter();

switch (Type)
{
case ResourceType.Meta:
_resourceManager.RenderMeta(sw);
break;

case ResourceType.HeadLink:
_resourceManager.RenderHeadLink(sw);
break;
await using var sw = new ZStringWriter();
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved

case ResourceType.Stylesheet:
_resourceManager.RenderStylesheet(sw);
break;
var processorContext = new ResourcesTagHelperProcessorContext(sw, Type);
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved

case ResourceType.HeadScript:
_resourceManager.RenderHeadScript(sw);
break;

case ResourceType.FootScript:
_resourceManager.RenderFootScript(sw);
break;

case ResourceType.Header:
_resourceManager.RenderMeta(sw);
_resourceManager.RenderHeadLink(sw);
_resourceManager.RenderStylesheet(sw);
_resourceManager.RenderHeadScript(sw);
break;

case ResourceType.Footer:
_resourceManager.RenderFootScript(sw);
break;

default:
break;
foreach (var processor in _processors)
{
await processor.ProcessAsync(processorContext);
}

output.Content.AppendHtml(sw.ToString());
hishamco marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
4 changes: 4 additions & 0 deletions src/docs/releases/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ You may have to adjust your GraphQL queries in that case.

Additionally, the `GetAliases` method in the `IIndexAliasProvider` interface is now asynchronous and has been renamed to `GetAliasesAsync`. Implementations of this interface should be modified by updating the method signature and ensure they handle asynchronous operations correctly.

### Resource Management

Previously the `<resources type="..." />` Razor tag helper and the `{% resources type: "..." %}` Liquid tag were only capable of handling a hard-coded set of resource definition types (`script`, `stylesheet`, etc). Now both can be extended with `IResourcesTagHelperProcessor` to run custom rendering logic. To make this possible, the `OrchardCore.ResourceManagement.TagHelpers.ResourceType` and `OrchardCore.Resources.Liquid.ResourcesTag.ResourceType` enums have been replaced with a common `OrchardCore.ResourceManagement.ResourceTagType`. It was renamed to avoid confusion with `ResourceDefinition.Type`. This change is breaking in code, but it does not affect the uses of the Razor tag helper or the Liquid tag in templates.

## Change Logs

### Azure AI Search Module
Expand Down