Skip to content

Support updating exsting swiftly installation from swiftly-install.sh #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions install/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ fi

tests_failed=0
tests_passed=0
failed_tests=()

for t in tests/*.sh; do
test_name=$(basename "$t")
line_print
echo "Running test $t"
echo "Running test $test_name"
echo ""
if bash "$t"; then
echo ""
echo "$t PASSED"
((tests_passed++))
else
echo ""
echo "$t FAILED"
((tests_failed++))
failed_tests+=("$test_name")
fi
done

Expand All @@ -38,6 +37,10 @@ line_print
if [[ "$tests_failed" -gt 0 ]]; then
echo ""
echo "$tests_failed test(s) FAILED, $tests_passed test(s) PASSED"
echo "Failed tests:"
for failed_test in "${failed_tests[@]}"; do
echo "- $failed_test"
done
exit 1
else
echo "All tests PASSED"
Expand Down
62 changes: 34 additions & 28 deletions install/swiftly-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ set -o errexit
shopt -s extglob

short_options='yhvp:'
long_options='disable-confirmation,no-modify-profile,no-install-system-deps,help,version,platform:'
long_options='disable-confirmation,no-modify-profile,no-install-system-deps,help,version,platform:,overwrite'

args=$(getopt --options "$short_options" --longoptions "$long_options" --name "swiftly-install" -- "${@}")
eval "set -- ${args}"
Expand All @@ -351,14 +351,18 @@ USAGE:
swiftly-install [options]

OPTIONS:
-y, --disable-confirmation Disable confirmation prompt.
-y, --disable-confirmation Disable confirmation prompts.
--no-modify-profile Do not attempt to modify the profile file to set environment
variables (e.g. PATH) on login.
--no-install-system-deps Do not attempt to install Swift's required system dependencies.
-p, --platform <platform> Specifies which platform's toolchains swiftly will download. If
unspecified, the platform will be automatically detected. Available
options are "ubuntu22.04", "ubuntu20.04", "ubuntu18.04", "rhel9", and
"amazonlinux2".
--overwrite Overwrite the existing swiftly installation found at the configured
SWIFTLY_HOME, if any. If this option is unspecified and an existing
installation is found, the swiftly executable will be updated, but
the rest of the installation will not be modified.
-h, --help Prints help information.
--version Prints version information.
EOF
Expand Down Expand Up @@ -415,6 +419,11 @@ EOF
shift 2
;;

"--overwrite")
overwrite_existing_intallation="true"
shift
;;

--)
shift
break
Expand Down Expand Up @@ -535,19 +544,14 @@ while [ -z "$DISABLE_CONFIRMATION" ]; do
done

if [[ -d "$HOME_DIR" ]]; then
if [[ "$DISABLE_CONFIRMATION" == "true" ]]; then
detected_existing_installation="true"
if [[ "$overwrite_existing_intallation" == "true" ]]; then
echo "Overwriting existing swiftly installation at $(replace_home_path $HOME_DIR)"
find $BIN_DIR -lname "$HOME_DIR/toolchains/**/bin/*" -delete
rm -r $HOME_DIR
else
echo "Existing swiftly installation detected at $(replace_home_path $HOME_DIR), overwrite? (Y/n)"

read_yn_input "true"
if [[ "$READ_INPUT_RETURN" == "false" ]]; then
echo "Cancelling installation."
exit 0
fi
echo "Updating existing swiftly installation at $(replace_home_path $HOME_DIR)"
fi

rm -r $HOME_DIR
fi

mkdir -p $HOME_DIR/toolchains
Expand All @@ -566,35 +570,37 @@ curl \

chmod +x "$BIN_DIR/swiftly"

echo "$JSON_OUT" > "$HOME_DIR/config.json"
if [[ "$detected_existing_installation" != "true" || "$overwrite_existing_intallation" == "true" ]]; then
echo "$JSON_OUT" > "$HOME_DIR/config.json"

# Verify the downloaded executable works. The script will exit if this fails due to errexit.
SWIFTLY_HOME_DIR="$HOME_DIR" SWIFTLY_BIN_DIR="$BIN_DIR" "$BIN_DIR/swiftly" --version > /dev/null
# Verify the downloaded executable works. The script will exit if this fails due to errexit.
SWIFTLY_HOME_DIR="$HOME_DIR" SWIFTLY_BIN_DIR="$BIN_DIR" "$BIN_DIR/swiftly" --version > /dev/null

ENV_OUT=$(cat <<EOF
ENV_OUT=$(cat <<EOF
export SWIFTLY_HOME_DIR="$(replace_home_path $HOME_DIR)"
export SWIFTLY_BIN_DIR="$(replace_home_path $BIN_DIR)"
if [[ ":\$PATH:" != *":\$SWIFTLY_BIN_DIR:"* ]]; then
export PATH="\$SWIFTLY_BIN_DIR:\$PATH"
fi
EOF
)
)

echo "$ENV_OUT" > "$HOME_DIR/env.sh"
echo "$ENV_OUT" > "$HOME_DIR/env.sh"

if [[ "$MODIFY_PROFILE" == "true" ]]; then
SOURCE_LINE=". $(replace_home_path $HOME_DIR)/env.sh"
if [[ "$MODIFY_PROFILE" == "true" ]]; then
SOURCE_LINE=". $(replace_home_path $HOME_DIR)/env.sh"

# Only append the line if it isn't in .profile already.
if [[ ! -f "$PROFILE_FILE" ]] || [[ ! "$(cat $PROFILE_FILE)" =~ "$SOURCE_LINE" ]]; then
echo "$SOURCE_LINE" >> "$PROFILE_FILE"
# Only append the line if it isn't in .profile already.
if [[ ! -f "$PROFILE_FILE" ]] || [[ ! "$(cat $PROFILE_FILE)" =~ "$SOURCE_LINE" ]]; then
echo "$SOURCE_LINE" >> "$PROFILE_FILE"
fi
fi
fi

if [[ "$SWIFTLY_INSTALL_SYSTEM_DEPS" != "false" ]]; then
echo ""
echo "Installing Swift's system dependencies via $package_manager (note: this may require root access)..."
install_system_deps
if [[ "$SWIFTLY_INSTALL_SYSTEM_DEPS" != "false" ]]; then
echo ""
echo "Installing Swift's system dependencies via $package_manager (note: this may require root access)..."
install_system_deps
fi
fi

echo ""
Expand Down
14 changes: 14 additions & 0 deletions install/test-util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@

export SWIFTLY_READ_FROM_STDIN=1

test_log () {
echo "==========================="
echo "$1"
echo "==========================="
}

has_command () {
command -v "$1" > /dev/null
}

test_name () {
basename "$0"
}

test_fail () {
if [ ! -z "$1" ]; then
printf "$1\n"
Expand All @@ -17,10 +27,14 @@ test_fail () {
printf "actual: $2\n"
printf "expected: $3\n"
fi
echo ""
echo "$(test_name) FAILED"
exit 1
}

test_pass () {
echo ""
echo "$(test_name) PASSED"
exit 0
}

Expand Down
17 changes: 1 addition & 16 deletions install/tests/disable-prompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,7 @@ bash --login -c "swiftly --version"
. "$HOME/.local/share/swiftly/env.sh"

if ! has_command "swiftly" ; then
fail_test "Can't find swiftly on the PATH"
fi

DUMMY_CONTENT="should be overwritten"
echo "$DUMMY_CONTENT" > "$HOME/.local/share/swiftly/config.json"

# Running it again should overwrite the previous installation without asking us for permission.
./swiftly-install.sh --disable-confirmation

if ! has_command "swiftly" ; then
fail_test "Can't find swiftly on the PATH"
fi

CONFIG_CONTENTS="$(cat $HOME/.local/share/swiftly/config.json)"
if [ "$CONFIG_CONTENTS" == "$DUMMY_CONTENT" ]; then
fail_test "Config should have been overwritten after second install"
test_fail "Can't find swiftly on the PATH"
fi

if has_command dpkg ; then
Expand Down
29 changes: 23 additions & 6 deletions install/tests/overwrite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ set -o errexit
source ./test-util.sh

export SWIFTLY_HOME_DIR="./overwrite-test-home"
export SWIFTLY_BIN_DIR="$SWIFTLY_HOME_DIR/bin"
export SWIFTLY_BIN_DIR="./overwrite-bin-dir"

cp "$HOME/.profile" "$HOME/.profile.bak"

cleanup () {
mv "$HOME/.profile.bak" "$HOME/.profile"
rm -r "$SWIFTLY_HOME_DIR"
rm -r "$SWIFTLY_BIN_DIR"
}
trap cleanup EXIT

test_log "Performing initial installation"
./swiftly-install.sh -y --no-install-system-deps

. "$SWIFTLY_HOME_DIR/env.sh"
Expand All @@ -29,10 +31,17 @@ fi
DUMMY_CONFIG_CONTENTS="hello world"
PROFILE_CONTENTS="$(cat $HOME/.profile)"
echo "$DUMMY_CONFIG_CONTENTS" > "$SWIFTLY_HOME_DIR/config.json"
mkdir "$SWIFTLY_HOME_DIR/toolchains/5.7.3"

# Attempt the same installation, but decline to overwrite.
printf "1\nn\n" | ./swiftly-install.sh
toolchain_dir="$SWIFTLY_HOME_DIR/toolchains/5.7.3"
mkdir -p "$toolchain_dir/usr/bin"
dummy_executable_name="foo"
touch "$toolchain_dir/usr/bin/$dummy_executable_name"

# Also set up a symlink as if the toolchain were in use.
ln -s -t $SWIFTLY_BIN_DIR "$toolchain_dir/usr/bin/$dummy_executable_name"

test_log "Attempting the same installation (no --overwrite flag specified)"
./swiftly-install.sh -y --no-install-system-deps

if ! has_command "swiftly" ; then
test_fail "Can't find swiftly on the PATH"
Expand All @@ -43,12 +52,16 @@ if [[ "$NEW_CONFIG_CONTENTS" != "$DUMMY_CONFIG_CONTENTS" ]]; then
test_fail "Expected config to remain unchanged" "$NEW_CONFIG_CONTENTS" "$DUMMY_CONFIG_CONTENTS"
fi

if ! [ -L "$SWIFTLY_BIN_DIR/$dummy_executable_name" ]; then
test_fail "Expected symlink to still exist, but it has been deleted"
fi

if [[ ! -d "$SWIFTLY_HOME_DIR/toolchains/5.7.3" ]]; then
test_fail "Expected installed toolchain directory to still exist, but it has been deleted"
fi

# Attempt the same installation, but overwrite this time.
printf "1\ny\n" | ./swiftly-install.sh --no-install-system-deps
test_log "Attempting the same installation (--overwrite flag is specified)"
./swiftly-install.sh -y --overwrite --no-install-system-deps

if ! has_command "swiftly" ; then
test_fail "Can't find swiftly on the PATH"
Expand All @@ -67,6 +80,10 @@ if [[ -d "$SWIFTLY_HOME_DIR/toolchains/5.7.3" ]]; then
test_fail "Expected installed toolchain directory to have been overwritten, but it still exists"
fi

if [ -L "$SWIFTLY_BIN_DIR/$dummy_executable_name" ]; then
test_fail "Expected symlink to have been deleted, but it still exists"
fi

swiftly --version

test_pass
2 changes: 1 addition & 1 deletion install/tests/platform-option.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trap cleanup EXIT
platforms=("ubuntu22.04" "ubuntu20.04" "ubuntu18.04" "amazonlinux2" "rhel9")

for platform in "${platforms[@]}"; do
./swiftly-install.sh --disable-confirmation --no-install-system-deps --platform "$platform"
./swiftly-install.sh --overwrite --disable-confirmation --no-install-system-deps --platform "$platform"
cat $HOME/.local/share/swiftly/config.json

if [[ "$platform" == "rhel9" ]]; then
Expand Down
2 changes: 1 addition & 1 deletion install/tests/update-bash-profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if [[ "$(cat $HOME/.bash_login)" != "" ]]; then
fi

rm "$HOME/.bash_profile"
printf "1\ny\n" | ./swiftly-install.sh --no-install-system-deps
./swiftly-install.sh -y --overwrite --no-install-system-deps

if [[ -f "$HOME/.bash_profile" ]]; then
test_fail "install created .bash_profile when it should not have"
Expand Down