From 7adcc2b7acc7f05a6111d43499327f23bd4a4348 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Tue, 3 Oct 2023 15:37:33 -0700 Subject: [PATCH 1/2] Add internal for setting caller as compat cmdlet --- src/code/InternalHooks.cs | 18 ++++++++++++++++++ src/code/ServerFactory.cs | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/code/InternalHooks.cs diff --git a/src/code/InternalHooks.cs b/src/code/InternalHooks.cs new file mode 100644 index 000000000..9b5ea0294 --- /dev/null +++ b/src/code/InternalHooks.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Reflection; + +namespace Microsoft.PowerShell.PSResourceGet.UtilClasses +{ + public class InternalHooks + { + internal static bool InvokedFromCompat; + + public static void SetTestHook(string property, object value) + { + var fieldInfo = typeof(InternalHooks).GetField(property, BindingFlags.Static | BindingFlags.NonPublic); + fieldInfo?.SetValue(null, value); + } + } +} \ No newline at end of file diff --git a/src/code/ServerFactory.cs b/src/code/ServerFactory.cs index 441e5eef4..9f4abb848 100644 --- a/src/code/ServerFactory.cs +++ b/src/code/ServerFactory.cs @@ -24,8 +24,9 @@ static UserAgentInfo() private static string _psVersion; private static string _psResourceGetVersion; private static string _distributionChannel; + private static string _psGetCompat = InternalHooks.InvokedFromCompat ? "true" : "false"; - internal static string UserAgentString => $"PSResourceGet/{_psResourceGetVersion} PowerShell/{_psVersion} DistributionChannel/{_distributionChannel}"; + internal static string UserAgentString => $"PSResourceGet/{_psResourceGetVersion} PowerShell/{_psVersion} DistributionChannel/{_distributionChannel} PowerShellGetCompat/{_psGetCompat}"; } internal class ServerFactory From ef624c1385ebea521dc6d1ad96f671950d0ece38 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Tue, 3 Oct 2023 16:48:00 -0700 Subject: [PATCH 2/2] Convert to static method --- src/code/ServerFactory.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/code/ServerFactory.cs b/src/code/ServerFactory.cs index 9f4abb848..8be08662e 100644 --- a/src/code/ServerFactory.cs +++ b/src/code/ServerFactory.cs @@ -24,9 +24,12 @@ static UserAgentInfo() private static string _psVersion; private static string _psResourceGetVersion; private static string _distributionChannel; - private static string _psGetCompat = InternalHooks.InvokedFromCompat ? "true" : "false"; - internal static string UserAgentString => $"PSResourceGet/{_psResourceGetVersion} PowerShell/{_psVersion} DistributionChannel/{_distributionChannel} PowerShellGetCompat/{_psGetCompat}"; + internal static string UserAgentString() + { + string psGetCompat = InternalHooks.InvokedFromCompat ? "true" : "false"; + return $"PSResourceGet/{_psResourceGetVersion} PowerShell/{_psVersion} DistributionChannel/{_distributionChannel} PowerShellGetCompat/{psGetCompat}"; + } } internal class ServerFactory @@ -35,7 +38,7 @@ public static ServerApiCall GetServer(PSRepositoryInfo repository, PSCmdlet cmdl { PSRepositoryInfo.APIVersion repoApiVersion = repository.ApiVersion; ServerApiCall currentServer = null; - string userAgentString = UserAgentInfo.UserAgentString; + string userAgentString = UserAgentInfo.UserAgentString(); switch (repoApiVersion) {