diff --git a/src/package.cpp b/src/package.cpp index 1e8bc3d..40ebf85 100644 --- a/src/package.cpp +++ b/src/package.cpp @@ -6,8 +6,10 @@ #include // replace #include -#include // fprintf, printf, stderr -#include // system +#include // fprintf, printf, stderr +#include // system +#include // exists +#include #include // istringstream #include // getline #include @@ -76,11 +78,43 @@ void Package::store() // ----------------------------------------- +std::optional Package::fetchAurHelper() +{ + const std::string helpers[] = { + "yay", + "paru", + "trizen", + }; + + for(const auto& helper : helpers) { + if (findDependency(helper)) { + return { helper }; + } + } + + return {}; +} + void Package::installOrAurInstall(InstallType type) { distroDetect(); distroDependencies(); + std::optional aurHelper; + if (type == InstallType::AurInstall) { + if (m_distro == Distro::Arch) { + aurHelper = fetchAurHelper(); + if (!aurHelper.has_value()) { + fprintf(stderr, "\033[31;1mPackage:\033[0m no supported AUR helper found\n"); + return; + } + } + else { + fprintf(stderr, "\033[31;1mPackage:\033[0m AUR is not supported on this distribution\n"); + return; + } + } + std::string command = ""; Util::System $; @@ -94,18 +128,13 @@ void Package::installOrAurInstall(InstallType type) // NOTE: Util::System does not support commands with newlines auto aurList = Util::Shell()("cat ./packages | " + aurCommand); - command = "trizen -Sy --devel --needed --noconfirm " + aurList.output(); + command = aurHelper.value() + " -Sy --devel --needed --noconfirm " + aurList.output(); } else { command = "sudo pacman -Sy --needed " + repoList.output(); } } else if (m_distro == Distro::Debian) { - if (type == InstallType::AurInstall) { - fprintf(stderr, "\033[31;1mPackage:\033[0m AUR is not supported on this distribution\n"); - return; - } - // Grab everything off enabled official repositories that is in the list auto repoList = $("apt-cache search .").cut(1, ' ') | $("grep -xf ./packages"); command = "sudo apt install " + repoList.output(); @@ -119,6 +148,13 @@ void Package::installOrAurInstall(InstallType type) system(command.c_str()); } +bool Package::findDependency(const std::string& search) +{ + return std::filesystem::exists("/bin/" + search) + || std::filesystem::exists("/usr/bin/" + search) + || std::filesystem::exists("/usr/local/bin/" + search); +} + bool Package::distroDetect() { std::string id = Machine::the().distroId(); diff --git a/src/package.h b/src/package.h index dad8277..7cee90f 100644 --- a/src/package.h +++ b/src/package.h @@ -7,6 +7,7 @@ #ifndef PACKAGE_H #define PACKAGE_H +#include #include #include @@ -32,8 +33,10 @@ public: void store(); private: + std::optional fetchAurHelper(); void installOrAurInstall(InstallType type); + bool findDependency(const std::string& search); bool distroDetect(); bool distroDependencies(); std::string getPackageList();