forked from semaphoreci/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sem-version
executable file
·214 lines (169 loc) · 4.58 KB
/
sem-version
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
source ~/.toolbox/sem-install
# Changing language versions
VERSION="0.7"
SUPPORTED_LANGUAGES=(elixir go java php ruby python node c cpp erlang scala)
# Misc
DATE_FORMAT='%H:%M %d/%m/%Y'
################################################################################
# Generic function which executes the passed 'command' to switch the version
# Globals:
# language, language_version
# Arguments:
# command
# Returns:
# 0, 1-127 (exit status of the language manager command)
version::change() {
command=$1
version::log "Changing '${language}' to version ${language_version}"
eval $command $language_version
ch_status=$?
return $ch_status
}
version::change_ruby() {
if ! [ -d ~/.rbenv/versions/${language_version} ]; then
sem-install ruby ${language_version}
fi
version::change "rbenv global"
return $?
}
version::change_node() {
version::change "nvm use"
return $?
}
version::change_elixir() {
if [ -z $(kiex list | grep ${language_version}) ];then
kiex install ${language_version}
fi
version::change "kiex use"
return $?
}
version::change_go() {
version::change "change-go-version"
return $?
}
version::change_java() {
version::change "change-java-version"
return $?
}
version::change_php() {
if ! [ -d ~/.phpbrew/php/php-${language_version} ]; then
sem-install php ${language_version}
fi
version::change "phpbrew use"
return $?
}
version::change_python() {
version::change "change-python-version"
return $?
}
version::change_c() {
version::change "change-gcc-version"
return $?
}
version::change_cpp() {
version::change "change-gcc-version"
return $?
}
version::change_scala() {
version::change "change-scala-version"
return $?
}
version::change_erlang() {
version::change "change-erlang-version"
return $?
}
################################################################################
# End of language manager wrappers
################################################################################
################################################################################
# Prints the usage
# Globals:
# none
# Arguments:
# none
# Returns:
# none
version::usage() {
echo -e "
################################################################################
sem-version ${VERSION} | Utility to change a selected languages active version
Usage:
sem-version language version
################################################################################"
}
################################################################################
# Helper to print date in the given format
# Globals:
# DATE_FORMAT
# Arguments:
# message
# Returns:
# none
################################################################################
version::date() {
date +"${DATE_FORMAT}"
}
################################################################################
# Helper to display info messages
# Globals:
# none
# Arguments:
# message
# Returns:
# none
################################################################################
version::log() {
echo -e "\n[$(version::date)]: $@" >&2
}
################################################################################
# Helper function to show error messages and to terminate execution on error
# Globals:
# DATE_FORMAT
# Arguments:
# message
# Returns:
# 1
################################################################################
version::err() {
echo -e "\n! [$(version::date)]: $@" >&2
return 1
}
################################################################################
# The main entrypoint to the script. Holds the switching logic.
# Globals:
# SUPPORTED_LANGUAGES
# Arguments:
# 'language', 'version'
# Returns:
# none
################################################################################
version::main() {
if ! [[ $# -eq 2 ]]; then
version::usage
version::err "Unsupported number of arguments provided!"
return $?
fi
language=$1
language_version=$2
for (( i=0; i < ${#SUPPORTED_LANGUAGES}; i++ )); do
if [[ ${SUPPORTED_LANGUAGES[i]} -eq $language ]]; then
fn_name="version::change_${language}"
if ! [[ $(type -t $fn_name) == function ]]; then
version::err "Function '${fn_name}' not implemented. Language '${language}' might not be supported."
return $?
fi
version::change_${language} $language_version
fn_status=$?
if [[ fn_status -ne 0 ]]; then
version::err "Failed to switch version.\nLog:\n$log"
return $?
fi
version::log "Switch successful."
break
fi
done
}
sem-version() {
version::main "$@"
}