Extract shell command helpers
This commit is contained in:
parent
080343f902
commit
b7249a99a5
|
@ -85,17 +85,14 @@ namespace BizHawk.Client.EmuHawk
|
|||
{
|
||||
try
|
||||
{
|
||||
_ffmpeg = new Process();
|
||||
_ffmpeg.StartInfo.FileName = OSTailoredCode.CurrentOS == OSTailoredCode.DistinctOS.Windows
|
||||
? Path.Combine(PathManager.GetDllDirectory(), "ffmpeg.exe")
|
||||
: "ffmpeg";
|
||||
_ffmpeg.StartInfo.Arguments = $"-y -f nut -i - {_token.Commandline} \"{_baseName}{(_segment == 0 ? string.Empty : $"_{_segment}")}{_ext}\"";
|
||||
_ffmpeg.StartInfo.CreateNoWindow = true;
|
||||
|
||||
// ffmpeg sends informative display to stderr, and nothing to stdout
|
||||
_ffmpeg.StartInfo.RedirectStandardError = true;
|
||||
_ffmpeg.StartInfo.RedirectStandardInput = true;
|
||||
_ffmpeg.StartInfo.UseShellExecute = false;
|
||||
_ffmpeg = OSTailoredCode.ConstructSubshell(
|
||||
OSTailoredCode.CurrentOS == OSTailoredCode.DistinctOS.Windows
|
||||
? Path.Combine(PathManager.GetDllDirectory(), "ffmpeg.exe")
|
||||
: "ffmpeg",
|
||||
$"-y -f nut -i - {_token.Commandline} \"{_baseName}{(_segment == 0 ? string.Empty : $"_{_segment}")}{_ext}\"",
|
||||
checkStdout: false,
|
||||
checkStderr: true // ffmpeg sends informative display to stderr, and nothing to stdout
|
||||
);
|
||||
|
||||
_commandline = $"ffmpeg {_ffmpeg.StartInfo.Arguments}";
|
||||
|
||||
|
|
|
@ -33,21 +33,7 @@ namespace BizHawk.Common
|
|||
|
||||
public static ILinkedLibManager LinkedLibManager => lazy.Value;
|
||||
|
||||
private static bool currentIsMacOS()
|
||||
{
|
||||
var proc = new Process {
|
||||
StartInfo = new ProcessStartInfo {
|
||||
Arguments = "-s",
|
||||
CreateNoWindow = true,
|
||||
FileName = "uname",
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false
|
||||
}
|
||||
};
|
||||
proc.Start();
|
||||
if (proc.StandardOutput.EndOfStream) throw new Exception("Can't determine OS (uname wrote nothing to stdout)!");
|
||||
return proc.StandardOutput.ReadLine() == "Darwin";
|
||||
}
|
||||
private static bool currentIsMacOS() => SimpleSubshell("uname", "-s", "Can't determine OS") == "Darwin";
|
||||
|
||||
private OSTailoredCode() {}
|
||||
|
||||
|
@ -125,5 +111,39 @@ namespace BizHawk.Common
|
|||
macOS,
|
||||
Windows
|
||||
}
|
||||
|
||||
/// <param name="cmd">POSIX <c>$0</c></param>
|
||||
/// <param name="args">POSIX <c>$*</c> (space-delimited)</param>
|
||||
/// <param name="checkStdout">stdout is discarded if false</param>
|
||||
/// <param name="checkStderr">stderr is discarded if false</param>
|
||||
/// <remarks>OS is implicit and needs to be checked at callsite, returned <see cref="Process"/> has not been started</remarks>
|
||||
public static Process ConstructSubshell(string cmd, string args, bool checkStdout = true, bool checkStderr = false) =>
|
||||
new Process {
|
||||
StartInfo = new ProcessStartInfo {
|
||||
Arguments = args,
|
||||
CreateNoWindow = true,
|
||||
FileName = cmd,
|
||||
RedirectStandardError = checkStderr,
|
||||
RedirectStandardOutput = checkStdout,
|
||||
UseShellExecute = false
|
||||
}
|
||||
};
|
||||
|
||||
/// <param name="cmd">POSIX <c>$0</c></param>
|
||||
/// <param name="args">POSIX <c>$*</c> (space-delimited)</param>
|
||||
/// <param name="noOutputMsg">used in exception</param>
|
||||
/// <returns>first line of stdout</returns>
|
||||
/// <exception cref="Exception">thrown if stdout is empty</exception>
|
||||
/// <remarks>OS is implicit and needs to be checked at callsite</remarks>
|
||||
public static string SimpleSubshell(string cmd, string args, string noOutputMsg)
|
||||
{
|
||||
using (var proc = ConstructSubshell(cmd, args))
|
||||
{
|
||||
proc.Start();
|
||||
var stdout = proc.StandardOutput;
|
||||
if (stdout.EndOfStream) throw new Exception($"{noOutputMsg} ({cmd} wrote nothing to stdout)");
|
||||
return stdout.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue