Skip to content

Commit

Permalink
(GH-219)(GH-56) Allow PowerShell interaction
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ferventcoder committed Jun 26, 2015
1 parent 0840f79 commit 8edaa75
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
Expand Up @@ -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]
Expand Down
Expand Up @@ -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());
Expand Down Expand Up @@ -503,7 +503,7 @@ public void uninstall_noop(ChocolateyConfiguration config)
}
// 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)
{
Expand Down
15 changes: 11 additions & 4 deletions src/chocolatey/infrastructure/commands/CommandExecutor.cs
Expand Up @@ -67,13 +67,14 @@ public int execute(string process, string arguments, int waitForExitInSeconds)
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,
Expand All @@ -82,7 +83,8 @@ public int execute(string process, string arguments, int waitForExitInSeconds, s
string workingDirectory,
Action<object, DataReceivedEventArgs> stdOutAction,
Action<object, DataReceivedEventArgs> stdErrAction,
bool updateProcessPath
bool updateProcessPath,
bool allowUseWindow
)
{
int exitCode = -1;
Expand All @@ -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;
Expand Down
13 changes: 12 additions & 1 deletion src/chocolatey/infrastructure/commands/PowershellExecutor.cs
Expand Up @@ -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;
Expand All @@ -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<string> _powershellLocations = new List<string>
{
Environment.ExpandEnvironmentVariables("%systemroot%\\SysNative\\WindowsPowerShell\\v1.0\\powershell.exe"),
Expand Down Expand Up @@ -53,7 +63,8 @@ public sealed class PowershellExecutor
workingDirectory: fileSystem.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty)),
stdOutAction: stdOutAction,
stdErrAction: stdErrAction,
updateProcessPath: true
updateProcessPath: true,
allowUseWindow: _allowUseWindow
);
}

Expand Down

0 comments on commit 8edaa75

Please sign in to comment.