From 29abaf31890dc76af6082602a2bcf50cf67baf2f Mon Sep 17 00:00:00 2001 From: BlackAngelTV Date: Mon, 24 Aug 2026 21:13:51 +0200 Subject: [PATCH] feat(plugin): add apt and pkg plugins --- plugins/apt/CMakeLists.txt | 8 ++ plugins/apt/apt_plugin.cpp | 146 ++++++++++++++++++++++++++++++++++++ plugins/pkg/CMakeLists.txt | 8 ++ plugins/pkg/pkg_plugin.cpp | 149 +++++++++++++++++++++++++++++++++++++ 4 files changed, 311 insertions(+) create mode 100644 plugins/apt/CMakeLists.txt create mode 100644 plugins/apt/apt_plugin.cpp create mode 100644 plugins/pkg/CMakeLists.txt create mode 100644 plugins/pkg/pkg_plugin.cpp diff --git a/plugins/apt/CMakeLists.txt b/plugins/apt/CMakeLists.txt new file mode 100644 index 0000000..1b2e9ae --- /dev/null +++ b/plugins/apt/CMakeLists.txt @@ -0,0 +1,8 @@ +add_library(apt_plugin SHARED apt_plugin.cpp) +target_include_directories(apt_plugin PRIVATE ${CMAKE_SOURCE_DIR}/include) +set_target_properties(apt_plugin PROPERTIES + PREFIX "" + OUTPUT_NAME "apt" + INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/PackageMapper/plugins" +) +install(TARGETS apt_plugin DESTINATION lib/PackageMapper/plugins) diff --git a/plugins/apt/apt_plugin.cpp b/plugins/apt/apt_plugin.cpp new file mode 100644 index 0000000..c677ee4 --- /dev/null +++ b/plugins/apt/apt_plugin.cpp @@ -0,0 +1,146 @@ +#include "IPackageManager.h" +#include +#include +#include +#include +#include +#include + +static std::string exec(const char* cmd) { + std::array buffer; + std::string result; + std::unique_ptr pipe(popen(cmd, "r"), pclose); + if (!pipe) throw std::runtime_error("popen() failed"); + while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { + result += buffer.data(); + } + return result; +} + +class AptPlugin : public IPackageManager { +public: + std::string getName() const override { return "apt"; } + std::string getVersion() const override { return "1.0.0"; } + std::string getDescription() const override { + return "Gestionnaire de paquets Debian/Ubuntu (apt)"; + } + + bool canSearch() const override { return true; } + bool canInstall() const override { return true; } + bool canRemove() const override { return true; } + bool canUpdate() const override { return true; } + bool canListInstalled() const override { return true; } + + bool isAvailable() const override { + return system("which apt > /dev/null 2>&1") == 0; + } + + std::vector search(const std::string& query) override { + std::vector results; + + std::string output = exec(("apt-cache search " + query + " 2>&1").c_str()); + std::istringstream iss(output); + std::string line; + + while (std::getline(iss, line)) { + if (line.empty()) continue; + + PackageInfo pkg; + size_t spacePos = line.find(' '); + if (spacePos != std::string::npos) { + pkg.name = line.substr(0, spacePos); + pkg.description = line.substr(spacePos + 1); + pkg.installed = isPackageInstalled(pkg.name); + pkg.repository = "debian"; + results.push_back(pkg); + } + } + + return results; + } + + bool install(const std::string& packageName, + std::function progressCallback) override { + if (progressCallback) progressCallback("Installation de " + packageName + "..."); + + std::string output = exec(("sudo apt install -y " + packageName + " 2>&1").c_str()); + + if (progressCallback) { + std::istringstream iss(output); + std::string line; + while (std::getline(iss, line)) { + if (!line.empty()) progressCallback(line); + } + } + + return isPackageInstalled(packageName); + } + + bool remove(const std::string& packageName, + std::function progressCallback) override { + if (progressCallback) progressCallback("Suppression de " + packageName + "..."); + + std::string output = exec(("sudo apt remove -y " + packageName + " 2>&1").c_str()); + + if (progressCallback) { + std::istringstream iss(output); + std::string line; + while (std::getline(iss, line)) { + if (!line.empty()) progressCallback(line); + } + } + + return !isPackageInstalled(packageName); + } + + bool update(const std::string& packageName = "", + std::function progressCallback = nullptr) override { + if (packageName.empty()) { + if (progressCallback) progressCallback("Mise à jour des paquets..."); + exec("sudo apt update 2>&1"); + exec("sudo apt upgrade -y 2>&1"); + return true; + } else { + if (progressCallback) progressCallback("Mise à jour de " + packageName + "..."); + exec(("sudo apt install --only-upgrade -y " + packageName + " 2>&1").c_str()); + return true; + } + } + + std::vector listInstalled() override { + std::vector results; + + std::string output = exec("dpkg --get-selections | grep -v deinstall 2>&1"); + std::istringstream iss(output); + std::string line; + + while (std::getline(iss, line)) { + if (line.empty()) continue; + + PackageInfo pkg; + size_t tabPos = line.find('\t'); + if (tabPos != std::string::npos) { + pkg.name = line.substr(0, tabPos); + pkg.installed = true; + results.push_back(pkg); + } + } + + return results; + } + +private: + bool isPackageInstalled(const std::string& name) { + return system(("dpkg -l " + name + " > /dev/null 2>&1").c_str()) == 0; + } +}; + +extern "C" { + IPackageManager* createPlugin() { + return new AptPlugin(); + } + + void destroyPlugin(IPackageManager* plugin) { + delete plugin; + } +} diff --git a/plugins/pkg/CMakeLists.txt b/plugins/pkg/CMakeLists.txt new file mode 100644 index 0000000..7c6b700 --- /dev/null +++ b/plugins/pkg/CMakeLists.txt @@ -0,0 +1,8 @@ +add_library(pkg_plugin SHARED pkg_plugin.cpp) +target_include_directories(pkg_plugin PRIVATE ${CMAKE_SOURCE_DIR}/include) +set_target_properties(pkg_plugin PROPERTIES + PREFIX "" + OUTPUT_NAME "pkg" + INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/PackageMapper/plugins" +) +install(TARGETS pkg_plugin DESTINATION lib/PackageMapper/plugins) diff --git a/plugins/pkg/pkg_plugin.cpp b/plugins/pkg/pkg_plugin.cpp new file mode 100644 index 0000000..ee0ab67 --- /dev/null +++ b/plugins/pkg/pkg_plugin.cpp @@ -0,0 +1,149 @@ +#include "IPackageManager.h" +#include +#include +#include +#include +#include +#include + +static std::string exec(const char* cmd) { + std::array buffer; + std::string result; + std::unique_ptr pipe(popen(cmd, "r"), pclose); + if (!pipe) throw std::runtime_error("popen() failed"); + while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { + result += buffer.data(); + } + return result; +} + +class PkgPlugin : public IPackageManager { +public: + std::string getName() const override { return "pkg"; } + std::string getVersion() const override { return "1.0.0"; } + std::string getDescription() const override { + return "Gestionnaire de paquets FreeBSD (pkg)"; + } + + bool canSearch() const override { return true; } + bool canInstall() const override { return true; } + bool canRemove() const override { return true; } + bool canUpdate() const override { return true; } + bool canListInstalled() const override { return true; } + + bool isAvailable() const override { + return system("which pkg > /dev/null 2>&1") == 0; + } + + std::vector search(const std::string& query) override { + std::vector results; + + std::string output = exec(("pkg search " + query + " 2>&1").c_str()); + std::istringstream iss(output); + std::string line; + + while (std::getline(iss, line)) { + if (line.empty()) continue; + + PackageInfo pkg; + // pkg search format: name-version [repo] + size_t dashPos = line.rfind('-'); + size_t spacePos = line.find(' '); + + if (dashPos != std::string::npos && spacePos != std::string::npos) { + pkg.name = line.substr(0, dashPos); + pkg.version = line.substr(dashPos + 1, spacePos - dashPos - 1); + pkg.repository = "freebsd"; + pkg.installed = isPackageInstalled(pkg.name); + results.push_back(pkg); + } + } + + return results; + } + + bool install(const std::string& packageName, + std::function progressCallback) override { + if (progressCallback) progressCallback("Installation de " + packageName + "..."); + + std::string output = exec(("sudo pkg install -y " + packageName + " 2>&1").c_str()); + + if (progressCallback) { + std::istringstream iss(output); + std::string line; + while (std::getline(iss, line)) { + if (!line.empty()) progressCallback(line); + } + } + + return isPackageInstalled(packageName); + } + + bool remove(const std::string& packageName, + std::function progressCallback) override { + if (progressCallback) progressCallback("Suppression de " + packageName + "..."); + + std::string output = exec(("sudo pkg delete -y " + packageName + " 2>&1").c_str()); + + if (progressCallback) { + std::istringstream iss(output); + std::string line; + while (std::getline(iss, line)) { + if (!line.empty()) progressCallback(line); + } + } + + return !isPackageInstalled(packageName); + } + + bool update(const std::string& packageName = "", + std::function progressCallback = nullptr) override { + if (packageName.empty()) { + if (progressCallback) progressCallback("Mise à jour de tous les paquets..."); + exec("sudo pkg upgrade -y 2>&1"); + return true; + } else { + if (progressCallback) progressCallback("Mise à jour de " + packageName + "..."); + exec(("sudo pkg install -y " + packageName + " 2>&1").c_str()); + return true; + } + } + + std::vector listInstalled() override { + std::vector results; + + std::string output = exec("pkg query '%n-%v' 2>&1"); + std::istringstream iss(output); + std::string line; + + while (std::getline(iss, line)) { + if (line.empty()) continue; + + PackageInfo pkg; + size_t dashPos = line.rfind('-'); + if (dashPos != std::string::npos) { + pkg.name = line.substr(0, dashPos); + pkg.version = line.substr(dashPos + 1); + pkg.installed = true; + results.push_back(pkg); + } + } + + return results; + } + +private: + bool isPackageInstalled(const std::string& name) { + return system(("pkg info " + name + " > /dev/null 2>&1").c_str()) == 0; + } +}; + +extern "C" { + IPackageManager* createPlugin() { + return new PkgPlugin(); + } + + void destroyPlugin(IPackageManager* plugin) { + delete plugin; + } +}