From 065c979da0a669875a925a26790c2d1d18933564 Mon Sep 17 00:00:00 2001 From: AdmiringWorm Date: Fri, 23 Sep 2022 12:52:45 +0200 Subject: [PATCH 1/2] (#1185) Re-add old powershell run host method Changing the interface or public methods are breaking changes which we need to wait with implementing before the next major release. As such, this commit adds back the old way of running the powershell host by adding an overload with the original contract for the interface that could possibly be used in other projects that makes use of the Chocolatey.Lib library. This overload has been made obsolete and will just call the new method while setting any hook scripts as empty enumerables. --- .../infrastructure.app/services/IPowershellService.cs | 6 +++++- .../infrastructure.app/services/PowershellService.cs | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/chocolatey/infrastructure.app/services/IPowershellService.cs b/src/chocolatey/infrastructure.app/services/IPowershellService.cs index 1dd30b2090..5e19da6c2d 100644 --- a/src/chocolatey/infrastructure.app/services/IPowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/IPowershellService.cs @@ -1,4 +1,4 @@ -// Copyright © 2017 - 2021 Chocolatey Software, Inc +// Copyright © 2017 - 2021 Chocolatey Software, Inc // Copyright © 2011 - 2017 RealDimensions Software, LLC // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -68,6 +68,10 @@ public interface IPowershellService bool before_modify(ChocolateyConfiguration configuration, PackageResult packageResult); void prepare_powershell_environment(IPackage package, ChocolateyConfiguration configuration, string packageDirectory); + + [Obsolete("This version of running the powershell host do not support running additional hooks. Use the appropriate overload instead")] + PowerShellExecutionResults run_host(ChocolateyConfiguration config, string chocoPowerShellScript, Action additionalActionsBeforeScript); + PowerShellExecutionResults run_host(ChocolateyConfiguration config, string chocoPowerShellScript, Action additionalActionsBeforeScript, IEnumerable hookPreScriptPathList, IEnumerable hookPostScriptPathList); } } diff --git a/src/chocolatey/infrastructure.app/services/PowershellService.cs b/src/chocolatey/infrastructure.app/services/PowershellService.cs index 8390cb2ae7..dba64d54fc 100644 --- a/src/chocolatey/infrastructure.app/services/PowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/PowershellService.cs @@ -1,4 +1,4 @@ -// Copyright © 2017 - 2021 Chocolatey Software, Inc +// Copyright © 2017 - 2021 Chocolatey Software, Inc // Copyright © 2011 - 2017 RealDimensions Software, LLC // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -593,6 +593,12 @@ private void remove_assembly_resolver() } } + [Obsolete("This version of running the powershell host do not support running additional hooks. Use the appropriate overload instead")] + public PowerShellExecutionResults run_host(ChocolateyConfiguration config, string chocoPowershellScript, Action additionalActionsBeforeScript) + { + return run_host(config, chocoPowershellScript, additionalActionsBeforeScript, Enumerable.Empty(), Enumerable.Empty()); + } + public PowerShellExecutionResults run_host(ChocolateyConfiguration config, string chocoPowerShellScript, Action additionalActionsBeforeScript, IEnumerable hookPreScriptPathList, IEnumerable hookPostScriptPathList) { // since we control output in the host, always set these true From 96b9ebe8acf9c123aa0c94ce8377d55adbee4dfa Mon Sep 17 00:00:00 2001 From: AdmiringWorm Date: Fri, 23 Sep 2022 12:53:23 +0200 Subject: [PATCH 2/2] (maint) Whitespace + formatting fix --- .../services/IPowershellService.cs | 2 +- .../services/PowershellService.cs | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/chocolatey/infrastructure.app/services/IPowershellService.cs b/src/chocolatey/infrastructure.app/services/IPowershellService.cs index 5e19da6c2d..097cb278e0 100644 --- a/src/chocolatey/infrastructure.app/services/IPowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/IPowershellService.cs @@ -1,4 +1,4 @@ -// Copyright © 2017 - 2021 Chocolatey Software, Inc +// Copyright © 2017 - 2022 Chocolatey Software, Inc // Copyright © 2011 - 2017 RealDimensions Software, LLC // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/chocolatey/infrastructure.app/services/PowershellService.cs b/src/chocolatey/infrastructure.app/services/PowershellService.cs index dba64d54fc..e57e66b98e 100644 --- a/src/chocolatey/infrastructure.app/services/PowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/PowershellService.cs @@ -1,4 +1,4 @@ -// Copyright © 2017 - 2021 Chocolatey Software, Inc +// Copyright © 2017 - 2022 Chocolatey Software, Inc // Copyright © 2011 - 2017 RealDimensions Software, LLC // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,10 +23,7 @@ namespace chocolatey.infrastructure.app.services using System.Management.Automation; using System.Management.Automation.Runspaces; using System.Reflection; - using System.Security.Cryptography; - using System.Text; using adapters; - using builders; using commandline; using configuration; using cryptography; @@ -38,8 +35,6 @@ namespace chocolatey.infrastructure.app.services using powershell; using results; using utility; - using Assembly = adapters.Assembly; - using Console = System.Console; using CryptoHashProvider = cryptography.CryptoHashProvider; using Environment = System.Environment; using IFileSystem = filesystem.IFileSystem; @@ -112,12 +107,15 @@ private IEnumerable get_hook_scripts(ChocolateyConfiguration configurati case CommandNameType.install: filenameBase += "install-"; break; + case CommandNameType.uninstall: filenameBase += "uninstall-"; break; + case CommandNameType.upgrade: filenameBase += "beforemodify-"; break; + default: throw new ApplicationException("Could not find CommandNameType '{0}' to get hook scripts".format_with(command)); } @@ -353,7 +351,6 @@ public bool run_action(ChocolateyConfiguration configuration, PackageResult pack `choco -h` for details."); } - if (result.ExitCode != 0) { Environment.ExitCode = result.ExitCode; @@ -534,7 +531,7 @@ public void prepare_powershell_environment(IPackage package, ChocolateyConfigura } } - SecurityProtocol.set_protocol(configuration, provideWarning:false); + SecurityProtocol.set_protocol(configuration, provideWarning: false); } private ResolveEventHandler _handler = null; @@ -738,11 +735,11 @@ public PowerShellExecutionResults run_host(ChocolateyConfiguration config, strin if (host.ExitCode == 0) host.SetShouldExit(1); host.HostException = pipeline.PipelineStateInfo.Reason; break; + case PipelineState.Completed: if (host.ExitCode == -1) host.SetShouldExit(0); break; } - } } }