First step in refactoring out the build running from the gui for later thread fiascos.

This commit is contained in:
Zed A. Shaw 2024-09-08 20:01:10 -04:00
parent b0c9fefa9b
commit 0bac4dbfd9
5 changed files with 125 additions and 80 deletions

View file

@ -36,71 +36,60 @@ Builder::Builder(GUI &g, GameEngine &engine)
config["build_cmd"].template get_to<string>(build_cmd);
}
void Builder::run_build() {
std::regex err_re("(.*?):([0-9]+):([0-9]+):\\s*(.*?):\\s*(.*)\n*");
char buffer[BUF_MAX]; // BUF_MAX is a define already?
std::ofstream stats_out;
stats_out.open("stats.csv", std::ios::out | std::ios::app);
std::time_t tstamp = std::time(nullptr);
dbc::check(stats_out.good(), "Error opening stats.csv file.");
dbc::pre("simple test", [&]() { return stats_out.good(); });
// need to catch the error message when the command is bad
FILE *start_command(string &build_cmd) {
FILE *build_out = popen(build_cmd.c_str(), "r");
dbc::check(build_out != nullptr, "Failed to run command.");
return build_out;
}
game.start_round();
string read_line(FILE *build_out, bool &done_out) {
char buffer[BUF_MAX];
char *res = fgets(buffer, BUF_MAX, build_out);
//!!! exception if res == nullptr
done_out = res == nullptr;
while(fgets(buffer, BUF_MAX, build_out) != nullptr) {
string line(buffer); // yeah, that's probably a problem
std::smatch err;
bool match = std::regex_match(line, err, err_re);
if(match) {
string file_name = err[1].str();
string lnumber = err[2].str();
string col = err[3].str();
string type = err[4].str();
string message = err[5].str();
string result = format("{:%FT%T},{},{},{},{},{}",
fmt::localtime(tstamp), file_name,
lnumber, col, type, message);
stats_out << result;
gui.output(format("HIT WITH {} @ {}:{}:{} {}", type, file_name, lnumber, col, message));
game.hit(type);
// refactor this
if(game.is_dead()) {
gui.you_died();
}
}
if(!done_out) {
return string{buffer}; // yeah, that's probably a problem
} else {
return "";
}
}
game.end_round();
void end_build(FILE *build_out, GUI &gui, string &build_cmd) {
int rc = pclose(build_out);
if(rc == 0) {
gui.build_works();
} else {
gui.output(format(">>>>>>>> DIED? {}", game.is_dead()));
gui.build_failed(!game.is_dead(), build_cmd);
// BUG: make it not play two sounds
gui.build_failed(true, build_cmd);
}
stats_out.close();
dbc::post("a post test", [&]() { return !stats_out.is_open(); });
}
void Builder::run_build(const string &line) {
std::regex err_re("(.*?):([0-9]+):([0-9]+):\\s*(.*?):\\s*(.*)\n*");
std::smatch err;
bool match = std::regex_match(line, err, err_re);
if(match) {
string file_name = err[1].str();
string lnumber = err[2].str();
string col = err[3].str();
string type = err[4].str();
string message = err[5].str();
gui.output(format("HIT WITH {} @ {}:{}:{} {}", type, file_name, lnumber, col, message));
game.hit(type);
}
}
enum BuildState {
WAITING, BUILDING, DONE
};
void Builder::run() {
git_repository* repo = nullptr;
@ -120,22 +109,53 @@ void Builder::run() {
gui.output(format("Watching directory {} for changes...", git_path));
efsw::WatchID wid = fileWatcher->addWatch(git_path, listener, true);
FILE *build_out = NULL;
bool build_done = false;
BuildState state = WAITING;
fileWatcher->watch();
int rc = gui.main_loop(game, [&] {
fileWatcher->watch();
switch(state) {
case WAITING:
if(listener->changes) {
game.start_round();
gui.building();
gui.output(format("CHANGES! Running build {}", build_cmd));
build_out = start_command(build_cmd);
state = BUILDING;
}
break;
if(listener->changes) {
gui.building();
gui.output(format("CHANGES! Running build {}", build_cmd));
case BUILDING: {
// check if there's output
string line = read_line(build_out, build_done);
if(build_done) {
end_build(build_out, gui, build_cmd);
build_out = NULL;
state = DONE;
} else {
// get the command line
// run the build if it's available
run_build(line);
state = BUILDING;
}
}
break;
run_build();
case DONE: {
game.end_round();
if(game.is_dead()) {
gui.output("!!!! YOU DIED! !!!! Learn to code luser.");
gui.you_died();
game.reset();
}
listener->reset_state();
gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^");
state = WAITING;
}
break;
}
return 0;