Skip to content

Commit 46d5706

Browse files
author
BRUNER Patrick
committed
small optimizations
1 parent 133dc3f commit 46d5706

1 file changed

Lines changed: 24 additions & 26 deletions

File tree

src/LogExpert.UI/Services/ToolLaunchService/ToolLaunchService.cs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,33 @@ internal sealed class ToolLaunchService (IPluginRegistry pluginRegistry) : ITool
1818

1919
public ToolLaunchResult Launch (ToolLaunchRequest request)
2020
{
21-
if (string.IsNullOrEmpty(request.Cmd))
22-
{
23-
return new ToolLaunchResult
21+
return string.IsNullOrEmpty(request.Cmd)
22+
? new ToolLaunchResult
2423
{
2524
HasError = true,
2625
ErrorMessage = "Command must not be empty."
27-
};
28-
}
29-
30-
if (request.SysoutPipe)
31-
{
32-
return LaunchWithSysoutPipe(request);
33-
}
34-
35-
return LaunchExternal(request);
26+
}
27+
: request.SysoutPipe
28+
? LaunchWithSysoutPipe(request)
29+
: LaunchExternal(request);
3630
}
3731

3832
private static ToolLaunchResult LaunchExternal (ToolLaunchRequest request)
3933
{
4034
var startInfo = BuildStartInfo(request);
35+
4136
startInfo.UseShellExecute = false;
42-
Process process = new() { StartInfo = startInfo, EnableRaisingEvents = true };
37+
38+
(bool flowControl, ToolLaunchResult value, _) = LaunchProcess(startInfo);
39+
40+
return !flowControl
41+
? value
42+
: new ToolLaunchResult { HasError = false };
43+
}
44+
45+
private static (bool flowControl, ToolLaunchResult value, Process process) LaunchProcess (ProcessStartInfo startInfo)
46+
{
47+
using Process process = new() { StartInfo = startInfo, EnableRaisingEvents = true };
4348

4449
try
4550
{
@@ -51,10 +56,10 @@ ObjectDisposedException or
5156
PlatformNotSupportedException)
5257
{
5358
_logger.Error(e);
54-
return new ToolLaunchResult { HasError = true, ErrorMessage = e.Message };
59+
return (false, new ToolLaunchResult { HasError = true, ErrorMessage = e.Message }, process);
5560
}
5661

57-
return new ToolLaunchResult { HasError = false };
62+
return (true, default, process);
5863
}
5964

6065
private ToolLaunchResult LaunchWithSysoutPipe (ToolLaunchRequest request)
@@ -67,19 +72,11 @@ private ToolLaunchResult LaunchWithSysoutPipe (ToolLaunchRequest request)
6772
startInfo.UseShellExecute = false;
6873
startInfo.RedirectStandardOutput = true;
6974

70-
Process process = new() { StartInfo = startInfo, EnableRaisingEvents = true };
75+
(bool flowControl, ToolLaunchResult value, Process process) = LaunchProcess(startInfo);
7176

72-
try
77+
if (!flowControl)
7378
{
74-
_ = process.Start();
75-
}
76-
catch (Exception e) when (e is Win32Exception or
77-
InvalidOperationException or
78-
ObjectDisposedException or
79-
PlatformNotSupportedException)
80-
{
81-
_logger.Error(e);
82-
return new ToolLaunchResult { HasError = true, ErrorMessage = e.Message };
79+
return value;
8380
}
8481

8582
// TODO: SysoutPipe temp file is never deleted — fire-and-forget lifetime by design.
@@ -97,6 +94,7 @@ ObjectDisposedException or
9794
private static ProcessStartInfo BuildStartInfo (ToolLaunchRequest request)
9895
{
9996
var startInfo = new ProcessStartInfo(request.Cmd, request.Args);
97+
10098
if (!string.IsNullOrEmpty(request.WorkingDir))
10199
{
102100
startInfo.WorkingDirectory = request.WorkingDir;

0 commit comments

Comments
 (0)