From cff2f845efb89d6bee7df815ec1424bcba949991 Mon Sep 17 00:00:00 2001 From: Luiz Henrique Cassettari Date: Thu, 14 Mar 2024 15:23:04 -0300 Subject: [PATCH 1/5] Add Switch and Simple NewInstanceTest --- source/Nuke.Tooling.Tests/NewInstanceTest.cs | 31 +++++++++++++++++++ .../SettingsEntity.NewInstance.cs | 12 ++++--- 2 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 source/Nuke.Tooling.Tests/NewInstanceTest.cs diff --git a/source/Nuke.Tooling.Tests/NewInstanceTest.cs b/source/Nuke.Tooling.Tests/NewInstanceTest.cs new file mode 100644 index 000000000..def17bec9 --- /dev/null +++ b/source/Nuke.Tooling.Tests/NewInstanceTest.cs @@ -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); + } + +} diff --git a/source/Nuke.Tooling/SettingsEntity.NewInstance.cs b/source/Nuke.Tooling/SettingsEntity.NewInstance.cs index 9b129fea6..0cacf3202 100644 --- a/source/Nuke.Tooling/SettingsEntity.NewInstance.cs +++ b/source/Nuke.Tooling/SettingsEntity.NewInstance.cs @@ -2,11 +2,11 @@ // Distributed under the MIT License. // https://github.com/nuke-build/nuke/blob/master/LICENSE +using JetBrains.Annotations; using System; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; -using JetBrains.Annotations; #pragma warning disable SYSLIB0011 namespace Nuke.Common.Tooling; @@ -17,18 +17,20 @@ public static partial class SettingsEntityExtensions public static T NewInstance(this T settingsEntity) where T : ISettingsEntity { + AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", 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; From ef745e52ee0a6ec6ccc2846115ce2de60133d3ed Mon Sep 17 00:00:00 2001 From: Luiz Henrique Cassettari Date: Thu, 14 Mar 2024 15:23:29 -0300 Subject: [PATCH 2/5] Update Nuke.Tooling.Tests to 8.0 --- source/Nuke.Tooling.Tests/Nuke.Tooling.Tests.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/Nuke.Tooling.Tests/Nuke.Tooling.Tests.csproj b/source/Nuke.Tooling.Tests/Nuke.Tooling.Tests.csproj index d2222e881..2267c07f2 100644 --- a/source/Nuke.Tooling.Tests/Nuke.Tooling.Tests.csproj +++ b/source/Nuke.Tooling.Tests/Nuke.Tooling.Tests.csproj @@ -1,7 +1,7 @@ - + - net6.0 + net8.0 From b563ac37bad09bcec91c265bcd9575286895a355 Mon Sep 17 00:00:00 2001 From: Luiz Henrique Cassettari Date: Thu, 14 Mar 2024 15:28:12 -0300 Subject: [PATCH 3/5] Disable `EnableUnsafeBinaryFormatterSerialization` in the csproj --- source/Directory.Build.props | 2 +- source/Nuke.Common/Nuke.Common.props | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/Directory.Build.props b/source/Directory.Build.props index 11a271277..e8991ece3 100644 --- a/source/Directory.Build.props +++ b/source/Directory.Build.props @@ -8,7 +8,7 @@ embedded true true - true + diff --git a/source/Nuke.Common/Nuke.Common.props b/source/Nuke.Common/Nuke.Common.props index 9827b4735..b0e169a42 100644 --- a/source/Nuke.Common/Nuke.Common.props +++ b/source/Nuke.Common/Nuke.Common.props @@ -5,7 +5,7 @@ false $(MSBuildWarningsAsErrors);CS8785 $(NoWarn);SYSLIB0050;SYSLIB0051 - true + From e93916b39c994e9a899a7631dba7410eb6b6906c Mon Sep 17 00:00:00 2001 From: Luiz Henrique Cassettari Date: Thu, 14 Mar 2024 15:34:30 -0300 Subject: [PATCH 4/5] Rollback File format --- source/Nuke.Tooling/SettingsEntity.NewInstance.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source/Nuke.Tooling/SettingsEntity.NewInstance.cs b/source/Nuke.Tooling/SettingsEntity.NewInstance.cs index 0cacf3202..fb79a2e39 100644 --- a/source/Nuke.Tooling/SettingsEntity.NewInstance.cs +++ b/source/Nuke.Tooling/SettingsEntity.NewInstance.cs @@ -2,11 +2,11 @@ // Distributed under the MIT License. // https://github.com/nuke-build/nuke/blob/master/LICENSE -using JetBrains.Annotations; using System; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; +using JetBrains.Annotations; #pragma warning disable SYSLIB0011 namespace Nuke.Common.Tooling; @@ -25,12 +25,12 @@ public static T NewInstance(this T settingsEntity) 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; From abeb8afc9ec80abf1c92e14549b3b8b2d00bae8f Mon Sep 17 00:00:00 2001 From: Matthias Koch Date: Sun, 14 Jul 2024 19:49:31 +0200 Subject: [PATCH 5/5] fix --- source/Directory.Build.props | 1 - source/Nuke.Common/Nuke.Common.props | 1 - source/Nuke.Tooling/SettingsEntity.NewInstance.cs | 13 +++++++------ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/source/Directory.Build.props b/source/Directory.Build.props index e8991ece3..8ece13d38 100644 --- a/source/Directory.Build.props +++ b/source/Directory.Build.props @@ -8,7 +8,6 @@ embedded true true - diff --git a/source/Nuke.Common/Nuke.Common.props b/source/Nuke.Common/Nuke.Common.props index b0e169a42..744f4e105 100644 --- a/source/Nuke.Common/Nuke.Common.props +++ b/source/Nuke.Common/Nuke.Common.props @@ -5,7 +5,6 @@ false $(MSBuildWarningsAsErrors);CS8785 $(NoWarn);SYSLIB0050;SYSLIB0051 - diff --git a/source/Nuke.Tooling/SettingsEntity.NewInstance.cs b/source/Nuke.Tooling/SettingsEntity.NewInstance.cs index fb79a2e39..54c7e05d5 100644 --- a/source/Nuke.Tooling/SettingsEntity.NewInstance.cs +++ b/source/Nuke.Tooling/SettingsEntity.NewInstance.cs @@ -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 @@ -7,6 +7,7 @@ using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using JetBrains.Annotations; + #pragma warning disable SYSLIB0011 namespace Nuke.Common.Tooling; @@ -17,7 +18,7 @@ public static partial class SettingsEntityExtensions public static T NewInstance(this T settingsEntity) where T : ISettingsEntity { - AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", true); + AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", isEnabled: true); var binaryFormatter = new BinaryFormatter(); @@ -25,12 +26,12 @@ public static T NewInstance(this T settingsEntity) 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;