Skip to content

Commit dad0ce7

Browse files
committed
Adds path to profile
1 parent 15ba717 commit dad0ce7

File tree

1 file changed

+105
-14
lines changed

1 file changed

+105
-14
lines changed

install_helper.sh

+105-14
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,32 @@
55

66
# The code-server binary is linked to ~/.local/share/code-server/bin/code-server
77

8-
set -euo pipefail
8+
set -eo pipefail
99

10-
RED=1
10+
red_color() {
11+
tput setaf 1
12+
}
13+
cyan_color() {
14+
tput setaf 6
15+
}
16+
no_color() {
17+
tput sgr 0
18+
}
1119

1220
get_releases() {
1321
curl --silent "https://api.github.com/repos/cdr/code-server/releases/latest" |
1422
grep '"browser_download_url":\|"tag_name":'
1523
}
1624

25+
bin_dir=$HOME/.code-server/bin
26+
bin_path=$bin_dir/code-server
27+
lib_path=$HOME/.code-server/$version
28+
1729
linux_install() {
1830
releases=$(get_releases)
1931
package=$(echo "$releases" | grep 'linux' | grep 'x86' | sed -E 's/.*"([^"]+)".*/\1/')
2032
version=$(echo $releases | sed -E 's/.*"tag_name": "([^"]+)".*/\1/')
2133

22-
bin_dir=$HOME/.local/share/code-server/bin
23-
bin_path=$bin_dir/code-server
24-
lib_path=$HOME/.local/share/code-server/$version
2534

2635
temp_path=/tmp/code-server-$version
2736

@@ -40,10 +49,10 @@ linux_install() {
4049
rm code-server*.tar.gz
4150

4251
if [ -d $lib_path ]; then
43-
tput setaf $RED
52+
red_color
4453
echo "-- ERROR: v$version already found in $lib_path"
4554
echo "-- ERROR: To reinstall, first delete this directory"
46-
tput sgr 0
55+
no_color
4756
rm -rf -f $temp_path
4857
exit 1
4958
fi
@@ -55,19 +64,101 @@ linux_install() {
5564
mkdir -p $bin_dir
5665
ln -f -s $lib_path/code-server $bin_path
5766

58-
code_server_bin=$(which code-server || true)
59-
if [ "$code_server_bin" == "" ]; then
60-
tput setaf $RED
61-
echo "-- WARNING: $bin_dir is not in your \$PATH"
62-
tput sgr 0
63-
fi
64-
6567
rm -rf -f $temp_path
6668
echo "-- Successfully installed code-server at $bin_path"
6769
}
6870

71+
# @see https://yarnpkg.com/install.sh
72+
detect_profile() {
73+
if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
74+
echo "${PROFILE}"
75+
return
76+
fi
77+
78+
local DETECTED_PROFILE
79+
DETECTED_PROFILE=''
80+
local SHELLTYPE
81+
SHELLTYPE="$(basename "/$SHELL")"
82+
83+
if [ "$SHELLTYPE" = "bash" ]; then
84+
if [ -f "$HOME/.bashrc" ]; then
85+
DETECTED_PROFILE="$HOME/.bashrc"
86+
elif [ -f "$HOME/.bash_profile" ]; then
87+
DETECTED_PROFILE="$HOME/.bash_profile"
88+
fi
89+
elif [ "$SHELLTYPE" = "zsh" ]; then
90+
DETECTED_PROFILE="$HOME/.zshrc"
91+
elif [ "$SHELLTYPE" = "fish" ]; then
92+
DETECTED_PROFILE="$HOME/.config/fish/config.fish"
93+
fi
94+
95+
if [ -z "$DETECTED_PROFILE" ]; then
96+
if [ -f "$HOME/.profile" ]; then
97+
DETECTED_PROFILE="$HOME/.profile"
98+
elif [ -f "$HOME/.bashrc" ]; then
99+
DETECTED_PROFILE="$HOME/.bashrc"
100+
elif [ -f "$HOME/.bash_profile" ]; then
101+
DETECTED_PROFILE="$HOME/.bash_profile"
102+
elif [ -f "$HOME/.zshrc" ]; then
103+
DETECTED_PROFILE="$HOME/.zshrc"
104+
elif [ -f "$HOME/.config/fish/config.fish" ]; then
105+
DETECTED_PROFILE="$HOME/.config/fish/config.fish"
106+
fi
107+
fi
108+
109+
if [ ! -z "$DETECTED_PROFILE" ]; then
110+
echo "$DETECTED_PROFILE"
111+
fi
112+
}
113+
114+
add_to_path() {
115+
echo "-- Adding to \$PATH..."
116+
DETECTED_PROFILE="$(detect_profile)"
117+
SOURCE_STR="\nexport PATH=\"\$HOME/.code-server/bin:\$PATH\"\n"
118+
119+
if [ -z "${DETECTED_PROFIL}" ] ; then
120+
red_color
121+
echo "-- Profile not found. Tried ${DETECTED_PROFILE} (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
122+
echo "-- Create one of them and run this script again"
123+
echo "-- Create it (touch ${DETECTED_PROFILE}) and run this script again"
124+
echo " OR"
125+
echo "-- Append the following line to the correct file yourself"
126+
no_color
127+
128+
cyan_color
129+
echo -e "${SOURCE_STR}"
130+
no_color
131+
else
132+
if ! grep -q 'code-server/bin' "$DETECTED_PROFILE"; then
133+
if [[ $DETECTED_PROFILE == *"fish"* ]]; then
134+
command fish -c 'set -U fish_user_paths $fish_user_paths ~/.code-server/bin'
135+
echo "-- We've added ~/.code-server/bin to your fish_user_paths universal variable\n"
136+
else
137+
echo -e "$SOURCE_STR" >> "$DETECTED_PROFILE"
138+
echo "-- We've added the following to your $DETECTED_PROFILE\n"
139+
fi
140+
141+
echo "-- If this isn't the profile of your current shell then please add the following to your correct profile:"
142+
143+
cyan_color
144+
echo -e "$SOURCE_STR"
145+
no_color
146+
fi
147+
148+
version=`$bin_path --version` || (
149+
red_color
150+
echo "-- code-server was installed, but doesn't seem to be working :("
151+
no_color
152+
exit 1;
153+
)
154+
155+
echo "-- Successfully installed code-server $version! Please open another terminal where the \`code-server\` command will now be available."
156+
fi
157+
}
158+
69159
if [[ $OSTYPE == "linux-gnu" ]]; then
70160
linux_install
161+
add_to_path
71162
else
72163
echo "Unknown operating system. Not installing."
73164
exit 1

0 commit comments

Comments
 (0)