Ajouter le plugin universal multi-source
Gestionnaire capable d'installer des paquets depuis pacman, apt, dnf, zypper, xbps, apk, pkg, brew, scoop, Flatpak, Snap, AUR, Nix et GitHub avec compilation source automatique.
This commit is contained in:
2 files changed
+956
No files matched your search
@@ -0,0 +1,948 @@
|
||||
#include "IPackageManager.h"
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include <cstring>
|
||||
|
||||
// ============================================================================
|
||||
// Helper: run shell commands
|
||||
// ============================================================================
|
||||
static std::string exec(const char* cmd, bool captureStderr = true) {
|
||||
std::array<char, 4096> buffer;
|
||||
std::string result;
|
||||
std::string fullCmd = std::string(cmd) + (captureStderr ? " 2>&1" : " 2>/dev/null");
|
||||
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(fullCmd.c_str(), "r"), pclose);
|
||||
if (!pipe) return "";
|
||||
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
|
||||
result += buffer.data();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool commandExists(const char* cmd) {
|
||||
std::string check = std::string("which ") + cmd + " > /dev/null 2>&1";
|
||||
return system(check.c_str()) == 0;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Detected package managers
|
||||
// ============================================================================
|
||||
struct DetectedManager {
|
||||
std::string name;
|
||||
std::string installCmd;
|
||||
std::string searchCmd;
|
||||
std::string removeCmd;
|
||||
std::string updateCmd;
|
||||
std::string listCmd;
|
||||
std::string isInstalledCheck; // command to check if a pkg is installed
|
||||
bool available = false;
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Source repository info
|
||||
// ============================================================================
|
||||
struct SourceRepo {
|
||||
std::string url;
|
||||
std::string type; // "github", "gitlab", "git", "tarball", "raw"
|
||||
std::string name;
|
||||
std::string branch;
|
||||
bool found = false;
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Universal Plugin Implementation
|
||||
// ============================================================================
|
||||
class UniversalPlugin : public IPackageManager {
|
||||
public:
|
||||
UniversalPlugin() {
|
||||
detectManagers();
|
||||
detectExtras();
|
||||
}
|
||||
|
||||
// --- IPackageManager interface ---
|
||||
std::string getName() const override { return "universal"; }
|
||||
std::string getVersion() const override { return "2.0.0"; }
|
||||
std::string getDescription() const override {
|
||||
return "Gestionnaire universel - pacman, apt, dnf, pkg, zypper, xbps, "
|
||||
"Flatpak, Snap, AppImage, AUR, Nix, GitHub, compilation source";
|
||||
}
|
||||
|
||||
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 true; } // Toujours dispo
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// SEARCH: essaie tout ce qui est disponible
|
||||
// -------------------------------------------------------------------------
|
||||
std::vector<PackageInfo> search(const std::string& query) override {
|
||||
std::vector<PackageInfo> results;
|
||||
|
||||
// 1) Chercher dans le gestionnaire natif
|
||||
for (auto& mgr : m_managers) {
|
||||
if (!mgr.available) continue;
|
||||
auto pkgs = searchWithManager(mgr, query);
|
||||
for (auto& p : pkgs) {
|
||||
p.repository = mgr.name;
|
||||
results.push_back(p);
|
||||
}
|
||||
}
|
||||
|
||||
// 2) Chercher dans Flatpak
|
||||
if (m_hasFlatpak) {
|
||||
auto pkgs = searchFlatpak(query);
|
||||
for (auto& p : pkgs) {
|
||||
p.repository = "flatpak";
|
||||
results.push_back(p);
|
||||
}
|
||||
}
|
||||
|
||||
// 3) Chercher dans Snap
|
||||
if (m_hasSnap) {
|
||||
auto pkgs = searchSnap(query);
|
||||
for (auto& p : pkgs) {
|
||||
p.repository = "snap";
|
||||
results.push_back(p);
|
||||
}
|
||||
}
|
||||
|
||||
// 4) Chercher sur GitHub
|
||||
auto githubPkgs = searchGitHub(query);
|
||||
for (auto& p : githubPkgs) {
|
||||
p.repository = "github";
|
||||
results.push_back(p);
|
||||
}
|
||||
|
||||
// 5) Chercher dans AUR
|
||||
if (m_hasAUR) {
|
||||
auto pkgs = searchAUR(query);
|
||||
for (auto& p : pkgs) {
|
||||
p.repository = "aur";
|
||||
results.push_back(p);
|
||||
}
|
||||
}
|
||||
|
||||
// 6) Chercher dans Nix
|
||||
if (m_hasNix) {
|
||||
auto pkgs = searchNix(query);
|
||||
for (auto& p : pkgs) {
|
||||
p.repository = "nix";
|
||||
results.push_back(p);
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// INSTALL: essaie la meilleure source
|
||||
// -------------------------------------------------------------------------
|
||||
bool install(const std::string& packageName,
|
||||
std::function<void(const std::string&)> progressCallback) override {
|
||||
if (progressCallback) progressCallback("🔍 Recherche de la meilleure source pour " + packageName + "...");
|
||||
|
||||
// 1) Vérifier si c'est un Flatpak
|
||||
if (m_hasFlatpak && isFlatpakPackage(packageName)) {
|
||||
return installFlatpak(packageName, progressCallback);
|
||||
}
|
||||
|
||||
// 2) Vérifier si c'est un Snap
|
||||
if (m_hasSnap && isSnapPackage(packageName)) {
|
||||
return installSnap(packageName, progressCallback);
|
||||
}
|
||||
|
||||
// 3) Vérifier si c'est dans AUR
|
||||
if (m_hasAUR && isAURPackage(packageName)) {
|
||||
return installAUR(packageName, progressCallback);
|
||||
}
|
||||
|
||||
// 4) Vérifier dans le gestionnaire natif
|
||||
for (auto& mgr : m_managers) {
|
||||
if (!mgr.available) continue;
|
||||
if (isPackageInManager(mgr, packageName)) {
|
||||
if (progressCallback) progressCallback("📦 Installation via " + mgr.name + "...");
|
||||
return installWithManager(mgr, packageName, progressCallback);
|
||||
}
|
||||
}
|
||||
|
||||
// 5) Essayer Nix
|
||||
if (m_hasNix && isNixPackage(packageName)) {
|
||||
return installNix(packageName, progressCallback);
|
||||
}
|
||||
|
||||
// 6) Dernier recours: GitHub / compilation source
|
||||
if (progressCallback) progressCallback("⚙️ Pas de paquet trouvé, tentative depuis GitHub/source...");
|
||||
return installFromSource(packageName, progressCallback);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// REMOVE
|
||||
// -------------------------------------------------------------------------
|
||||
bool remove(const std::string& packageName,
|
||||
std::function<void(const std::string&)> progressCallback) override {
|
||||
// Essayer chaque gestionnaire
|
||||
for (auto& mgr : m_managers) {
|
||||
if (!mgr.available) continue;
|
||||
if (isInstalledWithManager(mgr, packageName)) {
|
||||
if (progressCallback) progressCallback("🗑️ Suppression via " + mgr.name + "...");
|
||||
return removeWithManager(mgr, packageName, progressCallback);
|
||||
}
|
||||
}
|
||||
|
||||
// Flatpak
|
||||
if (m_hasFlatpak) {
|
||||
std::string check = "flatpak list --columns=application | grep -i " + packageName + " > /dev/null 2>&1";
|
||||
if (system(check.c_str()) == 0) {
|
||||
return removeFlatpak(packageName, progressCallback);
|
||||
}
|
||||
}
|
||||
|
||||
// Snap
|
||||
if (m_hasSnap) {
|
||||
std::string check = "snap list " + packageName + " > /dev/null 2>&1";
|
||||
if (system(check.c_str()) == 0) {
|
||||
return removeSnap(packageName, progressCallback);
|
||||
}
|
||||
}
|
||||
|
||||
if (progressCallback) progressCallback("❌ Paquet non trouvé dans aucune source");
|
||||
return false;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// UPDATE
|
||||
// -------------------------------------------------------------------------
|
||||
bool update(const std::string& packageName = "",
|
||||
std::function<void(const std::string&)> progressCallback = nullptr) override {
|
||||
if (packageName.empty()) {
|
||||
// Update all via le gestionnaire natif
|
||||
for (auto& mgr : m_managers) {
|
||||
if (!mgr.available) continue;
|
||||
if (progressCallback) progressCallback("🔄 Mise à jour via " + mgr.name + "...");
|
||||
std::string cmd = "sudo " + mgr.updateCmd + " 2>&1";
|
||||
exec(cmd.c_str());
|
||||
}
|
||||
// Flatpak
|
||||
if (m_hasFlatpak) {
|
||||
if (progressCallback) progressCallback("🔄 Mise à jour Flatpak...");
|
||||
exec("flatpak update -y 2>&1");
|
||||
}
|
||||
// Snap
|
||||
if (m_hasSnap) {
|
||||
if (progressCallback) progressCallback("🔄 Mise à jour Snap...");
|
||||
exec("sudo snap refresh 2>&1");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Update spécifique
|
||||
for (auto& mgr : m_managers) {
|
||||
if (!mgr.available) continue;
|
||||
if (isInstalledWithManager(mgr, packageName)) {
|
||||
if (progressCallback) progressCallback("🔄 Mise à jour via " + mgr.name + "...");
|
||||
std::string cmd = "sudo " + mgr.installCmd + " " + packageName + " 2>&1";
|
||||
exec(cmd.c_str());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// LIST INSTALLED
|
||||
// -------------------------------------------------------------------------
|
||||
std::vector<PackageInfo> listInstalled() override {
|
||||
std::vector<PackageInfo> results;
|
||||
|
||||
for (auto& mgr : m_managers) {
|
||||
if (!mgr.available) continue;
|
||||
auto pkgs = listInstalledWithManager(mgr);
|
||||
for (auto& p : pkgs) {
|
||||
p.repository = mgr.name;
|
||||
results.push_back(p);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_hasFlatpak) {
|
||||
auto pkgs = listInstalledFlatpak();
|
||||
for (auto& p : pkgs) {
|
||||
p.repository = "flatpak";
|
||||
results.push_back(p);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_hasSnap) {
|
||||
auto pkgs = listInstalledSnap();
|
||||
for (auto& p : pkgs) {
|
||||
p.repository = "snap";
|
||||
results.push_back(p);
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
// --- Public helpers ---
|
||||
std::vector<std::string> getAvailableManagers() const {
|
||||
std::vector<std::string> list;
|
||||
for (const auto& mgr : m_managers) {
|
||||
if (mgr.available) list.push_back(mgr.name);
|
||||
}
|
||||
if (m_hasFlatpak) list.push_back("flatpak");
|
||||
if (m_hasSnap) list.push_back("snap");
|
||||
if (m_hasAUR) list.push_back("aur");
|
||||
if (m_hasNix) list.push_back("nix");
|
||||
return list;
|
||||
}
|
||||
|
||||
private:
|
||||
// =========================================================================
|
||||
// Detection
|
||||
// =========================================================================
|
||||
void detectManagers() {
|
||||
// pacman (Arch)
|
||||
m_managers.push_back({
|
||||
"pacman", "pacman -S --noconfirm", "pacman -Ss",
|
||||
"pacman -R --noconfirm", "pacman -Syu --noconfirm",
|
||||
"pacman -Q", "pacman -Qi", commandExists("pacman")
|
||||
});
|
||||
|
||||
// apt (Debian/Ubuntu)
|
||||
m_managers.push_back({
|
||||
"apt", "apt install -y", "apt search",
|
||||
"apt remove -y", "apt update && apt upgrade -y",
|
||||
"dpkg --get-selections", "dpkg -l", commandExists("apt")
|
||||
});
|
||||
|
||||
// dnf (Fedora/RHEL)
|
||||
m_managers.push_back({
|
||||
"dnf", "dnf install -y", "dnf search",
|
||||
"dnf remove -y", "dnf upgrade -y",
|
||||
"dnf list installed", "rpm -q", commandExists("dnf")
|
||||
});
|
||||
|
||||
// yum (ancien RHEL/CentOS)
|
||||
m_managers.push_back({
|
||||
"yum", "yum install -y", "yum search",
|
||||
"yum remove -y", "yum update -y",
|
||||
"yum list installed", "rpm -q", commandExists("yum") && !commandExists("dnf")
|
||||
});
|
||||
|
||||
// zypper (openSUSE)
|
||||
m_managers.push_back({
|
||||
"zypper", "zypper install -y", "zypper search",
|
||||
"zypper remove -y", "zypper update -y",
|
||||
"zypper packages -i", "rpm -q", commandExists("zypper")
|
||||
});
|
||||
|
||||
// xbps (Void Linux)
|
||||
m_managers.push_back({
|
||||
"xbps-install", "xbps-install -y", "xbps-query -Rs",
|
||||
"xbps-remove -y", "xbps-install -Su",
|
||||
"xbps-query -l", "xbps-query -i", commandExists("xbps-install")
|
||||
});
|
||||
|
||||
// apk (Alpine)
|
||||
m_managers.push_back({
|
||||
"apk", "apk add", "apk search",
|
||||
"apk del", "apk upgrade",
|
||||
"apk list --installed", "apk info -e", commandExists("apk")
|
||||
});
|
||||
|
||||
// pkg (FreeBSD)
|
||||
m_managers.push_back({
|
||||
"pkg", "pkg install -y", "pkg search",
|
||||
"pkg delete -y", "pkg upgrade -y",
|
||||
"pkg query '%n-%v'", "pkg info", commandExists("pkg")
|
||||
});
|
||||
|
||||
// brew (macOS/Linux)
|
||||
m_managers.push_back({
|
||||
"brew", "brew install", "brew search",
|
||||
"brew uninstall", "brew upgrade",
|
||||
"brew list", "brew list", commandExists("brew")
|
||||
});
|
||||
|
||||
// scoop (Windows)
|
||||
m_managers.push_back({
|
||||
"scoop", "scoop install", "scoop search",
|
||||
"scoop uninstall", "scoop update",
|
||||
"scoop list", "scoop info", commandExists("scoop")
|
||||
});
|
||||
}
|
||||
|
||||
void detectExtras() {
|
||||
m_hasFlatpak = commandExists("flatpak");
|
||||
m_hasSnap = commandExists("snap");
|
||||
m_hasAUR = commandExists("makepkg");
|
||||
m_hasNix = commandExists("nix-env");
|
||||
m_hasCurl = commandExists("curl");
|
||||
m_hasGit = commandExists("git");
|
||||
m_hasMake = commandExists("make");
|
||||
m_hasCMake = commandExists("cmake");
|
||||
m_hasGcc = commandExists("gcc") || commandExists("g++");
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Native manager operations
|
||||
// =========================================================================
|
||||
std::vector<PackageInfo> searchWithManager(const DetectedManager& mgr, const std::string& query) {
|
||||
std::vector<PackageInfo> results;
|
||||
std::string cmd = mgr.searchCmd + " " + query + " 2>&1";
|
||||
std::string output = exec(cmd.c_str());
|
||||
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (line.empty() || line[0] == ' ' || line[0] == '\t') continue;
|
||||
if (line.find("error") != std::string::npos ||
|
||||
line.find("warning") != std::string::npos) continue;
|
||||
|
||||
PackageInfo pkg;
|
||||
// Parse common formats: "name version - description" or "name version description"
|
||||
size_t spacePos = line.find(' ');
|
||||
if (spacePos != std::string::npos) {
|
||||
pkg.name = line.substr(0, spacePos);
|
||||
size_t nextSpace = line.find(' ', spacePos + 1);
|
||||
if (nextSpace != std::string::npos) {
|
||||
pkg.version = line.substr(spacePos + 1, nextSpace - spacePos - 1);
|
||||
size_t descStart = line.find_first_not_of(" \t", nextSpace);
|
||||
if (descStart != std::string::npos) {
|
||||
pkg.description = line.substr(descStart);
|
||||
}
|
||||
} else {
|
||||
pkg.version = line.substr(spacePos + 1);
|
||||
}
|
||||
pkg.installed = isInstalledWithManager(mgr, pkg.name);
|
||||
results.push_back(pkg);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
bool installWithManager(const DetectedManager& mgr, const std::string& pkg,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
std::string cmd = "sudo " + mgr.installCmd + " " + pkg + " 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 isInstalledWithManager(mgr, pkg);
|
||||
}
|
||||
|
||||
bool removeWithManager(const DetectedManager& mgr, const std::string& pkg,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
std::string cmd = "sudo " + mgr.removeCmd + " " + pkg + " 2>&1";
|
||||
exec(cmd.c_str());
|
||||
return !isInstalledWithManager(mgr, pkg);
|
||||
}
|
||||
|
||||
bool isInstalledWithManager(const DetectedManager& mgr, const std::string& pkg) {
|
||||
std::string cmd = mgr.isInstalledCheck + " " + pkg + " > /dev/null 2>&1";
|
||||
return system(cmd.c_str()) == 0;
|
||||
}
|
||||
|
||||
bool isPackageInManager(const DetectedManager& mgr, const std::string& pkg) {
|
||||
std::string cmd = mgr.searchCmd + " " + pkg + " 2>&1 | grep -qi " + pkg;
|
||||
std::string output = exec(cmd.c_str());
|
||||
return !output.empty();
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> listInstalledWithManager(const DetectedManager& mgr) {
|
||||
std::vector<PackageInfo> results;
|
||||
std::string output = exec((mgr.listCmd + " 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.version = line.substr(spacePos + 1);
|
||||
pkg.installed = true;
|
||||
results.push_back(pkg);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Flatpak
|
||||
// =========================================================================
|
||||
bool isFlatpakPackage(const std::string& pkg) {
|
||||
std::string cmd = "flatpak search " + pkg + " --columns=application 2>/dev/null | grep -qi " + pkg;
|
||||
return !exec(cmd.c_str()).empty();
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> searchFlatpak(const std::string& query) {
|
||||
std::vector<PackageInfo> results;
|
||||
std::string output = exec(("flatpak 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 tabPos = line.find('\t');
|
||||
if (tabPos != std::string::npos) {
|
||||
pkg.name = line.substr(0, tabPos);
|
||||
size_t nextTab = line.find('\t', tabPos + 1);
|
||||
if (nextTab != std::string::npos) {
|
||||
pkg.description = line.substr(tabPos + 1, nextTab - tabPos - 1);
|
||||
}
|
||||
pkg.installed = isFlatpakInstalled(pkg.name);
|
||||
results.push_back(pkg);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
bool isFlatpakInstalled(const std::string& pkg) {
|
||||
std::string cmd = "flatpak list --columns=application | grep -q '^" + pkg + "$'";
|
||||
return system(cmd.c_str()) == 0;
|
||||
}
|
||||
|
||||
bool installFlatpak(const std::string& pkg,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("📦 Installation Flatpak: " + pkg + "...");
|
||||
exec(("flatpak install -y flathub " + pkg + " 2>&1").c_str());
|
||||
return isFlatpakInstalled(pkg);
|
||||
}
|
||||
|
||||
bool removeFlatpak(const std::string& pkg,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("🗑️ Suppression Flatpak: " + pkg + "...");
|
||||
exec(("flatpak uninstall -y " + pkg + " 2>&1").c_str());
|
||||
return !isFlatpakInstalled(pkg);
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> listInstalledFlatpak() {
|
||||
std::vector<PackageInfo> results;
|
||||
std::string output = exec("flatpak list --columns=application,version 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.version = line.substr(tabPos + 1);
|
||||
pkg.installed = true;
|
||||
results.push_back(pkg);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Snap
|
||||
// =========================================================================
|
||||
bool isSnapPackage(const std::string& pkg) {
|
||||
std::string cmd = "snap find " + pkg + " 2>&1 | head -5 | grep -qi " + pkg;
|
||||
return !exec(cmd.c_str()).empty();
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> searchSnap(const std::string& query) {
|
||||
std::vector<PackageInfo> results;
|
||||
std::string output = exec(("snap find " + query + " 2>&1").c_str());
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
bool headerSkipped = false;
|
||||
while (std::getline(iss, line)) {
|
||||
if (line.empty()) continue;
|
||||
if (!headerSkipped) { headerSkipped = true; continue; } // skip header
|
||||
PackageInfo pkg;
|
||||
size_t tabPos = line.find('\t');
|
||||
if (tabPos != std::string::npos) {
|
||||
pkg.name = line.substr(0, tabPos);
|
||||
pkg.installed = isSnapInstalled(pkg.name);
|
||||
results.push_back(pkg);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
bool isSnapInstalled(const std::string& pkg) {
|
||||
std::string cmd = "snap list " + pkg + " > /dev/null 2>&1";
|
||||
return system(cmd.c_str()) == 0;
|
||||
}
|
||||
|
||||
bool installSnap(const std::string& pkg,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("📦 Installation Snap: " + pkg + "...");
|
||||
exec(("sudo snap install " + pkg + " 2>&1").c_str());
|
||||
return isSnapInstalled(pkg);
|
||||
}
|
||||
|
||||
bool removeSnap(const std::string& pkg,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("🗑️ Suppression Snap: " + pkg + "...");
|
||||
exec(("sudo snap remove " + pkg + " 2>&1").c_str());
|
||||
return !isSnapInstalled(pkg);
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> listInstalledSnap() {
|
||||
std::vector<PackageInfo> results;
|
||||
std::string output = exec("snap list 2>&1");
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
bool headerSkipped = false;
|
||||
while (std::getline(iss, line)) {
|
||||
if (line.empty()) continue;
|
||||
if (!headerSkipped) { headerSkipped = true; 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;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// AUR (Arch User Repository)
|
||||
// =========================================================================
|
||||
bool isAURPackage(const std::string& pkg) {
|
||||
if (m_hasCurl) {
|
||||
std::string cmd = "curl -s \"https://aur.archlinux.org/rpc/v5/info/" + pkg + "\" 2>&1 | grep -q '\"results\"'";
|
||||
return system(cmd.c_str()) == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> searchAUR(const std::string& query) {
|
||||
std::vector<PackageInfo> results;
|
||||
if (!m_hasCurl) return results;
|
||||
|
||||
std::string cmd = "curl -s \"https://aur.archlinux.org/rpc/v5/search/" + query + "\" 2>&1";
|
||||
std::string output = exec(cmd.c_str());
|
||||
|
||||
// Simple JSON parse for AUR results
|
||||
size_t pos = 0;
|
||||
while ((pos = output.find("\"Name\":\"", pos)) != std::string::npos) {
|
||||
size_t start = pos + 8;
|
||||
size_t end = output.find("\"", start);
|
||||
if (end != std::string::npos) {
|
||||
PackageInfo pkg;
|
||||
pkg.name = output.substr(start, end - start);
|
||||
|
||||
// Find description
|
||||
size_t descPos = output.find("\"Description\":\"", end);
|
||||
if (descPos != std::string::npos) {
|
||||
size_t descStart = descPos + 15;
|
||||
size_t descEnd = output.find("\"", descStart);
|
||||
if (descEnd != std::string::npos) {
|
||||
pkg.description = output.substr(descStart, descEnd - descStart);
|
||||
}
|
||||
}
|
||||
|
||||
// Find version
|
||||
size_t verPos = output.find("\"Version\":\"", end);
|
||||
if (verPos != std::string::npos) {
|
||||
size_t verStart = verPos + 11;
|
||||
size_t verEnd = output.find("\"", verStart);
|
||||
if (verEnd != std::string::npos) {
|
||||
pkg.version = output.substr(verStart, verEnd - verStart);
|
||||
}
|
||||
}
|
||||
|
||||
pkg.repository = "aur";
|
||||
pkg.installed = isAURInstalled(pkg.name);
|
||||
results.push_back(pkg);
|
||||
pos = end;
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
bool isAURInstalled(const std::string& pkg) {
|
||||
std::string cmd = "pacman -Qi " + pkg + " > /dev/null 2>&1";
|
||||
return system(cmd.c_str()) == 0;
|
||||
}
|
||||
|
||||
bool installAUR(const std::string& pkg,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("⚙️ Clonage depuis AUR: " + pkg + "...");
|
||||
|
||||
std::string tmpDir = "/tmp/aur-build-" + pkg;
|
||||
std::string cloneCmd = "rm -rf " + tmpDir + " && git clone https://aur.archlinux.org/" + pkg + ".git " + tmpDir;
|
||||
exec(cloneCmd.c_str());
|
||||
|
||||
if (progressCallback) progressCallback("⚙️ Compilation avec makepkg...");
|
||||
std::string buildCmd = "cd " + tmpDir + " && makepkg -si --noconfirm 2>&1";
|
||||
std::string output = exec(buildCmd.c_str());
|
||||
|
||||
if (progressCallback) {
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty()) progressCallback(line);
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
exec(("rm -rf " + tmpDir).c_str());
|
||||
|
||||
return isAURInstalled(pkg);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Nix
|
||||
// =========================================================================
|
||||
bool isNixPackage(const std::string& pkg) {
|
||||
std::string cmd = "nix-env -qaP " + pkg + " 2>&1 | grep -qi " + pkg;
|
||||
return !exec(cmd.c_str()).empty();
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> searchNix(const std::string& query) {
|
||||
std::vector<PackageInfo> results;
|
||||
std::string output = exec(("nix-env -qaP " + 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(' ');
|
||||
size_t tabPos = line.find('\t');
|
||||
if (tabPos != std::string::npos) {
|
||||
// Format: "nixpkgs.package-name package-name-version"
|
||||
std::string fullPath = line.substr(0, tabPos);
|
||||
size_t lastDot = fullPath.rfind('.');
|
||||
if (lastDot != std::string::npos) {
|
||||
pkg.name = fullPath.substr(lastDot + 1);
|
||||
}
|
||||
pkg.version = line.substr(tabPos + 1);
|
||||
pkg.repository = "nix";
|
||||
pkg.installed = isNixInstalled(pkg.name);
|
||||
results.push_back(pkg);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
bool isNixInstalled(const std::string& pkg) {
|
||||
std::string cmd = "nix-env -q " + pkg + " > /dev/null 2>&1";
|
||||
return system(cmd.c_str()) == 0;
|
||||
}
|
||||
|
||||
bool installNix(const std::string& pkg,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("📦 Installation Nix: " + pkg + "...");
|
||||
exec(("nix-env -iA nixpkgs." + pkg + " 2>&1").c_str());
|
||||
return isNixInstalled(pkg);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GitHub search
|
||||
// =========================================================================
|
||||
std::vector<PackageInfo> searchGitHub(const std::string& query) {
|
||||
std::vector<PackageInfo> results;
|
||||
if (!m_hasCurl) return results;
|
||||
|
||||
std::string cmd = "curl -s 'https://api.github.com/search/repositories?q=" + query + "&sort=stars&per_page=5' 2>&1";
|
||||
std::string output = exec(cmd.c_str());
|
||||
|
||||
size_t pos = 0;
|
||||
while ((pos = output.find("\"full_name\":\"", pos)) != std::string::npos) {
|
||||
size_t start = pos + 13;
|
||||
size_t end = output.find("\"", start);
|
||||
if (end != std::string::npos) {
|
||||
PackageInfo pkg;
|
||||
std::string fullName = output.substr(start, end - start);
|
||||
size_t slashPos = fullName.find('/');
|
||||
pkg.name = (slashPos != std::string::npos) ? fullName.substr(slashPos + 1) : fullName;
|
||||
pkg.repository = "github";
|
||||
|
||||
// Find description
|
||||
size_t descPos = output.find("\"description\":\"", end);
|
||||
if (descPos != std::string::npos && descPos < end + 500) {
|
||||
size_t dStart = descPos + 16;
|
||||
size_t dEnd = output.find("\"", dStart);
|
||||
if (dEnd != std::string::npos) {
|
||||
pkg.description = output.substr(dStart, dEnd - dStart);
|
||||
}
|
||||
}
|
||||
|
||||
pkg.version = "source";
|
||||
pkg.installed = false;
|
||||
results.push_back(pkg);
|
||||
pos = end;
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GitHub / Source compilation
|
||||
// =========================================================================
|
||||
SourceRepo findSourceRepo(const std::string& query) {
|
||||
SourceRepo repo;
|
||||
|
||||
if (m_hasCurl) {
|
||||
// Search GitHub API
|
||||
std::string cmd = "curl -s \"https://api.github.com/search/repositories?q=" + query + "&sort=stars&per_page=1\" 2>&1";
|
||||
std::string output = exec(cmd.c_str());
|
||||
|
||||
// Parse JSON for html_url
|
||||
size_t urlPos = output.find("\"html_url\":\"");
|
||||
if (urlPos != std::string::npos) {
|
||||
size_t start = urlPos + 12;
|
||||
size_t end = output.find("\"", start);
|
||||
if (end != std::string::npos) {
|
||||
repo.url = output.substr(start, end - start);
|
||||
repo.type = "github";
|
||||
repo.found = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Parse name
|
||||
size_t namePos = output.find("\"full_name\":\"");
|
||||
if (namePos != std::string::npos) {
|
||||
size_t start = namePos + 13;
|
||||
size_t end = output.find("\"", start);
|
||||
if (end != std::string::npos) {
|
||||
repo.name = output.substr(start, end - start);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return repo;
|
||||
}
|
||||
|
||||
bool installFromSource(const std::string& packageName,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
SourceRepo repo = findSourceRepo(packageName);
|
||||
|
||||
if (!repo.found) {
|
||||
if (progressCallback) progressCallback("❌ Aucune source trouvée pour " + packageName);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (progressCallback) progressCallback("📥 Clonage depuis " + repo.url + "...");
|
||||
|
||||
std::string tmpDir = "/tmp/source-build-" + packageName;
|
||||
std::string cloneCmd = "rm -rf " + tmpDir + " && git clone --depth 1 " + repo.url + " " + tmpDir;
|
||||
exec(cloneCmd.c_str());
|
||||
|
||||
if (progressCallback) progressCallback("⚙️ Détection du système de build...");
|
||||
|
||||
// Try different build systems
|
||||
bool success = false;
|
||||
|
||||
// CMake
|
||||
if (!success && fileExists(tmpDir + "/CMakeLists.txt")) {
|
||||
if (progressCallback) progressCallback("🔨 Build CMake...");
|
||||
exec(("mkdir -p " + tmpDir + "/build").c_str());
|
||||
exec(("cd " + tmpDir + "/build && cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local 2>&1").c_str());
|
||||
exec(("cd " + tmpDir + "/build && make -j$(nproc) 2>&1").c_str());
|
||||
success = exec(("cd " + tmpDir + "/build && sudo make install 2>&1").c_str()).find("error") == std::string::npos;
|
||||
}
|
||||
|
||||
// Meson
|
||||
if (!success && fileExists(tmpDir + "/meson.build")) {
|
||||
if (progressCallback) progressCallback("🔨 Build Meson...");
|
||||
exec(("cd " + tmpDir + " && meson setup build --prefix=/usr/local 2>&1").c_str());
|
||||
exec(("cd " + tmpDir + "/build && ninja 2>&1").c_str());
|
||||
success = exec(("cd " + tmpDir + "/build && sudo ninja install 2>&1").c_str()).find("error") == std::string::npos;
|
||||
}
|
||||
|
||||
// Autotools (configure)
|
||||
if (!success && fileExists(tmpDir + "/configure")) {
|
||||
if (progressCallback) progressCallback("🔨 Build Autotools...");
|
||||
exec(("cd " + tmpDir + " && ./configure --prefix=/usr/local 2>&1").c_str());
|
||||
exec(("cd " + tmpDir + " && make -j$(nproc) 2>&1").c_str());
|
||||
success = exec(("cd " + tmpDir + " && sudo make install 2>&1").c_str()).find("error") == std::string::npos;
|
||||
}
|
||||
|
||||
// Makefile
|
||||
if (!success && fileExists(tmpDir + "/Makefile")) {
|
||||
if (progressCallback) progressCallback("🔨 Build Make...");
|
||||
exec(("cd " + tmpDir + " && make -j$(nproc) 2>&1").c_str());
|
||||
success = exec(("cd " + tmpDir + " && sudo make install 2>&1").c_str()).find("error") == std::string::npos;
|
||||
}
|
||||
|
||||
// Cargo (Rust)
|
||||
if (!success && fileExists(tmpDir + "/Cargo.toml")) {
|
||||
if (progressCallback) progressCallback("🔨 Build Cargo (Rust)...");
|
||||
success = exec(("cd " + tmpDir + " && cargo build --release 2>&1 && sudo cargo install --path . 2>&1").c_str()).find("error") == std::string::npos;
|
||||
}
|
||||
|
||||
// Go
|
||||
if (!success && fileExists(tmpDir + "/go.mod")) {
|
||||
if (progressCallback) progressCallback("🔨 Build Go...");
|
||||
success = exec(("cd " + tmpDir + " && go build -o /usr/local/bin/" + packageName + " . 2>&1").c_str()).find("error") == std::string::npos;
|
||||
}
|
||||
|
||||
// npm (Node.js)
|
||||
if (!success && fileExists(tmpDir + "/package.json")) {
|
||||
if (progressCallback) progressCallback("🔨 Build npm (Node.js)...");
|
||||
exec(("cd " + tmpDir + " && npm install 2>&1").c_str());
|
||||
success = exec(("cd " + tmpDir + " && sudo npm install -g . 2>&1").c_str()).find("error") == std::string::npos;
|
||||
}
|
||||
|
||||
// pip (Python)
|
||||
if (!success && (fileExists(tmpDir + "/setup.py") || fileExists(tmpDir + "/pyproject.toml"))) {
|
||||
if (progressCallback) progressCallback("🔨 Build pip (Python)...");
|
||||
success = exec(("cd " + tmpDir + " && sudo pip install . 2>&1").c_str()).find("error") == std::string::npos;
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
exec(("rm -rf " + tmpDir).c_str());
|
||||
|
||||
if (success) {
|
||||
if (progressCallback) progressCallback("✅ " + packageName + " installé depuis source !");
|
||||
} else {
|
||||
if (progressCallback) progressCallback("❌ Échec de la compilation de " + packageName);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool fileExists(const std::string& path) {
|
||||
std::string cmd = "test -f " + path + " && echo yes || echo no";
|
||||
std::string result = exec(cmd.c_str());
|
||||
return result.find("yes") != std::string::npos;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Data members
|
||||
// =========================================================================
|
||||
std::vector<DetectedManager> m_managers;
|
||||
bool m_hasFlatpak = false;
|
||||
bool m_hasSnap = false;
|
||||
bool m_hasAUR = false;
|
||||
bool m_hasNix = false;
|
||||
bool m_hasCurl = false;
|
||||
bool m_hasGit = false;
|
||||
bool m_hasMake = false;
|
||||
bool m_hasCMake = false;
|
||||
bool m_hasGcc = false;
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Plugin entry points
|
||||
// ============================================================================
|
||||
extern "C" {
|
||||
IPackageManager* createPlugin() {
|
||||
return new UniversalPlugin();
|
||||
}
|
||||
|
||||
void destroyPlugin(IPackageManager* plugin) {
|
||||
delete plugin;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user