Skip to content

Commit

Permalink
Made a minor tweak to the trimming (of the build) and fixed a potenti…
Browse files Browse the repository at this point in the history
…onal null bug for any future tests.
  • Loading branch information
metaljase committed Sep 5, 2024
1 parent f588db1 commit a12b705
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
<ApplicationIcon>icon.ico</ApplicationIcon>
<UserSecretsId>5d5dac08-f3c7-46fa-bdf1-5fc675286633</UserSecretsId>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -41,7 +41,7 @@
</ItemGroup>

<ItemGroup>
<_DeploymentManifestIconFile Remove="icon.ico" />
<TrimmerRootAssembly Include="Metalhead.SharesGainLossTracker.ConsoleApp" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 7 additions & 2 deletions Metalhead.SharesGainLossTracker.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@
builder.Services.AddSingleton<IFileStreamFactory, FileStreamFactory>();
builder.Services.AddSingleton<ISharesInputHelperWrapper, SharesInputHelperWrapper>();
builder.Services.AddSingleton<ISharesOutputDataTableHelperWrapper, SharesOutputDataTableHelperWrapper>();
builder.Services.AddSingleton<ISharesOutputHelperWrapper, SharesOutputHelperWrapper>();
builder.Services.AddSingleton<ISharesOutputHelperWrapper, SharesOutputHelperWrapper>();

// Suppress warnings as Metalhead.SharesGainLossTracker.Core has been configured to only trim it's dependencies, therefore IStock should be available.
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
var stockApiSources = Assembly.Load("Metalhead.SharesGainLossTracker.Core")
.GetTypes().Where(type => typeof(IStock).IsAssignableFrom(type) && !type.IsInterface);
.GetTypes().Where(type => typeof(IStock).IsAssignableFrom(type) && !type.IsInterface);
#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
foreach (var stockApiSource in stockApiSources)
{
#pragma warning disable IL2072 // Target parameter argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The return value of the source method does not have matching annotations.
builder.Services.AddSingleton(typeof(IStock), stockApiSource);
#pragma warning restore IL2072 // Target parameter argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The return value of the source method does not have matching annotations.
}

using var host = builder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static Mock<ILogger<T>> VerifyLogging<T>(this Mock<ILogger<T>> logger, Lo
{
times ??= Times.Once();

Func<object, Type, bool> state = (v, t) => v.ToString().StartsWith(expectedMessage);
Func<object, Type, bool> state = (v, t) => v?.ToString()?.StartsWith(expectedMessage) == true;

logger.Verify(
x => x.Log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>partial</TrimMode>
<PublishSingleFile>true</PublishSingleFile>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
Expand All @@ -15,6 +16,10 @@
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<TrimmerRootAssembly Include="Metalhead.SharesGainLossTracker.Core" />
</ItemGroup>

<ItemGroup>
<Folder Include="lib\" />
</ItemGroup>
Expand Down

0 comments on commit a12b705

Please sign in to comment.