aboutsummaryrefslogtreecommitdiff
blob: 5f1a5769a24ef3c2afd4ba2937abb87889745a88 (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
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
# -*-eselect-*-  vim: ft=eselect
# Copyright 2014 Gentoo Foundation
# Distributed under the terms of the GNU GPL version 2 or later

DESCRIPTION="Manage the rust compiler versions"
MAINTAINER="jauhien@gentoo.org"
VERSION="0.1"

inherit package-manager path-manipulation

SITELISP=/usr/share/emacs/site-lisp
SITEVIM=/usr/share/vim/vimfiles
SITEZSH=/usr/share/zsh/site-functions

# find a list of installed rust compilers
find_targets() {
	local f
	local version
	for f in "${EROOT}"/usr/bin/rustc-*; do
		version=""
		[[ -x ${f} ]] && version=`basename "${f}" | awk -F"-" '{ print $2 }'`
		has_version "~dev-lang/rust-${version}" && echo ${version}
	done
}

# remove symlink if exists

remove_symlink() {
	local symlink=$1

	if [[ -L ${symlink} ]]; then
		# existing symlink
		rm ${symlink} || die -q "Couldn't remove existing symlink ${symlink}"
	elif [[ -e ${symlink} ]]; then
		# we have something strange
		die -q "${symlink} exists but is not a symlink"
	fi
}

# Regenerate the site-gentoo.el file, based on packages' site
# initialisation files in the /usr/share/emacs/site-lisp/site-gentoo.d/
# directory.
# A slightly changed function from elisp-common.eclass

elisp-site-regen() {
	local sitelisp=${EROOT}${SITELISP}
	local sf i ret=0 null="" page=$'\f'
	local -a sflist
	local T=${EROOT}/tmp/eselect

	mkdir -p "${T}" || die -q "temporary directory creation failed for ${T}"

	[[ -d ${sitelisp} ]] \
		|| die -q "elisp-site-regen: Directory ${sitelisp} does not exist"

	[[ -d ${T} ]] \
		|| die -q "elisp-site-regen: Temporary directory ${T} does not exist"

	for sf in "${sitelisp}"/site-gentoo.d/[0-9][0-9]*.el; do
		[[ -r ${sf} ]] && sflist+=("${sf}")
	done

	cat <<-EOF >"${T}"/site-gentoo.el || ret=$?
	;;; site-gentoo.el --- site initialisation for Gentoo-installed packages

	;;; Commentary:
	;; Automatically generated by eselect-rust
	;; DO NOT EDIT THIS FILE

	;;; Code:
	EOF
	# Use sed instead of cat here, since files may miss a trailing newline.
	sed '$q' "${sflist[@]}" </dev/null >>"${T}"/site-gentoo.el || ret=$?
	cat <<-EOF >>"${T}"/site-gentoo.el || ret=$?

	${page}
	(provide 'site-gentoo)

	;; Local ${null}Variables:
	;; no-byte-compile: t
	;; buffer-read-only: t
	;; End:

	;;; site-gentoo.el ends here
	EOF

	if [[ ${ret} -ne 0 ]]; then
		die -q "elisp-site-regen: Writing site-gentoo.el failed."
	else
		mv "${T}"/site-gentoo.el "${sitelisp}"/site-gentoo.el || \
			die -q "elisp-site-regen: Replacing site-gentoo.el failed"
	fi

	rm -rf "${T}"
	true
}

# set symlink if source exists

set_symlink() {
	local source=$1
	local dest=$2

	if [[ -e ${source} ]]; then
		mkdir -p "$(dirname ${dest})" || die -q "directory creation failed for $(dirname ${dest})"
		ln -s "${source}" "${dest}" || die -q "${dest} symlink setting failed"
	else
		false
	fi
}

# unset the rust version

unset_version() {
	remove_symlink "${EROOT}"/usr/bin/rustc
	remove_symlink "${EROOT}"/usr/bin/rustdoc
	remove_symlink "${EROOT}/${SITELISP}"/rust-mode
	remove_symlink "${EROOT}/${SITELISP}"/site-gentoo.d/50rust-mode-gentoo.el

	for i in after/syntax autoload ftdetect ftplugin indent plugin syntax; do
		remove_symlink "${EROOT}/${SITEVIM}/${i}/rust.vim"
	done
	for i in compiler syntax_checkers/rust; do
		remove_symlink "${EROOT}/${SITEVIM}/${i}/rustc.vim"
	done
	remove_symlink "${EROOT}/${SITEVIM}/doc/rust.txt"

	remove_symlink "${EROOT}/${SITEZSH}/_rust"
}

# set the rust version

set_version() {
	local target=$1

	if is_number "${target}"; then
		local targets=( $(find_targets) )
		target=${targets[target-1]}
	fi

	[[ -z ${target} || ! -x "${EROOT}/usr/bin/rustc-${target}" ]] \
		&& die -q "Target \"$1\" doesn't appear to be valid!"

	unset_version

	ln -s "${EROOT}/usr/bin/rustc-${target}" "${EROOT}/usr/bin/rustc" || die -q "rustc symlink setting failed"
	ln -s "${EROOT}/usr/bin/rustdoc-${target}" "${EROOT}/usr/bin/rustdoc" || die -q "rustdoc symlink setting failed"

	local datadir="${EROOT}/usr/share/rust-${target}"

	set_symlink "${datadir}/emacs/site-lisp/rust-mode" "${EROOT}/${SITELISP}/rust-mode" && \
		set_symlink "${datadir}/emacs/site-lisp/site-gentoo.d/50rust-mode-gentoo.el" \
		"${EROOT}/${SITELISP}/site-gentoo.d/50rust-mode-gentoo.el" && \
		elisp-site-regen

	local rustvimdir="${datadir}/vim/vimfiles"
	for i in after/syntax autoload ftdetect ftplugin indent plugin syntax; do
		set_symlink "${rustvimdir}/${i}/rust.vim" "${EROOT}/${SITEVIM}/${i}/rust.vim"
	done
	for i in compiler syntax_checkers/rust; do
		set_symlink "${rustvimdir}/${i}/rustc.vim" "${EROOT}/${SITEVIM}/${i}/rustc.vim"
	done
	set_symlink "${rustvimdir}/doc/rust.txt" "${EROOT}/${SITEVIM}/doc/rust.txt"

	set_symlink "${datadir}/zsh/site-functions/_rust" "${EROOT}/${SITEZSH}/_rust"

	true
}

### list action ###

describe_list() {
	echo "List available rust versions"
}

do_list() {
	local i targets=( $(find_targets) )

	write_list_start "Available rust versions:"
    for (( i = 0; i < ${#targets[@]}; i++ )); do
        # highlight the target where the symlink is pointing to
        [[ rustc-${targets[i]} = \
            $(basename "$(canonicalise "${EROOT}/usr/bin/rustc")") ]] \
            && targets[i]=$(highlight_marker "${targets[i]}")
    done
    write_numbered_list -m "(none found)" "${targets[@]}"
}

### set action ###

describe_set() {
	echo "Set active rust version"
}

describe_set_parameters() {
    echo "<target>"
}

describe_set_options() {
    echo "target : Target number (from 'list' action)"
}

do_set() {
    [[ -z $1 ]] && die -q "You didn't tell me what to set the version to"
    [[ $# -gt 1 ]] && die -q "Too many parameters"

	set_version "$1" || die -q "Couldn't set new active version"
}

### update action ###

describe_update() {
	echo "Switch to the most recent rust compiler"
}

describe_update_options() {
	echo "--if-unset    : Do not override existing implementation"
}

do_update() {
	local if_unset="0"
	while [[ $# > 0 ]]; do
		case "$1" in
			--if-unset)
				if_unset="1"
				;;
			*)
				die -q "Unrecognized argument '$1'"
				;;
		esac
		shift
	done

	if [[ "${if_unset}" == "1" && -f "${EROOT}"/usr/bin/rustc ]]; then
		return
	fi

	local targets=( $(find_targets) )
	do_set ${#targets[@]}
}

### unset action ###

describe_unset() {
	echo "Unset active rust version"
}

describe_update_options() {
	echo "--if-invalid    : Unset only if symlink is invalid (e.g. package was uninstalled)"
}

do_unset() {
	local if_invalid="0"
	while [[ $# > 0 ]]; do
		case "$1" in
			--if-invalid)
				if_invalid="1"
				;;
			*)
				die -q "Unrecognized argument '$1'"
				;;
		esac
		shift
	done

	if [[ "${if_invalid}" == "1" && -e "${EROOT}"/usr/bin/rustc ]]; then
		return
	fi

	unset_version || die -q "Couldn't unset active version"
}