aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SQLExaminer/ExaminerProcess.cs
blob: ef088fdbae7d9dbd0b8e9cfa154aa38ef82df370 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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<String> 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<ExaminerProcessResult> Execute()
        {
            return Task.Factory.StartNew<ExaminerProcessResult>(() =>
            {
                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()
                };
            });
        }
    }
}