-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadCommand.cpp
161 lines (124 loc) · 3.39 KB
/
ThreadCommand.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "ThreadCommand.h"
#include <queue>
#include <thread>
#include <unistd.h>
#include <utility>
#include "App.h"
#include "ConsoleApp.h"
extern App *g_app;
#ifdef __WIN32__
char separator[] = " && \0";
#else
char separator[] = "\n\0";
#endif
int exec(std::string cmd) {
if (!cmd.starts_with("echo"))
console.AddLog("%s", cmd.c_str());
char buffer[128];
std::string output;
cmd.append(" 2>&1");
if (g_app && !g_app->project.project_settings.project_directory.empty())
chdir(g_app->project.project_settings.project_directory.c_str());
#ifdef DEBUG
{
char buff[FILENAME_MAX];
getcwd(buff, FILENAME_MAX);
console.AddLog("# WKDIR: %s", buff);
}
#endif
auto pipe = popen(cmd.c_str(), "r");
if (!pipe)
throw std::runtime_error("popen() failed!");
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != nullptr) {
output += buffer;
}
}
if (!output.empty()) {
console.AddLog("%s", output.c_str());
}
int result = pclose(pipe);
if (g_app && !g_app->project.project_settings.project_directory.empty())
chdir(g_app->GetEngineDirectory().c_str());
return result;
}
int exec_result(std::string cmd, std::string &result) {
if (!cmd.starts_with("echo"))
console.AddLog("%s", cmd.c_str());
result.clear();
char buffer[128];
std::string output;
cmd.append(" 2>&1");
if (g_app && !g_app->project.project_settings.project_directory.empty())
chdir(g_app->project.project_settings.project_directory.c_str());
#ifdef DEBUG
{
char buff[FILENAME_MAX];
getcwd(buff, FILENAME_MAX);
console.AddLog("# WKDIR: %s", buff);
}
#endif
auto pipe = popen(cmd.c_str(), "r");
if (!pipe)
throw std::runtime_error("popen() failed!");
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != nullptr) {
result += buffer;
}
}
int result_code = pclose(pipe);
if (g_app && !g_app->project.project_settings.project_directory.empty())
chdir(g_app->GetEngineDirectory().c_str());
return result_code;
}
static bool is_running_command;
static std::string current_command;
static std::queue<std::string> command_queue;
static void run_next_command();
static void command_thread(const char *command) {
int result = exec(command);
bool success = result == EXIT_SUCCESS;
if (success) {
if (command_queue.empty()) {
is_running_command = false;
} else {
run_next_command();
}
} else {
console.AddLog("[error] Process returned %d.", result);
if (!command_queue.empty()) {
// on failure stop all other queued commands
while (!command_queue.empty()) {
command_queue.pop();
}
}
is_running_command = false;
}
}
static void run_next_command() {
current_command = command_queue.front();
std::thread(command_thread, current_command.c_str()).detach();
command_queue.pop();
}
void ThreadCommand::QueueCommand(std::string command) {
command_queue.push(std::move(command));
if (is_running_command)
return;
is_running_command = true;
run_next_command();
}
static int run_command(std::string command) {
return exec(command);
}
static int run_command_result(std::string command, std::string &result) {
return exec_result(command, result);
}
int ThreadCommand::RunCommand(std::string command) {
return run_command(std::move(command));
}
int ThreadCommand::RunCommand(std::string command, std::string &result) {
return run_command_result(std::move(command), result);
}
void ThreadCommand::RunCommandDetached(std::string command) {
std::thread(run_command, command).detach();
}