Skip to content

Commit

Permalink
Add support for adding, removing, and updating pkgx packages (#111)
Browse files Browse the repository at this point in the history
Allow to add/remove/update pkgx packages
  • Loading branch information
felipecrs authored Apr 27, 2024
1 parent d2bf217 commit a1c1c5c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
46 changes: 46 additions & 0 deletions home/.chezmoiscripts/run_after_30-install-pkgx-packages.sh.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

# {{ template "scripts-library" }}

# The following line is for ShellCheck to correctly identify the above include
true || source ../.chezmoitemplates/scripts-library

ensure_path_entry "${HOME}/.local/bin"

function is_pkgx_package_installed() {
local package="$1"

grep --quiet --recursive --binary-files=without-match --word-regexp \
"exec pkgx +${package}" "${HOME}/.local/bin"
}

readonly unwanted_packages=()

for package in "${unwanted_packages[@]}"; do
# shellcheck disable=SC2310
if is_pkgx_package_installed "${package}"; then
log_task "Removing unwanted package '${package}' installed with pkgx"
{
grep --recursive --binary-files=without-match --files-with-matches \
--word-regexp "exec pkgx +${package}" "${HOME}/.local/bin" || true
} | xargs --no-run-if-empty rm -f
fi
done

readonly wanted_packages=(
opendev.org/git-review
)

missing_packages=()

for package in "${wanted_packages[@]}"; do
# shellcheck disable=SC2310
if ! is_pkgx_package_installed "${package}"; then
missing_packages+=("${package}")
fi
done

if [[ ${#missing_packages[@]} -gt 0 ]]; then
log_task "Installing missing packages with pkgx: ${missing_packages[*]}"
pkgx install "${missing_packages[@]}"
fi
53 changes: 53 additions & 0 deletions home/dot_local/bin/executable_full-upgrade.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ true || source ../../.chezmoitemplates/homebrew-library
true || source ../../.chezmoitemplates/volta-library
true || source ../../.chezmoitemplates/sdkman-library

ensure_path_entry "${HOME}/.local/bin"

if [[ "${CONTINUE_FROM_DOTFILES:-false}" == false ]]; then
if brew --version &>/dev/null; then
(
Expand Down Expand Up @@ -39,6 +41,57 @@ if [[ "${CONTINUE_FROM_DOTFILES:-false}" == false ]]; then
CONTINUE_FROM_DOTFILES=true exec "$0" "$@"
fi

if pkgx --version >/dev/null; then
log_task "Updating pkgx stubs"

pkgx_packages="$(
{
grep --recursive --binary-files=without-match --no-filename \
--only-matching --perl-regexp --word-regexp \
'exec pkgx \+\K[^ ]+' "${HOME}/.local/bin" || true
} | sort --unique
)"

if [[ -z "${pkgx_packages}" ]]; then
log_info "No pkgx packages found"
else
readarray -t pkgx_packages <<<"${pkgx_packages}"

# This removes all the stubs directly as depending on the version of pkgx
# used to create the stub, pkgx uninstall can't uninstall it
{
grep --recursive --binary-files=without-match --files-with-matches \
--extended-regexp --word-regexp \
'exec pkgx \+[^ ]+' "${HOME}/.local/bin" || true
} | xargs --no-run-if-empty rm -f

# This updates the pantry, which would be updated automatically otherwise,
# but better safe than sorry
pkgx --sync >/dev/null

# And finally this adds all the stubs back, updated
pkgx install "${pkgx_packages[@]}"

log_task "Updating pkgx packages"

pkgx_packages_to_update=()
for package in "${pkgx_packages[@]}"; do
# This ensures we don't try to update packages that have not been cached
# yet, like the ones installed but never used
if [[ -d "${PKGX_DIR:-"${HOME}/.pkgx"}/${package}" ]]; then
pkgx_packages_to_update+=("+${package}")
fi
done

if [[ "${#pkgx_packages_to_update[@]}" -gt 0 ]]; then
c pkgx --update "${pkgx_packages_to_update[@]}" >/dev/null
fi

unset pkgx_packages_to_update
fi
unset pkgx_packages
fi

if apt --version &>/dev/null; then
log_task "Updating apt packages"
c sudo DEBIAN_FRONTEND=noninteractive apt update --yes
Expand Down
2 changes: 0 additions & 2 deletions home/dot_local/bin/executable_git-review

This file was deleted.

0 comments on commit a1c1c5c

Please sign in to comment.