Skip to content

Commit

Permalink
Merge pull request #539 from Shane32/trimmable
Browse files Browse the repository at this point in the history
Make QRCoder trimmable and add trimming tests
  • Loading branch information
codebude authored May 28, 2024
2 parents 6b7311b + 535f927 commit 8bfcf40
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 13 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/wf-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ jobs:
- name: Run test .NET 6.0 Windows
working-directory: QRCoderTests
run: dotnet test -c Release -f net6.0-windows --nologo --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=opencover


- name: Run trim analysis
working-directory: QRCoderTrimAnalysis
run: dotnet publish -c Release -o bin/publish

- name: Run API approval tests
working-directory: QRCoderApiTests
run: dotnet test -c Release --nologo --no-build
Expand Down
22 changes: 20 additions & 2 deletions QRCoder.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoderTests", "QRCoderTest
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoder.Xaml", "QRCoder.Xaml\QRCoder.Xaml.csproj", "{A7A7E073-2504-4BA2-A63B-87AC34174789}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoderApiTests", "QRCoderApiTests\QRCoderApiTests.csproj", "{5FACE5F6-53C9-4B89-91D4-162677893574}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoderApiTests", "QRCoderApiTests\QRCoderApiTests.csproj", "{5FACE5F6-53C9-4B89-91D4-162677893574}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoderBenchmarks", "QRCoderBenchmarks\QRCoderBenchmarks.csproj", "{C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoderBenchmarks", "QRCoderBenchmarks\QRCoderBenchmarks.csproj", "{C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoderTrimAnalysis", "QRCoderTrimAnalysis\QRCoderTrimAnalysis.csproj", "{F046136A-7BEA-49F3-9415-70CE50AD533B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -155,6 +157,22 @@ Global
{C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}.Release|x64.Build.0 = Release|Any CPU
{C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}.Release|x86.ActiveCfg = Release|Any CPU
{C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}.Release|x86.Build.0 = Release|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|ARM.ActiveCfg = Debug|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|ARM.Build.0 = Debug|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|x64.ActiveCfg = Debug|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|x64.Build.0 = Debug|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|x86.ActiveCfg = Debug|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|x86.Build.0 = Debug|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|Any CPU.Build.0 = Release|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|ARM.ActiveCfg = Release|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|ARM.Build.0 = Release|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|x64.ActiveCfg = Release|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|x64.Build.0 = Release|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|x86.ActiveCfg = Release|Any CPU
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 6 additions & 0 deletions QRCoder/Extensions/StringValueAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
Expand All @@ -9,6 +10,7 @@ namespace QRCoder.Extensions
/// <summary>
/// Used to represent a string value for a value in an enum
/// </summary>
[Obsolete("This attribute will be removed in a future version of QRCoder.")]
public class StringValueAttribute : Attribute
{

Expand All @@ -31,13 +33,17 @@ public StringValueAttribute(string value)
}
}

[Obsolete("This class will be removed in a future version of QRCoder.")]
public static class CustomExtensions
{
/// <summary>
/// Will get the string value for a given enum's value
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("This method uses reflection to examine the provided enum value.")]
#endif
public static string GetStringValue(this Enum value)
{
#if NETSTANDARD1_3
Expand Down
15 changes: 7 additions & 8 deletions QRCoder/PayloadGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics.CodeAnalysis;
#if NETSTANDARD1_3
using System.Reflection;
#endif
Expand Down Expand Up @@ -2477,14 +2478,12 @@ public class RussiaPaymentOrder : Payload

//base
private CharacterSets characterSet;
private MandatoryFields mFields;
private OptionalFields oFields;
private readonly MandatoryFields mFields = new MandatoryFields();
private readonly OptionalFields oFields = new OptionalFields();
private string separator = "|";

private RussiaPaymentOrder()
{
mFields = new MandatoryFields();
oFields = new OptionalFields();
}

/// <summary>
Expand Down Expand Up @@ -2596,7 +2595,7 @@ private string DetermineSeparator()
private List<string> GetOptionalFieldsAsList()
{
#if NETSTANDARD1_3
return oFields.GetType().GetRuntimeProperties()
return typeof(OptionalFields).GetRuntimeProperties()
.Where(field => field.GetValue(oFields) != null)
.Select(field => {
var objValue = field.GetValue(oFields, null);
Expand All @@ -2605,7 +2604,7 @@ private List<string> GetOptionalFieldsAsList()
})
.ToList();
#else
return oFields.GetType().GetProperties()
return typeof(OptionalFields).GetProperties()
.Where(field => field.GetValue(oFields, null) != null)
.Select(field => {
var objValue = field.GetValue(oFields, null);
Expand All @@ -2624,7 +2623,7 @@ private List<string> GetOptionalFieldsAsList()
private List<string> GetMandatoryFieldsAsList()
{
#if NETSTANDARD1_3
return mFields.GetType().GetRuntimeFields()
return typeof(MandatoryFields).GetRuntimeFields()
.Where(field => field.GetValue(mFields) != null)
.Select(field => {
var objValue = field.GetValue(mFields);
Expand All @@ -2633,7 +2632,7 @@ private List<string> GetMandatoryFieldsAsList()
})
.ToList();
#else
return mFields.GetType().GetFields()
return typeof(MandatoryFields).GetFields()
.Where(field => field.GetValue(mFields) != null)
.Select(field => {
var objValue = field.GetValue(mFields);
Expand Down
1 change: 1 addition & 0 deletions QRCoder/QRCoder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<DefineConstants Condition="'$(TargetFramework)' == 'net6.0-windows'">$(DefineConstants);NET6_0_WINDOWS</DefineConstants>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
22 changes: 20 additions & 2 deletions QRCoder/SvgQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public MediaType GetMediaType()
/// <returns></returns>
public string GetDataUri()
{
return $"data:{_mediaType.GetStringValue()};base64,{_logoData}";
return $"data:{GetMimeType(_mediaType)};base64,{_logoData}";
}

/// <summary>
Expand All @@ -388,11 +388,29 @@ public bool FillLogoBackground()
/// </summary>
public enum MediaType : int
{
#pragma warning disable CS0618 // Type or member is obsolete
[StringValue("image/png")]
PNG = 0,
#pragma warning restore CS0618 // Type or member is obsolete
PNG = 0,
#pragma warning disable CS0618 // Type or member is obsolete
[StringValue("image/svg+xml")]
#pragma warning restore CS0618 // Type or member is obsolete
SVG = 1
}

private string GetMimeType(MediaType type)
{
switch (type)
{
case MediaType.PNG:
return "image/png";
case MediaType.SVG:
return "image/svg+xml";
default:
throw new ArgumentOutOfRangeException(nameof(type));
}
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -967,10 +967,12 @@ namespace QRCoder.Exceptions
}
namespace QRCoder.Extensions
{
[System.Obsolete("This class will be removed in a future version of QRCoder.")]
public static class CustomExtensions
{
public static string GetStringValue(this System.Enum value) { }
}
[System.Obsolete("This attribute will be removed in a future version of QRCoder.")]
public class StringValueAttribute : System.Attribute
{
public StringValueAttribute(string value) { }
Expand Down
3 changes: 3 additions & 0 deletions QRCoderApiTests/net60-windows/QRCoder.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -977,10 +977,13 @@ namespace QRCoder.Exceptions
}
namespace QRCoder.Extensions
{
[System.Obsolete("This class will be removed in a future version of QRCoder.")]
public static class CustomExtensions
{
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection to examine the provided enum value.")]
public static string GetStringValue(this System.Enum value) { }
}
[System.Obsolete("This attribute will be removed in a future version of QRCoder.")]
public class StringValueAttribute : System.Attribute
{
public StringValueAttribute(string value) { }
Expand Down
3 changes: 3 additions & 0 deletions QRCoderApiTests/net60/QRCoder.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -904,10 +904,13 @@ namespace QRCoder.Exceptions
}
namespace QRCoder.Extensions
{
[System.Obsolete("This class will be removed in a future version of QRCoder.")]
public static class CustomExtensions
{
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection to examine the provided enum value.")]
public static string GetStringValue(this System.Enum value) { }
}
[System.Obsolete("This attribute will be removed in a future version of QRCoder.")]
public class StringValueAttribute : System.Attribute
{
public StringValueAttribute(string value) { }
Expand Down
2 changes: 2 additions & 0 deletions QRCoderApiTests/netstandard13/QRCoder.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -831,10 +831,12 @@ namespace QRCoder.Exceptions
}
namespace QRCoder.Extensions
{
[System.Obsolete("This class will be removed in a future version of QRCoder.")]
public static class CustomExtensions
{
public static string GetStringValue(this System.Enum value) { }
}
[System.Obsolete("This attribute will be removed in a future version of QRCoder.")]
public class StringValueAttribute : System.Attribute
{
public StringValueAttribute(string value) { }
Expand Down
2 changes: 2 additions & 0 deletions QRCoderTrimAnalysis/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
21 changes: 21 additions & 0 deletions QRCoderTrimAnalysis/QRCoderTrimAnalysis.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<!-- This project, when published, verifies that QRCoder is trimmable; see below link for more details. -->
<!-- https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming -->

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishTrimmed>true</PublishTrimmed>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors><!-- Ensure that trim warnings cause the publish to fail -->
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\QRCoder\QRCoder.csproj" />
<TrimmerRootAssembly Include="QRCoder" />
</ItemGroup>

</Project>

0 comments on commit 8bfcf40

Please sign in to comment.