-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall
More file actions
executable file
·222 lines (194 loc) · 5.68 KB
/
install
File metadata and controls
executable file
·222 lines (194 loc) · 5.68 KB
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
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
set -o noclobber
CSVIZMO_REPO_ROOT=$(git rev-parse --show-toplevel)
RED="\033[31m"
GREEN="\033[32m"
BLUE="\033[34m"
RESET="\033[0m"
debug() {
echo -e "${BLUE}DEBUG:${RESET} $*" >&2
}
info() {
echo -e "${GREEN}INFO:${RESET} $*" >&2
}
error() {
echo -e "${RED}ERROR:${RESET} $*" >&2
}
usage() {
echo "Install/update/uninstall the csvizmo tools and scripts"
echo
echo "Usage: $0 [--help]"
echo
echo " --help, -h Show this help and exit"
echo " --dry-run,-n Don't actually install the tools"
echo " --remove,-r Uninstall the csvizmo tools"
echo " --prefix,-p <PREFIX> The installation prefix. By default, tries paths in the following order:"
echo " 1. ~/.local/bin/, if in the users \$PATH"
echo " 2. ~/bin/, if in the users \$PATH"
echo " 3. \$CARGO_HOME, if set"
echo " 4. ~/.cargo/bin/, if present"
}
check_dependencies() {
if ! command -v jq &>/dev/null; then
error "jq is required but not installed."
return 1
fi
}
default_prefix() {
local prefix=""
if [[ ":$PATH:" == *":$HOME/.local/bin:"* ]]; then
prefix="$HOME/.local"
elif [[ ":$PATH:" == *":$HOME/bin:"* ]]; then
prefix="$HOME/bin"
elif [[ -n "${CARGO_HOME+x}" ]]; then
prefix="$CARGO_HOME"
elif [[ -d ~/.cargo ]]; then
prefix="$HOME/.cargo"
fi
echo "$prefix"
}
csvizmo_binaries() {
cargo metadata --no-deps --format-version 1 |
jq -r '.packages[] | .targets[] | select(.kind[] == "bin") | .name' |
grep -v '^template$'
}
csvizmo_packages_with_bins() {
# Returns package paths for packages that have binary targets
cargo metadata --no-deps --format-version 1 |
jq -r '.packages[] | select(.targets[] | .kind[] == "bin") | .manifest_path' |
sort -u |
while read -r manifest; do
dirname "$manifest"
done
}
package_binaries() {
# Get binaries for a specific package
local -r pkg_path="$1"
cargo metadata --no-deps --format-version 1 |
jq -r --arg pkg_path "$pkg_path/Cargo.toml" \
'.packages[] | select(.manifest_path == $pkg_path) | .targets[] | select(.kind[] == "bin") | .name' |
grep -v '^template$'
}
csvizmo_scripts() {
for script in "$CSVIZMO_REPO_ROOT/scripts"/*; do
echo "$script"
done
}
install_csvizmo() {
local -r dry_run="$1"
local -r prefix="$2"
info "Installing Rust binaries to $prefix ..."
# Install from each workspace package that has binaries
while read -r pkg_path; do
local cargo_cmd=(
cargo install --path "$pkg_path" --root "$prefix"
)
# Add --bin flag for each binary in this package (excluding template)
local bins=()
while read -r bin; do
bins+=("$bin")
cargo_cmd+=(--bin "$bin")
done < <(package_binaries "$pkg_path")
if [[ ${#bins[@]} -eq 0 ]]; then
continue
fi
if [[ "$dry_run" = "true" ]]; then
info "Dry run. Would have run:"
echo " ${cargo_cmd[*]}"
else
info "Installing from $(basename "$pkg_path"): ${bins[*]}"
"${cargo_cmd[@]}"
fi
done < <(csvizmo_packages_with_bins)
info "Installing project scripts to $prefix ..."
for script in $(csvizmo_scripts); do
if [[ "$dry_run" = "true" ]]; then
info "Dry run. Would have installed '$script'"
else
info "Installing $script ..."
if [[ ! -d "$prefix/bin" ]]; then
mkdir -p "$prefix/bin"
fi
cp "$script" "$prefix/bin"
fi
done
}
uninstall_csvizmo() {
local -r dry_run="$1"
local -r prefix="$2"
info "Uninstalling csvizmo binaries ..."
for bin in $(csvizmo_binaries); do
if [[ "$dry_run" = "false" ]]; then
info "Uninstalling $bin ..."
cargo uninstall "$bin" || true
else
echo "Dry run. Would have run: 'cargo uninstall $bin'"
fi
done
info "Uninstalling project scripts from $prefix ..."
for script in $(csvizmo_scripts); do
script="$(basename -- "$script")"
script="$prefix/bin/$script"
if [[ -f "$script" ]]; then
if [[ "$dry_run" = "true" ]]; then
info "Dry run. Would have deleted '$script' ..."
else
info "Deleting '$script' ..."
rm "$script"
fi
fi
done
}
main() {
local operation="install"
local dry_run="false"
local prefix
prefix="$(default_prefix)"
# Check for required dependencies
if ! check_dependencies; then
return 1
fi
while [[ $# -gt 0 ]]; do
case "$1" in
--help | -h)
usage
exit 0
;;
--dry-run | -n | -d)
dry_run="true"
;;
--remove | --uninstall | -r | -u)
operation="uninstall"
;;
--prefix | -p)
prefix="$2"
shift
;;
-*)
error "Unexpected option: '$1'"
exit 1
;;
*)
error "Unexpected positional argument: '$1'"
exit 1
;;
esac
shift
done
if [[ ! -d "$prefix" ]]; then
error "No such directory '$prefix'"
return 1
fi
case "$operation" in
install)
install_csvizmo "$dry_run" "$prefix"
;;
uninstall)
uninstall_csvizmo "$dry_run" "$prefix"
;;
esac
}
main "$@"