.lib/sh/stubexec.sh

Thu, 01 Jun 2023 13:25:14 -0400

author
Meredith Howard <mhoward@roomag.org>
date
Thu, 01 Jun 2023 13:25:14 -0400
changeset 1117
3689f5ec5f37
parent 1115
15ca9da8cd59
child 1119
84cd2286f0bf
permissions
-rw-r--r--

add task stub

1115
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
1 cache="${XDG_CACHE_HOME:-$HOME/.cache}/stubexec"
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
2
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
3 stubexec() {
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
4 local real_bin="$(realbin "$0")"
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
5 if [ ! -x "$real_bin" ] || age_check $0; then
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
6 try_nix "$@" ||
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
7 try_install_callback
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
8 touch_checktime "$0"
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
9 stubexec "$@"
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
10 fi
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
11 exec "$real_bin" "$@"
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
12 }
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
13
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
14 db() {
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
15 mkdir -p "$cache"
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
16 kv "$cache/db" "$@"
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
17 }
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
18
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
19 touch_checktime() {
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
20 db touch "$(basename $1)"
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
21 }
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
22
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
23 age_check() {
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
24 db age_days_gt "$(basename $1)" "${age_limit:-90}"
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
25 }
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
26
932
f87943eb16aa stubexec fixes
Meredith Howard <mhoward@roomag.org>
parents: 915
diff changeset
27 realpath() {
f87943eb16aa stubexec fixes
Meredith Howard <mhoward@roomag.org>
parents: 915
diff changeset
28 local dir="$(dirname -- "$1")"
f87943eb16aa stubexec fixes
Meredith Howard <mhoward@roomag.org>
parents: 915
diff changeset
29 local file="$(basename -- "$1")"
f87943eb16aa stubexec fixes
Meredith Howard <mhoward@roomag.org>
parents: 915
diff changeset
30 (cd "$dir" 2>/dev/null && printf '%s/%s\n' "$(pwd -P)" "$file")
f87943eb16aa stubexec fixes
Meredith Howard <mhoward@roomag.org>
parents: 915
diff changeset
31 }
f87943eb16aa stubexec fixes
Meredith Howard <mhoward@roomag.org>
parents: 915
diff changeset
32
958
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
33 shim_filter() {
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
34 local binpath=''
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
35 while read binpath; do
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
36 case "$binpath" in
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
37 */.plenv/shims/*)
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
38 plenv which $1 >/dev/null 2>&1 || continue
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
39 ;;
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
40 */.asdf/shims/*)
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
41 asdf which $1 >/dev/null 2>&1 || continue
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
42 ;;
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
43 esac
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
44 echo "$binpath"
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
45 done
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
46 }
b883fb5c4792 filter out shims that wont work
Meredith Howard <mhoward@roomag.org>
parents: 957
diff changeset
47
957
ea852d545173 improve stubexec
Meredith Howard <mhoward@roomag.org>
parents: 932
diff changeset
48 realbin() {
960
4921edc4b518 fix stubexec
Meredith Howard <mhoward@roomag.org>
parents: 958
diff changeset
49 local bn="$(basename $1)"
4921edc4b518 fix stubexec
Meredith Howard <mhoward@roomag.org>
parents: 958
diff changeset
50 which -a "$bn" |
957
ea852d545173 improve stubexec
Meredith Howard <mhoward@roomag.org>
parents: 932
diff changeset
51 grep -v "$(realpath $1)" |
960
4921edc4b518 fix stubexec
Meredith Howard <mhoward@roomag.org>
parents: 958
diff changeset
52 shim_filter "$bn" |
957
ea852d545173 improve stubexec
Meredith Howard <mhoward@roomag.org>
parents: 932
diff changeset
53 head -n 1
ea852d545173 improve stubexec
Meredith Howard <mhoward@roomag.org>
parents: 932
diff changeset
54 }
ea852d545173 improve stubexec
Meredith Howard <mhoward@roomag.org>
parents: 932
diff changeset
55
1112
0ae7864c4ad8 add basic nix alternative for stubs
Meredith Howard <mhoward@roomag.org>
parents: 1110
diff changeset
56 has() {
0ae7864c4ad8 add basic nix alternative for stubs
Meredith Howard <mhoward@roomag.org>
parents: 1110
diff changeset
57 type "$1" >/dev/null 2>&1
0ae7864c4ad8 add basic nix alternative for stubs
Meredith Howard <mhoward@roomag.org>
parents: 1110
diff changeset
58 }
0ae7864c4ad8 add basic nix alternative for stubs
Meredith Howard <mhoward@roomag.org>
parents: 1110
diff changeset
59
1114
fd7f92c949fb use nix profile install/upgrade
Meredith Howard <mhoward@roomag.org>
parents: 1112
diff changeset
60 try_nix() {
fd7f92c949fb use nix profile install/upgrade
Meredith Howard <mhoward@roomag.org>
parents: 1112
diff changeset
61 [ "${nix_ref:-}" ] && has nix || return 1
1115
15ca9da8cd59 use distinct db for update checks
Meredith Howard <mhoward@roomag.org>
parents: 1114
diff changeset
62
1114
fd7f92c949fb use nix profile install/upgrade
Meredith Howard <mhoward@roomag.org>
parents: 1112
diff changeset
63 local installed_slot="$(
fd7f92c949fb use nix profile install/upgrade
Meredith Howard <mhoward@roomag.org>
parents: 1112
diff changeset
64 nix profile list | perl -anE 'BEGIN { $m = shift =~ s/#/#.+/r } say $F[0] if $F[1] =~ $m' "$nix_ref"
fd7f92c949fb use nix profile install/upgrade
Meredith Howard <mhoward@roomag.org>
parents: 1112
diff changeset
65 )"
fd7f92c949fb use nix profile install/upgrade
Meredith Howard <mhoward@roomag.org>
parents: 1112
diff changeset
66 if [ "$installed_slot" ]; then
fd7f92c949fb use nix profile install/upgrade
Meredith Howard <mhoward@roomag.org>
parents: 1112
diff changeset
67 nix profile upgrade "$installed_slot"
fd7f92c949fb use nix profile install/upgrade
Meredith Howard <mhoward@roomag.org>
parents: 1112
diff changeset
68 else
fd7f92c949fb use nix profile install/upgrade
Meredith Howard <mhoward@roomag.org>
parents: 1112
diff changeset
69 nix profile install "$nix_ref"
1112
0ae7864c4ad8 add basic nix alternative for stubs
Meredith Howard <mhoward@roomag.org>
parents: 1110
diff changeset
70 fi
1114
fd7f92c949fb use nix profile install/upgrade
Meredith Howard <mhoward@roomag.org>
parents: 1112
diff changeset
71 }
fd7f92c949fb use nix profile install/upgrade
Meredith Howard <mhoward@roomag.org>
parents: 1112
diff changeset
72
fd7f92c949fb use nix profile install/upgrade
Meredith Howard <mhoward@roomag.org>
parents: 1112
diff changeset
73 try_install_callback() {
fd7f92c949fb use nix profile install/upgrade
Meredith Howard <mhoward@roomag.org>
parents: 1112
diff changeset
74 install_it
1112
0ae7864c4ad8 add basic nix alternative for stubs
Meredith Howard <mhoward@roomag.org>
parents: 1110
diff changeset
75 }
0ae7864c4ad8 add basic nix alternative for stubs
Meredith Howard <mhoward@roomag.org>
parents: 1110
diff changeset
76
1110
e4edb1419e3f factor out bina install
Meredith Howard <mhoward@roomag.org>
parents: 1002
diff changeset
77 bina_install() {
e4edb1419e3f factor out bina install
Meredith Howard <mhoward@roomag.org>
parents: 1002
diff changeset
78 local github_repo="$1"
e4edb1419e3f factor out bina install
Meredith Howard <mhoward@roomag.org>
parents: 1002
diff changeset
79 mkdir -p ~/.local/bin
e4edb1419e3f factor out bina install
Meredith Howard <mhoward@roomag.org>
parents: 1002
diff changeset
80 cd ~/.local/bin
e4edb1419e3f factor out bina install
Meredith Howard <mhoward@roomag.org>
parents: 1002
diff changeset
81 curl -fsSL "https://bina.egoist.dev/${github_repo}?dir=." | sh
e4edb1419e3f factor out bina install
Meredith Howard <mhoward@roomag.org>
parents: 1002
diff changeset
82 cd -
e4edb1419e3f factor out bina install
Meredith Howard <mhoward@roomag.org>
parents: 1002
diff changeset
83 }
e4edb1419e3f factor out bina install
Meredith Howard <mhoward@roomag.org>
parents: 1002
diff changeset
84
992
4e3e37ce84bc Switch stubexec to self-invoke
Meredith Howard <mhoward@roomag.org>
parents: 960
diff changeset
85 stubexec "$@"

mercurial