summaryrefslogtreecommitdiff
blob: 8ab6608686fc0b56ad515e89505848cd8708c1c5 (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
#!@GENTOO_PORTAGE_EPREFIX@/bin/bash

set -euo pipefail
IFS=$'\n\t'

SYMLINK_RUSTUP_VERSION="0.0.4"
binpath="@GENTOO_PORTAGE_EPREFIX@/usr/bin/rustup-init"

: "${CARGO_HOME:=${HOME}/.cargo}"
: "${RUSTUP_HOME:=${HOME}/.rustup}"

__err_exists="already exists, try using -u|--unsymlink option first"

# dies with optional message
die() {
	[[ ${QUIET-no} ]] && echo -e "${NOCOLOR=\e[1;31m*\e[0m }ERROR: ${*}" >&2
	exit 1
} # die()


# outputs gentoo-style green * prefixed message, a good one ofc
good() {
	[[ ${QUIET-no} ]] && echo -e "${NOCOLOR=\e[1;32m*\e[0m }${*}"
	return 0
} # good()


# do I need to explain this?
usage() {
	echo "Usage: ${0} [<options>]"
} # usage()

# and this
help() {
	usage
	echo
	echo -n "Symlink system installation of rustup to"
	echo " ${CARGO_HOME}"
	echo
	echo "Options:"
	echo "	-s, --symlink	Setup rustup symlinks in ${CARGO_HOME}/bin"
	echo "	-C, --nocolor	Disable colored output"
	echo "	-d, --debug	Debug mode (sets -x shell option)"
	echo "	-V, --version	Print version number"
	echo "	-u, --unsymlink	Remove rustup symlinks from ${CARGO_HOME}/bin"
	echo "	-q, --quiet	Quiet mode"
} # help()


symlink_rustup() {
	local gentoo_rust tool
	# rustup calls those proxies
	# src/lib.rs TOOLS
	local tools=(
		rustc
		rustdoc
		cargo
		rust-lldb
		rust-gdb
		rust-gdbgui
		rls
		cargo-clippy
		clippy-driver
		cargo-miri
	)

	# src/lib.rs DUP_TOOLS
	# those can be installed via cargo and not with rust itself
	local dup_tools=(
		rust-analyzer
		rustfmt
		cargo-fmt
	)

	# we need rustup symlink too, so add it to final list
	tools+=( "${dup_tools[@]}" rustup )

	gentoo_rust="$(eselect --brief rust show 2>/dev/null)"

	mkdir -p "${CARGO_HOME}/bin" || die

	for tool in "${tools[@]}"; do
		local symlink_path="${CARGO_HOME}/bin/${tool}"
		if [[ -e "${symlink_path}" ]]; then
			die "${symlink_path} ${__err_exists}"
		else
			ln -s ${QUIET--v} "${binpath}" "${symlink_path}" || die
		fi
	done

	good "Setting gentoo ${gentoo_rust// /} as default toolchain"
	[[ ${QUIET+set} != set ]] && "${CARGO_HOME}/bin/rustup" -V
	"${CARGO_HOME}/bin/rustup" ${QUIET--v} toolchain link gentoo "/usr"
	"${CARGO_HOME}/bin/rustup" ${QUIET--v} default gentoo
	[[ ${QUIET+set} != set ]] && "${CARGO_HOME}/bin/rustup" show

	good "Prepend ${CARGO_HOME}/bin to your PATH to use rustup"
	good "rustup selfupdate is disabled, it will be updated by portage"
} # symlink_rustup()

unsymlink_rustup() {
	local symlinks
	IFS= mapfile -d '' symlinks < <(find -L "${CARGO_HOME}/bin" \
		-samefile "${binpath}" -print0 )
	if [[ "${symlinks-}" ]]; then
		rm -v "${symlinks[@]}" || die
	else
		die "already clean"
	fi
}

main(){
	[[ "$EUID" -eq 0 ]] && die "Running as root is not supported"
	local me
	me="$(basename "${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}")"

	local symlink=no
	local unsymlink=no

	while [[ ${#} -gt 0 ]]; do
		case ${1} in
			-s|--symlink)
				symlink=yes
				;;
			-u|--unsymlink)
				unsymlink=yes
				;;
			-h|--help)
				help
				exit 0
				;;
			-V|--version)
				echo "${me} ${SYMLINK_RUSTUP_VERSION:-unknown}"
				exit 0
				;;
			-d|--debug)
				set -x
				;;
			-C|--nocolor)
				NOCOLOR=
				;;
			-q|--quiet)
				QUIET=
				;;
			-*)
				usage >&2
				exit 1
				;;
		esac
		shift
	done
	if [[ ${symlink} == yes ]]; then
		symlink_rustup
	elif [[ ${unsymlink} == yes ]]; then
		unsymlink_rustup
	else
		help
	fi
} # main()


main "${@}"