Skip to content

Commit 8836d7a

Browse files
committed
Simplified the process helper and removed redundant code.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 3a1df3e commit 8836d7a

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

QtSharp.CLI/Program.cs

-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ public static int Main(string[] args)
203203
var libFile = qt.LibFiles[i];
204204
var libFileName = Path.GetFileNameWithoutExtension(libFile);
205205
if (Path.GetExtension(libFile) == ".exe" ||
206-
// QtDeclarative is obsolete and at the same time its headers cause conflicts with its successor of QtQuick
207-
libFileName == "QtDeclarative" || libFileName == "Qt5Declarative" ||
208206
// QtQuickTest is a QML module but has 3 main C++ functions and is not auto-ignored
209207
libFileName == "QtQuickTest" || libFileName == "Qt5QuickTest")
210208
{

QtSharp/CompileInlinesPass.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private void InvokeCompiler(string dir, string inlines, string makefile)
9393
try
9494
{
9595
string error;
96-
ProcessHelper.Run(this.make, string.Format("-j{0} -f {1}", Environment.ProcessorCount + 1, makefile), out error, true);
96+
ProcessHelper.Run(this.make, $"-f {makefile}", out error);
9797
if (string.IsNullOrEmpty(error))
9898
{
9999
var parserOptions = new ParserOptions();

QtSharp/ProcessHelper.cs

+38-37
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,57 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Text;
34

45
namespace QtSharp
56
{
67
public class ProcessHelper
78
{
8-
public static string Run(string path, string args, out string error, bool readOutputByLines = false)
9+
public static string Run(string path, string args, out string error)
910
{
10-
try
11+
using (Process process = new Process())
1112
{
12-
using (Process process = new Process())
13-
{
14-
Console.WriteLine("Run: " + path);
15-
Console.WriteLine("Args: " + args);
16-
process.StartInfo.FileName = path;
17-
process.StartInfo.Arguments = args;
18-
process.StartInfo.UseShellExecute = false;
19-
process.StartInfo.RedirectStandardOutput = true;
20-
process.StartInfo.RedirectStandardError = true;
13+
Console.WriteLine("Run: " + path);
14+
Console.WriteLine("Args: " + args);
15+
process.StartInfo.FileName = path;
16+
process.StartInfo.Arguments = args;
17+
process.StartInfo.UseShellExecute = false;
18+
process.StartInfo.RedirectStandardOutput = true;
19+
process.StartInfo.RedirectStandardError = true;
2120

22-
var reterror = "";
23-
var retout = "";
24-
process.OutputDataReceived += (sender, outargs) =>
21+
var reterror = new StringBuilder();
22+
var retout = new StringBuilder();
23+
process.OutputDataReceived += (sender, outargs) =>
2524
{
26-
if (retout != "" && outargs.Data != "") retout += "\r\n";
27-
retout += outargs.Data;
28-
Console.WriteLine("stdout: {0}", retout);
25+
if (!string.IsNullOrEmpty(outargs.Data))
26+
{
27+
if (retout.Length > 0)
28+
retout.AppendLine();
29+
retout.Append(outargs.Data);
30+
Console.WriteLine("stdout: {0}", outargs.Data);
31+
}
2932
};
30-
process.ErrorDataReceived += (sender, errargs) =>
33+
process.ErrorDataReceived += (sender, errargs) =>
3134
{
32-
if (reterror != "" && errargs.Data != "") reterror += "\r\n";
33-
reterror += errargs.Data;
34-
Console.WriteLine("stderr: {0}", reterror);
35+
if (!string.IsNullOrEmpty(errargs.Data))
36+
{
37+
if (reterror.Length > 0)
38+
reterror.AppendLine();
39+
reterror.Append(errargs.Data);
40+
Console.WriteLine("stderr: {0}", errargs.Data);
41+
}
3542
};
3643

37-
process.Start();
38-
process.BeginOutputReadLine();
39-
process.BeginErrorReadLine();
40-
process.WaitForExit();
41-
process.CancelOutputRead();
42-
process.CancelErrorRead();
44+
process.Start();
45+
process.BeginOutputReadLine();
46+
process.BeginErrorReadLine();
47+
process.WaitForExit();
48+
process.CancelOutputRead();
49+
process.CancelErrorRead();
4350

44-
error = reterror;
45-
if (process.ExitCode != 0)
46-
throw new Exception("Exit Code is not 0");
47-
return readOutputByLines ? string.Empty : retout.Trim().Replace(@"\\", @"\");
48-
}
49-
}
50-
catch (Exception exception)
51-
{
52-
error = string.Format("Calling {0} caused an exception: {1}.", path, exception.Message);
53-
return string.Empty;
51+
error = reterror.ToString();
52+
if (process.ExitCode != 0)
53+
Console.WriteLine($"Error. Process exited with code {process.ExitCode}.");
54+
return retout.ToString();
5455
}
5556
}
5657
}

0 commit comments

Comments
 (0)