Skip to content

Instantly share code, notes, and snippets.

@mpapis
Created May 30, 2011 14:24
Show Gist options
  • Save mpapis/998966 to your computer and use it in GitHub Desktop.
Save mpapis/998966 to your computer and use it in GitHub Desktop.
templates on bdsm
a={{a}}
b={{b}}
c={{c}}
d={{d}}
e={{e}}
case $target in
template_a.conf)
replacements=(
a "b a"
d e
".*" ""
)
;;
*)
echo "no settings for $target"
;;
esac
#!/bin/bash
source /usr/local/bdsm/modules/bash/trace/dsl
source /usr/local/bdsm/modules/bash/core/dsl
source /usr/local/bdsm/modules/bash/variables/dsl
source /usr/local/bdsm/modules/bash/filesystem/dsl
source /usr/local/bdsm/modules/bash/array/dsl
source /usr/local/bdsm/modules/bash/logging/dsl
source /usr/local/bdsm/modules/bash/templates/dsl
action=$1
shift
usage(){
echo "Usage:
$0 list #show available templates
$0 list all #show available templates with generated files
$0 list [template name] #show generated files for given template
$0 apply [target file name] #apply template on the given file
"
}
list_template() {
local template="$1"
local settings="$template.settings"
echo "Template: $template"
if [[ -f $settings ]]
then
echo "Template settings: $template.settings"
else
echo "Template settings are missing: $template.settings"
fi
[[ "$2" == "generated" ]] && find ${template/_./_\*.} -not -name $template
}
list_all() {
local template
for template in *_.*
do
list_template "$template" "$1"
done
}
apply(){
target="${1}"
template="${1/_*./_.}"
settings="${template}.settings"
. $settings
cp -f $template $target
seed_template $target "${replacements[@]}"
unset $replacements
}
less_diff(){
target="${1}"
target_new="${1}.new"
template="${1/_*./_.}"
settings="${template}.settings"
. $settings
cp -f $template $target_new
seed_template $target_new "${replacements[@]}"
diff -u $target $target_new | less
rm -rf $target_new
unset $replacements
}
case "$action" in
list)
case "x$1" in
xall)
list_all generated
;;
x)
list_all
;;
*)
list_template "$1" generated
;;
esac
;;
apply)
apply "$@"
;;
diff)
less_diff "$@"
;;
*)
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment