-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvimspector-update-program
executable file
·197 lines (175 loc) · 5.72 KB
/
vimspector-update-program
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
#!/bin/bash
#shellcheck enable=SC2155
set -o errexit
set -o pipefail
set -o nounset
set -o noclobber
export RED=$(tput setaf 1)
export GREEN=$(tput setaf 2)
export YELLOW=$(tput setaf 3)
export BLUE=$(tput setaf 4)
export RESET=$(tput sgr0)
debug() {
echo "${BLUE}DEBUG:${RESET} $*"
}
info() {
echo "${GREEN}INFO:${RESET} $*"
}
error() {
echo "${RED}ERROR:${RESET} $*" >&2
}
usage() {
echo "Usage: $0 [--help] [VIMSPECTOR_CONFIG]"
echo
echo " --help, -h Show this help and exit"
echo " --configuration, -C <CONFIGURATION> Pick the configuration from <VIMSPECTOR_CONFIG>"
echo " --cargo-test [...FLAGS] Set the Vimspector program to the Cargo test binary."
echo " All remaining CLI arguments will be passed to 'cargo test'"
echo " --cargo-run [...FLAGS] Set the Vimspector program to the Cargo run binary."
echo " All remaining CLI arguments will be passed to 'cargo run'"
echo " --program, -p <PROGRAM> Set the program to the given value"
echo " --restore, -r Restore the last operation"
}
get_program() {
local vimspector_file="$1"
local configuration="$2"
local program_key="${3:-program}"
jq ".configurations.$configuration.configuration.$program_key" "$vimspector_file"
}
set_program() {
local vimspector_file="$1"
local configuration="$2"
local new_program="$3"
local program_key="${4:-program}"
new_program="$(echo -n "$new_program" | tr -d "'\"")"
debug "Setting $program_key = $new_program for configuration '$configuration'"
jq \
--indent 4 \
".configurations.$configuration.configuration.$program_key = \"$new_program\"" \
"$vimspector_file" |
sponge .vimspector.json
}
backup_program() {
local vimspector_file="$1"
local configuration="$2"
local current_program
current_program="$(get_program "$vimspector_file" "$configuration")"
set_program "$vimspector_file" "$configuration" "$current_program" "old_program"
}
swap_programs() {
local vimspector_file="$1"
local configuration="$2"
old_program="$(get_program "$vimspector_file" "$configuration" "old_program")"
current_program="$(get_program "$vimspector_file" "$configuration")"
debug "Swapping program $current_program for $old_program in configuration '$configuration'"
set_program "$vimspector_file" "$configuration" "$current_program" "old_program"
set_program "$vimspector_file" "$configuration" "$old_program" "program"
}
cargo_test_binary() {
local binary
binary="$(cargo --quiet test --no-run --message-format json "$@" |
jq -r 'select(.executable != null) | .executable')"
local lines
lines="$(echo "$binary" | wc -l)"
if [[ "$lines" -ne 1 ]]; then
error "Found '$lines' matching binaries:"
echo "$binary" >&2
exit 1
fi
echo -n "$binary"
}
cargo_run_binary() {
local binary
binary="$(cargo --quiet run --no-run --message-format json "$@" |
jq -r 'select(.executable != null) | .executable')"
local lines
lines="$(echo "$binary" | wc -l)"
if [[ "$lines" -ne 1 ]]; then
error "Found '$lines' matching binaries:"
echo "$binary" >&2
exit 1
fi
echo -n "$binary"
}
main() {
local vimspector_file=".vimspector.json"
local configuration=""
local cargo_test="false"
local cargo_run="false"
local -a cargo_flags=()
local restore="false"
local program=""
while [[ $# -gt 0 ]]; do
case "$1" in
--help | -h)
usage
exit 0
;;
--configuration | -C)
configuration="$2"
shift
;;
--cargo-test)
cargo_test="true"
shift
cargo_flags=("${@}")
break
;;
--cargo-run)
cargo_run="true"
shift
cargo_flags=("${@}")
break
;;
--program | -p)
program="$2"
shift
;;
--restore | -r)
restore="true"
;;
-*)
error "Unexpected option: $1"
usage >&2
exit 1
;;
*)
vimspector_file="$1"
;;
esac
shift
done
if [[ ! -f "$vimspector_file" ]]; then
error "Vimspector config file '$vimspector_file' does not exist!"
exit 1
fi
if [[ -z "$configuration" ]]; then
error "Missing required '--configuration' flag"
exit 1
fi
local config
config="$(jq ".configurations.$configuration" "$vimspector_file")"
if [[ "$config" = "null" ]]; then
error "Configuration '$configuration' not found in '$vimspector_file'"
exit 1
fi
if [[ "$cargo_test" = "true" ]]; then
local binary
binary="$(cargo_test_binary "${cargo_flags[@]}")"
debug "Found binary '$binary' from Cargo"
backup_program "$vimspector_file" "$configuration"
set_program "$vimspector_file" "$configuration" "$binary"
elif [[ "$cargo_run" = "true" ]]; then
local binary
binary="$(cargo_run_binary "${cargo_flags[@]}")"
debug "Found binary '$binary' from Cargo"
backup_program "$vimspector_file" "$configuration"
set_program "$vimspector_file" "$configuration" "$binary"
elif [[ -n "$program" ]]; then
backup_program "$vimspector_file" "$configuration"
set_program "$vimspector_file" "$configuration" "$program"
elif [[ "$restore" = "true" ]]; then
swap_programs "$vimspector_file" "$configuration"
fi
}
main "$@"