#!/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

######
# Library Settings

sys_packages_config_init() {
	# $packages_visited[] - Used to prune package search
	lib_setting_assocs packages_visited
}


######
# 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[@]}\" )"
	info "$sym=( ${deps[*]} )"
	[ "${deps[*]}" ] || return 0
	for_each 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

	[ -z "${packages_visited[$name]}" ] || return 0
	packages_visited[$name]=1

	local sym
	sym=$(packages_symbol "$class" "$name")
	packages_symbol_read "$sym"

	local -a deps
	deps=( $(lib_deps "$name") )
	packages_lib_deps "$class" "${deps[@]}"
}

# packages_lib_deps() - Prints packages for the class ($1) for each of the
# given libraries ($@).
packages_lib_deps() {
	local class=$1
	shift

	local p
	for p in "$@"; 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=( "$runtime_name" $(lib_dep_list "$bindir/$script_name") )

	local -A packages_visited=()
	packages_lib_deps "$class" "${deps[@]}"
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:36:02 PDT 2017 by mcsh d14 v0.23.0.