Skip to content

Commit

Permalink
sync: re-factor installPkg()
Browse files Browse the repository at this point in the history
plus some fix when git pulling packages
  • Loading branch information
Toni500github committed Jun 9, 2024
1 parent 327f199 commit 63aaba0
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 81 deletions.
175 changes: 98 additions & 77 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <cstddef>
#pragma GCC diagnostic ignored "-Wvla"

#include "args.hpp"
Expand Down Expand Up @@ -144,7 +145,8 @@ int installPkg(alpm_list_t *pkgNames) {
bool useGit = config->useGit;
path cacheDir = config->cacheDir;

bool returnStatus = true, stat;
bool returnStatus = true;
bool stat;

vector<string> pacmanPkgs; // list of pacman packages to install, to avoid spamming pacman.
vector<string_view> pkgNamesVec;
Expand Down Expand Up @@ -184,101 +186,69 @@ int installPkg(alpm_list_t *pkgNames) {

// this feels horrible despite being 100% correct and probably not problematic.
// it just feels like we should ask for every package.
// Toni500 note: do someone agree with this above? because I don't
vector<string_view> AURPkgs = filterAURPkgsNames(pkgNamesVec, alpm_get_syncdbs(config->handle), true);

for (const auto& pkg : pkgNamesVec) {
// Check if pkg is not in aurPkgNamesSet
if (std::find(AURPkgs.begin(), AURPkgs.end(), pkg) == AURPkgs.end()) {
pacmanPkgs.push_back(pkg.data());
}
}

if (!op.op_s_cleanbuild && !AURPkgs.empty())
pkgsToCleanBuild = askUserForList<string_view>(AURPkgs, PROMPT_LIST_CLEANBUILDS);

if (!config->noconfirm && !AURPkgs.empty())
pkgsToReview = askUserForList<string_view>(AURPkgs, PROMPT_LIST_REVIEWS);


for (size_t i = 0; i < pkgNamesVec.size(); i++) {
vector<TaurPkg_t> pkgs = backend->search(pkgNamesVec[i], useGit, config->aurOnly, true);
for (auto& pkg_name : AURPkgs) {

optional<vector<TaurPkg_t>> oSelectedPkg = askUserForPkg(pkgs, *backend, useGit);
path pkgDir = path(cacheDir) / pkg_name;

if (!oSelectedPkg) {
stat = useGit ? backend->download_git(AUR_URL_GIT(pkg_name), pkgDir) : backend->download_tar(AUR_URL_TAR(pkg_name), pkgDir);
if (!stat) {
log_println(ERROR, _("Failed to download {}"), pkg_name);
returnStatus = false;
continue;
}

vector<TaurPkg_t> selectedPkg = oSelectedPkg.value();

for (size_t i = 0; i < selectedPkg.size(); i++) {
TaurPkg_t pkg = selectedPkg[i];

if (pkg.db_name != "aur") {
pacmanPkgs.push_back(pkg.name);
continue;
}

path pkgDir = path(cacheDir) / pkg.name;
bool cleanBuild = op.op_s_cleanbuild ? false : std::find(pkgsToCleanBuild.begin(), pkgsToCleanBuild.end(), pkg.name) != pkgsToCleanBuild.end();
bool review = config->noconfirm ? false : std::find(pkgsToReview.begin(), pkgsToReview.end(), pkg.name) != pkgsToReview.end();

if (cleanBuild) {
log_println(INFO, _("Removing {}"), pkgDir.c_str());
std::filesystem::remove_all(pkgDir);
}

stat = useGit ? backend->download_git(AUR_URL_GIT(pkg.name), pkgDir) : backend->download_tar(AUR_URL_TAR(pkg.name), pkgDir);
if (!stat) {
log_println(ERROR, _("Failed to download {}"), pkg.name);
returnStatus = false;
continue;
}

if (review || askUserYorN(YES, PROMPT_YN_EDIT_PKGBUILD, pkg.name)) {
// cmd is just a workaround for making possible
// that editor can have flags, e.g nano --modernbindings
// instead of creating another config variable
// This is really ugly
// because u can't convert std::vector<std::string> to std::vector<const char*>
vector<string> _cmd;
vector<const char *> cmd;
for (auto& str : config->editor)
_cmd.push_back(str);
_cmd.push_back((pkgDir / "PKGBUILD").string());

for (auto& str : _cmd)
cmd.push_back(str.c_str());

taur_exec(cmd);

if (!askUserYorN(YES, PROMPT_YN_PROCEED_INSTALL))
return false;
}

stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit);

if (!stat) {
log_println(ERROR, _("Installing AUR dependencies for your package has failed."));
returnStatus = false;
continue;
}

stat = backend->build_pkg(pkg.name, pkgDir, false);

if (!stat) {
log_println(ERROR, _("Building your package has failed."));
returnStatus = false;
continue;
}
}

pkgs_to_install += built_pkg + ' ';
for (string_view& pkg : pkgsToCleanBuild) {
path pkgDir = path(cacheDir) / pkg;
if (!useGit) {
log_println(INFO, _("Removing {}"), pkgDir.c_str());
std::filesystem::remove_all(pkgDir);
}
else {
log_println(INFO, _("Cleaning {}"), pkgDir.c_str());
taur_exec({config->git.c_str(), "-C", pkgDir.c_str(), "clean", "-xffd"});
}
}

if (!pkgs_to_install.empty()) {
log_println(DEBUG, _("Installing {}"), fmt::join(pkgNamesVec, " "));
pkgs_to_install.erase(pkgs_to_install.length() - 1);
if (!pacman_exec("-U", split(pkgs_to_install, ' '), false)) {
log_println(ERROR, _("Failed to install {}"), fmt::join(pkgNamesVec, " "));
returnStatus = false;
}
// cmd is just a workaround for making possible
// that editor can have flags, e.g nano --modernbindings
// instead of creating another config variable
// This is really ugly
// because you can't convert std::vector<std::string> to std::vector<const char*>
for (string_view& pkg : pkgsToReview) {
path pkgDir = path(cacheDir) / pkg;
vector<string> _cmd;
vector<const char *> cmd;
for (auto& str : config->editor)
_cmd.push_back(str);
_cmd.push_back((pkgDir / "PKGBUILD").string());

for (auto& str : _cmd)
cmd.push_back(str.c_str());

taur_exec(cmd);
}


if (!pkgsToReview.empty() && !askUserYorN(YES, PROMPT_YN_PROCEED_INSTALL))
return false;

if (!config->aurOnly && (op.op_s_upgrade || !pacmanPkgs.empty())) {
if (op.op_s_upgrade)
log_println(INFO, _("Upgrading system packages!"));
Expand All @@ -301,6 +271,57 @@ int installPkg(alpm_list_t *pkgNames) {
backend->update_all_aur_pkgs(cacheDir, useGit);
}

for (size_t i = 0; i < AURPkgs.size(); i++) {
vector<TaurPkg_t> pkgs = backend->search(AURPkgs[i], useGit, config->aurOnly, true);

optional<vector<TaurPkg_t>> oSelectedPkg = askUserForPkg(pkgs, *backend, useGit);

if (!oSelectedPkg) {
returnStatus = false;
continue;
}

vector<TaurPkg_t> selectedPkg = oSelectedPkg.value();

TaurPkg_t pkg = selectedPkg[0];

path pkgDir = path(cacheDir) / pkg.name;

stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit);

if (!stat) {
log_println(ERROR, _("Installing AUR dependencies for your package has failed."));
returnStatus = false;
continue;
}

stat = backend->build_pkg(pkg.name, pkgDir, false);

if (!stat) {
log_println(ERROR, _("Building {} has failed."), pkg.name);
returnStatus = false;
pkgs_failed_to_build += pkg.name + ' ';
log_println(DEBUG, "pkgs_failed_to_build = {}", pkgs_failed_to_build);
continue;
}

pkgs_to_install += built_pkg + ' ';
}

if (!pkgs_to_install.empty()) {
log_println(DEBUG, _("Installing {}"), fmt::join(pkgNamesVec, " "));
pkgs_to_install.erase(pkgs_to_install.length() - 1);
if (!pacman_exec("-U", split(pkgs_to_install, ' '), false)) {
log_println(ERROR, _("Failed to install {}"), fmt::join(pkgNamesVec, " "));
returnStatus = false;
}
}

if (!pkgs_failed_to_build.empty()) {
pkgs_failed_to_build.erase(pkgs_failed_to_build.end() - 1);
log_println(WARN, fg(color.red), _("Failed to upgrade: {}"), pkgs_failed_to_build);
log_println(INFO, fg(color.cyan), _("Tip: try to run taur with \"-S {}\" (e.g \"taur -S {}\")"), pkgs_failed_to_build, pkgs_failed_to_build);
}
return returnStatus;
}

Expand Down
8 changes: 4 additions & 4 deletions src/taur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TaurBackend::TaurBackend(Config& cfg) : config(cfg) {}

bool TaurBackend::download_git(string_view url, path out_path) {
if (std::filesystem::exists(path(out_path) / ".git")) {
return taur_exec({config.git.c_str(), "-C", out_path.c_str(), "pull", "--rebase", "--autostash", "--ff-only"});
return taur_exec({config.git.c_str(), "-C", out_path.c_str(), "pull", "--autostash", "--rebase", "--ff-only", "--force"});
} else {
if (std::filesystem::exists(path(out_path)))
std::filesystem::remove_all(out_path);
Expand Down Expand Up @@ -238,7 +238,7 @@ bool TaurBackend::build_pkg(string_view pkg_name, string extracted_path, bool al

if (!alreadyprepared) {
log_println(INFO, _("Verifying package sources.."));
makepkg_exec({"--verifysource", "--skippgpcheck", "-f", "-Cc"});
makepkg_exec({"--verifysource", "--skippgpcheck", "-fs", "-Cc"});

log_println(INFO, _("Preparing for compilation.."));
makepkg_exec({"--nobuild", "--skippgpcheck", "-fs", "-C", "--ignorearch"});
Expand Down Expand Up @@ -615,11 +615,11 @@ vector<TaurPkg_t> TaurBackend::search(string_view query, bool useGit, bool aurOn
if (!aurOnly)
pacPkgs = this->search_pac(query);

size_t count = aurPkgs.size() + pacPkgs.size();
size_t allPkgsSize = aurPkgs.size() + pacPkgs.size();

vector<TaurPkg_t> combined;

combined.reserve(count);
combined.reserve(allPkgsSize);

if (!aurPkgs.empty())
combined.insert(combined.end(), aurPkgs.begin(), aurPkgs.end());
Expand Down

0 comments on commit 63aaba0

Please sign in to comment.