Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 141 lines (132 sloc) 4.43 KB
#!/bin/bash
set -e -f -u -o pipefail
warn() {
echo -e "$@" 1>&2
}
die() {
warn "$@"
exit 1
}
usage() {
warn "Usage: $0 WORDPRESS-DIRECTORY wordpress [VERSION]"
warn "Usage: $0 WORDPRESS-DIRECTORY theme[s]|plugin[s] NAME [VERSION]"
die "If VERSION is not supplied the latest version will be installed."
}
# Don't require the user to pass "wordpress" "wordpress".
if [[ "$#" -eq 2 && "$2" == "wordpress" ]]; then
# Make $4 (version) empty but defined because later if $# == 3 and $2 ==
# wordpress we assume $3 is VERSION.
set -- "$1" "wordpress" "wordpress" ""
fi
if [[ "$#" -ne 4 && "$#" -ne 3 ]]; then
usage
fi
# VERSION is optional.
if [[ "$#" -eq 3 ]]; then
if [[ "$2" == "wordpress" ]]; then
set -- "$1" "wordpress" "wordpress" "$3"
else
# Make $4 (version) empty but defined because of -u.
set -- "$1" "$2" "$3" ""
fi
fi
WORDPRESS_BASE="$1"
TYPE="$2"
# Support plurals.
TYPE="${TYPE/plugins/plugin}"
TYPE="${TYPE/themes/theme}"
# Tab completion will add a trailing slash, remove it if present.
NAME="${3%/}"
VERSION="$4"
readonly WORDPRESS_BASE TYPE NAME VERSION SUBDIR
if [[ "${TYPE}" != "plugin" && "${TYPE}" != "theme" \
&& "${TYPE}" != "wordpress" ]]; then
usage
fi
cd "${WORDPRESS_BASE}"
git check-local-copy-is-clean
# The URL to download will be assembled using SEPARATORS and to downloaded file
# extracted using SUBDIRS.
declare -A SUBDIRS SEPARATORS
SUBDIRS[wordpress]=".."
SUBDIRS[plugin]="plugins"
SUBDIRS[theme]="themes"
SEPARATORS[wordpress]="-"
if [[ -n "${VERSION}" ]]; then
SEPARATORS[plugin]="."
SEPARATORS[theme]="."
else
SEPARATORS[plugin]=""
SEPARATORS[theme]=""
fi
readonly SUBDIRS SEPARATORS
readonly WORDPRESS_LATEST="latest.zip"
# When TYPE == wordpress, this will cd wp-content/.., staying in WORDPRESS_BASE.
cd "wp-content/${SUBDIRS[${TYPE}]}"
# Assemble the URL and download path for each TYPE.
DOWNLOAD_DIR="${HOME}/wordpress"
if [[ "${TYPE}" == "wordpress" && -z "${VERSION}" ]]; then
DOWNLOAD_FILE="${WORDPRESS_LATEST}"
else
DOWNLOAD_FILE="${NAME}${SEPARATORS[${TYPE}]}${VERSION}.zip"
fi
DOWNLOAD_PATH="${DOWNLOAD_DIR}/${DOWNLOAD_FILE}"
readonly DOWNLOAD_DIR DOWNLOAD_FILE DOWNLOAD_PATH
declare -A DOWNLOAD_URLS
DOWNLOAD_URLS[wordpress]="https://wordpress.org/${DOWNLOAD_FILE}"
DOWNLOAD_URLS[plugin]="https://downloads.wordpress.org/plugin/${DOWNLOAD_FILE}"
DOWNLOAD_URLS[theme]="https://downloads.wordpress.org/theme/${DOWNLOAD_FILE}"
readonly DOWNLOAD_URLS
declare -A BROWSER_URLS
BROWSER_URLS[wordpress]="https://wordpress.org/download"
BROWSER_URLS[plugin]="https://wordpress.org/plugins/${NAME}/"
BROWSER_URLS[theme]="https://wordpress.org/themes/${NAME}/"
readonly BROWSER_URLS
mkdir -p "${DOWNLOAD_DIR}"
# If we're grabbing the latest version rather than a specific version, delete
# the destination file because we want to download the newer version rather than
# using a previously downloaded one.
if [[ "${DOWNLOAD_FILE}" == "${WORDPRESS_LATEST}" || -z "${VERSION}" ]]; then
rm -f "${DOWNLOAD_PATH}"
fi
# Deal with corrupt downloads or HTML output.
if [[ -s "${DOWNLOAD_PATH}" ]]; then
if ! unzip -l "${DOWNLOAD_PATH}" > /dev/null; then
rm -f "${DOWNLOAD_PATH}"
fi
fi
if [[ ! -s "${DOWNLOAD_PATH}" ]]; then
echo "Downloading ${DOWNLOAD_URLS[${TYPE}]}"
if ! curl --fail --location --output "${DOWNLOAD_PATH}" \
"${DOWNLOAD_URLS[${TYPE}]}" ; then
rm -f "${DOWNLOAD_PATH}"
warn "Failed to download: ${DOWNLOAD_URLS[${TYPE}]}"
if [[ -n "${VERSION}" && "${TYPE}" != "wordpress" ]]; then
warn "Trying again without explicit version"
exec "$0" "${WORDPRESS_BASE}" "${TYPE}" "${NAME}" ""
else
die "Visit ${BROWSER_URLS[${TYPE}]}"
fi
fi
if [[ ! -s "${DOWNLOAD_PATH}" ]]; then
die "Missing or empty file: ${DOWNLOAD_PATH}"
fi
fi
# There's nothing named wordpress, so this is a no-op when TYPE == wordpress.
rm -rf "${NAME}"
unzip -q -o "${DOWNLOAD_PATH}"
if [[ "${TYPE}" == "wordpress" ]]; then
# Wordpress extracts into a directory named wordpress, and unzip doesn't have
# an option to strip a leading directory, so copy it to the rigth directory
# with tar, then delete it.
tar cf - -C wordpress . | tar xf -
rm -rf wordpress
fi
# --all marks deleted files for deletion in git.
git add --all .
# Give some idea of the scale of the changes.
(unset LESS; git diff --cached --shortstat)
# If there's anything to commit then commit it.
if ! git check-local-copy-is-clean 2>/dev/null; then
git commit --quiet --message "Installed ${NAME} ${VERSION}"
fi