#!/bin/bash
# dev/package/dist - package distribution support

set -e

lib_load 'dev/package/versions'
lib_load 'dev/package/changelog'
lib_load 'sys/tool/tar'
lib_load 'net/tool/rsync'


######
# Library Configuration

dev_package_dist_config_init() {
	# Package distribution directory, containing all release files
	lib_setting_vars package_distdir
}

dev_package_dist_config_check() {
	package_distdir=${package_distdir:-$package_repodir/dist}

}
######
# Package Distribution Filenames

# package_dist_dirname() - Prints distribution directory for an extension ($1).
# $1 - Extension name (optional)
# $2 - Extension version (option)
package_dist_dirname() {
	local name=$1
	local version=${2:-$(package_version_build_full)}
	echo "${PKG}${name:+-}${name}-$version"
}

# package_dist_tarname() - Prints name of distribution tarball
# $1 - Extension name (optional)
# $2 - Extension version (optional)
# $3 - Tarball extension (optional)
package_dist_tarname() {
	local tarfile=$(package_dist_dirname "$1" "$2")
	echo "$tarfile${3:+.}$3"
}

# package_dist_reldir() - Prints the full path of the distribution release
# directory for the given package version ($1).
# $1 - Package version
package_dist_reldir() {
	echo "$package_distdir/$(package_dist_dirname '' "$1")"
}

# package_dist_tarfile() - Prints full path of distribution tarball
# $1 - Extension name (optional)
# $2 - Extension version (optional)
# $3 - Tarball extension (optional)
package_dist_tarfile() {
	echo "$(package_dist_reldir "$@")/$(package_dist_tarname "$@")"
}

######
# Package Distribution Files

# package_dist_files_var() - Prints variable name for extension ($1) dist files
# $1 - Extension name (optional)
package_dist_files_var() { echo "DIST${1:+_}$1"; }

# package_dist_files_list() - Sets variable ($1) with extension ($2) dist files
# $1 - Output variable name
# $2 - Extension name (optional)
package_dist_files_list() {
	local var=$1
	local ext=$2

	local extvar=$(package_dist_files_var "$ext")
	eval "$var=( \"\${${extvar}[@]}\" )"
}

# package_dist_files() - Creates distribution files for the given version ($2)
# of the named extension ($1).
# $1 - Extension name
# $2 - Extension version
package_dist_files() {
	local ext=$1
	local vers=$2

	is_package_release || error "\$RELEASE_TYPE must be 'release'"

	local extdir
	extdir=$(package_dist_dirname "$@")

	local -a dist_inputs
	package_dist_files_list dist_inputs "$ext"
	if [ ${#dist_inputs[*]} -eq 0 ]; then
		warn "$extdir: no distribution files"
		return
	fi

	info "$extdir: staging files..."

	local dist_gendir="$package_objdir/$extdir"
	run rm -rf "$dist_gendir"
	run_mkdir "$dist_gendir"

	local rsync_archive=true
	rsync_run -a "${dist_inputs[@]}" "$dist_gendir"
	run find "$dist_gendir" -type d -exec chmod go-w,a+rx '{}' ';'
	run find "$dist_gendir" -type f -exec chmod go-w,a+r '{}' ';'

	info "$extdir: generating changelogs..."
	package_changelog_all HEAD $ext >"$dist_gendir/ChangeLog"

	local vstr=${vers:-$(package_version_build_full)}
	package_changelog_release HEAD $ext >"$dist_gendir/ChangeLog-$vstr"

	local reldir=$(package_dist_reldir "$vers")
	local tardir=$(dirname "$dist_gendir")
	local tar_outbase=$(package_dist_tarname "$ext" "$vers")
	local -a tar_inputs=( "$(basename "$dist_gendir")" )

	local tarext
	for tarext in "${tar_file_extensions[@]}"; do
		local tar_output="$reldir/$tar_outbase.$tarext"
		app_echo "$extdir: creating $tarext archive..."
		tar_create "$tar_output" "${tar_inputs[@]}"
		for_each package_dist_sign sha256 sha512
	done
}

# package_dist_sign() - Creates signature files for the given version ($2)
# of the named extension ($1).
# $1 - Distribution file name
package_dist_sign() {
	local hashext=$1
	local file=$tar_output
	local file_hashbin="${hashext}sum"
	file_hash_raw "$file" | sed -e "s,${file%/*}/,," >"$file.$hashext"
}

######
# Package Distribution CLI

# package_dist() - Dispatches package distribution commands
package_dist() { lib_cmd_dispatch package_dist "$@"; }

# package_dist_usage() - Prints usage for package distribution commands
package_dist_usage() {
	cat <<USAGE
...
Package Distribution Commands:
	all				Generates all package distribution files
	core				Generates core distribution files
	extensions [<name>+]		Generates extension distribution files
USAGE
}

# package_dist_all() - Generates all package distribution files
package_dist_all() {
	package_dist_core
	package_dist_extensions
}

# package_dist_core() - Generates distribution files for core package
package_dist_core() {
	package_dist_extension
}

# package_dist_extensions() - Generates distribution files for all extensions
# $1 - Package name
# $2 - Package version
# $@ - List of extension names (optional)
package_dist_extensions() {
	local -a exts=( "$@" )
	[ "$*" ] || exts=( "${package_extensions[@]}" )
	debug "creating distribution files for extensions:"
	debug "\t${exts[@]}"
	for_each package_dist_extension "${exts[@]}"
}

# package_dist_extension() - Generates distribution files for one extension
# $1 - Extension name
# $2 - Extension version
package_dist_extension() {
	local name=$1
	local version=$2
	# TODO: get extension version, use package version for now
	[ -z "$name" -o "$version" ] || version=$(package_version_build_full)
	package_dist_files "$name" "$version"
}

View the Developer Guide Index

View the Reference Manual Index


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