Skip to content

Commit

Permalink
chore: use source generated polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
latonz committed Aug 3, 2023
1 parent de36cdd commit 98518a1
Show file tree
Hide file tree
Showing 31 changed files with 100 additions and 491 deletions.
6 changes: 6 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@
<Authors>Mapperly Contributors, Lars Tönz, Manuel Allenspach</Authors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Polyfill" Version="1.0.21">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
1 change: 0 additions & 1 deletion benchmarks/Riok.Mapperly.Benchmarks/MappingBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace Riok.Mapperly.Benchmarks;

[MemoryDiagnoser]
[InProcess]
[HtmlExporter]
[JsonExporterAttribute.Full]
public class MappingBenchmarks
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Riok.Mapperly\Riok.Mapperly.csproj" />
<ProjectReference Include="..\..\src\Riok.Mapperly\Riok.Mapperly.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\Riok.Mapperly.Abstractions\Riok.Mapperly.Abstractions.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
<ProjectReference Include="..\..\src\Riok.Mapperly\Riok.Mapperly.csproj">
<SetTargetFramework>TargetFramework=netstandard2.0</SetTargetFramework>
</ProjectReference>
<ProjectReference Include="..\..\src\Riok.Mapperly\Riok.Mapperly.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false">
<SetTargetFramework>TargetFramework=netstandard2.0</SetTargetFramework>
</ProjectReference>
<ProjectReference Include="..\..\src\Riok.Mapperly.Abstractions\Riok.Mapperly.Abstractions.csproj">
<SetTargetFramework>TargetFramework=netstandard2.0</SetTargetFramework>
</ProjectReference>
<ProjectReference Include="..\..\test\Riok.Mapperly.IntegrationTests\Riok.Mapperly.IntegrationTests.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Riok.Mapperly.Benchmarks;

[MemoryDiagnoser]
[InProcess]
[HtmlExporter]
[JsonExporterAttribute.Full]
public class SourceGeneratorBenchmarks
{
Expand Down
1 change: 1 addition & 0 deletions build/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ for roslyn_version in "${roslyn_versions[@]}"; do
dotnet pack \
"${script_dir}/../src/Riok.Mapperly" \
-c Release \
/p:TargetFrameworks="netstandard2.0" \
/p:ROSLYN_VERSION="${roslyn_version}" \
-o "${artifacts_dir}/roslyn-${roslyn_version}" \
/p:Version="${RELEASE_VERSION}" \
Expand Down
12 changes: 1 addition & 11 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net7.0</TargetFrameworks>
<DebugType>embedded</DebugType>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
Expand All @@ -15,22 +15,12 @@
<LangVersion>11</LangVersion>
</PropertyGroup>


<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Riok.Mapperly.IntegrationTests</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(MSBuildProjectName).Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<!-- nullables for netstandard2.0 -->
<!-- see https://www.meziantou.net/how-to-use-nullable-reference-types-in-dotnet-standard-2-0-and-dotnet-.htm -->
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)/Polyfills/*.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.80">
Expand Down
16 changes: 0 additions & 16 deletions src/Polyfills/IsExternalInit.cs

This file was deleted.

212 changes: 0 additions & 212 deletions src/Polyfills/Nullable.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private ReferenceHolder GetReferenceHolder<TSource, TTarget>()

private class ReferenceHolder
{
private readonly Dictionary<object, object> _references = new(ReferenceEqualityComparer<object>.Instance);
private readonly Dictionary<object, object> _references = new(ReferenceEqualityComparer.Instance);

public bool TryGetRef<TSource, TTarget>(TSource source, [NotNullWhen(true)] out TTarget? target)
where TSource : notnull
Expand Down

This file was deleted.

8 changes: 6 additions & 2 deletions src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public T AccessSingle<T>(ISymbol symbol)
where T : Attribute => Access<T, T>(symbol).Single();

public TData? AccessFirstOrDefault<TAttribute, TData>(ISymbol symbol)
where TAttribute : Attribute => Access<TAttribute, TData>(symbol).FirstOrDefault();
where TAttribute : Attribute
where TData : notnull => Access<TAttribute, TData>(symbol).FirstOrDefault();

public IEnumerable<TAttribute> Access<TAttribute>(ISymbol symbol)
where TAttribute : Attribute => Access<TAttribute, TAttribute>(symbol);
Expand All @@ -39,6 +40,7 @@ public IEnumerable<TAttribute> Access<TAttribute>(ISymbol symbol)
/// <exception cref="InvalidOperationException">If a property or ctor argument of <see cref="TData"/> could not be read on the attribute.</exception>
public IEnumerable<TData> Access<TAttribute, TData>(ISymbol symbol)
where TAttribute : Attribute
where TData : notnull
{
var attrType = typeof(TAttribute);
var dataType = typeof(TData);
Expand All @@ -64,6 +66,7 @@ public IEnumerable<TData> Access<TAttribute, TData>(ISymbol symbol)
}

private TData Create<TData>(IReadOnlyCollection<ITypeSymbol> typeArguments, IReadOnlyCollection<TypedConstant> constructorArguments)
where TData : notnull
{
// The data class should have a constructor
// with generic type parameters of the attribute class
Expand All @@ -81,7 +84,8 @@ private TData Create<TData>(IReadOnlyCollection<ITypeSymbol> typeArguments, IRea
(arg, i) => BuildArgumentValue(arg, parameters[i + typeArguments.Count].ParameterType)
);
var constructorTypeAndValueArguments = typeArguments.Concat(constructorArgumentValues).ToArray();
return (TData)Activator.CreateInstance(typeof(TData), constructorTypeAndValueArguments);
return (TData?)Activator.CreateInstance(typeof(TData), constructorTypeAndValueArguments)
?? throw new InvalidOperationException($"Could not create instance of {typeof(TData)}");
}

throw new InvalidOperationException($"{typeof(TData)} does not have a constructor with {argCount} parameters");
Expand Down
Loading

0 comments on commit 98518a1

Please sign in to comment.