2022-02-26 17:19:37 -08:00
|
|
|
#!/bin/bash
|
|
|
|
# $1 file to bundle
|
|
|
|
# $2 bundled output file (optional)
|
|
|
|
# if no output file then the bundled source is returned
|
|
|
|
# it can be immediately invoked like this
|
|
|
|
# source <(bundle myscript.sh)
|
2022-03-01 22:44:26 -08:00
|
|
|
# or
|
|
|
|
# scpt=$(bundle myscript.sh)
|
|
|
|
# source <(echo -e "$scpt")
|
2024-02-16 23:49:08 -08:00
|
|
|
module_load debug
|
|
|
|
module_load file
|
2022-02-26 17:19:37 -08:00
|
|
|
|
|
|
|
bundle () {
|
|
|
|
|
2023-02-07 11:21:38 -08:00
|
|
|
local file
|
2024-02-16 23:49:08 -08:00
|
|
|
local code
|
|
|
|
local compact
|
|
|
|
if [[ $1 == "-c" ]]; then
|
|
|
|
module_load helpers
|
|
|
|
compact=true
|
2023-02-07 11:21:38 -08:00
|
|
|
shift 1
|
2024-02-16 23:49:08 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# todo add -c for compacting the bundle, change this to options parsing.
|
|
|
|
|
|
|
|
if [[ "$1" ]]; then
|
|
|
|
if [[ -f $1 ]]; then
|
|
|
|
file=$1
|
|
|
|
else
|
|
|
|
if [[ $1 == "-m" ]]; then
|
|
|
|
file=$(module_find $2)
|
|
|
|
shift 1
|
|
|
|
else
|
|
|
|
if [[ $1 == "-mf" ]]; then
|
|
|
|
module_load $2
|
|
|
|
code="$(declare -f $3)"
|
|
|
|
shift 1
|
|
|
|
else
|
|
|
|
code="$1"
|
|
|
|
shift 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "aborting, no file, module or function passed for bundling"
|
|
|
|
return 1
|
|
|
|
fi
|
2022-02-26 17:19:37 -08:00
|
|
|
|
2023-02-07 11:21:38 -08:00
|
|
|
tmp_file=$( mktemp -t TEMP_FILE_bundle.XXXXXXXX )
|
|
|
|
chmod 600 "$tmp_file"
|
2024-02-16 23:49:08 -08:00
|
|
|
# if [[ $2 == "__recurse__" ]] || [[ $file ]]; then
|
|
|
|
if [[ $file ]]; then
|
|
|
|
\cp $file $tmp_file
|
2023-02-07 11:21:38 -08:00
|
|
|
else
|
2024-02-16 23:49:08 -08:00
|
|
|
echo "$code" > $tmp_file
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
if [[ ! $2 == "__recurse__" ]]; then
|
|
|
|
_modules=""
|
|
|
|
output=$2
|
|
|
|
debug outputing bundle to: $output
|
|
|
|
debug "bash code to bundle \n $(cat $tmp_file)"
|
2023-02-07 11:21:38 -08:00
|
|
|
fi
|
2024-02-16 23:49:08 -08:00
|
|
|
|
|
|
|
modules=$(sed -n -e 's/^\s*module_load //p' < $tmp_file)
|
|
|
|
|
2023-02-07 11:21:38 -08:00
|
|
|
if [[ $modules ]]; then
|
2024-02-16 23:49:08 -08:00
|
|
|
# remove found module_load before continuing
|
|
|
|
sed -i '/^\s*module_load/d' $tmp_file
|
|
|
|
[[ $_modules ]] && debug "already bundled modules: $_modules"
|
2023-02-07 11:21:38 -08:00
|
|
|
for module in $(printf '%s\n' "${modules[@]}"|tac);
|
2024-02-16 23:49:08 -08:00
|
|
|
do
|
|
|
|
if [[ $_modules == " $module "* ]]; then
|
|
|
|
[[ $_modules ]] && debug module $module already bundled
|
|
|
|
else
|
|
|
|
if modp=$(module_find ${module}); then
|
|
|
|
debug bundling module ${module} at: $modp
|
|
|
|
prepend_file $modp $tmp_file
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
_modules="$_modules $modules"
|
2023-02-07 11:21:38 -08:00
|
|
|
bundle $tmp_file __recurse__
|
|
|
|
fi
|
|
|
|
|
2024-02-16 23:49:08 -08:00
|
|
|
# the recursion is done
|
2023-02-07 11:21:38 -08:00
|
|
|
if [[ ! $2 == "__recurse__" ]]; then
|
2024-02-16 23:49:08 -08:00
|
|
|
[[ $compact ]] && compact_file $tmp_file
|
|
|
|
if [[ $BASH_DEBUG ]]; then
|
|
|
|
debug "\n ------------code as bundled--------------\n \
|
|
|
|
$(cat $tmp_file) \
|
|
|
|
\n ------------------------------------------------------------ \n"
|
|
|
|
debug "bundled modules were $_modules"
|
|
|
|
fi
|
|
|
|
if [[ $2 ]]; then
|
|
|
|
\cp $tmp_file $output
|
|
|
|
else
|
2023-02-07 11:21:38 -08:00
|
|
|
echo -e "$(cat $tmp_file)"
|
|
|
|
fi
|
|
|
|
rm -f $tmp_file;
|
|
|
|
return 0
|
2022-02-26 17:19:37 -08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# # if script was executed then call the function
|
|
|
|
(return 0 2>/dev/null) || bundle $@
|