using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core.IO; namespace Tango.SQLExaminer { public class ExaminerProcess { public String ConfigurationFile { get; private set; } public ExaminerProcessType ProcessType { get; set; } public event EventHandler Progress; public ExaminerProcess(String configurationFile, ExaminerProcessType type) { ConfigurationFile = configurationFile; ProcessType = type; } public ExaminerProcess(ExaminerConfiguration configuration, ExaminerProcessType type) { ConfigurationFile = TemporaryManager.Default.CreateFile(); configuration.ToFile(ConfigurationFile); ProcessType = type; } public Task Execute() { return Task.Factory.StartNew(() => { Process p = new Process(); p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.CreateNoWindow = true; if (ProcessType == ExaminerProcessType.Schema) { p.StartInfo.FileName = Helper.SQL_EXAMINER_FOLDER + "\\SQLECmd.exe"; } else { p.StartInfo.FileName = Helper.SQL_EXAMINER_FOLDER + "\\SQLDECmd.exe"; } var sb = new StringBuilder(); p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.OutputDataReceived += (sender, args) => { sb.AppendLine(args.Data); Progress?.Invoke(this, args.Data); }; p.ErrorDataReceived += (sender, args) => { sb.AppendLine(args.Data); Progress?.Invoke(this, args.Data); }; p.StartInfo.UseShellExecute = false; p.StartInfo.Arguments = String.Format("/CONFIG:\"{0}\"", ConfigurationFile); p.Start(); p.BeginOutputReadLine(); p.BeginErrorReadLine(); p.WaitForExit(); return new ExaminerProcessResult() { ExitCode = (ExaminerProcessExitCode)p.ExitCode, Output = sb.ToString() }; }); } } }