From f5240dd77f3c57a5ab7d2e7f4d524585408dd2be Mon Sep 17 00:00:00 2001 From: ggod <46885632+GGodPL@users.noreply.github.com> Date: Sat, 9 Aug 2025 01:56:08 +0200 Subject: [PATCH 1/2] fix wakatime cli handling --- .github/workflows/multi-platform.yml | 2 ++ .gitmodules | 3 ++ CMakeLists.txt | 5 +++ external/tiny-process-library | 1 + src/wakatime/cli.cpp | 47 ++++++++++++++-------------- src/wakatime/tracker.cpp | 1 + 6 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 .gitmodules create mode 160000 external/tiny-process-library diff --git a/.github/workflows/multi-platform.yml b/.github/workflows/multi-platform.yml index f713975..a1bf5b2 100644 --- a/.github/workflows/multi-platform.yml +++ b/.github/workflows/multi-platform.yml @@ -35,6 +35,8 @@ jobs: steps: - uses: actions/checkout@v4 + with: + submodules: recursive - name: Build the mod uses: geode-sdk/build-geode-mod@main diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e9911cd --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "external/tiny-process-library"] + path = external/tiny-process-library + url = https://gitlab.com/eidheim/tiny-process-library.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 24077eb..f10d6e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,11 @@ file(GLOB_RECURSE SOURCES src/*.cpp src/*/*.cpp) # Set up the mod binary add_library(${PROJECT_NAME} SHARED ${SOURCES}) +add_subdirectory(external/tiny-process-library ${CMAKE_CURRENT_BINARY_DIR}/tiny-process-library) + +target_include_directories(${PROJECT_NAME} PRIVATE external/tiny-process-library) +target_link_libraries(${PROJECT_NAME} tiny-process-library) + if (NOT DEFINED ENV{GEODE_SDK}) message(FATAL_ERROR "Unable to find Geode SDK! Please define GEODE_SDK environment variable to point to Geode") else() diff --git a/external/tiny-process-library b/external/tiny-process-library new file mode 160000 index 0000000..8bbb5a2 --- /dev/null +++ b/external/tiny-process-library @@ -0,0 +1 @@ +Subproject commit 8bbb5a211c5c9df8ee69301da9d22fb977b27dc1 diff --git a/src/wakatime/cli.cpp b/src/wakatime/cli.cpp index 9e7f979..a51ffda 100644 --- a/src/wakatime/cli.cpp +++ b/src/wakatime/cli.cpp @@ -10,6 +10,7 @@ #include "../utils/which.hpp" #include "cli.hpp" #include "wakatime.hpp" +#include "process.hpp" #ifdef GEODE_IS_WINDOWS #define POPEN _popen @@ -101,19 +102,23 @@ namespace cli { std::string command = fmt::format("{} --version", utils::quote(path.string())); std::string result; - char buffer[128]; - FILE* pipe = POPEN(command.c_str(), "r"); - if (!pipe) { - geode::log::error("Unable to fetch wakatime-cli version"); - return ""; - } + TinyProcessLib::Process process( + command, "", + [&result](const char * bytes, size_t n) { + result += std::string(bytes, n); + }, + [](const char * bytes, size_t n) { + geode::log::error("WakaTime CLI version check failed: {}", std::string(bytes, n)); + } + ); - while (!feof(pipe)) { - if (fgets(buffer, sizeof(buffer), pipe) != nullptr) result += buffer; - } + int exitCode = process.get_exit_status(); - PCLOSE(pipe); + if (exitCode != 0) { + geode::log::error("WakaTime CLI closed with status: {}", exitCode); + return ""; + } result.erase(0, result.find_first_not_of(" \n\r\t")); result.erase(result.find_last_not_of(" \n\r\t") + 1); @@ -267,20 +272,16 @@ namespace cli { geode::log::debug("Executing WakaTime CLI command: {}", command); std::thread([command]() { - std::string fullCommand = utils::quote(command); - #ifdef GEODE_IS_WINDOWS - fullCommand += " >nul 2>&1"; - #else - fullCommand += " >/dev/null 2>&1"; - #endif + TinyProcessLib::Process process( + command.c_str(), "", + [](const char *bytes, size_t n) {}, + [](const char *bytes, size_t n) { + geode::log::error("WakaTime CLI command failed: {}", std::string(bytes, n)); + } + ); - FILE* pipe = POPEN(fullCommand.c_str(), "r"); - if (pipe) { - int result = PCLOSE(pipe); - if (result != 0) geode::log::error("WakaTime CLI command failed with exit code: {}", result); - } else { - geode::log::error("WakaTime CLI command failed to start"); - } + int exitCode = process.get_exit_status(); + if (exitCode != 0) geode::log::error("WakaTime CLI closed with status: {}", exitCode); }).detach(); return true; diff --git a/src/wakatime/tracker.cpp b/src/wakatime/tracker.cpp index 156cc6b..f385178 100644 --- a/src/wakatime/tracker.cpp +++ b/src/wakatime/tracker.cpp @@ -38,6 +38,7 @@ namespace tracker { active = true; activityStarted = std::chrono::system_clock::now(); geode::log::debug("New activity: {} {}", getCategory(), project); + onActivityChange(); // send heartbeat } void ActivityTracker::checkActivity() { From 6f717ea7c87b8f383e50afc439c1b179c185758d Mon Sep 17 00:00:00 2001 From: ggod <46885632+GGodPL@users.noreply.github.com> Date: Sat, 9 Aug 2025 02:31:14 +0200 Subject: [PATCH 2/2] change tiny-process-library to reproc --- .gitmodules | 6 ++-- CMakeLists.txt | 7 ++-- external/reproc | 1 + external/tiny-process-library | 1 - src/wakatime/cli.cpp | 67 ++++++++++++++++++----------------- 5 files changed, 41 insertions(+), 41 deletions(-) create mode 160000 external/reproc delete mode 160000 external/tiny-process-library diff --git a/.gitmodules b/.gitmodules index e9911cd..1e49aea 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "external/tiny-process-library"] - path = external/tiny-process-library - url = https://gitlab.com/eidheim/tiny-process-library.git +[submodule "external/reproc"] + path = external/reproc + url = https://github.com/DaanDeMeyer/reproc.git diff --git a/CMakeLists.txt b/CMakeLists.txt index f10d6e2..ace8359 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,10 +16,9 @@ file(GLOB_RECURSE SOURCES src/*.cpp src/*/*.cpp) # Set up the mod binary add_library(${PROJECT_NAME} SHARED ${SOURCES}) -add_subdirectory(external/tiny-process-library ${CMAKE_CURRENT_BINARY_DIR}/tiny-process-library) - -target_include_directories(${PROJECT_NAME} PRIVATE external/tiny-process-library) -target_link_libraries(${PROJECT_NAME} tiny-process-library) +set(REPROC++ ON) +add_subdirectory(external/reproc) +target_link_libraries(${PROJECT_NAME} reproc++) if (NOT DEFINED ENV{GEODE_SDK}) message(FATAL_ERROR "Unable to find Geode SDK! Please define GEODE_SDK environment variable to point to Geode") diff --git a/external/reproc b/external/reproc new file mode 160000 index 0000000..3179928 --- /dev/null +++ b/external/reproc @@ -0,0 +1 @@ +Subproject commit 3179928ae7b085e41dfb846d987519fa7c12ffb3 diff --git a/external/tiny-process-library b/external/tiny-process-library deleted file mode 160000 index 8bbb5a2..0000000 --- a/external/tiny-process-library +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 8bbb5a211c5c9df8ee69301da9d22fb977b27dc1 diff --git a/src/wakatime/cli.cpp b/src/wakatime/cli.cpp index a51ffda..7fc8eea 100644 --- a/src/wakatime/cli.cpp +++ b/src/wakatime/cli.cpp @@ -10,7 +10,7 @@ #include "../utils/which.hpp" #include "cli.hpp" #include "wakatime.hpp" -#include "process.hpp" +#include "reproc++/run.hpp" #ifdef GEODE_IS_WINDOWS #define POPEN _popen @@ -97,26 +97,27 @@ namespace cli { std::string getCurrentVersion() { if (!isInstalled()) return ""; - std::filesystem::path path = getPath(); - - std::string command = fmt::format("{} --version", utils::quote(path.string())); + std::vector args = { getPath().string(), "--version" }; - std::string result; - - TinyProcessLib::Process process( - command, "", - [&result](const char * bytes, size_t n) { - result += std::string(bytes, n); - }, - [](const char * bytes, size_t n) { - geode::log::error("WakaTime CLI version check failed: {}", std::string(bytes, n)); - } - ); + std::string result, stderr_output; + + reproc::options options; + options.redirect.parent = false; - int exitCode = process.get_exit_status(); + auto [status, ec] = reproc::run(args, options, reproc::sink::string(result), reproc::sink::string(stderr_output)); + + if (ec) { + geode::log::error("WakaTime CLI exited with error code: {}", ec.message()); + return ""; + } + + if (status != 0) { + geode::log::error("WakaTime CLI exited with status: {}", status); + return ""; + } - if (exitCode != 0) { - geode::log::error("WakaTime CLI closed with status: {}", exitCode); + if (!stderr_output.empty()) { + geode::log::error("WakaTime CLI stderr: {}", stderr_output); return ""; } @@ -263,25 +264,25 @@ namespace cli { } bool execute(const std::filesystem::path& path, const std::vector& args) { - std::string command = utils::quote(path.string()); - - for (const auto& arg : args) { - command += " " + arg; - } + std::vector command; + command.push_back(path.string()); + command.insert(command.end(), args.begin(), args.end()); geode::log::debug("Executing WakaTime CLI command: {}", command); std::thread([command]() { - TinyProcessLib::Process process( - command.c_str(), "", - [](const char *bytes, size_t n) {}, - [](const char *bytes, size_t n) { - geode::log::error("WakaTime CLI command failed: {}", std::string(bytes, n)); - } - ); - - int exitCode = process.get_exit_status(); - if (exitCode != 0) geode::log::error("WakaTime CLI closed with status: {}", exitCode); + reproc::options options; + options.redirect.parent = false; + + std::string stdout_output, stderr_output; + + auto [status, ec] = reproc::run(command, options, reproc::sink::string(stdout_output), reproc::sink::string(stderr_output)); + + if (ec) geode::log::error("WakaTime CLI exited with error code: {}", ec.message()); + + if (!stderr_output.empty()) geode::log::error("WakaTime CLI command stderr: {}", stderr_output); + + if (status != 0) geode::log::error("WakaTime CLI exited with status: {}", status); }).detach(); return true;