Skip to content

Commit

Permalink
Add ExecFunction.GetProcessStartInfo methods. (#26)
Browse files Browse the repository at this point in the history
These methods enable obtaining the ProcessStartInfo so it can be
passed to start a Process at a later time.
  • Loading branch information
tmds authored Jan 31, 2024
1 parent 31396f0 commit 8a2f30e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Tmds.ExecFunction/ExecFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,37 @@ public static Task RunAsync(Func<Task<int>> action, Action<ExecFunctionOptions>
public static Task RunAsync(Func<string[], Task<int>> action, string[] args, Action<ExecFunctionOptions> configure = null)
=> Start(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)), configure, returnTask: true).exitedTask;

public static ProcessStartInfo GetProcessStartInfo(Action action)
=> GetProcessStartInfo(GetMethodInfo(action), Array.Empty<string>());

public static ProcessStartInfo GetProcessStartInfo(Action<string[]> action, string[] args)
=> GetProcessStartInfo(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)));

public static ProcessStartInfo GetProcessStartInfo(Func<int> action)
=> GetProcessStartInfo(GetMethodInfo(action), Array.Empty<string>());

public static ProcessStartInfo GetProcessStartInfo(Func<string[], int> action, string[] args)
=> GetProcessStartInfo(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)));

public static ProcessStartInfo GetProcessStartInfo(Func<Task> action)
=> GetProcessStartInfo(GetMethodInfo(action), Array.Empty<string>());

public static ProcessStartInfo GetProcessStartInfo(Func<string[], Task> action, string[] args)
=> GetProcessStartInfo(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)));

public static ProcessStartInfo GetProcessStartInfo(Func<Task<int>> action)
=> GetProcessStartInfo(GetMethodInfo(action), Array.Empty<string>());

public static ProcessStartInfo GetProcessStartInfo(Func<string[], Task<int>> action, string[] args)
=> GetProcessStartInfo(GetMethodInfo(action), args ?? throw new ArgumentNullException(nameof(args)));

private static ProcessStartInfo GetProcessStartInfo(MethodInfo method, string[] args)
{
var psi = new ProcessStartInfo();
ConfigureProcessStartInfoForMethodInvocation(method, args, psi);
return psi;
}

private static (Process process, Task exitedTask) Start(MethodInfo method, string[] args, Action<ExecFunctionOptions> configure, bool waitForExit = false, bool returnTask = false)
{
Process process = null;
Expand Down
9 changes: 9 additions & 0 deletions test/Tmds.ExecFunction.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public void TestArgVoidReturnTaskOfInt()
};
}

[Fact]
public void GetProcessStartInfo()
{
ProcessStartInfo psi = ExecFunction.GetProcessStartInfo(() => 42);
Process process = Process.Start(psi);
process.WaitForExit();
Assert.Equal(42, process.ExitCode);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down

0 comments on commit 8a2f30e

Please sign in to comment.