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
34
26
35
temp_path=/tmp/code-server-$version
27
36
@@ -40,10 +49,10 @@ linux_install() {
40
49
rm code-server* .tar.gz
41
50
42
51
if [ -d $lib_path ]; then
43
- tput setaf $RED
52
+ red_color
44
53
echo " -- ERROR: v$version already found in $lib_path "
45
54
echo " -- ERROR: To reinstall, first delete this directory"
46
- tput sgr 0
55
+ no_color
47
56
rm -rf -f $temp_path
48
57
exit 1
49
58
fi
@@ -55,19 +64,101 @@ linux_install() {
55
64
mkdir -p $bin_dir
56
65
ln -f -s $lib_path /code-server $bin_path
57
66
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
67
rm -rf -f $temp_path
66
68
echo " -- Successfully installed code-server at $bin_path "
67
69
}
68
70
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
+
69
159
if [[ $OSTYPE == " linux-gnu" ]]; then
70
160
linux_install
161
+ add_to_path
71
162
else
72
163
echo " Unknown operating system. Not installing."
73
164
exit 1
0 commit comments