aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Core/Components/CmdCommand.cs
blob: 47bb663613ea58bc4bb1aecd2f50441640e36c83 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;

namespace Tango.Core.Components
{
    public class CmdCommand : ExtendedObject
    {
        public enum OutEncoding
        {
            Default,
            Unicode,
        }

        public enum ExitStrategies
        {
            WaitForExit,
            StandardOutput,
        }

        public class CmdCommandResult
        {
            public String StandardOutput { get; set; }
            public String StandardError { get; set; }
            public int ExitCode { get; set; }
        }

        private Process _process;

        public String Arguments { get; set; }

        public TimeSpan Timeout { get; set; }

        public String WorkingDir { get; set; }

        public OutEncoding OutputEncoding { get; set; }

        public ExitStrategies ExitStrategy { get; set; }

        public CmdCommand(String processName, String arguments)
        {
            Timeout = TimeSpan.FromSeconds(5);

            _process = new Process();
            _process.StartInfo.CreateNoWindow = true;
            _process.StartInfo.FileName = processName;
            _process.StartInfo.UseShellExecute = false;
            _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            _process.StartInfo.RedirectStandardError = true;
            _process.StartInfo.RedirectStandardOutput = true;
            _process.StartInfo.Arguments = arguments;
            _process.StartInfo.Verb = "runas";

            if (WorkingDir != null)
            {
                _process.StartInfo.WorkingDirectory = WorkingDir;
            }

            Arguments = arguments;
        }

        public Task<CmdCommandResult> Run()
        {
            return Task.Factory.StartNew<CmdCommandResult>(() =>
            {
                CmdCommandResult result = new CmdCommandResult();

                LogManager.Log($"Starting process {_process.StartInfo.FileName} with arguments {Arguments}...");
                _process.Start();

                if (ExitStrategy == ExitStrategies.WaitForExit)
                {
                    _process.WaitForExit((int)Timeout.TotalMilliseconds);
                }

                if (_process.HasExited || ExitStrategy == ExitStrategies.StandardOutput)
                {
                    String output = _process.StandardOutput.ReadToEnd();
                    String error = _process.StandardError.ReadToEnd();

                    if (OutputEncoding == OutEncoding.Unicode)
                    {
                        byte[] data = Encoding.Default.GetBytes(output);
                        output = Encoding.Unicode.GetString(data);

                        data = Encoding.Default.GetBytes(error);
                        error = Encoding.Unicode.GetString(data);
                    }

                    LogManager.Log($"Process exited with exit code {_process.ExitCode}.");
                    LogManager.Log($"Process Standard Output:\n{output}");
                    LogManager.Log($"Process Standard Error:\n{error}");

                    if (_process.ExitCode != 0)
                    {
                        throw new IOException($"The process {_process.StartInfo.FileName} has exited with the code {_process.ExitCode}.\n{error}");
                    }

                    result.StandardOutput = output;
                    result.StandardError = error;
                    result.ExitCode = _process.ExitCode;

                    return result;
                }
                else
                {
                    throw new TimeoutException($"The process {_process.StartInfo.FileName} has not exited within the given timeout of {Timeout.TotalSeconds} seconds.");
                }
            });
        }
    }
}