#!/bin/bash
# dev/package/publish - Package Release Publishing

set -e

lib_load 'dev/package/install'
lib_load 'net/remote'


######
# Package Publishing Wizards

# package_publish_upload() - Automates uploading the packages to the
# project publishing website.
# $1 - Package version
#
# This function performs the following individual steps:
package_publish_upload() {
	has_args 0 "$@"
	local version=${1:-$(package_version_build_full)}

	#   - Publishes package distribution files
	package_publish_tarballs "$version"

	#   - Publishes package documentation
	package_publish_docs "$version"

	#   - Publishes package installers
	package_publish_installers "$version"

	#   - Locks all published files
	package_publish_lock "$version"
}

# package_publish_finish() - Updates the current package links on the
# project publishing website and announces them.
# $1 - Package version
# $2 - Release branch
#
# This function performs the following individual steps:
package_publish_finish() {
	has_args 0 "$@"
	local version=${1:-$(package_version_build_full)}

	#   - Publishes updated links to the newly published version
	package_publish_links "$version"

	#   - Publishes package release announcements
	package_publish_announce "$version"
}


######
# SSH Publishing Support

# package_publish_ssh() - Publishes files using ``rsync`` via SSH.
# This remote host and pat are specified by the ``$PUBLISH_SSH_HOST``
# and ``$PUBLISH_SSH_PATH`` settings in the package configuration files.
# $1 - Source path
# $2 - Destination path
package_publish_ssh() {
	has_args 2 "$@"
	local src=$1
	local dst=$2

	local host=$PUBLISH_SSH_HOST
	local path=$PUBLISH_SSH_PATH
	if [ -z "$host" -o -z "$path" ]; then
		warn "ssh transport: host/path not definied, skipping $dst..."
		return
	fi

	rsync_run -a "$src" "$host:$path/$dst"
}

# package_publish_cmd() - Runs a command on the publishing host.
# $@ - Command to run.
package_publish_cmd() {
	min_args 1 "$@"
	remote_run "$PUBLISH_SSH_HOST" "$@"
}

# package_publish_filename() - Prints the name of the published release
# directory ($1) for the given version ($2)
# $1 - Directory name (installers, releases, docs)
# $2 - Package version
package_publish_filename() {
	local dirname

	local ext
	case "$1" in
	docs) ext='docs' ;;
	*) ext='' ;;
	esac

	dirname=$(package_dist_dirname "$ext" "$2")
	echo "$PUBLISH_SSH_PATH/$1/$dirname"
}

# package_publish_foreach_dir() - Runs a publishing function ($1)
# for each published release directory of a given version ($2).
# $1 - Command to run
# $2 - Package version
package_publish_foreach_dir() {
	local version
	version=${2:-$(package_version_build_full)}
	local dir
	for dir in installers releases docs; do
		"$1" "$dir" "$version"
	done
}


######
# Published File Permissions

# package_publish_lock() - Locks published files for the given
# package version ($1).
package_publish_lock() {
	package_publish_foreach_dir package_publish_lock_files
}

# package_publish_lock_files() - Locks the published release
# directory ($1) for the given version ($2)
# $1 - Directory name (installers, releases, docs)
# $2 - Package version
package_publish_lock_files() {
	app_echo "v$2 downloads: locking $1..."
	package_publish_cmd chmod -R a-w "$(package_publish_filename "$@")"
}

# package_publish_unlock() - Unlocks published files for the given
# package version ($1).
package_publish_unlock() {
	package_publish_foreach_dir package_publish_unlock_files
}

# package_publish_unlock_files() - Unlocks the published release
# directory ($1) for the given version ($2)
# $1 - Directory name (installers, releases, docs)
# $2 - Package version
package_publish_unlock_files() {
	app_echo "v$2 downloads: unlocking $1..."
	package_publish_cmd chmod -R u+w "$(package_publish_filename "$@")"
}


######
# Publishing Cleanup

# package_publish_abort() - Removes all published files for the given
# package version ($1).
# $1 - Package version
package_publish_abort() {
	package_publish_foreach_dir package_publish_unlock_files "$@"
	package_publish_foreach_dir package_publish_remove_files "$@"
}

# package_publish_remove_files() - Removes the published release
# directory ($1) for the given version ($2)
# $1 - Directory name (installers, releases, docs)
# $2 - Package version
package_publish_remove_files() {
	app_echo "v$3 downloads: removing $1..."
	package_publish_cmd rm -rf "$(package_publish_filename "$@")"
}


######
# Distribution Files Publishing

# package_publish_tarballs() - Publishes distribution tarballs for the
# given package version ($1).
# $1 - Package version
package_publish_tarballs() {
	local version=${1:-$(package_version_build_full)}

	local dirname
	dirname=$(package_dist_dirname '' "$version")

	app_echo "publishing v$version release tarballs..."
	package_publish_ssh "$package_distdir/$dirname" releases
}


######
# Documentation Publishing

# package_publish_docs() - Publishes documentation for the given package
# version ($1).
# $1 - Package version
package_publish_docs() {
	local version=${1:-$(package_version_build_full)}

	local dirname
	dirname=$(package_dist_dirname docs "$version")

	app_echo "publishing v$version documentation..."
	package_publish_ssh "$package_objdir/$dirname" docs
}


######
# Installer Publishing

# package_publish_installers() - Publishes installers for the given package
# version ($1).
# $1 - Package version
package_publish_installers() {
	local version=${1:-$(package_version_build_full)}

	local dirname
	dirname=$(package_dist_dirname '' "$version")

	app_echo "creating v$version installer directory..."
	package_publish_cmd mkdir -p "$PUBLISH_SSH_PATH/installers/$dirname"

	package_publish_installer_kind 'local' "$version"
	package_publish_installer_kind 'net' "$version"
	package_publish_installer_kind 'complete' "$version"
}

package_publish_installer_kind() {
	has_args 2 "$@"
	local kind=$1
	local version=$2

	app_echo "generating v$version $kind installer..."
	local tmp
	tmp=$(cmd_tempfile)
	package_install_script_$kind "$version" >"$tmp"
	run chmod 0444 "$tmp"

	app_echo "publishing v$version $kind installer..."
	local installer="installers/$dirname/${dirname}-${kind}-install.sh"
	package_publish_ssh "$tmp" "$installer"
}


######
# Current Link Publishing

package_publish_links() {
	package_publish_links_dir installers "$@"
	package_publish_links_dir releases "$@"
	package_publish_links_dir docs "$@"
}

package_publish_links_dir() {
	has_args 2 "$@"
	local dir=$1
	local version=$2

	local ext
	case "$dir" in
	(*) ext='' ;;
	(docs) ext='docs' ;;
	(*) error "$dir: unknown link publishing directory"
	esac

	local branch=$RELEASE_BRANCH
	[ "$RELEASE_BRANCH" ] || error "RELEASE_BRANCH must be set"
	package_release_branch_required "$branch"

	app_echo "generating symbolic link to v$version for $branch $dir..."
	local dirname
	dirname=$(package_dist_dirname "$ext" "$version")

	app_echo "publishing new $branch $dir link..."
	local link="$PUBLISH_SSH_PATH/$dir/current-$branch"
	package_publish_cmd rm -f "$link"
	package_publish_cmd ln -s "$dirname" "$link"
}


######
# Announcement Publishing

package_publish_announce() { warn "unimplemented"; }

View the Developer Guide Index

View the Reference Manual Index


Generated on Tue Jul 4 17:00:39 PDT 2017 by mcsh d14 v0.21.0.