Skip to content

Commit

Permalink
RazorProjectEngineFeatureBase: Clean up and add Initialize method
Browse files Browse the repository at this point in the history
This is similar to the last commit. All IRazorProjectEngineFeature implementations now inherit from RazorProjectEngineFeatureBase.
  • Loading branch information
DustinCampbell committed Dec 26, 2024
1 parent e36c4be commit f5b2355
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using Xunit;

namespace Microsoft.AspNetCore.Razor.Language;
Expand All @@ -16,7 +14,7 @@ public void ProjectEngineSetter_CallsOnInitialized()
var testFeature = new TestFeature();

// Act
testFeature.ProjectEngine = RazorProjectEngine.CreateEmpty();
testFeature.Initialize(RazorProjectEngine.CreateEmpty());

// Assert
Assert.Equal(1, testFeature.InitializationCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Microsoft.AspNetCore.Razor.Language.Components;

internal class ComponentImportProjectFeature : IImportProjectFeature
internal class ComponentImportProjectFeature : RazorProjectEngineFeatureBase, IImportProjectFeature
{
// Using explicit newlines here to avoid fooling our baseline tests
private const string DefaultUsingImportContent =
Expand All @@ -22,8 +22,6 @@ internal class ComponentImportProjectFeature : IImportProjectFeature
"@using global::System.Threading.Tasks\r\n" +
"@using global::" + ComponentsApi.RenderFragment.Namespace + "\r\n"; // Microsoft.AspNetCore.Components

public RazorProjectEngine ProjectEngine { get; set; }

public IReadOnlyList<RazorProjectItem> GetImports(RazorProjectItem projectItem)
{
if (projectItem == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

namespace Microsoft.AspNetCore.Razor.Language;

public interface IRazorProjectEngineFeature : IRazorFeature
{
RazorProjectEngine ProjectEngine { get; set; }
RazorProjectEngine ProjectEngine { get; init; }

void Initialize(RazorProjectEngine projectEngine);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal RazorProjectEngine(

foreach (var projectFeature in features)
{
projectFeature.ProjectEngine = this;
projectFeature.Initialize(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System;
using System.Threading;

namespace Microsoft.AspNetCore.Razor.Language;

public abstract class RazorProjectEngineFeatureBase : IRazorProjectEngineFeature
{
private RazorProjectEngine _projectEngine;
private RazorProjectEngine? _projectEngine;

public virtual RazorProjectEngine ProjectEngine
public RazorProjectEngine ProjectEngine
{
get => _projectEngine;
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
get => _projectEngine.AssumeNotNull(Resources.FeatureMustBeInitialized);
init => Initialize(value);
}

_projectEngine = value;
OnInitialized();
public void Initialize(RazorProjectEngine projectEngine)
{
ArgHelper.ThrowIfNull(projectEngine);

if (Interlocked.CompareExchange(ref _projectEngine, projectEngine, null) is not null)
{
throw new InvalidOperationException(Resources.FeatureAlreadyInitialized);
}

OnInitialized();
}

protected virtual void OnInitialized()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public void Configure(RazorCodeGenerationOptionsBuilder options)
}
}

private class TestImportProjectFeature : IImportProjectFeature
private class TestImportProjectFeature : RazorProjectEngineFeatureBase, IImportProjectFeature
{
private readonly List<RazorProjectItem> _imports;

Expand All @@ -419,8 +419,6 @@ public TestImportProjectFeature(List<RazorProjectItem> imports)
_imports = imports;
}

public RazorProjectEngine ProjectEngine { get; set; }

public IReadOnlyList<RazorProjectItem> GetImports(RazorProjectItem projectItem)
{
return _imports;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ protected override void OnInitialized()
{
if (_inner != null)
{
_inner.ProjectEngine = ProjectEngine;
_inner.Initialize(ProjectEngine);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ public void Configure(RazorCodeGenerationOptionsBuilder options)
}
}

private class TestImportProjectFeature : IImportProjectFeature
private class TestImportProjectFeature : RazorProjectEngineFeatureBase, IImportProjectFeature
{
private readonly List<RazorProjectItem> _imports;

Expand All @@ -528,8 +528,6 @@ public TestImportProjectFeature(List<RazorProjectItem> imports)
_imports = imports;
}

public RazorProjectEngine? ProjectEngine { get; set; }

public IReadOnlyList<RazorProjectItem> GetImports(RazorProjectItem projectItem)
{
return _imports;
Expand Down

0 comments on commit f5b2355

Please sign in to comment.