Example #1
0
        private int RunProcess(string file, string args, string envVars)
        {
            try
            {
                var runner = new ProcessRunner();
                var(exitCode, output) = runner.Run(file, args, EnvironmentVariableHelper.ToKeyValuePair(envVars));

                _logger.LogDebug($"Process completed with exit code {exitCode}");
                _logger.LogDebug($"Process output:{Environment.NewLine}{output}");
                return(exitCode);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error running process");
            }
            return(-1);
        }
Example #2
0
        private int RunProcess(string file, string args, string envVars)
        {
            try
            {
                var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
                if (!isWindows)
                {
                    file = EnvironmentVariableHelper.ToNixEnvVars(envVars) + file;
                }

                var process = new Process()
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName  = file,
                        Arguments = args,
                        RedirectStandardOutput = true,
                        RedirectStandardError  = true,
                        UseShellExecute        = false,
                        CreateNoWindow         = true,
                    }
                };

                if (isWindows)
                {
                    var transformed = EnvironmentVariableHelper.ToKeyValuePair(envVars);
                    // Only works for windows.
                    foreach (var kv in transformed)
                    {
                        process.StartInfo.EnvironmentVariables[kv.Key] = kv.Value;
                    }
                }

                using (process)
                {
                    process.Start();
                    var stdout = new StringBuilder();
                    var stderr = new StringBuilder();

                    process.OutputDataReceived += (sender, outputLine) => { if (outputLine.Data != null)
                                                                            {
                                                                                stdout.AppendLine(outputLine.Data);
                                                                            }
                    };
                    process.ErrorDataReceived += (sender, errorLine) => { if (errorLine.Data != null)
                                                                          {
                                                                              stderr.AppendLine(errorLine.Data);
                                                                          }
                    };
                    process.BeginErrorReadLine();
                    process.BeginOutputReadLine();

                    var exited = process.WaitForExit(PROCESS_WAIT_MS);
                    if (!exited)
                    {
                        process.Kill(true);
                    }

                    _logger.LogDebug($"Process completed with exit code {process.ExitCode}");
                    _logger.LogDebug($"Process stdout:{Environment.NewLine}{stdout}");
                    _logger.LogDebug($"Process stderr:{Environment.NewLine}{stderr}");

                    return(process.ExitCode);
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error running process");
            }
            return(-1);
        }