From 59f6f84f36323dc85e52cf527bb08aa974ce8ff5 Mon Sep 17 00:00:00 2001 From: James Truher Date: Mon, 24 Jan 2022 11:54:47 -0800 Subject: [PATCH 1/3] re-factor code to remove duplicate definition of ArgumentToPSVersionTransformationAttribute. --- ...rgumentToVersionTransformationAttribute.cs | 27 +++++++++++++++++++ .../engine/InternalCommands.cs | 17 ------------ .../remoting/commands/InvokeCommandCommand.cs | 20 -------------- 3 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/System.Management.Automation/engine/ArgumentToVersionTransformationAttribute.cs b/src/System.Management.Automation/engine/ArgumentToVersionTransformationAttribute.cs index facaa9e3e1a..b7556108119 100644 --- a/src/System.Management.Automation/engine/ArgumentToVersionTransformationAttribute.cs +++ b/src/System.Management.Automation/engine/ArgumentToVersionTransformationAttribute.cs @@ -8,6 +8,33 @@ namespace System.Management.Automation { + /// + /// To make it easier to specify a version, we add some conversions that wouldn't happen otherwise: + /// * A simple integer, i.e. 2; + /// * A string without a dot, i.e. "2". + /// * the string "off" sets the version to 0.0 (unset) + /// * the string "latest" sets to the current version + /// + internal sealed class ArgumentToPSVersionTransformationAttribute : ArgumentToVersionTransformationAttribute + { + protected override bool TryConvertFromString(string versionString, [NotNullWhen(true)] out Version? version) + { + if (string.Equals("off", versionString, StringComparison.OrdinalIgnoreCase)) + { + version = new Version(0, 0); + return true; + } + + if (string.Equals("latest", versionString, StringComparison.OrdinalIgnoreCase)) + { + version = PSVersionInfo.PSVersion; + return true; + } + + return base.TryConvertFromString(versionString, out version); + } + } + /// /// To make it easier to specify a version, we add some conversions that wouldn't happen otherwise: /// * A simple integer, i.e. 2; diff --git a/src/System.Management.Automation/engine/InternalCommands.cs b/src/System.Management.Automation/engine/InternalCommands.cs index c642e30e885..e7f6e90e207 100644 --- a/src/System.Management.Automation/engine/InternalCommands.cs +++ b/src/System.Management.Automation/engine/InternalCommands.cs @@ -2635,23 +2635,6 @@ public SwitchParameter Off private SwitchParameter _off; - /// - /// Handle 'latest', which we interpret to be the current version of PowerShell. - /// - private sealed class ArgumentToPSVersionTransformationAttribute : ArgumentToVersionTransformationAttribute - { - protected override bool TryConvertFromString(string versionString, [NotNullWhen(true)] out Version version) - { - if (string.Equals("latest", versionString, StringComparison.OrdinalIgnoreCase)) - { - version = PSVersionInfo.PSVersion; - return true; - } - - return base.TryConvertFromString(versionString, out version); - } - } - private sealed class ValidateVersionAttribute : ValidateArgumentsAttribute { protected override void Validate(object arguments, EngineIntrinsics engineIntrinsics) diff --git a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs index c194706151d..03bb69f1fcf 100644 --- a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs +++ b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs @@ -263,26 +263,6 @@ public override SwitchParameter UseSSL } } - private sealed class ArgumentToPSVersionTransformationAttribute : ArgumentToVersionTransformationAttribute - { - protected override bool TryConvertFromString(string versionString, [NotNullWhen(true)] out Version version) - { - if (string.Equals("off", versionString, StringComparison.OrdinalIgnoreCase)) - { - version = new Version(0, 0); - return true; - } - - if (string.Equals("latest", versionString, StringComparison.OrdinalIgnoreCase)) - { - version = PSVersionInfo.PSVersion; - return true; - } - - return base.TryConvertFromString(versionString, out version); - } - } - private static readonly Version s_OffVersion = new Version(0, 0); private sealed class ValidateVersionAttribute : ValidateArgumentsAttribute From 6f7d61f557473f3da214abe350b58c81cfe53f81 Mon Sep 17 00:00:00 2001 From: James Truher Date: Mon, 24 Jan 2022 15:33:45 -0800 Subject: [PATCH 2/3] update packaging.psm1 to remove the internal attributes in the reference assembly. --- tools/packaging/packaging.psm1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/packaging/packaging.psm1 b/tools/packaging/packaging.psm1 index bca41d3c816..0e064b8fbb8 100644 --- a/tools/packaging/packaging.psm1 +++ b/tools/packaging/packaging.psm1 @@ -2294,6 +2294,8 @@ function CleanupGeneratedSourceCode '[Microsoft.PowerShell.Commands.SetStrictModeCommand.ArgumentToPSVersionTransformationAttribute]' '[Microsoft.PowerShell.Commands.HttpVersionCompletionsAttribute]' '[System.Management.Automation.ArgumentToVersionTransformationAttribute]' + '[Microsoft.PowerShell.Commands.InvokeCommandCommand.ValidateVersionAttribute]' + '[System.Management.Automation.ArgumentToPSVersionTransformationAttribute]' ) $patternsToReplace = @( From ae3e9e664c808fc9eee234b7eda609d49eaad11c Mon Sep 17 00:00:00 2001 From: James Truher Date: Tue, 25 Jan 2022 11:39:54 -0800 Subject: [PATCH 3/3] fix code-factor complaint of missing period --- .../engine/ArgumentToVersionTransformationAttribute.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/ArgumentToVersionTransformationAttribute.cs b/src/System.Management.Automation/engine/ArgumentToVersionTransformationAttribute.cs index b7556108119..5ac86ac2bde 100644 --- a/src/System.Management.Automation/engine/ArgumentToVersionTransformationAttribute.cs +++ b/src/System.Management.Automation/engine/ArgumentToVersionTransformationAttribute.cs @@ -12,8 +12,8 @@ namespace System.Management.Automation /// To make it easier to specify a version, we add some conversions that wouldn't happen otherwise: /// * A simple integer, i.e. 2; /// * A string without a dot, i.e. "2". - /// * the string "off" sets the version to 0.0 (unset) - /// * the string "latest" sets to the current version + /// * the string "off" sets the version to 0.0 (unset). + /// * the string "latest" sets to the current version. /// internal sealed class ArgumentToPSVersionTransformationAttribute : ArgumentToVersionTransformationAttribute {