Last active
July 2, 2022 02:22
-
-
Save SDGGiesbrecht/4d76ad2f2b9c7bf9072ca1da9815d7e2 to your computer and use it in GitHub Desktop.
Install or update executables from a Swift package.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# update.sh | |
# | |
# Copyright ©2017–2019 Jeremy David Giesbrecht. | |
# | |
# Soli Deo gloria. | |
# | |
# Licensed under the Apache Licence, Version 2.0. | |
# See http://www.apache.org/licenses/LICENSE-2.0 for licence information. | |
# ••••••• ••••••• ••••••• ••••••• ••••••• ••••••• ••••••• | |
# Use this script to install or update executables from a Swift package. | |
# | |
# Works on both macOS and Linux. | |
# Tools will be registered for use in bash, zsh, and fish. | |
# | |
# For the required arguments, see the “Collect arguments.” section below. | |
# | |
# See https://github.com/SDGGiesbrecht/Workspace#installation for an example. | |
# ••••••• ••••••• ••••••• ••••••• ••••••• ••••••• ••••••• | |
# Stop if a command fails. | |
set -e | |
original_working_directory=$(pwd) | |
# Collect arguments. | |
package_name="$1" # The package name. | |
package_url="$2" # The package URL. | |
version="$3" # The version to install. | |
help_command="$4" # An initial help command to get users started. | |
tools="${@:5}" # The name of the tool(s). Supply each one as a separate argument if the package supplies more than one. | |
# Unique installation directory. | |
installation_directory="${HOME}/.SDG/Tools" | |
if [[ ! -z "${OVERRIDE_INSTALLATION_DIRECTORY}" ]]; then | |
installation_directory="${original_working_directory}/${OVERRIDE_INSTALLATION_DIRECTORY}" | |
fi | |
registry_directory="${HOME}/.SDG/Registry" | |
# Clone and build in a temporary directory. | |
build_directory="/tmp/build" | |
product_directory="${build_directory}/.build/release" | |
# Determine project install location | |
install_location="${installation_directory}/${package_name}" | |
# Reset, clone and enter project. | |
rm -rf "${build_directory}" | |
git clone "${package_url}" "${build_directory}" --branch "${version}" --depth 1 --config advice.detachedHead=false | |
cd "${build_directory}" | |
# Build | |
swift build --configuration release | |
# Clean out irrelevant build artifacts. | |
rm -rf "${product_directory}"/ModuleCache | |
rm -rf "${product_directory}"/plugins | |
rm -rf "${product_directory}"/*.a | |
rm -rf "${product_directory}"/*.build | |
rm -rf "${product_directory}"/*.dSYM | |
rm -rf "${product_directory}"/*.json | |
rm -rf "${product_directory}"/*.product | |
rm -rf "${product_directory}"/*.swiftdoc | |
rm -rf "${product_directory}"/*.swiftmodule | |
rm -rf "${product_directory}"/*.swiftsourceinfo | |
# Resolve product location. | |
cd "${product_directory}" | |
resolved_product_directory=$(pwd -P) | |
cd "${build_directory}" | |
# Install/re‐install the products. | |
mkdir -p "${installation_directory}" | |
rm -rf "${install_location}" | |
mv -fv "${resolved_product_directory}" "${install_location}" | |
if [[ ! -z "${OVERRIDE_INSTALLATION_DIRECTORY}" ]]; then | |
cd "${original_working_directory}" | |
exit 0 | |
fi | |
# Register the products. | |
for tool in $tools ; do | |
mkdir -p "${registry_directory}" | |
rm -rf "${registry_directory}/${tool}" | |
ln -s "${install_location}/${tool}" "${registry_directory}/${tool}" | |
done | |
# Clean up | |
rm -rf "${build_directory}" | |
# Register with system. | |
add_path=$'export PATH="$HOME/.SDG/Registry:$PATH"' | |
comment=$'\n\n# Register installed Swift packages\n' | |
trailing_space=$'\n\n' | |
register() { | |
grep -qF "${add_path}" "${HOME}/$1" || echo "${comment}${add_path}${trailing_space}" >> "${HOME}/$1" | |
} | |
register .bash_profile | |
register .bashrc | |
register .zshenv | |
eval "${add_path}" | |
add_path=$'set -gx PATH $PATH "$HOME/.SDG/Registry"' | |
mkdir -p "${HOME}/.config/fish" | |
register .config/fish/config.fish | |
# Note success. | |
echo "" | |
echo "${package_name} was installed successfully." | |
echo "" | |
# Show help. | |
echo "$ ${help_command}" | |
eval "${help_command}" | |
cd "${original_working_directory}" | |
echo "" | |
echo "(Note: If the terminal cannot find the newly installed tool yet, try restarting the terminal.)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment