#!/bin/bash
# dev/package/files - package file introspection and management

set -e

lib_load 'dev/package/load'


######
# Library Configuration

dev_package_files_config_init() {
	# $package_file_kinds[] - Lists valid kinds of package files.
	# Defaults: `dist package sources configs tools libs`
	lib_setting_arrays -ro package_file_kinds
	package_file_kinds=( dist package sources configs tools libs )
}


######
# Package source file kinds

# package_file_kind_valid() - Returns success if argument is valid kind
# of package file.
# $1 - Kind of package file
package_file_kind_valid() {
	in_list "$1" "${package_file_kinds[@]}" \
		|| error "$1: invalid kind of package file"
}

# package_file_kind_dir() - Prints package directory for a given kind of
# package file ($1).
# Only implemented for `tools`, `libs`, and `configs`.
package_file_kind_dir() {
	local kind=$1
	case "$kind" in
	(tools) echo "src" ;;
	(libs) echo "libs" ;;
	(configs) echo "conf" ;;
	(*) error "$kind: no known directory for this kind of package file" ;;
	esac
}

# package_file_kind_ext() - Prints file extension for a given file kind ($1).
# Only implemented for `tools`, `libs`, and `configs`.
package_file_kind_ext() {
	local kind=$1
	case "$kind" in
	(tools|libs) echo "in" ;;
	(configs) echo "conf.in" ;;
	(*) error "$kind: no known extension for this kind of package file" ;;
	esac
}


######
# Package Source Files

# package_file_name() - Prints the path for a given kind of package
# file ($1) and name ($2).
package_file_name() {
	local kind=$1
	local name=$2
	local dir ext
	dir=$(package_file_kind_dir "$kind")
	ext=$(package_file_kind_ext "$kind")
	echo "$dir/$name.$ext"
}

# package_files() - Prints all names of package files of the given kind ($1).
package_files() { local kind=$1; shift; package_files_$kind "$@"; }

# package_files_dist() - Prints all names of all package distribution
# content by calling ``find(1)`` on the files specified by the package
# configuration files (e.g. ``package.i7``).  The resulting list
# includes all directory and file names.
#
# $@ - Passed along to ``find``, allowing this raw list to be refined
#   further by the remaining functions in this section.
package_files_dist() { find "${DIST[@]}" "$@"; }

# package_files_package() - Prints names of all plain package files.
package_files_package() { package_files_dist -type f; }

# package_files_sources() - Prints names of all package source code files.
package_files_sources() { package_files_package | grep '\.in$' | grep -v Makefile; }

# package_files_configs() - Prints names of package configuration scripts.
package_files_configs() { package_files_sources | grep '^conf/'; }

# package_files_tools() - Prints names of package tool scripts.
package_files_tools() { package_files_sources | grep '^src/'; }

# package_files_libs() - Prints names of package library scripts.
package_files_libs() { package_files_sources | grep '^libs/'; }

# package_files_extension() - Prints names of package extension files.
package_files_extension() {
	has_args 1 "$@"
	package_files_package | grep "^$1/"
}


######
# Package File Arguments

# package_sources_or_args() - Saves arguments ($@) in named array ($1).
# If none given, saves the list of package sources.
package_sources_or_args() {
	min_args 1 "$@"
	local psoa_var=$1
	shift
	local -a files
	files=$(package_files_sources)
	list_copy_or_args $psoa_var files "$@"
}


######
# Package File Iteration

# with_package_files - Run a command with all package files
# $1 - Kind of files to use: dist, package, sources, configs, tools, libs
# $2 - Command to run
# $@ - Command arguments (optional)
with_package_files() {
	min_args 2 "$@"
	local kind=$1
	shift
	local -a files
	files=( $(package_files_$kind) )
	"$@" "${files[@]}"
}

# with_each_package_file - Run a command with each package file
# $1 - Kind of files to use: dist, package, sources, configs, tools, libs
# $2 - Command to run
# $@ - Command arguments (optional)
with_each_package_file() {
	min_args 2 "$@"
	local kind=$1
	shift
	local -a files
	files=( $(package_files_$kind) )
	for_each "$*" "${files[@]}"
}


######
# Package File Tests

# is_package_file() - Returns success if file ($1) is the given kind
# ($2) of package source.
is_package_source() {
	local kind=$1
	local file=$2
	local dir
	dir=$(package_file_kind_dir "$kind")
	[ "${file#$dir/}" != "$file" -a "${file%.in}" != "$file" ]
}

# is_package_lib() - Returns success if file ($1) is a library.
is_package_lib() { has_args 1 "$@"; is_package_source libs "$1"; }

# is_package_tool() - Returns success if file ($1) is a tool.
is_package_tool() { has_args 1 "$@"; is_package_source tools "$1"; }

View the Developer Guide Index

View the Reference Manual Index


Generated on Wed Jun 28 07:39:54 PDT 2017 by mcsh d14 v0.20.0.