5
5
6
6
# The code-server binary is linked to ~/.local/share/code-server/bin/code-server
7
7
8
- set -euo pipefail
8
+ set -eo pipefail
9
9
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
+ }
11
19
12
20
get_releases () {
13
21
curl --silent " https://api.github.com/repos/cdr/code-server/releases/latest" |
14
22
grep ' "browser_download_url":\|"tag_name":'
15
23
}
16
24
25
+ bin_dir=$HOME /.code-server/bin
26
+ bin_path=$bin_dir /code-server
27
+ lib_path=$HOME /.code-server/$version
28
+
17
29
linux_install () {
18
30
releases=$( get_releases)
19
31
package=$( echo " $releases " | grep ' linux' | grep ' x86' | sed -E ' s/.*"([^"]+)".*/\1/' )
20
32
version=$( echo $releases | sed -E ' s/.*"tag_name": "([^"]+)".*/\1/' )
21
33
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
-
26
34
temp_path=/tmp/code-server-$version
27
35
28
36
if [ -d $temp_path ]; then
@@ -40,10 +48,10 @@ linux_install() {
40
48
rm code-server* .tar.gz
41
49
42
50
if [ -d $lib_path ]; then
43
- tput setaf $RED
51
+ red_color
44
52
echo " -- ERROR: v$version already found in $lib_path "
45
53
echo " -- ERROR: To reinstall, first delete this directory"
46
- tput sgr 0
54
+ no_color
47
55
rm -rf -f $temp_path
48
56
exit 1
49
57
fi
@@ -55,19 +63,101 @@ linux_install() {
55
63
mkdir -p $bin_dir
56
64
ln -f -s $lib_path /code-server $bin_path
57
65
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
-
65
66
rm -rf -f $temp_path
66
67
echo " -- Successfully installed code-server at $bin_path "
67
68
}
68
69
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
+
69
158
if [[ $OSTYPE == " linux-gnu" ]]; then
70
159
linux_install
160
+ add_to_path
71
161
else
72
162
echo " Unknown operating system. Not installing."
73
163
exit 1
0 commit comments