Skip to content

Commit 8bfcf40

Browse files
authored
Merge pull request #539 from Shane32/trimmable
Make QRCoder trimmable and add trimming tests
2 parents 6b7311b + 535f927 commit 8bfcf40

File tree

12 files changed

+92
-13
lines changed

12 files changed

+92
-13
lines changed

.github/workflows/wf-build-test.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ jobs:
8686
- name: Run test .NET 6.0 Windows
8787
working-directory: QRCoderTests
8888
run: dotnet test -c Release -f net6.0-windows --nologo --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
89-
89+
90+
- name: Run trim analysis
91+
working-directory: QRCoderTrimAnalysis
92+
run: dotnet publish -c Release -o bin/publish
93+
9094
- name: Run API approval tests
9195
working-directory: QRCoderApiTests
9296
run: dotnet test -c Release --nologo --no-build

QRCoder.sln

+20-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoderTests", "QRCoderTest
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoder.Xaml", "QRCoder.Xaml\QRCoder.Xaml.csproj", "{A7A7E073-2504-4BA2-A63B-87AC34174789}"
1717
EndProject
18-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoderApiTests", "QRCoderApiTests\QRCoderApiTests.csproj", "{5FACE5F6-53C9-4B89-91D4-162677893574}"
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoderApiTests", "QRCoderApiTests\QRCoderApiTests.csproj", "{5FACE5F6-53C9-4B89-91D4-162677893574}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoderBenchmarks", "QRCoderBenchmarks\QRCoderBenchmarks.csproj", "{C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}"
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoderBenchmarks", "QRCoderBenchmarks\QRCoderBenchmarks.csproj", "{C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}"
21+
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoderTrimAnalysis", "QRCoderTrimAnalysis\QRCoderTrimAnalysis.csproj", "{F046136A-7BEA-49F3-9415-70CE50AD533B}"
2123
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -155,6 +157,22 @@ Global
155157
{C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}.Release|x64.Build.0 = Release|Any CPU
156158
{C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}.Release|x86.ActiveCfg = Release|Any CPU
157159
{C33AB74A-2AB3-4BEA-A67F-EAB578E74E25}.Release|x86.Build.0 = Release|Any CPU
160+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
161+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|Any CPU.Build.0 = Debug|Any CPU
162+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|ARM.ActiveCfg = Debug|Any CPU
163+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|ARM.Build.0 = Debug|Any CPU
164+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|x64.ActiveCfg = Debug|Any CPU
165+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|x64.Build.0 = Debug|Any CPU
166+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|x86.ActiveCfg = Debug|Any CPU
167+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Debug|x86.Build.0 = Debug|Any CPU
168+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|Any CPU.ActiveCfg = Release|Any CPU
169+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|Any CPU.Build.0 = Release|Any CPU
170+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|ARM.ActiveCfg = Release|Any CPU
171+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|ARM.Build.0 = Release|Any CPU
172+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|x64.ActiveCfg = Release|Any CPU
173+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|x64.Build.0 = Release|Any CPU
174+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|x86.ActiveCfg = Release|Any CPU
175+
{F046136A-7BEA-49F3-9415-70CE50AD533B}.Release|x86.Build.0 = Release|Any CPU
158176
EndGlobalSection
159177
GlobalSection(SolutionProperties) = preSolution
160178
HideSolutionNode = FALSE

QRCoder/Extensions/StringValueAttribute.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Linq;
45
using System.Reflection;
56
using System.Text;
@@ -9,6 +10,7 @@ namespace QRCoder.Extensions
910
/// <summary>
1011
/// Used to represent a string value for a value in an enum
1112
/// </summary>
13+
[Obsolete("This attribute will be removed in a future version of QRCoder.")]
1214
public class StringValueAttribute : Attribute
1315
{
1416

@@ -31,13 +33,17 @@ public StringValueAttribute(string value)
3133
}
3234
}
3335

