From 6d5b4274c422f6832d678ab5b4b7402cb72761c2 Mon Sep 17 00:00:00 2001 From: BlackAngelTV Date: Fri, 11 Sep 2026 20:13:40 +0200 Subject: [PATCH] feat(aur): add an AUR provider plugin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Community packages were invisible unless the user ran yay/paru by hand. Wrap the helper behind the provider interface, like the Flatpak plugin, so AUR results join search, installed and updates while staying optional. 🤖 Generated with Codebuff Co-Authored-By: Codebuff --- plugins/aur-provider/aur-manager.cpp | 234 ++++++++++++++++++++++++++ plugins/aur-provider/aur-manager.h | 60 +++++++ plugins/aur-provider/aur-provider.cpp | 22 +++ tests/aur-manager.test.cpp | 66 ++++++++ 4 files changed, 382 insertions(+) create mode 100644 plugins/aur-provider/aur-manager.cpp create mode 100644 plugins/aur-provider/aur-manager.h create mode 100644 plugins/aur-provider/aur-provider.cpp create mode 100644 tests/aur-manager.test.cpp diff --git a/plugins/aur-provider/aur-manager.cpp b/plugins/aur-provider/aur-manager.cpp new file mode 100644 index 0000000..2e04bfc --- /dev/null +++ b/plugins/aur-provider/aur-manager.cpp @@ -0,0 +1,234 @@ +#include "aur-manager.h" +#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; +} + +// Stream command output lines to the progress callback (text-only, +// percent = -1 keeps the UI bar indeterminate). +static void streamOutput(const std::string& output, ProgressCallback cb) { + if (!cb) return; + std::istringstream iss(output); + std::string line; + while (std::getline(iss, line)) { + if (!line.empty()) cb(line, -1); + } +} + +// Keep the tail of a command's output so the user sees the real failure. +static std::string lastOutputLines(const std::string& output, int maxLines = 4) { + std::istringstream iss(output); + std::string line; + std::vector lines; + while (std::getline(iss, line)) { + if (!line.empty()) lines.push_back(line); + } + std::string out; + const size_t start = lines.size() > static_cast(maxLines) + ? lines.size() - maxLines + : 0; + for (size_t i = start; i < lines.size(); ++i) { + if (!out.empty()) out += "\n"; + out += lines[i]; + } + return out; +} + +// Strip ANSI color escapes (`yay -Ss` colors repo/name pairs). +static std::string stripAnsi(const std::string& s) { + std::string out; + for (size_t i = 0; i < s.size(); ++i) { + if (s[i] == '\033' && i + 1 < s.size() && s[i + 1] == '[') { + i += 2; + while (i < s.size() && s[i] != 'm') ++i; + continue; + } + out += s[i]; + } + return out; +} + +std::string AurManager::helper() { + // yay first (most common), paru as fallback. + if (system("command -v yay > /dev/null 2>&1") == 0) return "yay"; + if (system("command -v paru > /dev/null 2>&1") == 0) return "paru"; + return {}; +} + +std::vector AurManager::parseSearchOutput(const std::string& output) { + std::vector results; + std::istringstream iss(stripAnsi(output)); + std::string header, desc; + while (std::getline(iss, header)) { + if (header.empty()) continue; + if (!std::getline(iss, desc)) desc.clear(); + // Header looks like "aur/name version [installed]". + size_t slash = header.find('/'); + if (slash == std::string::npos) continue; + std::string rest = header.substr(slash + 1); + std::istringstream ls(rest); + std::string name, version; + if (!(ls >> name >> version)) continue; + PackageInfo pkg; + pkg.name = name; + pkg.version = version; + pkg.description = desc; + pkg.repository = "aur"; + pkg.installed = header.find("[installed") != std::string::npos + || header.find("(Installed") != std::string::npos; + results.push_back(std::move(pkg)); + } + return results; +} + +std::vector AurManager::parseInstalledOutput(const std::string& output) { + std::vector results; + std::istringstream iss(output); + std::string line; + while (std::getline(iss, line)) { + if (line.empty()) continue; + std::istringstream ls(line); + std::string name, version; + if (!(ls >> name >> version)) continue; + PackageInfo pkg; + pkg.name = name; + pkg.version = version; + pkg.repository = "aur"; + pkg.installed = true; + results.push_back(std::move(pkg)); + } + return results; +} + +std::vector AurManager::parseUpdatesOutput(const std::string& output) { + std::vector results; + std::istringstream iss(output); + std::string line; + while (std::getline(iss, line)) { + if (line.empty()) continue; + // Format: "name old-version -> new-version". + std::istringstream ls(line); + std::string name, oldVer, arrow, newVer; + if (!(ls >> name >> oldVer)) continue; + ls >> arrow >> newVer; + PackageInfo pkg; + pkg.name = name; + pkg.version = newVer.empty() ? oldVer : newVer; + pkg.repository = "aur"; + pkg.installed = true; + pkg.updateAvailable = true; + results.push_back(std::move(pkg)); + } + return results; +} + +std::vector AurManager::search(const std::string& query) { + const std::string h = helper(); + if (h.empty()) return {}; + // --color=never keeps the output parseable. + return parseSearchOutput(exec((h + " -Ss --color=never " + query + " 2>&1").c_str())); +} + +std::vector AurManager::listInstalled() { + const std::string h = helper(); + if (h.empty()) return {}; + // -Qm lists foreign (AUR) packages only, so official repo packages stay + // owned by the pacman provider and rows are not duplicated. + return parseInstalledOutput(exec((h + " -Qm 2>&1").c_str())); +} + +std::vector AurManager::listUpdates() { + const std::string h = helper(); + if (h.empty()) return {}; + return parseUpdatesOutput(exec((h + " -Qua 2>&1").c_str())); +} + +bool AurManager::install(const std::string& packageName, + ProgressCallback progressCallback) { + m_lastError.clear(); + const std::string h = helper(); + if (h.empty()) { + m_lastError = "Aucun helper AUR (yay/paru) installe."; + return false; + } + if (progressCallback) progressCallback("Installation AUR de " + packageName + "...", -1); + // Helpers handle sudo themselves; run so the prompt reaches the user. + std::string output = exec((h + " -S --noconfirm " + packageName + " 2>&1").c_str()); + streamOutput(output, progressCallback); + if (system((h + " -Q " + packageName + " > /dev/null 2>&1").c_str()) != 0) { + m_lastError = lastOutputLines(output); + return false; + } + return true; +} + +bool AurManager::remove(const std::string& packageName, + ProgressCallback progressCallback) { + m_lastError.clear(); + const std::string h = helper(); + if (h.empty()) { + m_lastError = "Aucun helper AUR (yay/paru) installe."; + return false; + } + if (progressCallback) progressCallback("Suppression de " + packageName + "...", -1); + std::string output = exec((h + " -R --noconfirm " + packageName + " 2>&1").c_str()); + streamOutput(output, progressCallback); + if (system((h + " -Q " + packageName + " > /dev/null 2>&1").c_str()) == 0) { + m_lastError = lastOutputLines(output); + return false; + } + return true; +} + +bool AurManager::update(const std::string& packageName, + ProgressCallback progressCallback) { + m_lastError.clear(); + const std::string h = helper(); + if (h.empty()) { + m_lastError = "Aucun helper AUR (yay/paru) installe."; + return false; + } + std::string target = packageName.empty() ? "--aur" : packageName; + if (progressCallback) progressCallback("Mise a jour AUR...", -1); + std::string output = exec((h + " -Syu --noconfirm " + target + " 2>&1").c_str()); + streamOutput(output, progressCallback); + if (output.find("error:") != std::string::npos + || output.find("erreur") != std::string::npos) { + m_lastError = lastOutputLines(output); + return false; + } + return true; +} + +bool AurManager::refresh(ProgressCallback progressCallback) { + m_lastError.clear(); + const std::string h = helper(); + if (h.empty()) { + m_lastError = "Aucun helper AUR (yay/paru) installe."; + return false; + } + if (progressCallback) progressCallback("Rafraichissement AUR...", -1); + std::string output = exec((h + " -Sy 2>&1").c_str()); + streamOutput(output, progressCallback); + if (output.find("error:") != std::string::npos + || output.find("erreur") != std::string::npos) { + m_lastError = lastOutputLines(output); + return false; + } + return true; +} + diff --git a/plugins/aur-provider/aur-manager.h b/plugins/aur-provider/aur-manager.h new file mode 100644 index 0000000..42f90a0 --- /dev/null +++ b/plugins/aur-provider/aur-manager.h @@ -0,0 +1,60 @@ +#pragma once + +#include "package-manager.h" + +// AUR provider: manages Arch User Repository packages through an AUR helper +// (`yay` preferred, `paru` fallback). Runs per-user like flatpak for search +// and list (helpers handle sudo internally for install/remove/update), so +// requiresPassword() is false — the helper prompts on its own terminal. +class AurManager : public IPackageManager { +public: + std::string getName() const override { return "aur"; } + std::string getVersion() const override { return "1.0.0"; } + std::string getDescription() const override { + return "Paquets communautaires Arch (AUR via yay/paru)"; + } + + 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 !helper().empty(); + } + + // Last lines of the failed command output, shown to the user on failure. + std::string lastError() const override { return m_lastError; } + + // Name of the backing helper binary ("yay", "paru", or empty). + // Exposed as static so availability can be unit-tested without helpers. + static std::string helper(); + + // Parse `yay -Ss` output (pairs of "repo/name version" + description + // line, with ANSI colors stripped) into packages. + // Exposed as static so the parsing can be unit-tested without yay. + static std::vector parseSearchOutput(const std::string& output); + + // Parse `yay -Qm` output (one "name version" line per foreign package). + // Exposed as static so the parsing can be unit-tested without yay. + static std::vector parseInstalledOutput(const std::string& output); + + // Parse `yay -Qu` output (one "name old -> new" line per outdated pkg). + // Exposed as static so the parsing can be unit-tested without yay. + static std::vector parseUpdatesOutput(const std::string& output); + + std::vector search(const std::string& query) override; + bool install(const std::string& packageName, + ProgressCallback progressCallback) override; + bool remove(const std::string& packageName, + ProgressCallback progressCallback) override; + bool update(const std::string& packageName = "", + ProgressCallback progressCallback = nullptr) override; + std::vector listInstalled() override; + std::vector listUpdates() override; + bool refresh(ProgressCallback progressCallback = nullptr) override; + +private: + std::string m_lastError; +}; diff --git a/plugins/aur-provider/aur-provider.cpp b/plugins/aur-provider/aur-provider.cpp new file mode 100644 index 0000000..73c6ca2 --- /dev/null +++ b/plugins/aur-provider/aur-provider.cpp @@ -0,0 +1,22 @@ +// AUR provider plugin — an external provider loaded at runtime by +// PluginManager from the plugins/ directory next to the executable (or in +// ~/.local/share/PacManager/plugins/). Wraps the yay/paru helpers so AUR +// packages show up in search/installed/updates like any other provider. +// +// Built by default (see the FWS_BUILD_AUR_PLUGIN CMake option). + +#include "aur-manager.h" +#include "provider-plugin.h" + +extern "C" const char* fws_plugin_kind() { return "provider"; } +extern "C" const char* fws_plugin_id() { return "aur"; } +extern "C" const char* fws_plugin_name() { return "AUR"; } +extern "C" const char* fws_plugin_version() { return "1.0.0"; } +extern "C" const char* fws_plugin_description() { + return "Paquets communautaires Arch (AUR via yay/paru)"; +} +extern "C" const char* fws_plugin_author() { return "BlackAngelTV"; } +extern "C" IPackageManager* fws_create_provider() { return new AurManager(); } +// Helpers are installed from the AUR itself; the Plugins page can offer to +// install yay when the provider is unavailable. +extern "C" const char* fws_plugin_install_hint() { return "yay"; } diff --git a/tests/aur-manager.test.cpp b/tests/aur-manager.test.cpp new file mode 100644 index 0000000..1104f32 --- /dev/null +++ b/tests/aur-manager.test.cpp @@ -0,0 +1,66 @@ +#include "aur-manager.h" +#include +#include + +static void testParseSearchOutput_parsesPairs() { + std::string output = + "aur/yay 12.3.5-1 [installed]\n" + " Yet another yogurt - pacman wrapper and AUR helper\n" + "aur/paru 2.0.0-1\n" + " Feature packed AUR helper\n"; + auto pkgs = AurManager::parseSearchOutput(output); + + assert(pkgs.size() == 2); + assert(pkgs[0].name == "yay"); + assert(pkgs[0].version == "12.3.5-1"); + assert(pkgs[0].installed); + assert(pkgs[0].description.find("yogurt") != std::string::npos); + assert(pkgs[1].name == "paru"); + assert(!pkgs[1].installed); +} + +static void testParseSearchOutput_emptyInput() { + assert(AurManager::parseSearchOutput("").empty()); +} + +static void testParseInstalledOutput_parsesNames() { + std::string output = "yay 12.3.5-1\nparu 2.0.0-1\n"; + auto pkgs = AurManager::parseInstalledOutput(output); + + assert(pkgs.size() == 2); + assert(pkgs[0].name == "yay"); + assert(pkgs[0].installed); + assert(pkgs[1].name == "paru"); +} + +static void testParseInstalledOutput_emptyInput() { + assert(AurManager::parseInstalledOutput("").empty()); +} + +static void testParseUpdatesOutput_parsesArrows() { + std::string output = "yay 12.3.4-1 -> 12.3.5-1\n"; + auto pkgs = AurManager::parseUpdatesOutput(output); + + assert(pkgs.size() == 1); + assert(pkgs[0].name == "yay"); + assert(pkgs[0].version == "12.3.5-1"); + assert(pkgs[0].updateAvailable); +} + +static void testParseUpdatesOutput_emptyInput() { + assert(AurManager::parseUpdatesOutput("").empty()); +} + +int main() { + testParseSearchOutput_parsesPairs(); + testParseSearchOutput_emptyInput(); + + testParseInstalledOutput_parsesNames(); + testParseInstalledOutput_emptyInput(); + + testParseUpdatesOutput_parsesArrows(); + testParseUpdatesOutput_emptyInput(); + + std::cout << "All aur-manager tests passed." << std::endl; + return 0; +}