-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathinstall_helper.sh
executable file
·164 lines (135 loc) · 4.46 KB
/
install_helper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env bash
# install_helper allows for easy installation of code-server on Linux systems
# the latest official release is pulled from the Github API, where the proper asset
# is then unpackaged and installed into ~/.local/share/code-server/<version>
# The code-server binary is linked to ~/.local/share/code-server/bin/code-server
set -eo pipefail
red_color() {
tput setaf 1
}
cyan_color() {
tput setaf 6
}
no_color() {
tput sgr 0
}
get_releases() {
curl --silent "https://api.github.com/repos/cdr/code-server/releases/latest" |
grep '"browser_download_url":\|"tag_name":'
}
bin_dir=$HOME/.code-server/bin
bin_path=$bin_dir/code-server
lib_path=$HOME/.code-server/$version
linux_install() {
releases=$(get_releases)
package=$(echo "$releases" | grep 'linux' | grep 'x86' | sed -E 's/.*"([^"]+)".*/\1/')
version=$(echo $releases | sed -E 's/.*"tag_name": "([^"]+)".*/\1/')
temp_path=/tmp/code-server-$version
if [ -d $temp_path ]; then
rm -rf $temp_path
fi
mkdir $temp_path
cd $temp_path
echo "-- Downloading code-server v$version"
wget $package > /dev/null
echo "-- Unpacking code-server release"
tar -xzf code-server*.tar.gz > /dev/null
rm code-server*.tar.gz
if [ -d $lib_path ]; then
red_color
echo "-- ERROR: v$version already found in $lib_path"
echo "-- ERROR: To reinstall, first delete this directory"
no_color
rm -rf -f $temp_path
exit 1
fi
mkdir -p $lib_path
mv -f code-server*/* $lib_path/
mkdir -p $bin_dir
ln -f -s $lib_path/code-server $bin_path
rm -rf -f $temp_path
echo "-- Successfully installed code-server at $bin_path"
}
# @see https://yarnpkg.com/install.sh
detect_profile() {
if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
echo "${PROFILE}"
return
fi
local DETECTED_PROFILE
DETECTED_PROFILE=''
local SHELLTYPE
SHELLTYPE="$(basename "/$SHELL")"
if [ "$SHELLTYPE" = "bash" ]; then
if [ -f "$HOME/.bashrc" ]; then
DETECTED_PROFILE="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
DETECTED_PROFILE="$HOME/.bash_profile"
fi
elif [ "$SHELLTYPE" = "zsh" ]; then
DETECTED_PROFILE="$HOME/.zshrc"
elif [ "$SHELLTYPE" = "fish" ]; then
DETECTED_PROFILE="$HOME/.config/fish/config.fish"
fi
if [ -z "$DETECTED_PROFILE" ]; then
if [ -f "$HOME/.profile" ]; then
DETECTED_PROFILE="$HOME/.profile"
elif [ -f "$HOME/.bashrc" ]; then
DETECTED_PROFILE="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
DETECTED_PROFILE="$HOME/.bash_profile"
elif [ -f "$HOME/.zshrc" ]; then
DETECTED_PROFILE="$HOME/.zshrc"
elif [ -f "$HOME/.config/fish/config.fish" ]; then
DETECTED_PROFILE="$HOME/.config/fish/config.fish"
fi
fi
if [ ! -z "$DETECTED_PROFILE" ]; then
echo "$DETECTED_PROFILE"
fi
}
add_to_path() {
echo '-- Adding to $PATH...'
DETECTED_PROFILE="$(detect_profile)"
SOURCE_STR="\nexport PATH=\"\$HOME/.code-server/bin:\$PATH\"\n"
if [ -z "${DETECTED_PROFIL}" ]; then
red_color
echo "-- Profile not found. Tried ${DETECTED_PROFILE} (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
echo "-- Create one of them and run this script again"
echo "-- Create it (touch ${DETECTED_PROFILE}) and run this script again"
echo " OR"
echo "-- Append the following line to the correct file yourself"
no_color
cyan_color
echo -e "${SOURCE_STR}"
no_color
else
if ! grep -q 'code-server/bin' "$DETECTED_PROFILE"; then
if [[ $DETECTED_PROFILE == *"fish"* ]]; then
command fish -c 'set -U fish_user_paths $fish_user_paths ~/.code-server/bin'
echo "-- We've added ~/.code-server/bin to your fish_user_paths universal variable\n"
else
echo -e "$SOURCE_STR" >> "$DETECTED_PROFILE"
echo "-- We've added the following to your $DETECTED_PROFILE\n"
fi
echo "-- If this isn't the profile of your current shell then please add the following to your correct profile:"
cyan_color
echo -e "$SOURCE_STR"
no_color
fi
version=$($bin_path --version) || (
red_color
echo "-- code-server was installed, but doesn't seem to be working :("
no_color
exit 1
)
echo "-- Successfully installed code-server $version! Please open another terminal where the \`code-server\` command will now be available."
fi
}
if [[ $OSTYPE == "linux-gnu" ]]; then
linux_install
add_to_path
else
echo "Unknown operating system. Not installing."
exit 1
fi