aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Console/ConsoleExecutionEngine.cs
blob: c4bdee0a8bc9f50e9b7ca4e149a499249a8c8ed2 (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
118
119
120
121
122
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 CreateResult(parsedCommand.Arguments, String.Empty);
                    }
                    else if (Directory.Exists(Path.Combine(command.WorkingFolder, parsedCommand.Arguments)))
                    {
                        return CreateResult(Path.GetFullPath(Path.Combine(command.WorkingFolder, parsedCommand.Arguments)), String.Empty);
                    }
                    else
                    {
                        return CreateResult(null, "The directory does not exists.");
                    }
                }

                String output = String.Empty;
                String error = String.Empty;

                var process = new Process();
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.RedirectStandardOutput = true;
                if (command.WorkingFolder != null)
                {
                    process.StartInfo.WorkingDirectory = command.WorkingFolder;
                }
                //process.StartInfo.Verb = "runas";

                if (ConsoleDictionary.GetKnownCommands().Exists(x => x.Name.ToLower() == parsedCommand.Command))
                {
                    process.StartInfo.FileName = "cmd.exe";
                    process.StartInfo.Arguments = "/C " + command.Command;
                }
                else
                {
                    process.StartInfo.FileName = parsedCommand.Command;
                    process.StartInfo.Arguments = parsedCommand.Arguments;
                }

                process.Start();

                bool hasGuid = false;

                try
                {
                    process.WaitForInputIdle(5000);
                    hasGuid = true;
                }
                catch { }

                if (!hasGuid)
                {
                    output = process.StandardOutput.ReadToEnd();
                    error = process.StandardError.ReadToEnd();
                }

                return CreateResult(command.WorkingFolder, String.IsNullOrWhiteSpace(error) ? output.Replace(command.WorkingFolder + ">", "") : error);
            });
        }

        private ConsoleCommandExecutionResult CreateResult(String workingFolder, String output)
        {
            ConsoleCommandExecutionResult result = new ConsoleCommandExecutionResult();
            result.WorkingFolder = workingFolder;
            result.Output = output;

            foreach (var dir in Directory.GetDirectories(Path.GetFullPath(workingFolder)))
            {
                result.Suggestions.Add(new ConsoleSuggestion()
                {
                    Type = ConsoleSuggestionType.Folder,
                    Name = Path.GetFileName(dir)
                });
            }

            foreach (var file in Directory.GetFiles(Path.GetFullPath(workingFolder)))
            {
                result.Suggestions.Add(new ConsoleSuggestion()
                {
                    Type = ConsoleSuggestionType.File,
                    Name = Path.GetFileName(file),
                });
            }

            return result;
        }
    }
}