Skip to content

Commit

Permalink
refactor(pacstall): add has_command(), improve is_array() (pacstall#1142
Browse files Browse the repository at this point in the history
)
  • Loading branch information
vigress8 authored Jun 17, 2024
1 parent a34e09a commit f6fb257
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
14 changes: 7 additions & 7 deletions misc/scripts/srcinfo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function srcinfo.parse() {
unset "${split_up[1]}"
# So now we need to check if the thing we're trying to insert is a variable,
# or an array.
if [[ "$(declare -p -- "${part_two}")" == "declare -a "* ]]; then
if is_array "${part_two}"; then
declare -ga "${part_two}"
# shellcheck disable=SC2004
addarr[${split_up[1]}]="${part_two}"
Expand Down Expand Up @@ -221,13 +221,13 @@ function srcinfo.cleanup() {
#
# @example
# srcinfo_depends_vala_panel_appmenu_xfce_git=(["vala-panel-appmenu-valapanel-git-0"]="gtk3")
# srcinfo.reformat_assarr "srcinfo_depends_vala_panel_appmenu_xfce_git" "eviler"
# srcinfo.reformat_assoc_arr "srcinfo_depends_vala_panel_appmenu_xfce_git" "eviler"
#
# converts to `srcinfo_depends_vala_panel_appmenu_valapanel_git=([0]="gtk3")`
#
# @arg $1 string Associative array to reformat
# @arg $2 string Ref string of indexed array to append conversion to (can be anything)
function srcinfo.reformat_assarr() {
function srcinfo.reformat_assoc_arr() {
local pfx base ida new pfs in_name="${1}"
local -n in_arr="${in_name}" app="${2}"
IFS='_' read -r -a pfs <<< "${in_name}"
Expand Down Expand Up @@ -285,12 +285,12 @@ function srcinfo.print_var() {
# shellcheck disable=SC2294
eval "${evil[@]}"
if [[ -n ${globase} && ${globase} != "temporary_pacstall_pkgbase" ]]; then
srcinfo.reformat_assarr "${var_prefix}_${found}_${globase//-/_}" "eviler"
srcinfo.reformat_assoc_arr "${var_prefix}_${found}_${globase//-/_}" "eviler"
unset "${var_prefix}_${found}_${globase//-/_}"
# shellcheck disable=SC2294
eval "${eviler[@]}"
else
srcinfo.reformat_assarr "${var_prefix}_${found}_${name//-/_}" "eviler"
srcinfo.reformat_assoc_arr "${var_prefix}_${found}_${name//-/_}" "eviler"
unset "${var_prefix}_${found}_${name//-/_}"
# shellcheck disable=SC2294
eval "${eviler[@]}"
Expand Down Expand Up @@ -325,7 +325,7 @@ function srcinfo.match_pkg() {
[[ ${search} == "pkgbase" && -z ${declares[*]} ]] \
&& mapfile -t declares < <(srcinfo.print_var "${srcfile}" "pkgname" | awk '{sub(/^declare -a |^declare -- /, ""); print}')
for d in "${declares[@]}"; do
if [[ "${d%=\(*}" =~ = ]]; then
if [[ ${d%=\(*} =~ = ]]; then
declare -- "${d}"
bases+=("${d%=*}")
else
Expand All @@ -335,7 +335,7 @@ function srcinfo.match_pkg() {
done
for b in "${bases[@]}"; do
guy="${b}[@]"
if [[ -z "${pkg}" ]]; then
if [[ -z ${pkg} ]]; then
if [[ ${search} == "pkgname" || ${search} == "pkgbase" ]]; then
if [[ -n ${pkgbase} ]]; then
out="${pkgbase/\"/}"
Expand Down
19 changes: 9 additions & 10 deletions pacstall
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,22 @@ function check_url() {
fi
}

# Checks if a command is available.
# Plain "command -v" succeeds if the argument is in PATH, even if not executable.
function has_command() {
[[ -x $(command -v "$1" 2> /dev/null) ]]
}

# use axel if available
function download() {
local src="$1" out="${2:-${src##*/}}"
if [[ $PACSTALL_DOWNLOADER != "payload" ]]; then
sudo rm -f "$out"
fi
if [[ -z $PACSTALL_DOWNLOADER || -f "/tmp/pacstall-pacdeps-$PACKAGE" ]]; then
if command -v axel &> /dev/null; then
if has_command axel; then
PACSTALL_DOWNLOADER=axel
elif command -v wget &> /dev/null; then
elif has_command wget; then
PACSTALL_DOWNLOADER=wget
else
PACSTALL_DOWNLOADER=curl
Expand Down Expand Up @@ -428,14 +434,7 @@ function is_apt_package_installed() {
}

function is_array() {
local input raw
input="${1}"
raw="$(declare -p "${input}" 2> /dev/null)"
if [[ ${raw} == "declare -a ${input}"* ]]; then
return 0
else
return 1
fi
[[ ${!1@a} == *a* ]]
}

function is_function() {
Expand Down

0 comments on commit f6fb257

Please sign in to comment.