-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkotlin.eselect.in
300 lines (261 loc) · 8.61 KB
/
kotlin.eselect.in
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Copyright 2021-2022 Gentoo Authors
# Distributed under the terms of the GNU GPL version 2 or later
DESCRIPTION="Manage Kotlin compiler preferences"
MAINTAINER="@PACKAGE_BUGREPORT@"
VERSION="@VERSION@"
PREF_SYSTEM_ROOT="${EROOT}/etc/eselect/kotlin/homes"
PREF_USER_SYMLINK="${HOME}/.gentoo${EPREFIX}/kotlin/home"
PKG_DESC_ROOT="${EROOT}/usr/share/eselect-kotlin/pkgs"
# Checks if the string specified via the first positional parameter is a valid
# Kotlin feature release version string, or calls 'die' otherwise.
validate_ver() {
grep -q -x "[0-9]\+.[0-9]\+" <<< "$1" ||
die "Invalid version: $1\nThe version string's format is x.y"
}
# Checks if there is permission to modify system-wide preferences, or calls
# 'die' otherwise.
check_system_perm() {
[[ -w "${PREF_SYSTEM_ROOT}" || -w "${PREF_SYSTEM_ROOT%/*/*/*}" ]] ||
die "Could not set system-wide preference: Permission denied"
}
# Returns whether the file name specified via the first positional parameter is
# for a symbolic link to a directory.
is_dir_symlink() {
[[ -L "$1" && -d "$1" ]]
return $?
}
# Echoes a list of Kotlin compiler packages currently installed on the system.
# Accepts an optional argument for a Kotlin feature release version number,
# which causes only Kotlin compiler packages for that feature release to be
# included in the list.
find_targets() {
[[ -d "${PKG_DESC_ROOT}" ]] || return
local vers
if [[ -n "$1" ]]; then
vers="${PKG_DESC_ROOT}/$1"
# Ensure the directory for the specified version exists to prevent
# "No such file or directory" error
[[ -d "${vers}" ]] || return
else
vers="${PKG_DESC_ROOT}"/*
fi
for ver in ${vers}; do
[[ -d "${ver}" ]] || continue
for pkg in "${ver}"/*; do
[[ -f "${pkg}" ]] && basename "${pkg}"
done
done
}
# Echoes the name of the package pointed by the symbolic link specified via the
# first positional parameter.
get_pkg_from_symlink() {
basename "$(readlink "$1")"
}
# Echoes the feature release version number in the package name specified via
# the first positional parameter.
get_ver_from_pkg() {
local pkg_desc="$(find "${PKG_DESC_ROOT}" -type f -name "$1")"
basename "$(dirname "${pkg_desc}")"
}
# Sets to a target package a symbolic link, which are specified via the first
# and the second positional parameter respectively. A third optional positional
# parameter can be used to set a Kotlin feature release version. This will be
# used if the target is specified as a number, in which case the number should
# match an item in the list returned by the 'list <version>' action. In
# addition, specifying a feature release will also cause an additional check for
# verifying if the target package is exactly for the feature release.
set_symlink() {
local target="$1" symlink="$2" list_ver="$3"
if is_number "${target}"; then
local targets=( $(find_targets "${list_ver}") )
target=${targets[$(( ${target} - 1 ))]}
fi
[[ -n "${target}" ]] || die "Invalid compiler specification: '$1'"
if [[ -n "${list_ver}" &&
"$(get_ver_from_pkg "${target}")" != "${list_ver}" ]]; then
die "Package ${target} is not for Kotlin ${list_ver}"
fi
local pkg_desc="$(find "${PKG_DESC_ROOT}" -type f -name "${target}")"
[[ -n "${pkg_desc}" ]] || die "Could not find package ${target}"
local pkg_home="$(
. "${pkg_desc}"
echo "${GENTOO_KOTLIN_HOME}"
)"
if [[ -d "${pkg_home}" ]]; then
local sym_dir="$(dirname "${symlink}")"
mkdir -p "${sym_dir}" ||
die "Could not create the directory for preferences: ${sym_dir}"
ln -snf "${pkg_home}" "${symlink}" ||
die "Could not create a symbolic link to ${pkg_home} at ${symlink}"
else
die "Could not find the contents for package ${target} at ${pkg_home}"
fi
}
# 'show' action
describe_show() {
echo "Show the current Kotlin compiler"
}
describe_show_options() {
echo "<version> : Limit to a specific Kotlin version (optional)"
}
describe_show_parameters() {
echo "[user|system|<version>]"
}
my_show() {
local symlink="$1" symlink_type="$2"
write_list_start "Current Kotlin compiler for ${symlink_type}"
if [[ -L "${symlink}" ]]; then
write_kv_list_entry "$(get_pkg_from_symlink "${symlink}")" ""
else
write_kv_list_entry "(unset)" ""
fi
}
do_show() {
if [[ "$1" == "user" ]]; then
my_show "${PREF_USER_SYMLINK}" "$1"
elif [[ "$1" == "system" ]]; then
my_show "${PREF_SYSTEM_ROOT}/$1" "$1"
elif [[ -n "$1" ]]; then
validate_ver "$1"
my_show "${PREF_SYSTEM_ROOT}/$1" "$1"
else
for ver in "${PREF_SYSTEM_ROOT}"/*; do
if is_dir_symlink "${ver}"; then
my_show "${ver}" "$(basename "${ver}")"
fi
done
# If the system compiler symbolic link is not present, the previous
# for loop will not print anything for the system compiler. In this
# case, print it separately to report that it is unset
local system_symlink="${PREF_SYSTEM_ROOT}/system"
if ! is_dir_symlink "${system_symlink}"; then
my_show "${system_symlink}" "system"
fi
my_show "${PREF_USER_SYMLINK}" "user"
fi
}
# 'list' action
describe_list() {
echo "List installed Kotlin compilers"
}
describe_list_options() {
echo "<version> : Limit to a specific Kotlin version (optional)"
}
describe_list_parameters() {
echo "[<version>]"
}
do_list() {
if [[ -n "$1" ]]; then
validate_ver "$1"
write_list_start "Available Kotlin compilers for Kotlin $1:"
else
write_list_start "Available Kotlin compilers:"
fi
local targets=( $(find_targets "$1") )
for (( i = 0; i < ${#targets[@]}; i++ )); do
local target="${targets[i]}"
local mark=""
for symlink in "${PREF_SYSTEM_ROOT}"/*; do
ver="$(basename "${symlink}")"
if [[ "${target}" == "$(get_pkg_from_symlink "${symlink}")" ]]; then
mark+=" ${ver}"
fi
done
if [[ "${target}" == \
"$(get_pkg_from_symlink "${PREF_USER_SYMLINK}")" ]]; then
mark+=" user"
fi
targets[i]="${targets[i]} $(highlight ${mark})"
done
write_numbered_list -m "(none found)" "${targets[@]}"
}
# 'set' action
describe_set() {
echo "Set a default Kotlin compiler"
}
describe_set_options() {
echo "--if-unset : Do not change unless no valid compiler is set"
echo "<version> : Limit to a specific Kotlin version (optional)"
}
describe_set_parameters() {
echo "<user|system|<version>> <compiler>"
}
my_set() {
local symlink="$1" target="$2" if_unset="$3" list_ver="$4"
[[ -n "${target}" ]] || die -q "No compiler specified"
if [[ -L "${symlink}" ]]; then
if [[ ! "${if_unset}" || ! -d "${symlink}" ]]; then
# Overwrite the existing symbolic link
set_symlink "${target}" "${symlink}" "${list_ver}"
fi
elif [[ -e "${symlink}" ]]; then
die -q "Target already exists but is not a symbolic link: ${symlink}"
else
# Set a new symbolic link
set_symlink "${target}" "${symlink}" "${list_ver}"
fi
}
do_set() {
local if_unset
if [[ "$1" == "--if-unset" ]]; then
if_unset=true
shift
fi
[[ $# == 2 ]] || die -q "Usage: <user|system|<version>> <compiler>"
if [[ "$1" == "user" ]]; then
[[ "${UID}" != 0 ]] || die -q "Cannot set the user compiler for root"
my_set "${PREF_USER_SYMLINK}" "$2" "${if_unset}"
else
check_system_perm
if [[ "$1" != "system" ]]; then
validate_ver "$1"
my_set "${PREF_SYSTEM_ROOT}/$1" "$2" "${if_unset}" "$1"
else
my_set "${PREF_SYSTEM_ROOT}/$1" "$2" "${if_unset}"
fi
fi
}
# 'update' action
describe_update() {
echo "Ensure compiler preference for every installed Kotlin version is set"
}
do_update() {
check_system_perm
[[ -d "${PKG_DESC_ROOT}" ]] || return
local system_symlink="${PREF_SYSTEM_ROOT}/system"
if [[ ! -L "${system_symlink}" && -e "${system_symlink}" ]]; then
die "System preference file is not a symbolic link: ${system_symlink}"
fi
if ! is_dir_symlink "${system_symlink}"; then
# System compiler is not set; arbitrarily select a compiler package for
# the latest feature release and set it
local latest_ver="$(ls -1 -v "${PKG_DESC_ROOT}" | tail -1)"
local system_target=( $(find_targets "${latest_ver}") )
set_symlink "${system_target}" "${system_symlink}"
fi
for ver in "${PKG_DESC_ROOT}"/*; do
ver="$(basename "${ver}")"
local symlink="${PREF_SYSTEM_ROOT}/${ver}"
if [[ ! -L "${symlink}" && -e "${symlink}" ]]; then
die "Target already exists but is not a symbolic link: ${symlink}"
fi
if ! is_dir_symlink "${symlink}"; then
# If any compiler packages are available, arbitrarily select one
local target=( $(find_targets "${ver}") )
[[ -n "${target}" ]] && set_symlink "${target}" "${symlink}"
fi
done
}
# 'cleanup' action
describe_cleanup() {
echo "Unset system-wide preferences that are invalid"
}
do_cleanup() {
check_system_perm
[[ -d "${PREF_SYSTEM_ROOT}" ]] || return
for symlink in "${PREF_SYSTEM_ROOT}"/*; do
if [[ -L "${symlink}" && ! -d "${symlink}" ]]; then
rm "${symlink}" || die "Failed to unset compiler preference"
fi
done
}