36+
[Obsolete("This class will be removed in a future version of QRCoder.")]
3437
public static class CustomExtensions
3538
{
3639
/// <summary>
3740
/// Will get the string value for a given enum's value
3841
/// </summary>
3942
/// <param name="value"></param>
4043
/// <returns></returns>
44+
#if NET6_0_OR_GREATER
45+
[RequiresUnreferencedCode("This method uses reflection to examine the provided enum value.")]
46+
#endif
4147
public static string GetStringValue(this Enum value)
4248
{
4349
#if NETSTANDARD1_3

QRCoder/PayloadGenerator.cs

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Globalization;
55
using System.Text;
66
using System.Text.RegularExpressions;
7+
using System.Diagnostics.CodeAnalysis;
78
#if NETSTANDARD1_3
89
using System.Reflection;
910
#endif
@@ -2477,14 +2478,12 @@ public class RussiaPaymentOrder : Payload
24772478

24782479
//base
24792480
private CharacterSets characterSet;
2480-
private MandatoryFields mFields;
2481-
private OptionalFields oFields;
2481+
private readonly MandatoryFields mFields = new MandatoryFields();
2482+
private readonly OptionalFields oFields = new OptionalFields();
24822483
private string separator = "|";
24832484

24842485
private RussiaPaymentOrder()
24852486
{
2486-
mFields = new MandatoryFields();
2487-
oFields = new OptionalFields();
24882487
}
24892488

24902489
/// <summary>
@@ -2596,7 +2595,7 @@ private string DetermineSeparator()
25962595
private List<string> GetOptionalFieldsAsList()
25972596
{
25982597
#if NETSTANDARD1_3
2599-
return oFields.GetType().GetRuntimeProperties()
2598+
return typeof(OptionalFields).GetRuntimeProperties()
26002599
.Where(field => field.GetValue(oFields) != null)
26012600
.Select(field => {
26022601
var objValue = field.GetValue(oFields, null);
@@ -2605,7 +2604,7 @@ private List<string> GetOptionalFieldsAsList()
26052604
})
26062605
.ToList();
26072606
#else
2608-
return oFields.GetType().GetProperties()
2607+
return typeof(OptionalFields).GetProperties()
26092608
.Where(field => field.GetValue(oFields, null) != null)
26102609
.Select(field => {
26112610
var objValue = field.GetValue(oFields, null);
@@ -2624,7 +2623,7 @@ private List<string> GetOptionalFieldsAsList()
26242623
private List<string> GetMandatoryFieldsAsList()
26252624
{
26262625
#if NETSTANDARD1_3
2627-
return mFields.GetType().GetRuntimeFields()
2626+
return typeof(MandatoryFields).GetRuntimeFields()
26282627
.Where(field => field.GetValue(mFields) != null)
26292628
.Select(field => {
26302629
var objValue = field.GetValue(mFields);
@@ -2633,7 +2632,7 @@ private List<string> GetMandatoryFieldsAsList()
26332632
})
26342633
.ToList();
26352634
#else
2636-
return mFields.GetType().GetFields()
2635+
return typeof(MandatoryFields).GetFields()
26372636
.Where(field => field.GetValue(mFields) != null)
26382637
.Select(field => {
26392638
var objValue = field.GetValue(mFields);

QRCoder/QRCoder.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<DefineConstants Condition="'$(TargetFramework)' == 'net6.0-windows'">$(DefineConstants);NET6_0_WINDOWS</DefineConstants>
99
<CheckEolTargetFramework>false</CheckEolTargetFramework>
1010
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
11+
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
1112
</PropertyGroup>
1213

1314
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">

QRCoder/SvgQRCode.cs

+20-2
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ public MediaType GetMediaType()
362362
/// <returns></returns>
363363
public string GetDataUri()
364364
{
365-
return $"data:{_mediaType.GetStringValue()};base64,{_logoData}";
365+
return $"data:{GetMimeType(_mediaType)};base64,{_logoData}";
366366
}
367367

368368
/// <summary>
@@ -388,11 +388,29 @@ public bool FillLogoBackground()
388388
/// </summary>
389389
public enum MediaType : int
390390
{
391+
#pragma warning disable CS0618 // Type or member is obsolete
391392
[StringValue("image/png")]
392-
PNG = 0,
393+
#pragma warning restore CS0618 // Type or member is obsolete
394+
PNG = 0,
395+
#pragma warning disable CS0618 // Type or member is obsolete
393396
[StringValue("image/svg+xml")]
397+
#pragma warning restore CS0618 // Type or member is obsolete
394398
SVG = 1
395399
}
400+
401+
private string GetMimeType(MediaType type)
402+
{
403+
switch (type)
404+
{
405+
case MediaType.PNG:
406+
return "image/png";
407+
case MediaType.SVG:
408+
return "image/svg+xml";
409+
default:
410+
throw new ArgumentOutOfRangeException(nameof(type));
411+
}
412+
}
413+
396414
}
397415
}
398416

QRCoderApiTests/net35+net40+net50+net50-windows+netstandard20/QRCoder.approved.txt

+2
Original file line numberDiff line numberDiff line change
@@ -967,10 +967,12 @@ namespace QRCoder.Exceptions
967967
}
968968
namespace QRCoder.Extensions
969969
{
970+
[System.Obsolete("This class will be removed in a future version of QRCoder.")]
970971
public static class CustomExtensions
971972
{
972973
public static string GetStringValue(this System.Enum value) { }
973974
}
975+
[System.Obsolete("This attribute will be removed in a future version of QRCoder.")]
974976
public class StringValueAttribute : System.Attribute
975977
{
976978
public StringValueAttribute(string value) { }

QRCoderApiTests/net60-windows/QRCoder.approved.txt

+3
Original file line numberDiff line numberDiff line change
@@ -977,10 +977,13 @@ namespace QRCoder.Exceptions
977977
}
978978
namespace QRCoder.Extensions
979979
{
980+
[System.Obsolete("This class will be removed in a future version of QRCoder.")]
980981
public static class CustomExtensions
981982
{
983+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection to examine the provided enum value.")]
982984
public static string GetStringValue(this System.Enum value) { }
983985
}
986+
[System.Obsolete("This attribute will be removed in a future version of QRCoder.")]
984987
public class StringValueAttribute : System.Attribute
985988
{
986989
public StringValueAttribute(string value) { }

QRCoderApiTests/net60/QRCoder.approved.txt

+3
Original file line numberDiff line numberDiff line change
@@ -904,10 +904,13 @@ namespace QRCoder.Exceptions
904904
}
905905
namespace QRCoder.Extensions
906906
{
907+
[System.Obsolete("This class will be removed in a future version of QRCoder.")]
907908
public static class CustomExtensions
908909
{
910+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection to examine the provided enum value.")]
909911
public static string GetStringValue(this System.Enum value) { }
910912
}
913+
[System.Obsolete("This attribute will be removed in a future version of QRCoder.")]
911914
public class StringValueAttribute : System.Attribute
912915
{
913916
public StringValueAttribute(string value) { }

QRCoderApiTests/netstandard13/QRCoder.approved.txt

+2
Original file line numberDiff line numberDiff line change
@@ -831,10 +831,12 @@ namespace QRCoder.Exceptions
831831
}
832832
namespace QRCoder.Extensions
833833
{
834+
[System.Obsolete("This class will be removed in a future version of QRCoder.")]
834835
public static class CustomExtensions
835836
{
836837
public static string GetStringValue(this System.Enum value) { }
837838
}
839+
[System.Obsolete("This attribute will be removed in a future version of QRCoder.")]
838840
public class StringValueAttribute : System.Attribute
839841
{
840842
public StringValueAttribute(string value) { }

QRCoderTrimAnalysis/Program.cs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// See https://aka.ms/new-console-template for more information
2+
Console.WriteLine("Hello, World!");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<!-- This project, when published, verifies that QRCoder is trimmable; see below link for more details. -->
4+
<!-- https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming -->
5+
6+
<PropertyGroup>
7+
<OutputType>Exe</OutputType>
8+
<TargetFramework>net8.0</TargetFramework>
9+
<ImplicitUsings>enable</ImplicitUsings>
10+
<Nullable>enable</Nullable>
11+
<PublishTrimmed>true</PublishTrimmed>
12+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors><!-- Ensure that trim warnings cause the publish to fail -->
13+
<IsPackable>false</IsPackable>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\QRCoder\QRCoder.csproj" />
18+
<TrimmerRootAssembly Include="QRCoder" />
19+
</ItemGroup>
20+
21+
</Project>

0 commit comments

Comments
 (0)