-
Notifications
You must be signed in to change notification settings - Fork 0
/
tvim
executable file
·347 lines (298 loc) · 10.4 KB
/
tvim
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env bash
set -eo pipefail
OS=$(uname -s)
current_script_path=$(readlink -f "${BASH_SOURCE[0]}")
current_script_dir=$(dirname "$current_script_path")
source "$current_script_dir/messages"
source "$current_script_dir/functions"
persisting=false
testing=false
profile="default"
freeze=false
plenary_url="https://github.com/nvim-lua/plenary.nvim.git"
structlog_url="https://github.com/Tastyep/structlog.nvim.git"
clear=false
print_usage() {
pretty_usage "Usage: %s [OPTIONS]\n" "$(basename "$0")"
pretty_usage " --profile The profile to use. (Optional, default: default)"
pretty_usage " --clear Clear the last temporary session. Prepares for a new fresh session. (Optional)"
pretty_usage " --persist Use default Neovim's standard user directories appended with a profile instead of temporary directories. (Optional, default: false)\n"
pretty_usage " --freeze Copy all files from the /tmp/XDG directories to the real XDG directories. Not to be used with --persist. (Optional, default: false)"
pretty_usage " NOTE: Implicitely removes the reference to the last temporary session.\n"
pretty_usage " --testing Run tests in headless mode. This enables the -x options. (Optional, default: false)"
pretty_usage " -x, --test-arg Pass an argument to the test runner. (Optional, default: none)\n"
pretty_usage " --plenary-url The URL to clone plenary from. (Optional, default: https://github.com/nvim-lua/plenary.nvim.git)"
pretty_usage " --structlog-url The URL to clone structlog from. (Optional, default: https://github.com/Tastyep/structlog.nvim.git)\n"
pretty_usage " -h, --help Show this help message and exit.\n"
}
while (( "$#" )); do
case "$1" in
--profile)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
profile=$2
shift 2
else
compact_error_msg "Argument for $1 is missing" >&2
exit 1
fi
;;
--freeze)
freeze=true
shift
;;
--testing)
testing=true
shift
;;
--clear)
clear=true
shift
;;
-x|--test-arg)
if [ "$testing" = true ]; then
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
test_arg=$2
shift 2
else
compact_error_msg "Argument for $1 is missing" >&2
exit 1
fi
else
compact_error_msg "$1 is only valid when --testing is set" >&2
exit 1
fi
;;
--persist)
persisting=true
shift
;;
-h|--help)
print_usage
exit 0
;;
--plenary-url)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
plenary_url=$2
shift 2
else
compact_error_msg "Argument for $1 is missing" >&2
exit 1
fi
;;
--structlog-url)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
structlog_url=$2
shift 2
else
compact_error_msg "Argument for $1 is missing" >&2
exit 1
fi
;;
*)
PARAMS="$PARAMS $1"
shift
;;
esac
done
if [ "$clear" = true ]; then
compact_info_msg "Clearing last session..."
rm /tmp/testnvim/vars.sh
fi
if [ "$freeze" = true ] && [ "$persisting" = true ]; then
compact_error_msg "--freeze and --persist cannot be used together" >&2
exit 1
fi
export TESTNVIM_APPNAME=${TESTNVIM_APPNAME:-"tvim"}
export NVIM_APPNAME="$TESTNVIM_APPNAME"
function tvim_tmp(){
if [[ "$OS" == "Darwin" ]]; then
# macOS
mktemp -d -t "${TESTNVIM_APPNAME}-${profile}-$1-XXXX"
else
# Linux and other UNIX-like systems
mktemp -d --suffix="-${TESTNVIM_APPNAME}-${profile}-$1"
fi
}
if [ "$persisting" = false ]; then
if [ -f /tmp/testnvim/vars.sh ]; then
source /tmp/testnvim/vars.sh
if [ "$profile" != "$TESTNVIM_USER_PROFILE" ]; then
warn_msg "Please clear your last session with '--clear' before creating a new profile."
fi
else
TESTNVIM_CACHE_PROFILE="$(tvim_tmp "cache")"
TESTNVIM_DATA_PROFILE="$(tvim_tmp "data")"
TESTNVIM_LOG_PROFILE="$(tvim_tmp "log")"
mkdir -p /tmp/testnvim
{
echo "export TESTNVIM_USER_PROFILE=$profile";
echo "export TESTNVIM_CACHE_PROFILE=$TESTNVIM_CACHE_PROFILE" ;
echo "export TESTNVIM_DATA_PROFILE=$TESTNVIM_DATA_PROFILE" ;
echo "export TESTNVIM_LOG_PROFILE=$TESTNVIM_LOG_PROFILE" ;
} > /tmp/testnvim/vars.sh
fi
fi
export TESTNVIM_USER_PROFILE=${TESTNVIM_USER_PROFILE:-"$profile"}
source "$current_script_dir/vars"
info_msg "Using profile: $TESTNVIM_USER_PROFILE"
declare -a persisting_dirs=(
# order matters
"$XDG_DATA_HOME/$TESTNVIM_APPNAME/$TESTNVIM_USER_PROFILE"
"$XDG_CACHE_HOME/$TESTNVIM_APPNAME/$TESTNVIM_USER_PROFILE"
"$XDG_LOG_HOME/$TESTNVIM_APPNAME/$TESTNVIM_USER_PROFILE"
)
declare -a old_tmp_dirs=(
# order matters
"$TESTNVIM_DATA_PROFILE"
"$TESTNVIM_CACHE_PROFILE"
"$TESTNVIM_LOG_PROFILE"
)
TESTNVIM_PACK_DIR="${TESTNVIM_DATA_PROFILE}/after/pack/lazy/opt"
TESTNVIM_PLENARY_DIR="${TESTNVIM_PACK_DIR}/plenary"
TESTNVIM_STRUCTLOG_DIR="${TESTNVIM_PACK_DIR}/structlog"
declare -xr TESTNVIM_PACK_DIR TESTNVIM_PLENARY_DIR TESTNVIM_STRUCTLOG_DIR
### Creates a new persisting profile
function create_persisting_profile() {
if [ -f /tmp/testnvim/vars.sh ]; then
warn_msg "Next call with '--freeze' might conflict with this profile."
fi
info_msg "Preparing profile: $TESTNVIM_USER_PROFILE"
for dir in "${persisting_dirs[@]}"; do
if [ ! -d "$dir" ]; then
mkdir -p "$dir"
compact_info_msg "Created profile directory: $dir"
else
compact_info_msg "Using existing profile directory: $dir"
fi
done
compact_success_msg "Profile prepared successfully."
}
### Freezes the last session into a persisting state
function freeze_config() {
info_msg "Freezing configuration..."
declare -a existing_dirs=()
for dir in "${persisting_dirs[@]}"; do
if [ -d "$dir" ]; then
existing_dirs+=("$dir")
fi
done
if [ "${#existing_dirs[@]}" -gt 0 ]; then
compact_warn_msg "Existing profiles detected:"
for dir in "${existing_dirs[@]}"; do
compact_warn_msg " $dir"
done
compact_warn_msg "Would you like to backup and replace them? (y/n)"
read -r answer
if [[ "$answer" != "y" ]]; then
compact_error_msg "Aborting freeze."
exit 1
else
for dir in "${existing_dirs[@]}"; do
echo "Removing $dir"
# remove existing plugins
if [ -d "$dir/after" ]; then
rm -rf "$dir/after"
fi
__backup_dir "$dir"
done
fi
fi
for i in "${!old_tmp_dirs[@]}"; do
mkdir -p "${persisting_dirs[$i]}"
cp -r "${old_tmp_dirs[$i]}/." "${persisting_dirs[$i]}"
done
rm /tmp/testnvim/vars.sh
compact_info_msg "[INFO]: Use this profile with: $TESTNVIM_APPNAME --persist --profile $TESTNVIM_USER_PROFILE"
compact_success_msg "Freeze complete."
}
function print_tvim_config(){
info_msg "TestNvim configuration:"
compact_info_msg " Profile: ${TESTNVIM_USER_PROFILE}"
compact_info_msg " Cache dir: $TESTNVIM_CACHE_PROFILE"
compact_info_msg " State dir: $TESTNVIM_STATE_DIR"
compact_info_msg " Data dir: $TESTNVIM_DATA_PROFILE"
compact_info_msg " Config dir: $TESTNVIM_CONFIG_PROFILE"
compact_info_msg " Log dir: $TESTNVIM_LOG_PROFILE\n"
}
function clone_plugins() {
declare -a plugins=(
# order matters
"$plenary_url"
"$structlog_url"
)
declare -a plugin_dirs=(
# order matters
"$TESTNVIM_PLENARY_DIR"
"$TESTNVIM_STRUCTLOG_DIR"
)
counter=0
for i in "${!plugins[@]}"; do
if [ ! -d "${plugin_dirs[$i]}" ]; then
counter=$((counter+1))
fi
done
if [ "$counter" -eq 0 ]; then
return
fi
info_msg "Cloning plugins..."
mkdir -p "${TESTNVIM_PACK_DIR}"
for i in "${!plugins[@]}"; do
if [ ! -d "${plugin_dirs[$i]}" ]; then
compact_info_msg "[INFO]: Cloning plugin: ${plugins[$i]} into: ${plugin_dirs[$i]}"
output=$(git clone "${plugins[$i]}" "${plugin_dirs[$i]}" 2>&1)
if [ ! "$output" ]; then
echo "$output" | tail -n +2 | while IFS= read -r line; do
warn_msg "$line"
done
fi
fi
done
compact_success_msg "Plugins cloned successfully."
}
function create_user_config() {
if [ ! -d "$TESTNVIM_CONFIG_PROFILE" ]; then
mkdir -p "$TESTNVIM_CONFIG_PROFILE"
cp -r "$TESTNVIM_STATE_DIR/utils/conf"/* "$TESTNVIM_CONFIG_PROFILE"
info_msg "Created config profile: $TESTNVIM_CONFIG_PROFILE"
else
info_msg "Using existing config profile: $TESTNVIM_CONFIG_PROFILE"
fi
}
# Run tests in the user config directory
function test_nvim() {
# shellcheck disable=SC2086
nvim -u "${TESTNVIM_CONFIG_PROFILE}/tests/minimal_init.lua" \
"$@" $PARAMS
}
# Run neovim
function run_nvim() {
# shellcheck disable=SC2086
nvim -u "${TESTNVIM_STATE_DIR}/init.lua" \
$PARAMS
}
function setup() {
if [ "$freeze" = true ]; then
freeze_config
exit 0
fi
clone_plugins
create_user_config
if [ "$persisting" = true ] ; then
create_persisting_profile
fi
print_tvim_config
if [ -n "$PARAMS" ]; then
info_msg "Neovim parameters: $PARAMS"
fi
sleep 1
if [ "$testing" = true ]; then
if [ -n "$test_arg" ]; then
test_nvim --headless -c "lua require('plenary.busted').run('$test_arg')"
else
test_nvim --headless -c "PlenaryBustedDirectory ${TESTNVIM_CONFIG_PROFILE}/tests/specs { minimal_init = '${TESTNVIM_CONFIG_PROFILE}/tests/minimal_init.lua' }"
fi
else
run_nvim
fi
}
setup