Skip to content

Commit 3d3a9d8

Browse files
committed
Cache and environment var fixes.
1 parent 3a4783f commit 3d3a9d8

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

junonia

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ _junonia_help () {
13311331
return 0
13321332
fi
13331333

1334-
case "$JUNONIA_SPEC_TYPE" in
1334+
case "${JUNONIA_SPEC_TYPE#cached_}" in
13351335
dir)
13361336
mds="$(for f in "$JUNONIA_MD"/*.md; do cat "$f"; done;
13371337
_junonia_mds)"
@@ -1770,7 +1770,7 @@ junonia_init () {
17701770
JUNONIA_COL1="${JUNONIA_COL1:-18}"
17711771
JUNONIA_COL2="${JUNONIA_COL2:-60}"
17721772

1773-
readonly JUNONIA_VERSION="1.0.4"
1773+
readonly JUNONIA_VERSION="1.0.5"
17741774

17751775
# Information Separator control characters (IS4 - IS1)
17761776
readonly JUNONIA_FS="" # File Separator (FS / IS4 / dec 28)
@@ -1874,11 +1874,15 @@ junonia_init () {
18741874
### Argument parsing
18751875
###
18761876

1877-
# Use _junonia_get_envvars to examine the current environment using env and
1878-
# extract the names of variables of interest. These are the ones that start
1879-
# with SCRIPT_. Unfortunately it is IMPOSSIBLE to determine from the output of
1880-
# env what actually are variables just by inspection. It's possible to have a
1881-
# multiline variable whose contents looks like a variable assignment:
1877+
# Use junonia_envvars to examine the current environment using env and extract
1878+
# the names of variables of interest. Options are to list all variables (a),
1879+
# readonly variables (r), or variables that are writable / can be set (2) as
1880+
# the first argument. A prefix can also be supplied as the second argument,
1881+
# which junonia itself sets to SCRIPT_ mostly typically.
1882+
#
1883+
# Unfortunately it is IMPOSSIBLE to determine from the output of env what
1884+
# actually are variables just by inspection. It's possible to have a multiline
1885+
# variable whose contents looks like a variable assignment:
18821886
#
18831887
# foo="one=two
18841888
# three=four"
@@ -1895,12 +1899,35 @@ junonia_init () {
18951899
#
18961900
# Eval is used very carefully by only sending it things from known sources.
18971901
# The output of each line of env that is extracted must match the pattern
1898-
# 'SCRIPT_<valid identifier chars>=', and the first field split on = is
1899-
# evaluated. Therefore, what is being 'eval'ed is a potential variable name.
1900-
_junonia_get_envvars () {
1901-
for v in $(env | awk -F= -v n="$JUNONIA_CAPNAME" \
1902-
'$0 ~ "^" n "_[_A-Za-z0-9]+=" {print $1}'); do
1903-
eval if [ \"'${'$v+set}\" = set ]\; then echo $v\; fi
1902+
# given, which is most typically 'SCRIPT_<valid identifier chars>=', and the
1903+
# first field split on = is evaluated. Therefore, what is being 'eval'ed is a
1904+
# potential variable name.
1905+
junonia_envvars () {
1906+
case "$1" in
1907+
a)
1908+
mode_fmt=
1909+
;;
1910+
r)
1911+
mode_fmt='&& ! (unset %s 2>/dev/null)'
1912+
;;
1913+
w)
1914+
mode_fmt='&& (unset %s 2>/dev/null)'
1915+
;;
1916+
*)
1917+
echoerr "could not retrieve env vars. type must be one of:"
1918+
echoerr "a - get all environment variables"
1919+
echoerr "r - get all readonly environment variables"
1920+
echoerr "w - get all writable environment variables"
1921+
return 1
1922+
;;
1923+
esac
1924+
1925+
for v in $(env | awk -F= -v prefix="$2" \
1926+
'$0 ~ "^" prefix "[_A-Za-z0-9]+=" {print $1}'); do
1927+
var_mode="$(printf "$mode_fmt" "$v")"
1928+
eval 'if [ "${'$v'+set}" = set ] '$var_mode'; then
1929+
echo $v
1930+
fi'
19041931
done
19051932
}
19061933

@@ -1933,7 +1960,7 @@ _junonia_set_args () {
19331960
if [ -f "$JUNONIA_CONFIG" ]; then
19341961

19351962
# Make a list of script related variables that are set.
1936-
set_vars="$(_junonia_get_envvars)"
1963+
set_vars="$(junonia_envvars w ${JUNONIA_CAPNAME}_)"
19371964

19381965
# Once the list of known variables that are already set is made, execute a
19391966
# subshell in a command substitution that outputs the text of some export
@@ -1953,7 +1980,7 @@ _junonia_set_args () {
19531980
# export SCRIPT_bar='string value of bar from env var'
19541981

19551982
evalcmds="$(
1956-
for v in $(_junonia_get_envvars); do
1983+
for v in $(junonia_envvars w ${JUNONIA_CAPNAME}_); do
19571984
eval echo export $v=\\\'\"'$'$v\"\\\'
19581985
done
19591986
)"
@@ -2404,8 +2431,10 @@ junonia_runmd_filtered () {
24042431
if [ -f "$spec_cache" ] &&
24052432
[ "$JUNONIA_CACHE" != 0 ]; then
24062433
spec="$(cat "$spec_cache")"
2434+
cached=cached_
24072435
ret=$?
24082436
else
2437+
cached=
24092438
spec=
24102439
fi
24112440

@@ -2415,15 +2444,15 @@ junonia_runmd_filtered () {
24152444
readonly JUNONIA_DOCDIR="$md"
24162445
spec="${spec:-"$(_junonia_plugin_docs | _junonia_md2spec "$md"/*.md -)"}"
24172446
ret=$?
2418-
spec_type="dir"
2447+
spec_type="${cached}dir"
24192448
elif [ -f "$md" ]; then
24202449
spec="${spec:-"$(_junonia_plugin_docs | _junonia_md2spec "$md" -)"}"
24212450
ret=$?
2422-
spec_type="file"
2451+
spec_type="${cached}file"
24232452
elif [ "$(echo "$md" | wc -l)" -gt 1 ]; then
2424-
spec="${spec:-"$({echo "$md"; _junonia_plugin_docs} | _junonia_md2spec "" -)"}"
2453+
spec="${spec:-"$( ( echo "$md"; _junonia_plugin_docs ) | _junonia_md2spec "" -)"}"
24252454
ret=$?
2426-
spec_type="md_string"
2455+
spec_type="${cached}md_string"
24272456
fi
24282457

24292458
if [ -z "$spec" ] || [ "$ret" -ne 0 ]; then
@@ -2475,9 +2504,7 @@ _junonia_run_final () {
24752504
spec="$1"
24762505
shift
24772506

2478-
spec_cache="$JUNONIA_CACHEDIR/spec"
2479-
if [ ! -f "$spec_cache" ] ||
2480-
[ "$JUNONIA_CACHE" != 0 ]; then
2507+
if [ "${spec_type%_*}" != cached ]; then
24812508
# Insert the standard meta parameters.
24822509
spec="$(
24832510
echo "$spec" | awk '
@@ -2510,7 +2537,7 @@ _junonia_run_final () {
25102537
}
25112538
END {
25122539
if(insert_meta) {
2513-
#print s
2540+
print s
25142541
}
25152542
}
25162543
' "$_junonia_cmds" -

0 commit comments

Comments
 (0)