Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .github/workflows/pr-gate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Setup .NET Core 8
- name: Setup .NET Core 10
uses: actions/setup-dotnet@v1
with:
dotnet-version: 8.0.x
dotnet-version: 10.0.x
- name: Install dependencies
run: dotnet restore
- name: Build
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-nugets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ jobs:
echo Version number extracted from ${GITHUB_REF} is invalid
exit 1
- uses: actions/checkout@v2
- name: Setup .NET Core 8
- name: Setup .NET Core 10
uses: actions/setup-dotnet@v1
with:
dotnet-version: 8.0.x
dotnet-version: 10.0.x
- name: Install dependencies
run: dotnet restore
- name: Build
Expand Down
7 changes: 0 additions & 7 deletions Buildenator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Buildenator.IntegrationTest
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Buildenator.IntegrationTests.SharedEntities", "Tests\Buildenator.IntegrationTests.SharedEntities\Buildenator.IntegrationTests.SharedEntities.csproj", "{A9E79A1B-8E38-4815-AC1A-129CCE999BE1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Buildenator.UnitTests", "Tests\Buildenator.UnitTests\Buildenator.UnitTests.csproj", "{635AFFCA-8ACF-4F92-924C-FB7463D17C3C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Buildenator.IntegrationTests.SourceNullable", "Tests\Buildenator.IntegrationTests.SourceNullable\Buildenator.IntegrationTests.SourceNullable.csproj", "{5F93F84C-D4C2-43DD-B979-083BD7951FF7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Buildenator.IntegrationTests.SharedEntitiesNullable", "Tests\Buildenator.IntegrationTests.SharedEntitiesNullable\Buildenator.IntegrationTests.SharedEntitiesNullable.csproj", "{0AA5BA5D-7119-4E80-8943-F29EA495F752}"
Expand Down Expand Up @@ -96,10 +94,6 @@ Global
{A9E79A1B-8E38-4815-AC1A-129CCE999BE1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A9E79A1B-8E38-4815-AC1A-129CCE999BE1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A9E79A1B-8E38-4815-AC1A-129CCE999BE1}.Release|Any CPU.Build.0 = Release|Any CPU
{635AFFCA-8ACF-4F92-924C-FB7463D17C3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{635AFFCA-8ACF-4F92-924C-FB7463D17C3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{635AFFCA-8ACF-4F92-924C-FB7463D17C3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{635AFFCA-8ACF-4F92-924C-FB7463D17C3C}.Release|Any CPU.Build.0 = Release|Any CPU
{5F93F84C-D4C2-43DD-B979-083BD7951FF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5F93F84C-D4C2-43DD-B979-083BD7951FF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F93F84C-D4C2-43DD-B979-083BD7951FF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -120,7 +114,6 @@ Global
{7023762A-B29A-4A4E-888E-5D6A54E72D4E} = {34D14563-E89B-4337-B751-C50D9149C827}
{63A6F9E6-56D5-4AF9-8725-54E2EC09E7F4} = {34D14563-E89B-4337-B751-C50D9149C827}
{A9E79A1B-8E38-4815-AC1A-129CCE999BE1} = {34D14563-E89B-4337-B751-C50D9149C827}
{635AFFCA-8ACF-4F92-924C-FB7463D17C3C} = {34D14563-E89B-4337-B751-C50D9149C827}
{5F93F84C-D4C2-43DD-B979-083BD7951FF7} = {34D14563-E89B-4337-B751-C50D9149C827}
{0AA5BA5D-7119-4E80-8943-F29EA495F752} = {34D14563-E89B-4337-B751-C50D9149C827}
EndGlobalSection
Expand Down
43 changes: 43 additions & 0 deletions Buildenator/AssemblyPropertiesWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.CodeAnalysis;
#if DEBUG
#endif
using System.Linq;
using System.Collections.Immutable;
using System;

namespace Buildenator;

internal sealed class AssemblyPropertiesWrapper(
ImmutableArray<TypedConstant>? globalFixtureProperties,
ImmutableArray<TypedConstant>? mockingConfigurationBuilder,
ImmutableArray<TypedConstant>? globalBuilderProperties) : IEquatable<AssemblyPropertiesWrapper>
{
public readonly ImmutableArray<TypedConstant>? GlobalFixtureProperties = globalFixtureProperties;
public readonly ImmutableArray<TypedConstant>? MockingConfigurationBuilder = mockingConfigurationBuilder;
public readonly ImmutableArray<TypedConstant>? GlobalBuilderProperties = globalBuilderProperties;

public override bool Equals(object? obj) => obj is AssemblyPropertiesWrapper other && Equals(other);

public override int GetHashCode()
{
int hashCode = 874553126;
hashCode = hashCode * -1521134295 + GlobalFixtureProperties?.GetHashCode() ?? 0;
hashCode = hashCode * -1521134295 + MockingConfigurationBuilder?.GetHashCode() ?? 0;
hashCode = hashCode * -1521134295 + GlobalBuilderProperties?.GetHashCode() ?? 0;
return hashCode;
}

public void Deconstruct(out ImmutableArray<TypedConstant>? globalFixtureProperties, out ImmutableArray<TypedConstant>? mockingConfigurationBuilder, out ImmutableArray<TypedConstant>? globalBuilderProperties)
{
globalFixtureProperties = GlobalFixtureProperties;
mockingConfigurationBuilder = MockingConfigurationBuilder;
globalBuilderProperties = GlobalBuilderProperties;
}

public bool Equals(AssemblyPropertiesWrapper other)
{
return (GlobalFixtureProperties?.SequenceEqual(other.GlobalFixtureProperties ?? []) ?? (GlobalFixtureProperties is null && other.GlobalFixtureProperties is null))
&& (MockingConfigurationBuilder?.SequenceEqual(other.MockingConfigurationBuilder ?? []) ?? (MockingConfigurationBuilder is null && other.MockingConfigurationBuilder is null))
&& (GlobalBuilderProperties?.SequenceEqual(other.GlobalBuilderProperties ?? []) ?? (GlobalBuilderProperties is null && other.GlobalBuilderProperties is null));
}
}
4 changes: 2 additions & 2 deletions Buildenator/Buildenator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
72 changes: 72 additions & 0 deletions Buildenator/BuilderAttributesWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Microsoft.CodeAnalysis;
using System.Linq;
using System.Collections.Immutable;

namespace Buildenator;

internal sealed class BuilderAttributesWrapper(
INamedTypeSymbol BuilderSymbol,
MakeBuilderAttributeInternal BuilderAttribute,
ImmutableArray<TypedConstant>? MockingAttribute,
ImmutableArray<TypedConstant>? FixtureAttribute) : System.IEquatable<BuilderAttributesWrapper>
{
public INamedTypeSymbol BuilderSymbol { get; } = BuilderSymbol;
public MakeBuilderAttributeInternal BuilderAttribute { get; } = BuilderAttribute;
public ImmutableArray<TypedConstant>? MockingAttribute { get; } = MockingAttribute;
public ImmutableArray<TypedConstant>? FixtureAttribute { get; } = FixtureAttribute;

public bool Equals(BuilderAttributesWrapper other)
{
return
BuilderSymbol.Equals(other.BuilderSymbol, SymbolEqualityComparer.Default)
&& BuilderAttribute.Equals(other.BuilderAttribute)
&& (MockingAttribute?.SequenceEqual(other.MockingAttribute ?? []) ?? (MockingAttribute is null && other.MockingAttribute is null))
&& (FixtureAttribute?.SequenceEqual(other.FixtureAttribute ?? []) ?? (FixtureAttribute is null && other.FixtureAttribute is null));
}

public void Deconstruct(out INamedTypeSymbol BuilderSymbol, out MakeBuilderAttributeInternal BuilderAttribute, out ImmutableArray<TypedConstant>? MockingAttribute, out ImmutableArray<TypedConstant>? FixtureAttribute)
{
BuilderSymbol = this.BuilderSymbol;
BuilderAttribute = this.BuilderAttribute;
MockingAttribute = this.MockingAttribute;
FixtureAttribute = this.FixtureAttribute;
}

public override bool Equals(object? obj) => obj is BuilderAttributesWrapper other && Equals(other);

public override int GetHashCode()
{
unchecked
{
var hash = 17;
hash = (hash * 31) + SymbolEqualityComparer.Default.GetHashCode(BuilderSymbol);
hash = (hash * 31) + BuilderAttribute.GetHashCode();

if (MockingAttribute != null)
{
foreach (var item in MockingAttribute)
{
hash = (hash * 31) + item.GetHashCode();
}
}
else
{
hash *= 31;
}

if (FixtureAttribute != null)
{
foreach (var item in FixtureAttribute)
{
hash = (hash * 31) + item.GetHashCode();
}
}
else
{
hash *= 31;
}

return hash;
}
}
}
49 changes: 16 additions & 33 deletions Buildenator/BuildersGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
transform: static (ctx, _) =>
{
var attributes = ctx.TargetSymbol.GetAttributes();
return (
BuilderSymbol: (INamedTypeSymbol)ctx.TargetSymbol,
BuilderAttribute: new MakeBuilderAttributeInternal(
return new BuilderAttributesWrapper(
(INamedTypeSymbol)ctx.TargetSymbol,
new MakeBuilderAttributeInternal(
attributes.Single(
attributeData => attributeData.AttributeClass?.Name == nameof(MakeBuilderAttribute))),
MockingAttribute: GetMockingConfigurationOrDefault(attributes),
FixtureAttribute: GetLocalFixturePropertiesOrDefault(attributes)
GetMockingConfigurationOrDefault(attributes),
GetLocalFixturePropertiesOrDefault(attributes)
);
}
);
Expand All @@ -57,25 +57,17 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.Select(static (c, _) => c.Assembly)
.Select(static (assembly, _) => assembly.GetAttributes())
.Select(static (attributes, _) =>
(
Mocking: attributes.Where(x =>
new AssemblyPropertiesWrapper(
attributes.Where(x =>
x.AttributeClass.HasNameOrBaseClassHas(nameof(FixtureConfigurationAttribute)))
.FirstOrDefault()?.ConstructorArguments,
attributes.Where(x =>
x.AttributeClass.HasNameOrBaseClassHas(nameof(MockingConfigurationAttribute)))
.FirstOrDefault(),
Builder: attributes.Where(x =>
.FirstOrDefault()?.ConstructorArguments,
attributes.Where(x =>
x.AttributeClass.HasNameOrBaseClassHas(nameof(BuildenatorConfigurationAttribute)))
.FirstOrDefault(),
Fixture: attributes.Where(x =>
x.AttributeClass.HasNameOrBaseClassHas(nameof(FixtureConfigurationAttribute)))
.FirstOrDefault()
))
.Select(static (assembly, _) =>
{
var globalFixtureProperties = assembly.Fixture?.ConstructorArguments;
var mockingConfigurationBuilder = assembly.Mocking?.ConstructorArguments;
var globalBuilderProperties = assembly.Builder?.ConstructorArguments;

return (globalFixtureProperties, mockingConfigurationBuilder, globalBuilderProperties);
});
.FirstOrDefault()?.ConstructorArguments
));

// Collect all builders and their entity types to create a mapping
// This allows us to find child builders for properties when useChildBuilders is enabled
Expand Down Expand Up @@ -121,15 +113,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var builderProperties =
BuilderProperties.Create(builderNamedTypeSymbol, builderAttribute, globalBuilderProperties, nullableOptions.AnnotationsEnabled());

return (fixtureProperties, mockingConfiguration: mockingProperties, builderProperties,
builderAttribute.TypeForBuilder, builderMappings);
})
.Select(static (properties, _) =>
{
var (fixtureProperties, mockingProperties, builderProperties, typeForBuilder, builderMappings) = properties;
return new BuilderSourceStringGenerator(builderProperties,
new EntityToBuild(
typeForBuilder,
builderAttribute.TypeForBuilder,
mockingProperties,
fixtureProperties,
builderProperties.NullableStrategy,
Expand Down Expand Up @@ -166,9 +152,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
});
}

private static (SyntaxTree SyntaxTree, SemanticModel SemanticModel) SelectSyntaxTreeAndSemanticModel((SyntaxTree SyntaxTree, Compilation Compilation) tuple, CancellationToken _)
=> (tuple.SyntaxTree, SemanticModel: tuple.Compilation.GetSemanticModel(tuple.SyntaxTree));

private static ImmutableArray<TypedConstant>? GetLocalFixturePropertiesOrDefault(ImmutableArray<AttributeData> attributeData)
{
var attribute = attributeData.SingleOrDefault(x => x.AttributeClass.HasNameOrBaseClassHas(nameof(FixtureConfigurationAttribute)));
Expand All @@ -179,4 +162,4 @@ private static (SyntaxTree SyntaxTree, SemanticModel SemanticModel) SelectSyntax
attributeData
.SingleOrDefault(x => x.AttributeClass.HasNameOrBaseClassHas(nameof(MockingConfigurationAttribute)))
?.ConstructorArguments;
}
}
1 change: 0 additions & 1 deletion Buildenator/Generators/BuilderSourceStringGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Buildenator.Abstraction;
using Buildenator.CodeAnalysis;
using Buildenator.Configuration.Contract;
using System.Collections.Generic;
using Buildenator.Configuration;
Expand Down
Loading
Loading