aboutsummaryrefslogtreecommitdiff
blob: da9ecf05390104ec313269b6beb752b5c52ac3d7 (plain)
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
# -*-eselect-*-  vim: ft=eselect
# Copyright 1999-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

inherit config

DESCRIPTION="Manage D symlinks"
MAINTAINER="marco.leise@gmx.de"
VERSION="20240103"

ETC_PATH="${EROOT}/etc"
COMPILER_PATH="${EROOT}/usr/bin"
MAN1_PATH="${EROOT}/usr/share/man/man1"
MAN5_PATH="${EROOT}/usr/share/man/man5"
INC_PATH="${EROOT}/usr/include/dlang"
CONFIG_FILE="${EROOT}/var/lib/dlang"
declare -A COMPILER_NAMES=([dmd]="Digital Mars D (dmd)"
                           [ldc2]="LLVM D2 (ldc2)")


### list action ###

describe_list() {
	echo "List installed D compilers by vendor (or all installed compilers)"
}

describe_list_options() {
	for compiler in ${!COMPILER_NAMES[@]}; do
		printf "% -11s : List installed %s compilers\n" $compiler "${COMPILER_NAMES[$compiler]}"
	done
}

describe_list_parameters() {
	echo "[<vendor>]"
}

do_list() {
	[[ $# -eq 1 ]] && has "$1" ${!COMPILER_NAMES[@]} || [[ $# -eq 0 ]] \
		|| die -q "Syntax: eselect dlang list [$(compiler_options)]"

	for compiler in ${!COMPILER_NAMES[@]}; do
		if [[ $# -eq 0 ]] || [[ $compiler == $1 ]]; then
			write_list_start "Available ${COMPILER_NAMES[$compiler]} compilers:"
			local targets=($(find_targets $compiler))
			local active="$(do_show $compiler)"
			local mode="$(load_config "$CONFIG_FILE" $compiler)"
			for ((i = 0; i < ${#targets[@]}; i++)); do
				if [[ "${targets[$i]}" == "$active" ]]; then
					if [[ "${mode:-auto}" == "auto" ]]; then
						targets[$i]="$(highlight_marker "${targets[$i]}" auto)"
					else
						targets[$i]="$(highlight_marker "${targets[$i]}" manual)"
					fi
				fi
			done
			write_numbered_list -m "(none found)" "${targets[@]}"
			echo
		fi
	done
}


### set action ###

describe_set() {
	echo "Set active version of D compilers"
}

describe_set_options() {
	for compiler in ${!COMPILER_NAMES[@]}; do
		printf "% -11s : Set active %s compiler\n" $compiler "${COMPILER_NAMES[$compiler]}"
	done
}

describe_set_parameters() {
	echo "<vendor>"
}

do_set() {
	[[ $# -eq 2 ]] && has "$1" ${!COMPILER_NAMES[@]} \
		|| die -q "2 arguments required: eselect dlang set $(compiler_options) <version|index|\"auto\">"

	local targets=($(find_targets $1))
	local target="$2"
	if [[ "$target" == "auto" ]]; then
		local autoTarget="${targets[$((${#targets[@]} - 1))]}"
		if [[ -n "${targets[$(($target - 1))]}" ]]; then
			target="$autoTarget"
		fi
	elif is_number "$2" && [[ $2 -ge 1 ]]; then
		local idxToTarget="${targets[$(($target - 1))]}"
		if [[ -n "${targets[$(($target - 1))]}" ]]; then
			target="$idxToTarget"
		fi
	fi

	has "$target" "${targets[@]}" || die -q "Invalid or unavailable target"
	if [[ "$2" == "auto" ]]; then
		echo -n "Switching $1 to always latest version, currently $target..."
	else
		echo -n "Switching $1 to $target..."
	fi

	case $1 in
		dmd)
			symlink_helper "${EROOT}/usr/lib/dmd/${target}/bin/dmd"      "${COMPILER_PATH}/dmd"
			symlink_helper "${EROOT}/usr/lib/dmd/${target}/bin/dmd.conf" "${ETC_PATH}/dmd.conf"
			symlink_helper "${EROOT}/usr/lib/dmd/${target}/import"       "${INC_PATH}/dmd"
			[[ -d ${MAN1_PATH} ]] && symlink_helper "/usr/lib/dmd/${target}/man/man1/dmd.1"      "${MAN1_PATH}/dmd.1"
			[[ -d ${MAN5_PATH} ]] && symlink_helper "/usr/lib/dmd/${target}/man/man5/dmd.conf.5" "${MAN5_PATH}/dmd.conf.5"
			;;
		ldc2)
			symlink_helper "${EROOT}/usr/lib/ldc2/${target}/bin/ldc2"  "${COMPILER_PATH}/ldc2"
			symlink_helper "${EROOT}/usr/lib/ldc2/${target}/bin/ldmd2" "${COMPILER_PATH}/ldmd2"
			symlink_helper "${EROOT}/usr/lib/ldc2/${target}/include/d" "${INC_PATH}/ldc"
			;;
	esac
	store_config "$CONFIG_FILE" $1 "$2"

	echo " done"
}


### show action ###

describe_show() {
	echo "Show active D compiler by vendor"
}

describe_show_options() {
	for compiler in ${!COMPILER_NAMES[@]}; do
		printf "% -11s : Show active %s compiler\n" $compiler "${COMPILER_NAMES[$compiler]}"
	done
}

describe_show_parameters() {
	echo "<vendor>"
}

do_show() {
	[[ $# -eq 1 ]] && has "$1" ${!COMPILER_NAMES[@]} \
		|| die -q "1 argument required: eselect dlang show $(compiler_options)"

	local interpreter="$(readlink "${COMPILER_PATH}/$1" | sed -r "s#^/usr/lib(32|64)?/$1/##;s#/bin/$1\$##")"
	[[ -n "$interpreter" ]] && echo "$interpreter"
}


### update action ###

describe_update() {
	echo "Update active D compilers to the latest installed version in absense of a manually set version"
}

describe_update_options() {
	for compiler in ${!COMPILER_NAMES[@]}; do
		printf "% -11s : Update active %s compiler\n" $compiler "${COMPILER_NAMES[$compiler]}"
	done
}

describe_update_parameters() {
	echo "<vendor>"
}

do_update() {
	[[ $# -eq 1 ]] && has "$1" ${!COMPILER_NAMES[@]} \
		|| die -q "1 argument required: eselect dlang update $(compiler_options)"

	local targets=($(find_targets $1))
	if [[ ${#targets[@]} -eq 0 ]]; then
		# No compiler avalable, remove symlinks
		echo "No installed ${COMPILER_NAMES[$1]} compilers. Removing any existing symlinks."
		case $1 in
			# Keep this in sync with `do_set`
			dmd)
				rm -f "${COMPILER_PATH}/dmd"
				rm -f "${ETC_PATH}/dmd.conf"
				rm -f "${INC_PATH}/dmd"
				rm -f "${MAN1_PATH}/dmd.1"
				rm -f "${MAN5_PATH}/dmd.conf.5"
				;;
			ldc2)
				rm -f "${COMPILER_PATH}/ldc2"
				rm -f "${COMPILER_PATH}/ldmd2"
				rm -f "${INC_PATH}/ldc"
				;;
		esac
	else
		# Check if the active compiler is actually available, update otherwise
		local compiler=$1
		local active="$(do_show $compiler)"
		local latest="${targets[$((${#targets[@]} - 1))]}"
		local mode="$(load_config "$CONFIG_FILE" $compiler)"
		if [[ "$active" == "" ]]; then
			# First installation of a compiler
			do_set $compiler auto
		elif ! has "$active" "${targets[@]}"; then
			# Active compiler is no longer valid (uninstalled), switch to "auto"
			echo "Switching $1 from uninstalled version '$active' to '$latest'"
			do_set $compiler auto
		elif [[ "${mode:-auto}" == "auto" ]] && [[ "$active" != "$latest" ]]; then
			# Active compiler was set to auto and we have a more recent version
			echo "Updating active $1 version from '$active' to '$latest'"
			do_set $compiler auto
		fi
	fi
}


### helper functions ###

# Find a list of D versions
find_targets() {
	case "$1" in
		dmd)
			ls /usr/lib/dmd/?.???/bin/dmd 2> /dev/null | sed "s#^/usr/lib/dmd/##;s#/.*##"
			;;
		ldc2)
			ls -v /usr/lib/ldc2/?.*/bin/ldc2 2> /dev/null | sed "s#^/usr/lib/ldc2/##;s#/.*##"
			;;
		*)
			die "Unknown compiler '$1'"
	esac
}

# Creates a symlink or prints a message and quits on error
symlink_helper() {
	local link="$(canonicalise "$1")"
	ln -nfs "$link" "$2" || die -q "Couldn't symlink '$link' as '$2'!"
}

# Prints compilers as argument options <comp1|comp2|...>
compiler_options() {
	local additional=0
	echo -n "<"
	for compiler in ${!COMPILER_NAMES[@]}; do
		[[ $additional -eq 1 ]] && echo -n "|"
		echo -n $compiler
		additional=1
	done
	echo -n ">"
}