aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Console/ConsoleExecutionEngine.cs
blob: 194ff8454063fe5f4388b33774de8d1784bd873a (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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Console
{
    public class ConsoleExecutionEngine
    {
        private class ParsedCommand
        {
            public String Command { get; set; }
            public String Arguments { get; set; }

            public ParsedCommand(ConsoleCommandDTO dto)
            {
                String[] s = dto.Command.Split(' ');
                Command = s.First();
                Arguments = String.Join(" ", s.Skip(1));
            }
        }

        public Task<ConsoleCommandExecutionResult> Execute(ConsoleCommandDTO command)
        {
            return Task.Factory.StartNew<ConsoleCommandExecutionResult>(() =>
            {
                ParsedCommand parsedCommand = new ParsedCommand(command);

                if (parsedCommand.Command.ToLower() == "cd")
                {
                    if (parsedCommand.Arguments.Contains(":\\") && Directory.Exists(parsedCommand.Arguments))
                    {
                        return new ConsoleCommandExecutionResult()
                        {
                            Output = String.Empty,
                            WorkingFolder = parsedCommand.Arguments,
                        };
                    }
                    else if (Directory.Exists(Path.Combine(command.WorkingFolder, parsedCommand.Arguments)))
                    {
                        return new ConsoleCommandExecutionResult()
                        {
                            Output = String.Empty,
                            WorkingFolder = Path.GetFullPath(Path.Combine(command.WorkingFolder, parsedCommand.Arguments)),
                        };
                    }
                    else
                    {
                        return new ConsoleCommandExecutionResult()
                        {
                            Output = "The directory does not exists.",
                        };
                    }
                }

                var process = new Process();
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.Arguments = "/C " + command.Command;
                //process.StartInfo.Verb = "runas";

                if (command.WorkingFolder != null)
                {
                    process.StartInfo.WorkingDirectory = command.WorkingFolder;
                }

                process.Start();

                String output = process.StandardOutput.ReadToEnd();
                String error = process.StandardError.ReadToEnd();

                return new ConsoleCommandExecutionResult()
                {
                    WorkingFolder = command.WorkingFolder,
                    Output = String.IsNullOrWhiteSpace(error) ? output.Replace(command.WorkingFolder + ">", "") : error,
                };
            });
        }
    }
}