Skip to content

Commit

Permalink
feat: support roslyn 4.5 (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
latonz authored Mar 13, 2023
1 parent d0d1c2e commit 56c5aca
Show file tree
Hide file tree
Showing 25 changed files with 975 additions and 28 deletions.
2 changes: 1 addition & 1 deletion build/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -Eeuo pipefail
# pack a nupkg for each roslyn version that is supported by Mapperly
# and merge them together into one nupkg

roslyn_versions=('4.0' '4.4')
roslyn_versions=('4.0' '4.4' '4.5')

RELEASE_VERSION=${RELEASE_VERSION:-'0.0.1-dev'}
RELEASE_NOTES=${RELEASE_NOTES:-''}
Expand Down
7 changes: 5 additions & 2 deletions docs/docs/98-contributing/01-architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ See `build/package.sh` for details.
2. Create a new file `Riok.Mapperly.Roslyn$(Version).props` in `src/Riok.Mapperly` similar to the existing ones
and define constants as needed.
3. Update the default `ROSLYN_VERSION` in `src/Riok.Mapperly/Riok.Mapperly.csproj`.
4. Adjust the .NET version matrix of the `integration-test` GitHub Actions job (defined in `.github/workflows/test.yml`)
4. Update the `Microsoft.CodeAnalysis.CSharp` dependency version in `test/Riok.Mapperly.Tests/Riok.Mapperly.Tests.csproj`.
5. Adjust the .NET version matrix of the `integration-test` GitHub Actions job (defined in `.github/workflows/test.yml`)
to include a dotnet version which is based on the added Roslyn version.
5. Adjust the .NET version in the `global.json` file as needed.
6. Adjust the .NET version in the `global.json` file as needed.
7. If generated code changes based on the new Roslyn version,
introduce a new `roslynVersionName` in `Riok.Mapperly.IntegrationTests.BaseMapperTest.GetRoslynVersion()` and generate the new snapshots.
3 changes: 3 additions & 0 deletions docs/docs/98-contributing/02-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ These tests run locally by referencing the source generator as an analyzer.
In the CI pipeline, the integration tests reference the built NuGet package and
are run on several supported target frameworks (including .NET 7.0 but also .NET Framework).

Snapshots of the generated code are stored per Roslyn version.
You may need to manually update older versions.

Debugging integration tests is a lot harder than debugging unit tests.
Therefore if an integration test needs to be debugged,
it is often easier to implement an unit test for the to be tested behaviour
Expand Down
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.100",
"rollForward": "minor"
"version": "7.0.200",
"rollForward": "latestPatch"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private static bool BuildParameters(
method.ContainingType.ToDisplayString(),
method.Name,
ctx.Types.IReferenceHandler.ToDisplayString(),
refHandlerParameterSymbol.ToDisplayString());
refHandlerParameterSymbol.Type.ToDisplayString());
}

if (!ctx.MapperConfiguration.UseReferenceHandling)
Expand Down
11 changes: 11 additions & 0 deletions src/Riok.Mapperly/Riok.Mapperly.Roslyn4.5.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project>

<PropertyGroup>
<DefineConstants>$(DefineConstants);ROSLYN4_4_OR_GREATER</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" PrivateAssets="all" />
</ItemGroup>

</Project>
3 changes: 1 addition & 2 deletions src/Riok.Mapperly/Riok.Mapperly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageIcon>logo.png</PackageIcon>

<!-- default roslyn version -->
<ROSLYN_VERSION Condition="'$(ROSLYN_VERSION)' == ''">4.4</ROSLYN_VERSION>
<ROSLYN_VERSION Condition="'$(ROSLYN_VERSION)' == ''">4.5</ROSLYN_VERSION>
</PropertyGroup>

<Import Project="Riok.Mapperly.Roslyn$(ROSLYN_VERSION).props" />
Expand All @@ -44,5 +44,4 @@
<None Include="$(OutputPath)\$(AssemblyName).Abstractions.dll" Pack="true" PackagePath="lib\netstandard2.0" Visible="true" />
<None Include="$(OutputPath)\$(AssemblyName).Abstractions.xml" Pack="true" PackagePath="lib\netstandard2.0" Visible="true" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion test/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<ItemGroup>
<Compile Remove="_snapshots\*"/>
<Compile Remove="_snapshots\**"/>
</ItemGroup>
</Project>
17 changes: 13 additions & 4 deletions test/Riok.Mapperly.IntegrationTests/BaseMapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
using System.IO;
using System.Runtime.CompilerServices;
using Riok.Mapperly.IntegrationTests.Dto;
#if !NET6_0_OR_GREATER
using Riok.Mapperly.IntegrationTests.Helpers;
#endif
using Riok.Mapperly.IntegrationTests.Models;
using VerifyTests;
using VerifyXunit;
#if !NET6_0_OR_GREATER
using Riok.Mapperly.IntegrationTests.Helpers;
#endif

