aboutsummaryrefslogtreecommitdiff
blob: cc049d3768db6936397cba4217e76179f9d47948 (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
# -*-eselect-*-  vim: ft=eselect
# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

DESCRIPTION="Manage Maven targets"
MAINTAINER="java@gentoo.org"
VERSION="0.2"

MVN="${EROOT}/usr/bin/mvn"

# find a list of mvn symlink targets, best first.
find_targets() {
	for f in $(ls -r "${MVN}"-[0-9]* 2> /dev/null) ; do
		if [[ -f "${f}" ]] ; then
			echo $(basename "${f}")
		fi
	done
}

# get a named or numbered target.
find_target() {
	local target=${1}

	if is_number "${target}" && [[ ${target} -ge 1 ]] ; then
		targets=( $(find_targets ) )
		[[ -z "${targets}" ]] && die -q "No targets found!"
		target=${targets[target-1]}
	fi

	if [[ "${target}" = mvn-[0-9]* ]] && [[ -f "${EROOT}/usr/bin/${target}" ]] ; then
		echo ${target}
	else
		die -q "Target \"${1}\" doesn't appear to be valid!"
	fi
}

# try to remove the mvn symlink.
remove_symlink() {
	rm "${MVN}" &>/dev/null
}

# determine the current target.
get_target() {
	local canonicalised=$(canonicalise "${MVN}")
	echo $(basename "${canonicalised}")
}

# set the Maven symlink.
set_symlink() {
	local target=$(find_target "${1}")
	remove_symlink
	ln -s "${target}" "${MVN}" || die "Couldn't set ${target} symlink."
}

### show action ###

describe_show() {
	echo "Show current Maven target"
}

do_show() {
	if [[ ${#} -gt 0 ]]; then
		die -q "No parameters allowed."
	fi

	if [[ -L "${MVN}" ]] ; then
		get_target
		return 0
	elif [[ -e "${MVN}" ]] ; then
		echo "(not a symlink)" >&2
		return 1
	else
		echo "(unset)" >&2
		return 1
	fi
}

### list action ###

describe_list() {
	echo "List available Maven targets"
}

do_list() {
	if [[ ${#} -gt 0 ]]; then
		die -q "Usage error: no parameters allowed."
	fi

	local i targets
	targets=( $(find_targets) )

	for (( i = 0; i < ${#targets[@]}; i++ )); do
		[[ ${targets[i]} = $(basename "$(canonicalise "${MVN}")") ]] \
			&& targets[i]=$(highlight_marker "${targets[i]}")
	done

	write_list_start "Available Maven targets:"
	write_numbered_list -m "(none found)" "${targets[@]}"
}

### set action ###

describe_set() {
	echo "Set a new Maven target"
}

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

describe_set_parameters() {
	echo "<target>"
}

do_set() {
	if [[ $# -gt 1 ]]; then
		die -q "Too many parameters. Expected only one."
	fi

	local target=${1}

	if [[ -z "${target}" ]] ; then
		die -q "You didn't give me a target name or number."
	elif [[ -L "${MVN}" ]] ; then
		if ! remove_symlink ; then
			die -q "Can't remove existing Maven provider."
		elif ! set_symlink "${1}" ; then
			die -q "Can't set new Maven provider."
		fi
	elif [[ -e "${MVN}" ]] ; then
		write_warning_msg "Can't set a new Maven provider. There's a file in the way at ${MVN}. You can try removing it manually, and then re-running this command."
	else
		set_symlink "${target}" || die -q "Wasn't able to set a new provider."
	fi
}

### update action ###

describe_update() {
	echo "Set the Maven target to the latest if the current target is invalid or if the given target is the latest"
}

describe_update_options() {
	echo "target (optional) : Target name (from 'list' action)"
}

describe_update_parameters() {
	echo "<target>"
}

do_update() {
	if [[ $# -gt 1 ]] ; then
		die -q "Too many parameters. Expected only one."
	fi

	# For pkg_postrm
	if [[ ! $(find_targets) ]]; then
		remove_symlink
		return
	fi

	local canonicalised=$(canonicalise "${MVN}")

	if [[ ! -L "${MVN}" ]] || [[ ! -f "${canonicalised}" ]] ; then
		do_set 1
	elif [[ -n "${1}" ]] ; then
		# Check whether target name is valid.
		find_target "${1}" > /dev/null

		if [[ "${1}" == "$(find_target 1)" ]] ; then
			do_set 1
		fi
	fi
}