Example #1
0
        public ProcessExecutionResult Start()
        {
            var result  = new ProcessExecutionResult();
            var process = new Process();

            var startInfo = process.StartInfo;

            startInfo.FileName  = ToolName;
            startInfo.Arguments = Arguments;
            if (WorkingDirectory != null)
            {
                startInfo.WorkingDirectory = WorkingDirectory;
            }
            startInfo.RedirectStandardInput  = true;
            startInfo.RedirectStandardError  = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute        = false;
            startInfo.CreateNoWindow         = true;

            process.ErrorDataReceived += (sender, e) =>
            {
                EventHandler <string> errorDataReceived = ErrorDataReceived;
                if (!string.IsNullOrEmpty(e.Data) && errorDataReceived != null)
                {
                    errorDataReceived(sender, e.Data);
                }
            };
            process.OutputDataReceived += (sender, e) =>
            {
                EventHandler <string> outputDataReceived = OutputDataReceived;
                if (!string.IsNullOrEmpty(e.Data) && outputDataReceived != null)
                {
                    outputDataReceived(sender, e.Data);
                }
            };
            process.EnableRaisingEvents = true;
            process.Start();
            process.StandardInput.WriteLine();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            result.ProcessId = process.Id;
            if (WaitForExit)
            {
                if (Timeout == 0)
                {
                    process.WaitForExit();
                }
                else
                {
                    process.WaitForExit(Timeout);
                }
            }
            result.ExitCode = process.ExitCode;

            return(result);
        }
Example #2
0
        public void Extract(string zipPath, string extractPath)
        {
            if (!Is7zInstalled)
            {
                throw new Exception($"{ToolName} tool is not installed");
            }

            string errorMessage = null;

            try
            {
                var processor = new Processor(ToolName, $@"x ""{zipPath}"" -o""{extractPath}"" -y");
                processor.ErrorDataReceived += (sender, error) =>
                {
                    Logger.LogInfo($"{ToolName}: error: {error}");
                };
                processor.OutputDataReceived += (sender, message) =>
                {
                    Logger.LogInfo($"{ToolName}: {message}");
                };
                ProcessExecutionResult execResult = processor.Start();

                if (execResult.ExitCode != 0)
                {
                    errorMessage = $"{ToolName} error while extracting {extractPath}. Exit code: {execResult.ExitCode}";
                }
            }
            catch (Exception ex) when(!(ex is ThreadAbortException))
            {
                errorMessage = $"{ToolName} error while extracting {extractPath}: {ex.Message}";
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                throw new ReadException(new TextFile("")
                {
                    Name = zipPath
                }, message: errorMessage);
            }
        }
Example #3
0
        public void Render(string filePath)
        {
            if (!IsDotInstalled)
            {
                Logger.LogError(new FileNotFoundException("dot tool (Graphviz) is not installed"));
                return;
            }

            string ext = Path.GetExtension(filePath);

            if (ext.Length > 0)
            {
                ext = ext.Substring(1);
            }
            GraphvizOutputFormat outputFormat = OutputFormat;
            string imagePath = filePath;
            bool   appendExt = false;

            if (OutputFormat == GraphvizOutputFormat.None)
            {
                if (!Enum.TryParse(ext, true, out outputFormat))
                {
                    outputFormat = GraphvizOutputFormat.Png;
                    imagePath    = $"{imagePath}.{outputFormat.ToString().ToLowerInvariant()}";
                    appendExt    = true;
                }
            }
            else
            {
                string outputFormatString = OutputFormat.ToString().ToLowerInvariant();
                if (!filePath.EndsWith(outputFormatString))
                {
                    imagePath = $"{imagePath}.{outputFormatString}";
                    appendExt = true;
                }
            }

            string dotFilePath = appendExt ? filePath + ".dot" : Path.ChangeExtension(filePath, "dot");

            File.WriteAllText(dotFilePath, DotGraph);

            var processor = new Processor("dot")
            {
                Arguments = $"\"{dotFilePath}\" -T{outputFormat.ToString().ToLowerInvariant()} -o \"{imagePath}\""
            };

            processor.ErrorDataReceived += (sender, e) =>
            {
                Logger.LogError(new Exception($"Error while graph rendering: {e}"));
            };
            ProcessExecutionResult executionResult = processor.Start();

            if (!SaveDot)
            {
                File.Delete(dotFilePath);
            }

            if (executionResult.ExitCode != 0)
            {
                Logger.LogError(new Exception($"Graph rendering completed with error code {executionResult.ExitCode}"));
            }
        }