From 8edaa75be85dd0720eb2b0112c132518c174af03 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Fri, 26 Jun 2015 16:25:27 -0500 Subject: [PATCH] (GH-219)(GH-56) Allow PowerShell interaction Allow PowerShell to use the same window as the choco process that brings back the download progress bar and being able to do things like read-host. --- .../commands/CommandExecutorSpecs.cs | 2 +- .../services/ChocolateyPackageService.cs | 4 ++-- .../infrastructure/commands/CommandExecutor.cs | 15 +++++++++++---- .../infrastructure/commands/PowershellExecutor.cs | 13 ++++++++++++- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/chocolatey.tests.integration/infrastructure/commands/CommandExecutorSpecs.cs b/src/chocolatey.tests.integration/infrastructure/commands/CommandExecutorSpecs.cs index 80b2868545..07d67cb769 100644 --- a/src/chocolatey.tests.integration/infrastructure/commands/CommandExecutorSpecs.cs +++ b/src/chocolatey.tests.integration/infrastructure/commands/CommandExecutorSpecs.cs @@ -47,7 +47,7 @@ public override void Context() public override void Because() { - result = CommandExecutor.execute("cmd.exe", "/c bob123123", ApplicationParameters.DefaultWaitForExitInSeconds, fileSystem.get_current_directory(), null, (s, e) => { errorOutput += e.Data; }, updateProcessPath: false); + result = CommandExecutor.execute("cmd.exe", "/c bob123123", ApplicationParameters.DefaultWaitForExitInSeconds, fileSystem.get_current_directory(), null, (s, e) => { errorOutput += e.Data; }, updateProcessPath: false, allowUseWindow: false); } [Fact] diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index c8a5294021..02108c33e7 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -178,7 +178,7 @@ public void handle_package_result(PackageResult packageResult, ChocolateyConfigu if (powerShellRan) { // we don't care about the exit code - if (config.Information.PlatformType == PlatformType.Windows) CommandExecutor.execute("shutdown", "/a", config.CommandExecutionTimeoutSeconds, _fileSystem.get_current_directory(), (s, e) => { }, (s, e) => { }, false); + if (config.Information.PlatformType == PlatformType.Windows) CommandExecutor.execute("shutdown", "/a", config.CommandExecutionTimeoutSeconds, _fileSystem.get_current_directory(), (s, e) => { }, (s, e) => { }, false, false); } var difference = _registryService.get_differences(before, _registryService.get_installer_keys()); @@ -503,7 +503,7 @@ public ConcurrentDictionary uninstall_run(ChocolateyConfi } // we don't care about the exit code - if (config.Information.PlatformType == PlatformType.Windows) CommandExecutor.execute("shutdown", "/a", config.CommandExecutionTimeoutSeconds, _fileSystem.get_current_directory(), (s, e) => { }, (s, e) => { }, false); + if (config.Information.PlatformType == PlatformType.Windows) CommandExecutor.execute("shutdown", "/a", config.CommandExecutionTimeoutSeconds, _fileSystem.get_current_directory(), (s, e) => { }, (s, e) => { }, false, false); if (packageResult.Success) { diff --git a/src/chocolatey/infrastructure/commands/CommandExecutor.cs b/src/chocolatey/infrastructure/commands/CommandExecutor.cs index 409850d7b8..e4548eb3a8 100644 --- a/src/chocolatey/infrastructure/commands/CommandExecutor.cs +++ b/src/chocolatey/infrastructure/commands/CommandExecutor.cs @@ -67,13 +67,14 @@ public int execute( file_system.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty)), stdOutAction, stdErrAction, - updateProcessPath + updateProcessPath, + allowUseWindow:false ); } public int execute(string process, string arguments, int waitForExitInSeconds, string workingDirectory) { - return execute(process, arguments, waitForExitInSeconds, workingDirectory, null, null, updateProcessPath: true); + return execute(process, arguments, waitForExitInSeconds, workingDirectory, null, null, updateProcessPath: true, allowUseWindow: false); } public static int execute(string process, @@ -82,7 +83,8 @@ public static int execute(string process, string workingDirectory, Action stdOutAction, Action stdErrAction, - bool updateProcessPath + bool updateProcessPath, + bool allowUseWindow ) { int exitCode = -1; @@ -105,9 +107,14 @@ bool updateProcessPath WorkingDirectory = workingDirectory, RedirectStandardOutput = true, RedirectStandardError = true, - CreateNoWindow = true + CreateNoWindow = !allowUseWindow, }; + if (allowUseWindow) + { + psi.WindowStyle = ProcessWindowStyle.Minimized; + } + using (var p = initialize_process()) { p.StartInfo = psi; diff --git a/src/chocolatey/infrastructure/commands/PowershellExecutor.cs b/src/chocolatey/infrastructure/commands/PowershellExecutor.cs index e79081b469..c81552da50 100644 --- a/src/chocolatey/infrastructure/commands/PowershellExecutor.cs +++ b/src/chocolatey/infrastructure/commands/PowershellExecutor.cs @@ -17,6 +17,7 @@ namespace chocolatey.infrastructure.commands { using System; using System.Collections.Generic; + using System.ComponentModel; using System.Diagnostics; using System.IO; using adapters; @@ -25,6 +26,15 @@ namespace chocolatey.infrastructure.commands public sealed class PowershellExecutor { + private static bool _allowUseWindow = true; + + [EditorBrowsable(EditorBrowsableState.Never)] + public static bool AllowUseWindow + { + get { return _allowUseWindow; } + set { _allowUseWindow = value; } + } + private static readonly IList _powershellLocations = new List { Environment.ExpandEnvironmentVariables("%systemroot%\\SysNative\\WindowsPowerShell\\v1.0\\powershell.exe"), @@ -53,7 +63,8 @@ Action stdErrAction workingDirectory: fileSystem.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty)), stdOutAction: stdOutAction, stdErrAction: stdErrAction, - updateProcessPath: true + updateProcessPath: true, + allowUseWindow: _allowUseWindow ); }