You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
3.9 KiB
149 lines
3.9 KiB
#include <iostream>
|
|
#include <fstream>
|
|
#include <fmt/core.h>
|
|
#include <fmt/color.h>
|
|
#include <fmt/chrono.h>
|
|
#include <string>
|
|
#include "dbc.hpp"
|
|
#include "game_engine.hpp"
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include <git2.h>
|
|
#include <efsw/efsw.hpp>
|
|
#include <regex>
|
|
#include <filesystem>
|
|
|
|
using namespace std;
|
|
using namespace fmt;
|
|
namespace fs = std::filesystem;
|
|
|
|
#define BUF_MAX 1024
|
|
|
|
class UpdateListener : public efsw::FileWatchListener {
|
|
public:
|
|
bool changes = false;
|
|
git_repository* repo = nullptr;
|
|
|
|
UpdateListener(git_repository *r) : repo(r) {};
|
|
|
|
void handleFileAction(efsw::WatchID watchid,
|
|
const std::string& dir,
|
|
const std::string& filename,
|
|
efsw::Action action,
|
|
std::string oldFilename) override
|
|
{
|
|
|
|
// this is some gnarly BS here, probably tons
|
|
// of memory leaks for now but it's working
|
|
int ignored = 1;
|
|
auto the_path = fs::path(dir) / fs::path(filename);
|
|
string full_path = the_path.lexically_normal().string();
|
|
|
|
std::replace(full_path.begin(),
|
|
full_path.end(), '\\', '/');
|
|
|
|
int rc = git_ignore_path_is_ignored(&ignored, repo, full_path.c_str());
|
|
|
|
dbc::check(rc == 0, "git ignored failed.");
|
|
|
|
if(!ignored) {
|
|
println("filename={}, ignored={}", full_path.c_str(), ignored);
|
|
|
|
changes = changes || !ignored;
|
|
}
|
|
}
|
|
|
|
void reset_state() {
|
|
changes = false;
|
|
}
|
|
};
|
|
|
|
void run_build(GameEngine &game, const char* command) {
|
|
regex err_re("(.*?):([0-9]+):([0-9]+):\\s*(.*?):\\s*(.*)\n*");
|
|
|
|
char buffer[BUF_MAX]; // BUF_MAX is a define already?
|
|
ofstream stats_out;
|
|
stats_out.open("stats.csv", ios::out | 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(); });
|
|
|
|
FILE *build_out = popen(command, "r");
|
|
dbc::check(build_out != nullptr, "Failed to run command.");
|
|
|
|
while(fgets(buffer, BUF_MAX, build_out) != nullptr) {
|
|
string line(buffer); // yeah, that's probably a problem
|
|
|
|
smatch err;
|
|
bool match = 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},{},{},{},{},{}\n",
|
|
fmt::localtime(tstamp), file_name,
|
|
lnumber, col, type, message);
|
|
|
|
stats_out << result;
|
|
println("{} @ {}:{}:{} {}", type, file_name, lnumber, col, message);
|
|
|
|
game.hit(type);
|
|
}
|
|
}
|
|
|
|
stats_out.close();
|
|
dbc::post("a post test", [&]() { return !stats_out.is_open(); });
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
git_repository* repo = nullptr;
|
|
|
|
try {
|
|
dbc::check(argc == 3, "USAGE: watchgit PATH BUILD_CMD");
|
|
const char *git_path = argv[1];
|
|
const char *build_cmd = argv[2];
|
|
|
|
println("Using build command: {}", build_cmd);
|
|
efsw::FileWatcher* fileWatcher = new efsw::FileWatcher();
|
|
dbc::check(fileWatcher != nullptr, "Failed to create filewatcher.");
|
|
|
|
git_libgit2_init();
|
|
|
|
int err = git_repository_open(&repo, git_path);
|
|
dbc::check(err == 0, git_error_last()->message);
|
|
|
|
UpdateListener* listener = new UpdateListener(repo);
|
|
dbc::check(listener != nullptr, "Failed to create listener.");
|
|
|
|
print("Watching directory {} for changes...\n", git_path);
|
|
efsw::WatchID wid = fileWatcher->addWatch(git_path, listener, true);
|
|
|
|
GameEngine game{100};
|
|
|
|
while(true) {
|
|
print("WAITING...\r");
|
|
fileWatcher->watch();
|
|
|
|
if(listener->changes) {
|
|
sleep(1);
|
|
println("CHANGES! Running build {}", build_cmd);
|
|
run_build(game, build_cmd);
|
|
listener->reset_state();
|
|
}
|
|
sleep(1);
|
|
}
|
|
|
|
git_libgit2_shutdown();
|
|
} catch(dbc::Error &err) {
|
|
print("ERROR: {}\n", err.message);
|
|
if(repo != nullptr) git_repository_free(repo);
|
|
git_libgit2_shutdown();
|
|
return 1;
|
|
}
|
|
}
|
|
|