Skip to content

Commit

Permalink
Removed reference to Cortex.Net and Cortex.Net.Blazor from Cortex.Net…
Browse files Browse the repository at this point in the history
….Fody.
  • Loading branch information
jspuij committed Nov 14, 2019
1 parent 8227b83 commit ea49a2f
Show file tree
Hide file tree
Showing 22 changed files with 742 additions and 159 deletions.
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2019 Michel Weststrate, Jan-Willem Spuij

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 changes: 9 additions & 0 deletions build/build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>
<PropertyGroup>
<Description>IL-instrumentation based observables</Description>
<Authors>Jan-Willem Spuij</Authors>
<PackageLicenseUrl>https://github.com/jspuij/Cortex.Net/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/jspuij/Cortex.Net</PackageProjectUrl>
<DisableFody>true</DisableFody>
</PropertyGroup>
</Project>
3 changes: 2 additions & 1 deletion src/Cortex.Net.Blazor/Cortex.Net.Blazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fody" Version="6.0.5" PrivateAssets="None" />
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.1.0-preview2.19528.8" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.7">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -31,5 +32,5 @@
<ItemGroup>
<ProjectReference Include="..\Cortex.Net\Cortex.Net.csproj" />
</ItemGroup>

<Import Project="..\..\build\build.props"/>
</Project>
35 changes: 26 additions & 9 deletions src/Cortex.Net.Fody/ActionWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ namespace Cortex.Net.Fody
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Cortex.Net.Api;
using Cortex.Net.Fody.Properties;
using global::Fody;
using Mono.Cecil;
using Mono.Cecil.Cil;

