-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·417 lines (366 loc) · 12.2 KB
/
run.sh
File metadata and controls
executable file
·417 lines (366 loc) · 12.2 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env bash
# ╭──────────────────────╮
# │ 🅅 ersion │
# ╰──────────────────────╯
# version 0.1.0
# ╭──────────────────────╮
# │ 🛈 Info │
# ╰──────────────────────╯
# Cocaco runscript.
# ╭──────────────────────╮
# │ ⚙ Boilerplate │
# ╰──────────────────────╯
# shellcheck disable=SC2154,SC2034
declare -gr dotfiles="${DOTFILES:-"$HOME/dotfiles"}"; # TOKEN_DOTFILES_GLOBAL
# ☯ Every file prevents multi-loads itself using this global dict
declare -gA _sourced_files=( ["runscript"]="" ); # Source only once
# 🖈 If the runscript requires a specific location, set it here
#declare -gr this_location="";
# HACK: Depends on location
# shellcheck source=../../dotfiles/scripts/boilerplate.sh
source "$dotfiles/scripts/boilerplate.sh" "${BASH_SOURCE[0]}" "$@";
# ╭──────────────────────╮
# │ 🛠Configuration │
# ╰──────────────────────╯
_run_config["versioning"]=0;
_run_config["log_loads"]=0;
_run_config["error_frames"]=1;
# ╭──────────────────────╮
# │ 🗀 Dependencies │
# ╰──────────────────────╯
# ✔ Ensure important boilerplate is present
satisfy_version "$dotfiles/scripts/boilerplate.sh" "0.0.0";
# Ensure versions even if included already
load_version "$dotfiles/scripts/version.sh" "0.0.0";
load_version "$dotfiles/scripts/bash_meta.sh" "0.0.0";
load_version "$dotfiles/scripts/fileinteracts.sh" "0.0.0";
load_version "$dotfiles/scripts/git_utils.sh" 0.0.0;
load_version "$dotfiles/scripts/setargs.sh" "0.0.0";
load_version "$dotfiles/scripts/termcap.sh" "0.0.0";
load_version "$dotfiles/scripts/userinteracts.sh" "0.0.0";
load_version "$dotfiles/scripts/utils.sh" "0.0.0";
# ╭──────────────────────╮
# │ 🗺 Globals │
# ╰──────────────────────╯
# We put our local game dumps
declare -r share_dir="$HOME/.local/share/cocaco/";
declare -r dump_data_dir="$share_dir/data";
# ╭──────────────────────╮
# │ ⌨ Commands │
# ╰──────────────────────╯
command_default()
{
default_delegate "$@";
}
command_serve() {
# Recurse once setting --now to use the exit-logging of the runscript when
# stopping to serve, but also run in the background to return immediately.
set_args "--now" "$@";
eval "$get_args";
if [[ "$now" == "false" ]]; then
# subcommand serve --now &
echon "file://${HOME}/github/cocaco/html/sidebar.html";
return;
fi
if ! 2>/dev/null python3 -m http.server -d html/sidebar.html; then
echoe "Failed to start server. Already serving?";
fi
}
command_game()
{
declare most_recent;
most_recent="$({ command ls -A -t "$dump_data_dir/"*".json" || [[ "$?" == "141" ]]; } | head -1)";
show_variable most_recent;
[[ "$most_recent" == *" "* ]] && abort "Filename must not contain spaces";
set_args "--help --file=${most_recent} --no-format --list" "$@";
eval "$get_args";
if [[ "$list" == "true" ]]; then
command ls --color=auto -Fh -A -ltr "$dump_data_dir/"*".json";
command du -h "$dump_data_dir";
return 0;
fi
ensure_jq;
# Copy to temporary location
declare tmp_dir;
if [[ "$no_format" == "true" ]]; then
tmp_dir="/tmp/cocaco_raw/";
else
tmp_dir="/tmp/cocaco_unwrapped";
fi
declare base;
base="$(basename "$file")";
declare -r tmp_dir file_unwrapped="$tmp_dir/$base";
ensure_directory "$tmp_dir";
show_variable file;
show_variable file_unwrapped;
# Format file if needed
if [[ ! -f "$file_unwrapped" ]]; then
echol "Unwrapping...";
pushd "$dump_data_dir" || return;
declare line;
declare cmd="jq";
if [[ "$no_format" == "true" ]]; then
cmd="cat";
fi
while read -r line; do
command "$cmd" <<< "$line";
done < "$file" >> "$file_unwrapped";
popd || return;
else
echos "Already present";
fi
command nvim -u NONE -U NONE -N -i NONE "$file_unwrapped";
#command batcat -l json;
}
command_version()
{
set_args "--help" "$@";
eval "$get_args";
declare version="";
version="$(current_version)";
echo "${text_bo}[🥥]${text_normal} $version";
}
command_dump_install()
{
set_args "--help" "$@";
eval "$get_args";
generate_dump_manifest;
declare -r manifest_dir="$HOME/.mozilla/native-messaging-hosts";
declare -r manifest_name="cocaco_dump.json";
declare -r manifest_path="$manifest_dir/$manifest_name";
ensure_directory "$manifest_dir";
[[ -f "dump/$manifest_name" ]];
command ln -vfs -t "$manifest_dir" "$parent_path/dump/$manifest_name" || true;
[[ -f "$manifest_path" ]];
declare -r program_dir="$HOME/.local/share/cocaco";
declare -r program_name="cocaco_dump.py";
ensure_directory "$program_dir";
ensure_directory "$program_dir/data";
command ln -vfs -t "$program_dir" "$parent_path/dump/$program_name" || true;
echok "Provided cocaco dump manifest and program";
}
command_check()
{
set_args "--help" "$@";
eval "$get_args";
# Sanity checks
if [[ "$(basename "$parent_path")" != cocaco* ]]; then
abort "This should be run from within the cocaco directory";
fi
if [[ "$(git rev-parse --abbrev-ref HEAD)" != "master" ]]; then
echow "Check: Not in master branch";
fi
if [[ "$(git status --porcelain)" != "" ]]; then
echow "Check: Uncommitted changes";
fi
git submodule update --init --recursive;
# Validate git repository state
declare fsck_output;
fsck_output="$(git fsck --no-dangling)";
if [[ -n "$fsck_output" ]]; then
echon "Run 'git fsck' to diagnose, git gc --prune=now to clean";
abort "Git repository corrupted";
fi
echok "All good.";
}
command_release()
{
set_args "--force --help" "$@"
eval "$get_args";
declare force_flag="";
if [[ "$force" == "true" ]]; then
force_flag="--force";
fi
# Sanity check
if [[ "$(is_clean_master)" != "true" ]]; then
abort "Must be clean master";
fi
command git s || errchow "Could not display commits";
declare choice;
declare version;
declare tag;
version="$(current_version)";
tag="$(get_tag_name "$version")";
choice="$(boolean_prompt "Release ${text_user}${force_flag}${text_normal} ${version}?")";
if [[ "$choice" == "n" ]]; then
abort "Abort: No changes";
fi
add_git_tag --version="${version}" --force="$force";
command git push $force_flag origin &&
command git push $force_flag origin "$tag" &&
command git push $force_flag lolli &&
command git push $force_flag lolli "$tag";
echok "Released ${version}";
}
command_symbols()
{
set_args "--help" "$@";
eval "$get_args";
python3 symbols.py;
}
command_uninstall()
{
echou "To uninstall, delete the 'cocaco' directory";
echon "rm -r '${parent_path_coloured}'";
}
# ╭──────────────────────╮
# │ 🖩 Utils │
# ╰──────────────────────╯
default_delegate()
{
echou "Execute '$0 install' to install the cocaco extension";
ok="$(boolean_prompt "Execute now?")"
if [[ "$ok" == "n" ]]; then
subcommand help;
abort "Aborted by user"
fi
subcommand install;
}
# ╭──────────────────────╮
# │ 𝑓 Functional │
# ╰──────────────────────╯
add_git_tag()
{
set_args "--force --version" "$@";
eval "$get_args";
declare force_flag="";
if [[ "$force" == "true" ]]; then
force_flag="--force";
fi
declare version_str="";
if [[ "$version" != "false" ]]; then
version_str="$version";
else
version_str="$(current_version)";
fi
declare tag_name;
tag_name="$(get_tag_name "$version_str")";
command git tag $force_flag "$tag_name";
echok "git tag $force_flag $tag_name";
}
get_tag_name() {
# @param $1: Version string
# @output: Tag name
if (( "$#" != 1 )); then
abort "Must have exactly one argument";
fi
printf -- "%s" "v${1}";
}
# Prints currently present (raw) value for 'version' in manifest.json
current_version()
{
ensure_jq;
declare version;
version="$(jq -r .version manifest.json)";
printf -- "%s" "${version}";
}
generate_dump_manifest()
{
pushd dump || return;
sed -e "s|HOME_DIR_PLACEHOLDER|${HOME}|" \
cocaco_dump.json.template \
> cocaco_dump.json;
popd || reuturn;
}
# Prints "false" or "true"
is_clean_master()
{
declare current_branch clean;
current_branch="$(git_current_branch)";
git_is_clean clean;
if [[ "$current_branch" != "master" || "$clean" != "y" ]]; then
>&2 show_variable current_branch;
>&2 show_variable clean;
printf -- "false";
else
printf -- "true";
fi
}
sanity_check_git_user() {
# This function prints errors when finding misconfigured git user name or
# email. Makes it easier to notice inconsistent values.
declare name email;
name="$(git config get user.name)";
email="$(git config get user.email)";
1>&2 show_variable name
1>&2 show_variable email
if [[ "$name" != "Lolligerhans" || "$email" != "lolli@ger.hans" ]]; then
errchoe "Expected different git user"
fi
}
sanity_check_version_loose() {
# This function prints errors when finding obviously misconfigured versions.
# Not a thorough version check.
declare tag version expected tag;
tag="$(git tag --points-at HEAD)";
version="$(current_version)";
expected_tag="$(get_tag_name "$version")";
declare -r tag version expected tag;
1>&2 show_variable version;
# if [[ "$(is_clean_master 2>/dev/null )" != "true" ]]; then
# return;
# fi
# During development, set -dev version
if [[ "$tag" == "" && "$version" != *"dev"* ]]; then
errchoe "Forgot to set to dev version?";
fi
# If we have a tag, it should match the manifest version
if [[ "$tag" == v* ]]; then
if [[ "$tag" != "$expected_tag" ]]; then
errchoe "Unexpected version tag";
show_variable tag;
show_variable expected_tag;
fi
fi
}
ensure_jq() {
if ! &>/dev/null command jq --version; then
echol "Installing 'jq'";
sudo apt install jq;
fi
if ! &>/dev/null command jq --version; then
abort "Unable to install 'jq'";
return 1;
fi
}
# ╭──────────────────────╮
# │ 🖹 Help strings │
# ╰──────────────────────╯
declare -r game_help_string="Show dumped game
SYNOPSIS
game
game --file=filename
DESCRIPTION
Show one of the games dumped by cocaco, in JSON format.
In the first form, show the most recent dump.
In the second form, show the specified file.
The filename is relative to '$share_dir/dump/'.
OPTIONS
--file=FILENAME: Show dump/FILENAME. By default, show the most recent dump.
--no-format: Do not format with jq when showing a file.
--list: List available files instead of showing one.";
declare -r version_help_string="Show extension verison";
declare -r check_help_string="Verify files with git-fsck";
declare -r dump_install_help_string="Install dump utility
SYNOPSIS
dump_install
DESCRIPTION
Places the 'dump' manifest into the appropriate path. Uses fixed paths, may
not work outside of Ubuntu.
This is only needed when enabling frame dumping in the config."
declare -r release_help_string="Release HEAD->master
DESCRIPTION
Create version tag. Push remotes. Push tags.
OPTIONS
--force: Use --force for 'git tag' and 'git push'.";
declare -r symbols_help_string="Show symbols available in plotly";
# ╭──────────────────────╮
# │ ⚙ Boilerplate │
# ╰──────────────────────╯
sanity_check_version_loose;
sanity_check_git_user;
# ⌂ Transition to provided command
subcommand "${@}";
# ╭──────────────────────╮
# │ 🕮 Documentation │
# ╰──────────────────────╯