#!/bin/bash
# sys/packages: Native Package Dependency Support
#
# The ``sys/packages`` library provides support for MCSH scripts to
# specify the lists of native packages required to use them.
set -e
######
# Internal Functions
# packages_symbol() - Prints name of package list symbol for the given
# package class ($1) and script name ($2)
packages_symbol() {
has_args 2 "$@"
local class=$1
local name=$2
lib_symbol "${name}_${class}_packages"
}
# packages_symbol_read() - Prints list of packages for the given
# symbol ($1), as generated by ``packages_symbol()``.
packages_symbol_read() {
has_args 1 "$@"
local sym=$1
local -a deps
eval "deps=( \"\${$sym[@]}\" )"
[ "${deps[*]}" ] || return 0
IFS=$'\n' echo "${deps[*]}"
}
######
# Library Dependencies
# lib_packages() - Prints all packages for the package class ($1) and
# named library ($2).
lib_packages() {
has_args 2 "$@"
local class=$1
local name=$2
local sym
sym=$(packages_symbol "$class" "$name")
packages_symbol_read "$sym"
local -a deps
deps=( $(lib_deps "$name") )
local p
for p in "${deps[@]}"; do
local name=${p#!}
[ "$name" = "${name//\$}" ] || continue
in_list "$name" "${lib_inited[@]}" || lib_load "$name"
lib_packages "$class" "$name"
done
}
######
# Tool Dependencies
# tool_packages() - Prints all packages for the package class ($1)
# and the named tool script ($2).
# $1 - Package class
# $2 - Tool script name (default: ``$script_name``)
tool_packages() {
min_max_args 1 2 "$@"
local class=$1
local name=${2:-$script_name}
local sym
sym=$(packages_symbol "$class" "$script_name")
packages_symbol_read "$sym"
local -a dep
deps=( $(lib_dep_list "$bindir/$script_name") )
local d
for d in "${deps[@]}"; do
lib_packages "$class" "${d#!}"
done
}