feat(plugin): add pacman plugin for Arch Linux
This commit is contained in:
1 parent
7541b29996
commit
f2704527b0
2 files changed
+195
No files matched your search
@@ -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)
|
||||
@@ -0,0 +1,187 @@
|
||||
#include "IPackageManager.h"
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
// Helper to run shell commands and capture output
|
||||
static std::string exec(const char* cmd) {
|
||||
std::array<char, 256> buffer;
|
||||
std::string result;
|
||||
std::unique_ptr<FILE, decltype(&pclose)> 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<PackageInfo> search(const std::string& query) override {
|
||||
std::vector<PackageInfo> 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<void(const std::string&)> 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<void(const std::string&)> 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<void(const std::string&)> 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<PackageInfo> listInstalled() override {
|
||||
std::vector<PackageInfo> 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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user