Skip to content

Commit 78741fa

Browse files
committed
Adds path to profile
1 parent e36c1a4 commit 78741fa

File tree

1 file changed

+105
-15
lines changed

1 file changed

+105
-15
lines changed

install_helper.sh

+105-15
Original file line numberDiff line numberDiff line change
@@ -5,24 +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
25-
2634
temp_path=/tmp/code-server-$version
2735

2836
if [ -d $temp_path ]; then
@@ -40,10 +48,10 @@ linux_install() {
4048
rm code-server*.tar.gz
4149

4250
if [ -d $lib_path ]; then
43-
tput setaf $RED
51+
red_color
4452
echo "-- ERROR: v$version already found in $lib_path"
4553
echo "-- ERROR: To reinstall, first delete this directory"
46-
tput sgr 0
54+
no_color
4755
rm -rf -f $temp_path
4856
exit 1
4957
fi
@@ -55,19 +63,101 @@ linux_install() {
5563
mkdir -p $bin_dir
5664
ln -f -s $lib_path/code-server $bin_path
5765

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-
6566
rm -rf -f $temp_path
6667
echo "-- Successfully installed code-server at $bin_path"
6768
}
6869

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

0 commit comments

Comments
 (0)