.lib/sh/stubexec.sh

changeset 1115
15ca9da8cd59
parent 1114
fd7f92c949fb
child 1119
84cd2286f0bf
equal deleted inserted replaced
1114:fd7f92c949fb 1115:15ca9da8cd59
1 cache="${XDG_CACHE_HOME:-$HOME/.cache}/stubexec"
2
3 stubexec() {
4 local real_bin="$(realbin "$0")"
5 if [ ! -x "$real_bin" ] || age_check $0; then
6 try_nix "$@" ||
7 try_install_callback
8 touch_checktime "$0"
9 stubexec "$@"
10 fi
11 exec "$real_bin" "$@"
12 }
13
14 db() {
15 mkdir -p "$cache"
16 kv "$cache/db" "$@"
17 }
18
19 touch_checktime() {
20 db touch "$(basename $1)"
21 }
22
23 age_check() {
24 db age_days_gt "$(basename $1)" "${age_limit:-90}"
25 }
26
1 realpath() { 27 realpath() {
2 local dir="$(dirname -- "$1")" 28 local dir="$(dirname -- "$1")"
3 local file="$(basename -- "$1")" 29 local file="$(basename -- "$1")"
4 (cd "$dir" 2>/dev/null && printf '%s/%s\n' "$(pwd -P)" "$file") 30 (cd "$dir" 2>/dev/null && printf '%s/%s\n' "$(pwd -P)" "$file")
5 } 31 }
17 esac 43 esac
18 echo "$binpath" 44 echo "$binpath"
19 done 45 done
20 } 46 }
21 47
22 age_check() {
23 local subject=''
24 read subject
25 find "$subject" -mtime -${age_limit:-90} -print
26 }
27
28 realbin() { 48 realbin() {
29 local bn="$(basename $1)" 49 local bn="$(basename $1)"
30 which -a "$bn" | 50 which -a "$bn" |
31 grep -v "$(realpath $1)" | 51 grep -v "$(realpath $1)" |
32 shim_filter "$bn" | 52 shim_filter "$bn" |
33 head -n 1 53 head -n 1
34 } 54 }
35 55
36 stubexec() {
37 local real_bin="$(realbin "$0" | age_check)"
38 if [ -x "$real_bin" ]; then
39 exec "$real_bin" "$@"
40 fi
41 try_nix "$@"
42 try_install_callback
43 stubexec "$@"
44 }
45
46 has() { 56 has() {
47 type "$1" >/dev/null 2>&1 57 type "$1" >/dev/null 2>&1
48 } 58 }
49 59
50 try_nix() { 60 try_nix() {
51 [ "${nix_ref:-}" ] && has nix || return 1 61 [ "${nix_ref:-}" ] && has nix || return 1
62
52 local installed_slot="$( 63 local installed_slot="$(
53 nix profile list | perl -anE 'BEGIN { $m = shift =~ s/#/#.+/r } say $F[0] if $F[1] =~ $m' "$nix_ref" 64 nix profile list | perl -anE 'BEGIN { $m = shift =~ s/#/#.+/r } say $F[0] if $F[1] =~ $m' "$nix_ref"
54 )" 65 )"
55 if [ "$installed_slot" ]; then 66 if [ "$installed_slot" ]; then
56 nix profile upgrade "$installed_slot" 67 nix profile upgrade "$installed_slot"
57 else 68 else
58 nix profile install "$nix_ref" 69 nix profile install "$nix_ref"
59 fi 70 fi
60 # skip age check
61 exec "$(realbin "$0")" "$@"
62 # FIXME: so right now, this works but will check for upgrades every run past
63 # the age check
64 } 71 }
65 72
66 try_install_callback() { 73 try_install_callback() {
67 install_it 74 install_it
68 touch "$(realbin "$0")" # In case of no updates
69 } 75 }
70 76
71 bina_install() { 77 bina_install() {
72 local github_repo="$1" 78 local github_repo="$1"
73 mkdir -p ~/.local/bin 79 mkdir -p ~/.local/bin

mercurial