Skip to content

Commit

Permalink
changes to uv_watch and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lsproule committed Dec 18, 2023
1 parent 6e20bd5 commit 6141492
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 71 deletions.
2 changes: 1 addition & 1 deletion include/Frate/Command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ namespace Frate::Command {
std::string homepage{"null"};
std::string bugs{"null"};
std::string lang{"cpp"};
std::string cmake_version{"3.10"};
std::string cmake_version{"3.28"};
std::string lang_version{"20"};
std::string compiler{"g++"};
std::string license{""};
Expand Down
34 changes: 0 additions & 34 deletions memcheck

This file was deleted.

117 changes: 81 additions & 36 deletions src/Command/Actions/UvWatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,35 @@ namespace Frate::Command::UvWatch{
return inter->parse();
}
//TODO Move to RemoteServer.hpp

std::optional<json> parseJsonFile(const std::string& filePath) {
std::ifstream file(filePath);
if (!file) {
std::cerr << "Unable to open file: " << filePath << std::endl;
return std::nullopt;
}

try {
json j = json::parse(file);
return j;
} catch (const json::parse_error& e) {
std::cerr << "JSON parse error in file " << filePath << ": " << e.what() << std::endl;
return std::nullopt;
}
}

RemoteServer get_current_build_server() {
std::string current_build_server = std::string(std::getenv("HOME")) +
"/.config/frate/" +
"current_build_server.json";
json current_build_server_json =
json::parse(std::ifstream(current_build_server));
std::optional<json> data = parseJsonFile(current_build_server);
if (!data.has_value()) {
std::cerr << "No current build server found" << std::endl;
std::cerr << "Please run frate add server to add a server" << std::endl;
std::cerr << "Please run frate set server to set a default a server" << std::endl;
return RemoteServer();
}
json current_build_server_json = data.value();
if (!current_build_server_json["name"].is_null()) {
return RemoteServer(
current_build_server_json["name"].get<std::string>(),
Expand All @@ -34,36 +57,44 @@ namespace Frate::Command::UvWatch{
return RemoteServer();
}

bool runCommand(Interface* inter){
//TODO CLEAN UP THIS SHIT
std::string command = "cmake . && make && " + inter->pro->path.string() + "/" + inter->pro->build_dir + "/" +inter->pro->name;
#ifdef DEBUG
command = "cd build && cmake . && make && " + inter->pro->build_dir + "/" +inter->pro->name;
#endif
std::string remote_build_command(std::shared_ptr<Interface> inter) {
std::string command;
std::string current_build_server = std::string(std::getenv("HOME")) +
"/.config/frate/" +
"current_build_server.json";
inter->pro->build_server = get_current_build_server();

std::string sync_files = "rsync -avh --exclude-from='.gitignore' --update -e 'ssh -p " +
std::to_string(inter->pro->build_server.port) + "' --progress . " +
inter->pro->build_server.username + "@" +inter->pro->build_server.ip +
":/tmp/frate ";

bool build_server =inter->args->operator[]("remote-build").as<bool>();
if (build_server == true) {
std::string current_build_server = std::string(std::getenv("HOME")) +
"/.config/frate/" +
"current_build_server.json";
inter->pro->build_server = get_current_build_server();
std::string ssh = "&& ssh -p " + std::to_string(inter->pro->build_server.port) + " " +
inter->pro->build_server.username + "@" +inter->pro->build_server.ip;

std::string ssh_command =
" 'cd /tmp/frate && cmake . && make -j ${nproc} && " + inter->pro->build_dir + "/" + inter->pro->name + "'";

command = sync_files + ssh + ssh_command;
return command;
}

std::string sync_files = "rsync -avh --exclude-from='.gitignore' --update -e 'ssh -p " +
std::to_string(inter->pro->build_server.port) + "' --progress . " +
inter->pro->build_server.username + "@" +inter->pro->build_server.ip +
":/tmp/frate ";

std::string ssh = "&& ssh -p " + std::to_string(inter->pro->build_server.port) + " " +
inter->pro->build_server.username + "@" +inter->pro->build_server.ip;
bool runCommand(std::shared_ptr<Interface> inter){
//TODO CLEAN UP THIS SHIT
std::string command = "cmake . && make && " + inter->pro->path.string() + "/" + inter->pro->build_dir + "/" +inter->pro->name;
#ifdef DEBUG
command = "cd build && cmake . && make && " + inter->pro->build_dir + "/" +inter->pro->name;
#endif

std::string ssh_command =
" 'cd /tmp/frate && cmake . && make -j ${nproc} && " + inter->pro->build_dir + "/" + inter->pro->name + "'";

command = sync_files + ssh + ssh_command;
bool build_server =inter->args->operator[]("remote-build").as<bool>();
if (build_server == true) {

command = remote_build_command(inter);
}
if (inter->args->count("args") != 0) {
// historical reasons
std::cout << "estamos aqui" << std::endl;
std::vector<std::string> args_vec =
inter->args->operator[]("args").as<std::vector<std::string>>();
Expand Down Expand Up @@ -96,7 +127,8 @@ namespace Frate::Command::UvWatch{
// Ignoring subsequent events
return;
}
Interface* inter = static_cast<Interface*>(handle->data);
std::shared_ptr<Interface> inter = *static_cast<std::shared_ptr<Interface>*>(handle->data);

if (status < 0) {
fprintf(stderr, "Filesystem watch error: %s\n", uv_strerror(status));
return;
Expand All @@ -112,19 +144,20 @@ namespace Frate::Command::UvWatch{
timer->data = handle;

uv_timer_start(timer, [](uv_timer_t *timer) {
event_triggered = 0;
uv_close((uv_handle_t *)timer, [](uv_handle_t *handle) {
free(handle);
});
}, 2000, 0); // 1000 ms delay
event_triggered = 0;
uv_close((uv_handle_t *)timer, [](uv_handle_t *handle) {
free(handle);
});
delete timer;
}, 2000, 0); // 1000 ms delay
}



#ifdef __linux__
namespace fs = std::filesystem;

void start_watchers_for_directory(const fs::path& path, uv_loop_t* loop, std::vector<uv_fs_event_t*>& watchers,std::shared_ptr<Interface> inter) {
void start_watchers_for_directory(const fs::path& path, uv_loop_t* loop, std::vector<uv_fs_event_t*>& watchers, std::shared_ptr<Interface> inter) {
for (const auto& entry : fs::directory_iterator(path)) {
if (fs::is_directory(entry)) {
uv_fs_event_t* watcher = new uv_fs_event_t{
Expand All @@ -140,22 +173,34 @@ namespace Frate::Command::UvWatch{
#endif

bool watch(std::shared_ptr<Interface> inter){

options(inter);
uv_loop_t *loop = uv_default_loop();
std::vector<uv_fs_event_t*> watchers;
std::vector<uv_fs_event_t*> watchers;


uv_fs_event_t fs_event{.data = inter.get()};
uv_fs_event_init(loop, &fs_event);
#ifdef __linux__
start_watchers_for_directory(inter->pro->src_dir, loop, watchers, inter);
#endif
if (uv_fs_event_start(&fs_event, fs_event_callback, (inter->pro->path / inter->pro->src_dir).c_str(), UV_FS_EVENT_RECURSIVE)!=0) {
fprintf(stderr, "Error starting filesystem watcher.\n");
return 1;
}
std::cout << "Watching for changes in " << inter->pro->src_dir << std::endl;
return uv_run(loop, UV_RUN_DEFAULT) == 0;

if (uv_run(loop, UV_RUN_DEFAULT) != 0) {
std::cerr << "Error running uv loop" << std::endl;
return false;
}

#ifdef __linux__
start_watchers_for_directory(inter->pro->src_dir, loop, watchers, inter);
#endif

int close = uv_loop_close(loop);
if (close != 0) {
std::cerr << "Error closing uv loop" << std::endl;
return false;
}

return true;
};
}
12 changes: 12 additions & 0 deletions src/Command/RemoteServers/RemoteServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,21 @@ namespace Frate::Command::RemoteServers{
}
return servers;
}
bool validateServerInput(std::string input){
if(input.empty()){
return false;
}
if (input.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_") != std::string::npos){
return false;
}
return true;
}

bool getServerName(std::string& name){
Prompt name_promp("Enter the name of the server: ");
name_promp.setValidator(validateServerInput);
name_promp.run();

auto[valid, _name] = name_promp.get<std::string>();
if(!valid){
return false;
Expand Down

0 comments on commit 6141492

Please sign in to comment.