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
|
2024-02-21 11:11:07 -08:00
|
|
|
module_load helpers
|
|
|
|
|
|
|
|
# TODO keep debug statements if requested, other remove them
|
2022-02-26 17:19:37 -08:00
|
|
|
|
|
|
|
bundle () {
|
|
|
|
|
2024-02-21 11:11:07 -08:00
|
|
|
local file; local xfunc; local compact; local func;
|
2022-02-26 17:19:37 -08:00
|
|
|
|
2024-02-21 11:11:07 -08:00
|
|
|
debug $( ( IFS=$','; echo bundle arguments: "$*" ) )
|
|
|
|
|
|
|
|
declare OPTION; declare OPTARG; declare OPTIND
|
|
|
|
while getopts 'cf:x:' OPTION; do
|
|
|
|
case "$OPTION" in
|
|
|
|
c)
|
|
|
|
compact=true
|
|
|
|
;;
|
|
|
|
f)
|
|
|
|
# can pass any arguments along with the function to run. Use " " for any argument with spaces
|
|
|
|
func="$OPTARG"
|
|
|
|
;;
|
|
|
|
x)
|
|
|
|
# will extract a function from a file or module to be used for bundling
|
|
|
|
xfunc="$OPTARG"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo unknown bundle option $OPTION
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2024-02-16 23:49:08 -08:00
|
|
|
|
2024-02-21 11:11:07 -08:00
|
|
|
shift $(( OPTIND - 1 ))
|
2024-02-16 23:49:08 -08:00
|
|
|
|
2024-02-21 11:11:07 -08:00
|
|
|
tmp_file=$( mktemp -t TEMP_FILE_bundle.XXXXXXXX )
|
|
|
|
chmod 600 "$tmp_file"
|
|
|
|
|
|
|
|
if [[ -f $1 ]]; then file=$1; else
|
|
|
|
if ! file=$(module_find "$1"); then >&2 echo fatal: bundle error, no module of name: $1; file=""; fi; fi
|
|
|
|
[[ ! $file ]] && >&2 echo "fatal: bundle error aborting, no file, module passed for bundling" && return 1
|
|
|
|
\cp $file $tmp_file
|
|
|
|
if [[ $xfunc ]]; then
|
|
|
|
( source $file; declare -f $xfunc > $tmp_file )
|
|
|
|
fi
|
|
|
|
|
2024-02-16 23:49:08 -08:00
|
|
|
if [[ ! $2 == "__recurse__" ]]; then
|
|
|
|
_modules=""
|
|
|
|
output=$2
|
2024-02-21 11:11:07 -08:00
|
|
|
[[ $output ]] && debug outputing bundle to: $output || debug outputting bundle to console
|
2024-02-16 23:49:08 -08:00
|
|
|
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)
|
2024-02-21 11:11:07 -08:00
|
|
|
# if [[ ( ! $modules ) && ( ! $2 == "__recurse__" ) ]]; then
|
|
|
|
# if [[ $output ]]; then
|
|
|
|
# \cp $tmp_file $output
|
|
|
|
# else
|
|
|
|
# echo -e "$(cat $tmp_file)"
|
|
|
|
# fi
|
|
|
|
# fi
|
2024-02-16 23:49:08 -08:00
|
|
|
|
2024-02-21 11:11:07 -08:00
|
|
|
# remove debug module by default so it doesn't get bundled
|
|
|
|
|
2023-02-07 11:21:38 -08:00
|
|
|
if [[ $modules ]]; then
|
2024-02-21 11:11:07 -08:00
|
|
|
# remove all found module_load before continuing
|
2024-02-16 23:49:08 -08:00
|
|
|
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-21 11:11:07 -08:00
|
|
|
[[ $compact ]] && compact_file $tmp_file
|
|
|
|
# TODO remove debug statements by default
|
|
|
|
# remove any default function calls
|
|
|
|
sed -i '/\s*(\s*return 0 2>\/dev\/null\s*)\s*|| /d' $tmp_file
|
|
|
|
# add in a function call if requested
|
|
|
|
[[ $func ]] && echo -e "\n $func \"\$@\"" >> $tmp_file
|
|
|
|
if [[ $BASH_DEBUG ]]; then
|
2024-02-16 23:49:08 -08:00
|
|
|
debug "\n ------------code as bundled--------------\n \
|
|
|
|
$(cat $tmp_file) \
|
|
|
|
\n ------------------------------------------------------------ \n"
|
|
|
|
debug "bundled modules were $_modules"
|
|
|
|
fi
|
2024-02-21 11:11:07 -08:00
|
|
|
if [[ $output ]]; then
|
2024-02-16 23:49:08 -08:00
|
|
|
\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 $@
|
2024-02-21 11:11:07 -08:00
|
|
|
|
|
|
|
|