|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace GeneXus.Application |
| 6 | +{ |
| 7 | + |
| 8 | + public static class GxRunner |
| 9 | + { |
| 10 | + public static void RunAsync( |
| 11 | + string commandLine, |
| 12 | + string workingDir, |
| 13 | + string virtualPath, |
| 14 | + string schema, |
| 15 | + Action<int> onExit = null) |
| 16 | + { |
| 17 | + var stdout = new StringBuilder(); |
| 18 | + var stderr = new StringBuilder(); |
| 19 | + |
| 20 | + using var proc = new Process |
| 21 | + { |
| 22 | + StartInfo = new ProcessStartInfo |
| 23 | + { |
| 24 | + FileName = commandLine, |
| 25 | + WorkingDirectory = workingDir, |
| 26 | + UseShellExecute = false, // required for redirection |
| 27 | + CreateNoWindow = true, |
| 28 | + RedirectStandardOutput = true, |
| 29 | + RedirectStandardError = true, |
| 30 | + RedirectStandardInput = false, // flip to true only if you need to write to stdin |
| 31 | + StandardOutputEncoding = Encoding.UTF8, |
| 32 | + StandardErrorEncoding = Encoding.UTF8 |
| 33 | + }, |
| 34 | + EnableRaisingEvents = true |
| 35 | + }; |
| 36 | + |
| 37 | + proc.StartInfo.ArgumentList.Add(virtualPath); |
| 38 | + proc.StartInfo.ArgumentList.Add(schema); |
| 39 | + |
| 40 | + proc.OutputDataReceived += (_, e) => |
| 41 | + { |
| 42 | + if (e.Data is null) return; |
| 43 | + stdout.AppendLine(e.Data); |
| 44 | + Console.WriteLine(e.Data); // forward to parent console (stdout) |
| 45 | + }; |
| 46 | + |
| 47 | + proc.ErrorDataReceived += (_, e) => |
| 48 | + { |
| 49 | + if (e.Data is null) return; |
| 50 | + stderr.AppendLine(e.Data); |
| 51 | + Console.Error.WriteLine(e.Data); // forward to parent console (stderr) |
| 52 | + }; |
| 53 | + |
| 54 | + proc.Exited += (sender, e) => |
| 55 | + { |
| 56 | + var p = (Process)sender!; |
| 57 | + int exitCode = p.ExitCode; |
| 58 | + p.Dispose(); |
| 59 | + |
| 60 | + Console.WriteLine($"[{DateTime.Now:T}] Process exited with code {exitCode}"); |
| 61 | + |
| 62 | + // Optional: call user-provided callback |
| 63 | + onExit?.Invoke(exitCode); |
| 64 | + }; |
| 65 | + |
| 66 | + if (!proc.Start()) |
| 67 | + throw new InvalidOperationException("Failed to start process"); |
| 68 | + Console.WriteLine($"[{DateTime.Now:T}] MCP Server Started."); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments