Skip to content

Commit 22ba150

Browse files
authored
swiftly-install: support fish shell (#72) (#91)
1 parent 76f1ac0 commit 22ba150

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

install/swiftly-install.sh

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
# to store platform information, downloaded toolchains, and other state required to manage
1313
# the toolchains.
1414
#
15-
# After installation, the script will create $SWIFTLY_HOME_DIR/env.sh, which can be sourced
15+
# After installation, the script will create $SWIFTLY_HOME_DIR/env.{sh,fish}, which can be sourced
1616
# to properly set up the environment variables required to run swiftly. Unless --no-modify-profile
17-
# was specified, the script will also update ~/.profile, ~/.bash_profile, ~/.bash_login, or ~/.zprofile,
18-
# depending on the value of $SHELL and the existence of the files, to source the env.sh file on login.
17+
# was specified, the script will also update ~/.profile, ~/.bash_profile, ~/.bash_login, ~/.zprofile,
18+
# $XDG_CONFIG_HOME/fish/conf.d/swiftly.fish, or ~/.config/fish/conf.d/swiftly.fish depending on
19+
# the value of $SHELL and the existence of the files, to source the env.{sh,fish} file on login.
1920
# This will ensure that future logins will automatically configure SWIFTLY_HOME_DIR, SWIFTLY_BIN_DIR,
2021
# and PATH.
2122
#
@@ -486,6 +487,15 @@ case "$SHELL" in
486487
PROFILE_FILE="$HOME/.bash_login"
487488
fi
488489
;;
490+
*"fish")
491+
# $XDG_CONFIG_HOME/fish/conf.d/swiftly.fish or ~/.config/fish/conf.d/swiftly.fish
492+
# https://github.com/fish-shell/fish-shell/issues/3170#issuecomment-228311857
493+
if [ -n "$XDG_CONFIG_HOME" ]; then
494+
PROFILE_FILE="$XDG_CONFIG_HOME/fish/conf.d/swiftly.fish"
495+
else
496+
PROFILE_FILE="$HOME/.config/fish/conf.d/swiftly.fish"
497+
fi
498+
;;
489499
*)
490500
esac
491501

@@ -570,25 +580,41 @@ curl \
570580

571581
chmod +x "$BIN_DIR/swiftly"
572582

573-
if [[ "$detected_existing_installation" != "true" || "$overwrite_existing_intallation" == "true" ]]; then
574-
echo "$JSON_OUT" > "$HOME_DIR/config.json"
575-
576-
# Verify the downloaded executable works. The script will exit if this fails due to errexit.
577-
SWIFTLY_HOME_DIR="$HOME_DIR" SWIFTLY_BIN_DIR="$BIN_DIR" "$BIN_DIR/swiftly" --version > /dev/null
578-
579-
ENV_OUT=$(cat <<EOF
583+
case "$SHELL" in
584+
*"fish")
585+
ENV_OUT=$(cat <<EOF
586+
set -x SWIFTLY_HOME_DIR "$(replace_home_path $HOME_DIR)"
587+
set -x SWIFTLY_BIN_DIR "$(replace_home_path $BIN_DIR)"
588+
if not contains "\$SWIFTLY_BIN_DIR" \$PATH
589+
set -x PATH "\$SWIFTLY_BIN_DIR" \$PATH
590+
end
591+
EOF
592+
)
593+
ENV_FILE="env.fish"
594+
;;
595+
*)
596+
ENV_OUT=$(cat <<EOF
580597
export SWIFTLY_HOME_DIR="$(replace_home_path $HOME_DIR)"
581598
export SWIFTLY_BIN_DIR="$(replace_home_path $BIN_DIR)"
582599
if [[ ":\$PATH:" != *":\$SWIFTLY_BIN_DIR:"* ]]; then
583600
export PATH="\$SWIFTLY_BIN_DIR:\$PATH"
584601
fi
585602
EOF
586-
)
603+
)
604+
ENV_FILE="env.sh"
605+
;;
606+
esac
607+
608+
if [[ "$detected_existing_installation" != "true" || "$overwrite_existing_intallation" == "true" ]]; then
609+
echo "$JSON_OUT" > "$HOME_DIR/config.json"
610+
611+
# Verify the downloaded executable works. The script will exit if this fails due to errexit.
612+
SWIFTLY_HOME_DIR="$HOME_DIR" SWIFTLY_BIN_DIR="$BIN_DIR" "$BIN_DIR/swiftly" --version > /dev/null
587613

588-
echo "$ENV_OUT" > "$HOME_DIR/env.sh"
614+
echo "$ENV_OUT" > "$HOME_DIR/$ENV_FILE"
589615

590616
if [[ "$MODIFY_PROFILE" == "true" ]]; then
591-
SOURCE_LINE=". $(replace_home_path $HOME_DIR)/env.sh"
617+
SOURCE_LINE=". $(replace_home_path $HOME_DIR)/$ENV_FILE"
592618

593619
# Only append the line if it isn't in .profile already.
594620
if [[ ! -f "$PROFILE_FILE" ]] || [[ ! "$(cat $PROFILE_FILE)" =~ "$SOURCE_LINE" ]]; then
@@ -613,7 +639,7 @@ if ! has_command "swiftly" || [[ "$HOME_DIR" != "$DEFAULT_HOME_DIR" || "$BIN_DIR
613639
fi
614640
echo "To begin using swiftly from your current shell, first run the following command:"
615641
echo ""
616-
echo " . $(replace_home_path $HOME_DIR)/env.sh"
642+
echo " . $(replace_home_path $HOME_DIR)/$ENV_FILE"
617643
echo ""
618644
echo "Then to install the latest version of Swift, run 'swiftly install latest'"
619645
else

install/tests/update-fish-config.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
3+
# Tests that swiftly.fish is updated properly if the user's shell is fish.
4+
# WARNING: this test makes changes to the local filesystem and is intended to be run in a containerized environment.
5+
6+
set -o errexit
7+
source ./test-util.sh
8+
9+
cleanup () {
10+
set +o errexit
11+
12+
rm "$HOME/.config/fish/conf.d/swiftly.fish"
13+
rm "$HOME/.xdg/config/fish/conf.d/swiftly.fish"
14+
15+
rm -r "$HOME/.local/share/swiftly"
16+
rm "$HOME/.local/bin/swiftly"
17+
}
18+
trap cleanup EXIT
19+
20+
export SHELL="fish"
21+
22+
mkdir -p "$HOME/.config/fish/conf.d"
23+
echo "1" | ./swiftly-install.sh --no-install-system-deps
24+
25+
if [[ ! "$(cat $HOME/.config/fish/conf.d/swiftly.fish)" =~ "swiftly/env.fish" ]]; then
26+
test_fail "install did not update ~/.config/fish/conf.d/swiftly.fish"
27+
fi
28+
29+
export XDG_CONFIG_HOME="$HOME/.xdg/config"
30+
mkdir -p "$XDG_CONFIG_HOME/fish/conf.d"
31+
./swiftly-install.sh -y --overwrite --no-install-system-deps
32+
33+
if [[ ! "$(cat $XDG_CONFIG_HOME/fish/conf.d/swiftly.fish)" =~ "swiftly/env.fish" ]]; then
34+
test_fail "install did not update \$XDG_CONFIG_HOME/fish/conf.d/swiftly.fish"
35+
fi
36+
37+
38+
test_pass

0 commit comments

Comments
 (0)