summaryrefslogtreecommitdiff
blob: b2379177ef7a2292af2f11ffd51f5517107c2431 (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
# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

DESCRIPTION="Manage Ruby symlinks"
MAINTAINER="a3li@gentoo.org"
SVN_DATE="20120105"
VERSION="20120105"

bindir=/usr/bin
man1dir=/usr/share/man/man1

find_targets() {
	for t in ${EROOT}${bindir}/${1:-ruby}{18,19,20,ee18} ; do
		[[ -e $t ]] || continue
		echo ${t}
	done
}

check_target() {
	local target=${1} targets
	targets=( $(find_targets ) )

	# number from the list
	if is_number ${target} && [[ ${target} -ge 1 ]] ; then
		if [[ ${target} -gt ${#targets[@]} ]] ; then
			target="invalid"
		elif [[ -e "${targets[$(( ${target} - 1 ))]}" ]] ; then
			target=$(basename ${targets[$((${target} - 1 ))]} )
		else
			write_warning_msg ${targets[$((${target} - 1 ))]}
			target="invalid"
		fi
	# `rubyXX' string
	else
	[[ -e "${EROOT}${bindir}/${target}" ]] || \
			target="invalid"
	fi

	echo ${target}
}

remove_symlinks() {
	rm -f ${EROOT}${bindir}/{ruby,gem,irb,erb,ri,rdoc,testrb} && \
	rm -f ${EROOT}${man1dir}/ruby.{1,1.gz,1.bz2,1.lzma}
}

create_man_symlinks() {
	local version=${1}

	for m in ${EROOT}${man1dir}/ruby${version}.{1,1.gz,1.bz2,1.lzma}
	do
		if [[ -e ${m} ]] ; then
			break
		fi
	done

	ln -s "${m}" "${m/ruby${version}/ruby}" || \
		write_error_msg "Could not set man symlink"
}

create_symlinks() {
	local target=${1} version

	version=${target##*ruby}
	
	local path="${EROOT}${bindir}/"
	# these have to work
	for f in "ruby" "irb" "erb" "ri" "testrb" "rdoc" ; do
		ln -s "${f}${version}" "${path}${f}" || \
			die -q "Could not set ${f} symlink"
	done

	# these can fail
	if [[ -e "${path}gem${version}" ]] ; then
		ln -s "gem${version}" "${path}gem" || \
			die -q "Could not set gem symlink"
	else
		write_warning_msg "Could not set gem symlink"
		echo "It appears you do not have RubyGems installed for this profile."
		echo "If you need RubyGems, emerge dev-ruby/rubygems with the appropriate RUBY_TARGETS setting."
		echo
	fi

	[[ $(portageq envvar FEATURES) =~ noman ]] || create_man_symlinks ${version}

	write_list_start "Successfully switched to profile:"
	write_kv_list_entry "${target}" ""
}

### show action ###
describe_show() {
	echo "Prints the current configuration."
}

do_show() {
	[[ -z "${@}" ]] || die -q "This function does not expect any arguments"

	local rb=""

	write_list_start "Current Ruby version:"
	if [[ -L "${EROOT}${bindir}/ruby" ]] ; then
		rb=$(basename $(canonicalise ${EROOT}${bindir}/ruby ) )
		write_kv_list_entry $rb ""
	elif [[ -e "${EROOT}${bindir}/ruby" ]] ; then
		write_warning_msg "${bindir}/ruby is a SLOT incompatible version."
		write_kv_list_entry "$(basename $(canonicalise ${EROOT}${bindir}/ruby ) )" ""
	else
		write_kv_list_entry "(none)"
		return 1
	fi
	
	write_list_start "Current Rubygems version:"
	if [[ -L "${EROOT}${bindir}/gem" ]] ; then
		write_kv_list_entry "$(basename $(canonicalise ${EROOT}${bindir}/gem ) )" ""
	elif [[ -e "${EROOT}${bindir}/gem" ]] ; then
		write_warning_msg "${bindir}/gem is a SLOT incompatible version."
		write_kv_list_entry "$(basename $(canonicalise ${EROOT}${bindir}/gem) )" ""
	else
		write_kv_list_entry "gem??" "(not found)"
	fi
}

### list action ###
describe_list() {
	echo "Lists available Ruby profiles."
}

do_list() {
	write_list_start "Available Ruby profiles:"
	local targets=( $(find_targets) )
	local i line

	for (( i = 0; i < ${#targets[@]}; i++ )) ; do
		line=$(basename "${targets[i]}")

		# Do we have a matching rubygems?
		if [[ -e ${targets[i]/ruby/gem} ]] ; then
			line="${line} (with Rubygems)"
		fi

		# find out the current version
		if [[ ${targets[i]} = $(canonicalise "${EROOT}${bindir}/ruby") ]] ; then
			targets[i]=$(highlight_marker "${line}")
		else
			targets[i]=${line}
		fi
	done
	write_numbered_list -m "(none found)" "${targets[@]}"
}

### set action ###
describe_set() {
	echo "Switches to a ruby profile."
}

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

describe_set_parameters() {
	echo "<target>"
}

do_set() {
	[[ -z ${@} ]] && die -q "Parameter expected."
	
	local target=$(check_target ${1})
	
	[[ $target = "invalid" ]] && die -q "Can't use that profile. No suitable Ruby interpreter found."
	
	remove_symlinks || \
		die -q "Could not remove symlinks"

	create_symlinks ${target}
}

### cleanup action ###
describe_cleanup() {
	echo "This action is not to be called manually."
}

do_cleanup() {
	[[ -z ${@} ]] || die -q "This function does not expect any arguments"

	# Do we need to clean up?
	if [[ -e "${EROOT}${bindir}"/$(readlink "${EROOT}${bindir}/ruby") ]]; then
		echo "Nothing to clean up."
		return
	fi

	local targets=( $(find_targets) )

	remove_symlinks || \
		die -q "Could not remove symlinks"

	if [[ ${#targets[@]} -ne 0 ]] ; then
		echo "Marking the latest still installed version as default..."
		create_symlinks $(basename ${targets[@]: -1})
	else
		echo "No ruby profiles left on the system. Stale symlinks removed."
	fi
}