Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli: Option to always hide CMD output and prevent opining new window #13611

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class AbpCliOptions
/// Default value: "CLI".
/// </summary>
public string ToolName { get; set; } = "CLI";

public bool AlwaysHideExternalCommandOutput { get; set; }

public AbpCliOptions()
{
Expand Down
38 changes: 36 additions & 2 deletions framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/CmdHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;

namespace Volo.Abp.Cli.Utils;
Expand All @@ -10,6 +11,13 @@ public class CmdHelper : ICmdHelper, ITransientDependency
{
private const int SuccessfulExitCode = 0;

protected AbpCliOptions CliOptions { get; }

public CmdHelper(IOptionsSnapshot<AbpCliOptions> cliOptions)
{
CliOptions = cliOptions.Value;
}

public void OpenWebPage(string url)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand All @@ -27,10 +35,15 @@ public void OpenWebPage(string url)
}
}


public void Run(string file, string arguments)
{
var procStartInfo = new ProcessStartInfo(file, arguments);

if (CliOptions.AlwaysHideExternalCommandOutput)
{
HideNewCommandWindow(procStartInfo);
}

Process.Start(procStartInfo)?.WaitForExit();
}

Expand All @@ -46,6 +59,11 @@ public void RunCmd(string command, out int exitCode, string workingDirectory = n
GetArguments(command)
);

if (CliOptions.AlwaysHideExternalCommandOutput)
{
HideNewCommandWindow(procStartInfo);
}

if (!string.IsNullOrEmpty(workingDirectory))
{
procStartInfo.WorkingDirectory = workingDirectory;
Expand All @@ -66,10 +84,14 @@ public Process RunCmdAndGetProcess(string command, string workingDirectory = nul
GetArguments(command)
);

if (CliOptions.AlwaysHideExternalCommandOutput)
{
HideNewCommandWindow(procStartInfo);
}

if (!string.IsNullOrEmpty(workingDirectory))
{
procStartInfo.WorkingDirectory = workingDirectory;
procStartInfo.CreateNoWindow = false;
}

return Process.Start(procStartInfo);
Expand Down Expand Up @@ -98,6 +120,7 @@ public string RunCmdAndGetOutput(string command, out int exitCode, string workin
Arguments = GetArguments(command),
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
RedirectStandardError = true
};
Expand Down Expand Up @@ -137,6 +160,11 @@ public void RunCmdAndExit(string command, string workingDirectory = null, int? d
{
procStartInfo.WorkingDirectory = workingDirectory;
}

if (CliOptions.AlwaysHideExternalCommandOutput)
{
HideNewCommandWindow(procStartInfo);
}

Process.Start(procStartInfo);
Environment.Exit(0);
Expand Down Expand Up @@ -178,4 +206,10 @@ public string GetFileName()
$"Framework: {System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription} | " +
$"Process Architecture{System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture}");
}

private void HideNewCommandWindow(ProcessStartInfo procStartInfo)
{
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
procStartInfo.CreateNoWindow = true;
}
}