diff options
author | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-02-22 10:54:10 -0500 |
---|---|---|
committer | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-02-22 10:54:10 -0500 |
commit | dbec1d221b1f309f1e8981883d1d03229ba06028 (patch) | |
tree | e737f0dfe903773b4bc959d2e652500314d7db34 | |
parent | 4a090e5eee2baf815d0e3f4fc8845d28a9158ea4 (diff) |
holy shit, SOURCES.bash actually almost works
-rw-r--r-- | bash/SOURCES.bash | 95 |
1 files changed, 61 insertions, 34 deletions
diff --git a/bash/SOURCES.bash b/bash/SOURCES.bash index a274223..609e98e 100644 --- a/bash/SOURCES.bash +++ b/bash/SOURCES.bash | |||
@@ -4,7 +4,7 @@ source() | |||
4 | if [[ $1 != */* ]] && | 4 | if [[ $1 != */* ]] && |
5 | ( | 5 | ( |
6 | command -v base64_embedded_source__"$1" || | 6 | command -v base64_embedded_source__"$1" || |
7 | command -v src/"$1" | 7 | [ -f src/"$1" ] |
8 | ) >&2 # >/dev/null | 8 | ) >&2 # >/dev/null |
9 | then | 9 | then |
10 | rewrite_self \ | 10 | rewrite_self \ |
@@ -17,67 +17,94 @@ source() | |||
17 | fi | 17 | fi |
18 | } | 18 | } |
19 | 19 | ||
20 | denominate_function() | ||
21 | { | ||
22 | declare -n func_name="$1" | ||
23 | local func_output_b64encoded output_hash | ||
24 | read -r -d '' \ | ||
25 | func_output_b64encoded \ | ||
26 | < <(base64) || [ "${func_output_b64encoded}" ] | ||
27 | read output_hash _ \ | ||
28 | < <(base64 -d <<< "$func_output_b64encoded" | sha256sum) | ||
29 | [ ${#output_hash} = 64 ] || return | ||
30 | func_name=SHA256_${output_hash:0:32} | ||
31 | source <(cat <<END | ||
32 | ${func_name} () | ||
33 | { | ||
34 | base64 -d <<. | ||
35 | ${func_output_b64encoded} | ||
36 | . | ||
37 | } | ||
38 | END | ||
39 | ) | ||
40 | } | ||
41 | |||
20 | rewrite_self() | 42 | rewrite_self() |
21 | { | 43 | { |
22 | # TODO: Check if version in src/ updates this version | 44 | declare -n do_rewrite=do_rewrite${RANDOM} |
23 | # by checking for its hash in a record kept in the source | 45 | do_rewrite= |
24 | # file in src/ | 46 | |
25 | if command -v "$3" >/dev/null | 47 | if [ -f "$2" ] |
26 | then | 48 | then |
27 | ( | 49 | if [ -w "$0" ] |
28 | set +e | 50 | then |
29 | sha256sum <("$3") >&2 | 51 | do_rewrite=$(mktemp) || return |
30 | sha256sum <(builtin source "$2" "${@:4}" && "$3") >&2 | 52 | source <(stdin_to_function "$3" < "$2" | |
31 | ) | 53 | tee "$do_rewrite") || return |
32 | builtin source \ | 54 | else |
33 | <("$3") \ | 55 | source <(stdin_to_function "$3" < "$2") || return |
34 | "${@:4}" | 56 | fi |
35 | return | ||
36 | fi | 57 | fi |
37 | [ -f "$2" -a -r "$2" ] || return | 58 | command -v "$3" >/dev/null || return |
38 | declare -n tmpfile=tmpfile${RANDOM} | 59 | |
39 | tmpfile=$(mktemp) || return | 60 | builtin source <("$3") "${@:4}" || return |
40 | if embed "$1" "$2" > "$tmpfile" && | 61 | |
41 | builtin source "$tmpfile" && | 62 | [ "$do_rewrite" ] || return 0 |
42 | command -v "$3" >/dev/null && | 63 | |
43 | builtin source <("$3") "${@:4}" && | 64 | if sed \ |
44 | [ -w "$0" ] && | 65 | -i~"$(date -Ins)" \ |
45 | sed \ | 66 | -e "1 { r ${do_rewrite}"$'\n'" }" \ |
46 | -i~"$(date -Ins)" \ | 67 | "$0" |
47 | -e "1 { r ${tmpfile}"$'\n'" }" \ | ||
48 | "$0" | ||
49 | then | 68 | then |
50 | local r=0 | 69 | local r=0 |
51 | else | 70 | else |
52 | local r=$? | 71 | local r=$? |
53 | fi | 72 | fi |
54 | rm "$tmpfile" | 73 | rm "$do_rewrite" |
55 | return $r | 74 | return $r |
56 | } | 75 | } |
57 | 76 | ||
58 | embed() | 77 | stdin_to_function() |
59 | { | 78 | { |
60 | cat <<. | 79 | cat <<. |
61 | base64_embedded_source__${1}() | 80 | ${1}() |
62 | { | 81 | { |
63 | base64 -d <<. | 82 | base64 -d <<. |
64 | $(base64 < "$2" && echo .) | 83 | $(base64 && echo .) |
65 | } | 84 | } |
66 | . | 85 | . |
67 | } | 86 | } |
68 | 87 | ||
88 | embed_source() | ||
89 | { | ||
90 | stdin_to_function "base64_embedded_source__${1}" "${@:2}" | ||
91 | } | ||
92 | |||
69 | print_test_script_for_SOURCES.bash() | 93 | print_test_script_for_SOURCES.bash() |
70 | { | 94 | { |
71 | cat <<. | 95 | cat <<. |
72 | #!/bin/bash | 96 | #!/bin/bash |
73 | source SOURCES.bash | 97 | source SOURCES.bash |
74 | source SOURCES.bash | ||
75 | source rpc.bash | 98 | source rpc.bash |
76 | : base64_embedded_source__rpc.bash | 99 | r() |
77 | : base64_embedded_source__dependencies.bash | 100 | { |
101 | : base64_embedded_source__rpc.bash | ||
102 | : base64_embedded_source__dependencies.bash | ||
103 | source rpc.bash | ||
104 | } | ||
78 | BASH_RPC_REMOTE_DEST=localhost \\ | 105 | BASH_RPC_REMOTE_DEST=localhost \\ |
79 | remote_run_function \\ | 106 | remote_run_function \\ |
80 | source rpc.bash | 107 | r |
81 | . | 108 | . |
82 | } | 109 | } |
83 | 110 | ||