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")
|
2022-02-26 17:19:37 -08:00
|
|
|
|
|
|
|
bundle () {
|
|
|
|
|
2023-01-18 16:43:21 -08:00
|
|
|
local file
|
|
|
|
|
|
|
|
if [[ $1 == "-m" ]]; then
|
|
|
|
shift 1
|
|
|
|
file=$(module_find $1)
|
|
|
|
else
|
|
|
|
file=$1
|
|
|
|
fi
|
|
|
|
|
|
|
|
[[ ! -f $file ]] && return 1
|
2022-02-26 17:19:37 -08:00
|
|
|
module_load file
|
|
|
|
|
|
|
|
if [[ ! $2 == "__recurse__" ]]; then
|
|
|
|
tmp_file=$( mktemp -t TEMP_FILE_bundle.XXXXXXXX )
|
|
|
|
chmod 600 "$tmp_file"
|
2023-01-18 16:43:21 -08:00
|
|
|
\cp $file $tmp_file
|
2022-02-26 17:19:37 -08:00
|
|
|
else
|
2023-01-18 16:43:21 -08:00
|
|
|
tmp_file=$file
|
2022-02-26 17:19:37 -08:00
|
|
|
fi
|
|
|
|
# echo current temp file: $tmp_file
|
|
|
|
modules=$(sed -n -e 's/^module_load //p' < $tmp_file)
|
|
|
|
# echo found: $modules
|
|
|
|
# return
|
|
|
|
if [[ $modules ]]; then
|
|
|
|
# echo Modules: $modules
|
|
|
|
sed -i '/module_load/d' $tmp_file
|
|
|
|
for module in $(printf '%s\n' "${modules[@]}"|tac);
|
|
|
|
do
|
|
|
|
# echo module: $module
|
|
|
|
# echo module path: $(module_find $module)
|
|
|
|
prepend_file $(module_find $module) $tmp_file
|
|
|
|
done
|
|
|
|
bundle $tmp_file __recurse__
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! $2 == "__recurse__" ]]; then
|
|
|
|
if [[ $2 ]]; then
|
2022-03-01 22:44:26 -08:00
|
|
|
\cp $tmp_file $2
|
|
|
|
# echo $2
|
2022-02-26 17:19:37 -08:00
|
|
|
else
|
|
|
|
echo -e "$(cat $tmp_file)"
|
|
|
|
fi
|
|
|
|
rm -f $tmp_file;
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# # if script was executed then call the function
|
|
|
|
(return 0 2>/dev/null) || bundle $@
|