namespace Riok.Mapperly.IntegrationTests
{
Expand All @@ -26,7 +26,7 @@ static BaseMapperTest()
VerifierSettings.DontScrubDateTimes();

Verifier.DerivePathInfo((file, _, type, method)
=> new PathInfo(Path.Combine(Path.GetDirectoryName(file)!, "_snapshots"), type.Name, method.Name));
=> new PathInfo(Path.Combine(Path.GetDirectoryName(file)!, "_snapshots", "Roslyn_" + GetRoslynVersion()), type.Name, method.Name));
}

protected string GetGeneratedMapperFilePath(string name, [CallerFilePath] string filePath = "")
Expand Down Expand Up @@ -78,5 +78,14 @@ protected TestObject NewTestObj()
SourceTargetSameObjectType = new TestObject(8) { IntValue = 99, RequiredValue = 98, }
};
}

private static string GetRoslynVersion()
{
#if NET7_0_OR_GREATER || NET48_OR_GREATER
return "4_5";
#else
return "4_4_OR_LOWER";
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@
<ItemGroup Condition="'$(MapperlyIntegrationTestsTargetFramework)' == 'net48'">
<PackageReference Include="Portable.System.DateTimeOnly" Version="7.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,4 @@ private Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject MapToInherita
return target;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,4 @@ private static Riok.Mapperly.IntegrationTests.Models.InheritanceSubObject MapToI
return target;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#nullable enable
namespace Riok.Mapperly.IntegrationTests.Mapper
{
public static partial class CircularReferenceMapper
{
public static partial Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto ToDto(Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject obj)
{
return MapToCircularReferenceDto(obj, new Riok.Mapperly.Abstractions.ReferenceHandling.Internal.PreserveReferenceHandler());
}

private static Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto MapToCircularReferenceDto(Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject source, Riok.Mapperly.Abstractions.ReferenceHandling.IReferenceHandler refHandler)
{
if (refHandler.TryGetReference<Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject, Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto>(source, out var existingTargetReference))
return existingTargetReference;
var target = new Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto();
refHandler.SetReference<Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject, Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto>(source, target);
if (source.Parent != null)
{
target.Parent = MapToCircularReferenceDto(source.Parent, refHandler);
}

target.Value = source.Value;
return target;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
CtorValue: 7,
CtorValue2: 100,
IntValue: 10,
IntInitOnlyValue: 3,
RequiredValue: 4,
StringValue: fooBar+after-map,
RenamedStringValue2: fooBar2,
FlatteningIdValue: 10,
NullableFlatteningIdValue: 100,
Unflattening: {
IdValue: 20
},
NullableUnflattening: {
IdValue: 200
},
NestedNullableIntValue: 100,
NestedNullable: {
IntValue: 100
},
NestedNullableTargetNotNullable: {},
StringNullableTargetNotNullable: fooBar3,
RecursiveObject: {
CtorValue: 5,
CtorValue2: 100,
RequiredValue: 4,
StringValue: +after-map,
RenamedStringValue2: ,
Unflattening: {},
NestedNullableTargetNotNullable: {},
StringNullableTargetNotNullable: ,
EnumValue: DtoValue1,
EnumName: Value30,
EnumStringValue: 0,
EnumReverseStringValue: DtoValue3
},
SourceTargetSameObjectType: {
CtorValue: 8,
CtorValue2: 100,
IntValue: 99,
RequiredValue: 98,
StringValue: ,
RenamedStringValue: ,
Flattening: {},
EnumReverseStringValue:
},
NullableReadOnlyObjectCollection: [
{
IntValue: 10
},
{
IntValue: 20
}
],
EnumValue: DtoValue1,
EnumName: Value10,
EnumRawValue: 20,
EnumStringValue: Value30,
EnumReverseStringValue: DtoValue3,
SubObject: {
SubIntValue: 2,
BaseIntValue: 1
},
DateTimeValueTargetDateOnly: 2020-01-03,
DateTimeValueTargetTimeOnly: 3:10 PM
}
Loading

0 comments on commit 56c5aca

Please sign in to comment.