shell-base/modules/scripting/bundle.sh

62 lines
1.3 KiB
Bash

#!/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)
# or
# scpt=$(bundle myscript.sh)
# source <(echo -e "$scpt")
bundle () {
local file
if [[ $1 == "-m" ]]; then
shift 1
file=$(module_find $1)
else
file=$1
fi
[[ ! -f $file ]] && return 1
module_load file
if [[ ! $2 == "__recurse__" ]]; then
tmp_file=$( mktemp -t TEMP_FILE_bundle.XXXXXXXX )
chmod 600 "$tmp_file"
\cp $file $tmp_file
else
tmp_file=$file
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
\cp $tmp_file $2
# echo $2
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 $@