Skip to content

Commit

Permalink
fix(tooling): use AppContext to set EnableUnsafeBinaryFormatterSerial…
Browse files Browse the repository at this point in the history
…ization (#1349)
  • Loading branch information
ricaun authored Jul 14, 2024
1 parent 8dd7fe4 commit 5be064d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
1 change: 0 additions & 1 deletion source/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<DebugType>embedded</DebugType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>

<PropertyGroup>
Expand Down
1 change: 0 additions & 1 deletion source/Nuke.Common/Nuke.Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<MSBuildWarningsAsErrors>$(MSBuildWarningsAsErrors);CS8785</MSBuildWarningsAsErrors>
<NoWarn>$(NoWarn);SYSLIB0050;SYSLIB0051</NoWarn>
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>

</Project>
31 changes: 31 additions & 0 deletions source/Nuke.Tooling.Tests/NewInstanceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2023 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using FluentAssertions;
using Nuke.Common.Tooling;
using System;
using Xunit;

namespace Nuke.Common.Tests;

public class NewInstanceTest
{
[Serializable]
public class SimpleEntity : ISettingsEntity
{
public int Integer { get; set; }
public string String { get; set; }
}

[Fact]
public void TestSimpleEntity()
{
var entity = new SimpleEntity { Integer = 1, String = "test" };
var newInstance = entity.NewInstance();

newInstance.Integer.Should().Be(entity.Integer);
newInstance.String.Should().Be(entity.String);
}

}
4 changes: 2 additions & 2 deletions source/Nuke.Tooling.Tests/Nuke.Tooling.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
13 changes: 8 additions & 5 deletions source/Nuke.Tooling/SettingsEntity.NewInstance.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Maintainers of NUKE.
// Copyright 2024 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

Expand All @@ -7,6 +7,7 @@
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using JetBrains.Annotations;

#pragma warning disable SYSLIB0011

namespace Nuke.Common.Tooling;
Expand All @@ -17,18 +18,20 @@ public static partial class SettingsEntityExtensions
public static T NewInstance<T>(this T settingsEntity)
where T : ISettingsEntity
{
AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", isEnabled: true);

var binaryFormatter = new BinaryFormatter();

using var memoryStream = new MemoryStream();
binaryFormatter.Serialize(memoryStream, settingsEntity);
memoryStream.Seek(offset: 0, loc: SeekOrigin.Begin);

var newInstance = (T) binaryFormatter.Deserialize(memoryStream);
var newInstance = (T)binaryFormatter.Deserialize(memoryStream);
if (newInstance is ToolSettings toolSettings)
{
toolSettings.ProcessArgumentConfigurator = ((ToolSettings) (object) settingsEntity).ProcessArgumentConfigurator;
toolSettings.ProcessLogger = ((ToolSettings) (object) settingsEntity).ProcessLogger;
toolSettings.ProcessExitHandler = ((ToolSettings) (object) settingsEntity).ProcessExitHandler;
toolSettings.ProcessArgumentConfigurator = ((ToolSettings)(object)settingsEntity).ProcessArgumentConfigurator;
toolSettings.ProcessLogger = ((ToolSettings)(object)settingsEntity).ProcessLogger;
toolSettings.ProcessExitHandler = ((ToolSettings)(object)settingsEntity).ProcessExitHandler;
}

return newInstance;
Expand Down

0 comments on commit 5be064d

Please sign in to comment.