/// <summary>
/// Weaves methods decorated with an <see cref="ActionAttribute"/>.
/// Weaves methods decorated with an ActionAttribute.
/// </summary>
internal class ActionWeaver
{
Expand All @@ -51,16 +50,34 @@ internal class ActionWeaver
/// </summary>
private readonly ISharedStateAssignmentILProcessorQueue processorQueue;

/// <summary>
/// A type reference to the ActionAttribute type.
/// </summary>
private readonly TypeReference actionAttributeReference;

/// <summary>
/// A type reference to the ActionExtensions type.
/// </summary>
private readonly TypeReference actionExtensionsReference;

/// <summary>
/// Initializes a new instance of the <see cref="ActionWeaver"/> class.
/// </summary>
/// <param name="parentWeaver">A reference to the Parent Cortex.Net weaver.</param>
/// <param name="processorQueue">The queue to add ILProcessor actions to.</param>
/// <param name="resolvedTypes">The resolved types necessary by this weaver.</param>
/// <exception cref="ArgumentNullException">When any of the arguments is null.</exception>
public ActionWeaver(BaseModuleWeaver parentWeaver, ISharedStateAssignmentILProcessorQueue processorQueue)
public ActionWeaver(BaseModuleWeaver parentWeaver, ISharedStateAssignmentILProcessorQueue processorQueue, IDictionary<string, TypeReference> resolvedTypes)
{
if (resolvedTypes is null)
{
throw new ArgumentNullException(nameof(resolvedTypes));
}

this.parentWeaver = parentWeaver ?? throw new ArgumentNullException(nameof(parentWeaver));
this.processorQueue = processorQueue ?? throw new ArgumentNullException(nameof(processorQueue));
this.actionAttributeReference = resolvedTypes["Cortex.Net.Api.ActionAttribute"];
this.actionExtensionsReference = resolvedTypes["Cortex.Net.Api.ActionExtensions"];
}

/// <summary>
Expand All @@ -76,7 +93,7 @@ from m in t.Methods
t.BaseType != null &&
m != null &&
m.CustomAttributes != null &&
m.CustomAttributes.Any(x => x.AttributeType.FullName == typeof(ActionAttribute).FullName)
m.CustomAttributes.Any(x => x.AttributeType.FullName == this.actionAttributeReference.FullName)
select m;

foreach (var method in decoratedMethods.ToList())
Expand Down Expand Up @@ -169,7 +186,7 @@ private static void ExtendActionMethodBody(MethodDefinition methodDefinition, Ty
/// <param name="methodDefinition">The inner definition of the action method.</param>
/// <param name="actionType">The action type.</param>
/// <param name="actionFieldDefinition">The field definition.</param>
private static void EmitSharedStateSetter(
private void EmitSharedStateSetter(
ILProcessor processor,
FieldReference sharedStateBackingField,
MethodDefinition methodDefinition,
Expand All @@ -179,15 +196,15 @@ private static void EmitSharedStateSetter(
var moduleDefinition = sharedStateBackingField.Module;

// determine the name of the action.
var attribute = methodDefinition.CustomAttributes.Single(x => x.AttributeType.FullName == typeof(ActionAttribute).FullName);
var attribute = methodDefinition.CustomAttributes.Single(x => x.AttributeType.FullName == this.actionAttributeReference.FullName);
var actionName = methodDefinition.Name;
var attributeArgument = attribute.ConstructorArguments.FirstOrDefault();
if (!string.IsNullOrEmpty(attributeArgument.Value as string))
{
actionName = attributeArgument.Value as string;
}

var actionExtensions = moduleDefinition.ImportReference(typeof(ActionExtensions));
var actionExtensions = this.actionExtensionsReference;
var voidType = moduleDefinition.ImportReference(typeof(void));

MethodReference createActionMethod;
Expand Down Expand Up @@ -228,7 +245,7 @@ private static void EmitSharedStateSetter(
}

/// <summary>
/// Weaves a method that was Decorated with the <see cref="ActionAttribute"/>.
/// Weaves a method that was Decorated with the Action attribute.
/// </summary>
/// <param name="methodDefinition">The method definition.</param>
/// <remarks>
Expand Down Expand Up @@ -259,7 +276,7 @@ private void WeaveMethod(MethodDefinition methodDefinition)
this.processorQueue.SharedStateAssignmentQueue.Enqueue(
(declaringType,
false,
(processor, sharedStateBackingField) => EmitSharedStateSetter(
(processor, sharedStateBackingField) => this.EmitSharedStateSetter(
processor,
sharedStateBackingField,
methodDefinition,
Expand Down
36 changes: 26 additions & 10 deletions src/Cortex.Net.Fody/BlazorObserverWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ namespace Cortex.Net.Fody
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Cortex.Net.Blazor;
using global::Fody;
using Microsoft.AspNetCore.Components;
using Mono.Cecil;
using Mono.Cecil.Cil;

/// <summary>
/// Weaver for Blazor components decorated with the <see cref="ObserverAttribute" /> class.
/// Weaver for Blazor components decorated with the ObserverAttribute class.
/// </summary>
public class BlazorObserverWeaver
{
Expand All @@ -51,6 +49,16 @@ public class BlazorObserverWeaver
/// </summary>
private readonly ISharedStateAssignmentILProcessorQueue processorQueue;

/// <summary>
/// Type reference to ObserverAttribute.
/// </summary>
private readonly TypeReference observerAttributeReference;

/// <summary>
/// Type reference to ObserverObject.
/// </summary>
private readonly TypeReference observerObjectReference;

/// <summary>
/// The parent weaver.
/// </summary>
Expand All @@ -61,10 +69,18 @@ public class BlazorObserverWeaver
/// </summary>
/// <param name="parentWeaver">The parent weaver of this weaver.</param>
/// <param name="processorQueue">The processor queue to add delegates to, to be executed on ISharedState property assignment.</param>
public BlazorObserverWeaver(BaseModuleWeaver parentWeaver, ISharedStateAssignmentILProcessorQueue processorQueue)
/// <param name="resolvedTypes">The resolved types necessary by this weaver.</param>
public BlazorObserverWeaver(BaseModuleWeaver parentWeaver, ISharedStateAssignmentILProcessorQueue processorQueue, IDictionary<string, TypeReference> resolvedTypes)
{
if (resolvedTypes is null)
{
throw new ArgumentNullException(nameof(resolvedTypes));
}

this.parentWeaver = parentWeaver ?? throw new ArgumentNullException(nameof(parentWeaver));
this.processorQueue = processorQueue ?? throw new ArgumentNullException(nameof(processorQueue));
this.observerAttributeReference = resolvedTypes["Cortex.Net.Blazor.ObserverAttribute"];
this.observerObjectReference = resolvedTypes["Cortex.Net.Blazor.ObserverObject"];
}

/// <summary>
Expand All @@ -78,7 +94,7 @@ internal void Execute()
t.IsClass &&
t.BaseType != null &&
t.CustomAttributes != null &&
t.CustomAttributes.Any(x => x.AttributeType.FullName == typeof(ObserverAttribute).FullName)
t.CustomAttributes.Any(x => x.AttributeType.FullName == this.observerAttributeReference.FullName)
select t;

foreach (var decoratedClass in decoratedClasses.ToList())
Expand All @@ -98,7 +114,7 @@ internal void Execute()
}

/// <summary>
/// Weave the <see cref="ComponentBase" /> derived class.
/// Weave the ComponentBase derived class.
/// </summary>
/// <param name="decoratedClass">The derived class to weave.</param>
private void WeaveClass(TypeDefinition decoratedClass)
Expand All @@ -125,12 +141,12 @@ private void WeaveClass(TypeDefinition decoratedClass)
}
}

var observerObjectType = module.ImportReference(typeof(ObserverObject));
var observerObjectType = this.observerObjectReference;
var innerObserverObjectField = decoratedType.CreateField(observerObjectType, InnerObserverObjectFieldName);

// observerName name
var observerName = decoratedClass.Name;
var observerAttribute = decoratedClass.CustomAttributes.SingleOrDefault(x => x.AttributeType.FullName == typeof(ObserverAttribute).FullName);
var observerAttribute = decoratedClass.CustomAttributes.SingleOrDefault(x => x.AttributeType.FullName == this.observerAttributeReference.FullName);

if (observerAttribute != null)
{
Expand Down Expand Up @@ -221,7 +237,7 @@ private void EmitObserverObjectInit(ILProcessor processor, string observerName,
{
var module = this.parentWeaver.ModuleDefinition;

var observableObjectConstructor = module.ImportReference(this.parentWeaver.ModuleDefinition.ImportReference(typeof(ObserverObject)).Resolve().Methods.Single(x => x.IsConstructor));
var observableObjectConstructor = module.ImportReference(this.parentWeaver.ModuleDefinition.ImportReference(this.observerObjectReference).Resolve().Methods.Single(x => x.IsConstructor));
var buildRenderTreeReference = module.ImportReference(buildRenderTreeMethod);
var stateChangedReference = module.ImportReference(stateChangedMethod);

Expand Down Expand Up @@ -257,7 +273,7 @@ private void EmitObserverObjectInit(ILProcessor processor, string observerName,
}

/// <summary>
/// Weaves the <see cref="ComponentBase.BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder)" /> Method.
/// Weaves the BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder) Method.
/// </summary>
/// <param name="buildRenderTreeMethod">The method definition of the method to weave.</param>
/// <param name="observerObjectType">The type of the inner observer object.</param>
Expand Down
Loading

0 comments on commit ea49a2f

Please sign in to comment.