Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright 2019 Tom Deseyn <tom.deseyn@gmail.com> | ||
// This software is made available under the MIT License | ||
// See COPYING for details | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
|
||
namespace Tmds.Utils | ||
{ | ||
public class FunctionExecutor | ||
{ | ||
private readonly Action<ProcessStartInfo> _configure; | ||
private readonly Action<Process> _onExit; | ||
|
||
public FunctionExecutor(Action<ProcessStartInfo> configure, Action<Process> onExit = null) | ||
{ | ||
_configure = configure; | ||
_onExit = onExit; | ||
} | ||
|
||
public void Run(Action action, Action<ProcessStartInfo> configure = null) | ||
{ | ||
using (Process process = ExecFunction.Start(action, CombineConfigures(_configure, configure))) | ||
{ | ||
process.WaitForExit(); | ||
_onExit?.Invoke(process); | ||
} | ||
} | ||
|
||
public void Run(Action<string[]> action, string[] args, Action<ProcessStartInfo> configure = null) | ||
{ | ||
using (Process process = ExecFunction.Start(action, args, CombineConfigures(_configure, configure))) | ||
{ | ||
process.WaitForExit(); | ||
_onExit?.Invoke(process); | ||
} | ||
} | ||
|
||
public void Run(Func<int> action, Action<ProcessStartInfo> configure = null) | ||
{ | ||
using (Process process = ExecFunction.Start(action, CombineConfigures(_configure, configure))) | ||
{ | ||
process.WaitForExit(); | ||
_onExit?.Invoke(process); | ||
} | ||
} | ||
|
||
public void Run(Func<string[], int> action, string[] args, Action<ProcessStartInfo> configure = null) | ||
{ | ||
using (Process process = ExecFunction.Start(action, args, CombineConfigures(_configure, configure))) | ||
{ | ||
process.WaitForExit(); | ||
_onExit?.Invoke(process); | ||
} | ||
} | ||
|
||
public void Run(Func<Task> action, Action<ProcessStartInfo> configure = null) | ||
{ | ||
using (Process process = ExecFunction.Start(action, CombineConfigures(_configure, configure))) | ||
{ | ||
process.WaitForExit(); | ||
_onExit?.Invoke(process); | ||
} | ||
} | ||
|
||
public void Run(Func<string[], Task> action, string[] args, Action<ProcessStartInfo> configure = null) | ||
{ | ||
using (Process process = ExecFunction.Start(action, args, CombineConfigures(_configure, configure))) | ||
{ | ||
process.WaitForExit(); | ||
_onExit?.Invoke(process); | ||
} | ||
} | ||
|
||
public void Run(Func<Task<int>> action, Action<ProcessStartInfo> configure = null) | ||
{ | ||
using (Process process = ExecFunction.Start(action, CombineConfigures(_configure, configure))) | ||
{ | ||
process.WaitForExit(); | ||
_onExit?.Invoke(process); | ||
} | ||
} | ||
|
||
public void Run(Func<string[], Task<int>> action, string[] args, Action<ProcessStartInfo> configure = null) | ||
{ | ||
using (Process process = ExecFunction.Start(action, args, CombineConfigures(_configure, configure))) | ||
{ | ||
process.WaitForExit(); | ||
_onExit?.Invoke(process); | ||
} | ||
} | ||
|
||
private static Action<ProcessStartInfo> CombineConfigures(Action<ProcessStartInfo> first, Action<ProcessStartInfo> second) | ||
{ | ||
if (first == null) | ||
{ | ||
return second; | ||
} | ||
if (second == null) | ||
{ | ||
return first; | ||
} | ||
return psi => { first.Invoke(psi); second.Invoke(psi); }; | ||
} | ||
} | ||
} |