From f2704527b09ad5b1d0d0313d63ce3bdfdcdf33ac Mon Sep 17 00:00:00 2001 From: BlackAngelTV Date: Mon, 24 Aug 2026 21:13:51 +0200 Subject: [PATCH] feat(plugin): add pacman plugin for Arch Linux --- plugins/pacman/CMakeLists.txt | 8 ++ plugins/pacman/pacman_plugin.cpp | 187 +++++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 plugins/pacman/CMakeLists.txt create mode 100644 plugins/pacman/pacman_plugin.cpp diff --git a/plugins/pacman/CMakeLists.txt b/plugins/pacman/CMakeLists.txt new file mode 100644 index 0000000..7bb214b --- /dev/null +++ b/plugins/pacman/CMakeLists.txt @@ -0,0 +1,8 @@ +add_library(pacman_plugin SHARED pacman_plugin.cpp) +target_include_directories(pacman_plugin PRIVATE ${CMAKE_SOURCE_DIR}/include) +set_target_properties(pacman_plugin PROPERTIES + PREFIX "" + OUTPUT_NAME "pacman" + INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/PackageMapper/plugins" +) +install(TARGETS pacman_plugin DESTINATION lib/PackageMapper/plugins) diff --git a/plugins/pacman/pacman_plugin.cpp b/plugins/pacman/pacman_plugin.cpp new file mode 100644 index 0000000..cd62531 --- /dev/null +++ b/plugins/pacman/pacman_plugin.cpp @@ -0,0 +1,187 @@ +#include "IPackageManager.h" +#include +#include +#include +#include +#include +#include +#include + +// Helper to run shell commands and capture output +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 PacmanPlugin : public IPackageManager { +public: + std::string getName() const override { return "pacman"; } + std::string getVersion() const override { return "1.0.0"; } + std::string getDescription() const override { + return "Gestionnaire de paquets natif d'Arch Linux"; + } + + 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 pacman > /dev/null 2>&1") == 0; + } + + std::vector search(const std::string& query) override { + std::vector results; + + std::string cmd = "pacman -Ss " + query + " 2>&1"; + std::string output = exec(cmd.c_str()); + + std::istringstream iss(output); + std::string line; + std::string currentRepo; + + while (std::getline(iss, line)) { + if (line.empty()) continue; + + // Repository line: repo/name version + if (line[0] != ' ' && line[0] != '\t') { + PackageInfo pkg; + size_t slashPos = line.find('/'); + size_t spacePos = line.find(' ', slashPos); + + if (slashPos != std::string::npos) { + pkg.repository = line.substr(0, slashPos); + pkg.name = line.substr(slashPos + 1, spacePos - slashPos - 1); + + if (spacePos != std::string::npos) { + size_t nextSpace = line.find(' ', spacePos + 1); + pkg.version = line.substr(spacePos + 1, nextSpace - spacePos - 1); + } + } + + pkg.installed = isPackageInstalled(pkg.name); + results.push_back(pkg); + currentRepo = pkg.name; + } + // Description line (starts with spaces) + else if (!results.empty()) { + std::string desc = line; + // Trim leading spaces + size_t start = desc.find_first_not_of(" \t"); + if (start != std::string::npos) { + desc = desc.substr(start); + } + results.back().description = desc; + } + } + + return results; + } + + bool install(const std::string& packageName, + std::function progressCallback) override { + if (progressCallback) progressCallback("Installation de " + packageName + "..."); + + std::string cmd = "sudo pacman -S --noconfirm " + packageName + " 2>&1"; + std::string output = exec(cmd.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 cmd = "sudo pacman -R --noconfirm " + packageName + " 2>&1"; + std::string output = exec(cmd.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..."); + std::string cmd = "sudo pacman -Syu --noconfirm 2>&1"; + exec(cmd.c_str()); + return true; + } else { + if (progressCallback) progressCallback("Mise à jour de " + packageName + "..."); + std::string cmd = "sudo pacman -S --noconfirm " + packageName + " 2>&1"; + exec(cmd.c_str()); + return true; + } + } + + std::vector listInstalled() override { + std::vector results; + + std::string output = exec("pacman -Q 2>&1"); + std::istringstream iss(output); + std::string line; + + while (std::getline(iss, line)) { + if (line.empty()) continue; + + PackageInfo pkg; + size_t spacePos = line.find(' '); + size_t dashPos = line.find('-'); + + // Handle package names with hyphens (version starts after last pair of hyphens) + size_t lastDash = line.rfind('-'); + size_t secondLastDash = line.rfind('-', lastDash - 1); + + if (secondLastDash != std::string::npos && lastDash != std::string::npos) { + pkg.name = line.substr(0, secondLastDash); + pkg.version = line.substr(secondLastDash + 1); + } else if (spacePos != std::string::npos) { + pkg.name = line.substr(0, spacePos); + pkg.version = line.substr(spacePos + 1); + } + + pkg.installed = true; + results.push_back(pkg); + } + + return results; + } + +private: + bool isPackageInstalled(const std::string& name) { + std::string cmd = "pacman -Qi " + name + " > /dev/null 2>&1"; + return system(cmd.c_str()) == 0; + } +}; + +extern "C" { + IPackageManager* createPlugin() { + return new PacmanPlugin(); + } + + void destroyPlugin(IPackageManager* plugin) { + delete plugin; + } +}