diff --git a/README.md b/README.md index 29e05b0..18f4b42 100644 --- a/README.md +++ b/README.md @@ -397,6 +397,20 @@ The above will execute `echo 'vim installed' > /tmp/mydotdrop.log` when vimrc is installed and `echo 'xinitrc installed' > /tmp/myotherlog.log'` when xinitrc is installed. +Variables (see [Available variables](#available-variables)) can be used +in actions for more advanced usage: +```yaml +dynvariables: + trizen_itself_available: (command -v trizen>/dev/null || cd /tmp; git clone https://aur.archlinux.org/trizen.git; cd trizen; makepkg -si) + trizen_package_install: {{@@ trizen_itself_available @@}} && trizen -S --needed + +actions: + trizen_install: {{@@ trizen_package_install @@}} {0} + pre: + pip_install: {{@@ trizen_package_install @@}} python-pip && pip install {0} + yarn_install: {{@@ trizen_package_install @@}} yarn && yarn global add {0} +``` + ## Use transformations There are two types of transformations available: diff --git a/dotdrop/config.py b/dotdrop/config.py index a4c5ca2..f337946 100644 --- a/dotdrop/config.py +++ b/dotdrop/config.py @@ -119,11 +119,20 @@ class Cfg: raise ValueError('config is not valid') def eval_dotfiles(self, profile, debug=False): - """resolve dotfiles src/dst templating for this profile""" + """resolve dotfiles src/dst/actions templating for this profile""" t = Templategen(variables=self.get_variables(profile, debug=debug)) for d in self.get_dotfiles(profile): + # src and dst path d.src = t.generate_string(d.src) d.dst = t.generate_string(d.dst) + # pre actions + if self.key_actions_pre in d.actions: + for action in d.actions[self.key_actions_pre]: + action.action = t.generate_string(action.action) + # post actions + if self.key_actions_post in d.actions: + for action in d.actions[self.key_actions_post]: + action.action = t.generate_string(action.action) def _load_file(self): """load the yaml file""" diff --git a/tests-ng/dynactions.sh b/tests-ng/dynactions.sh new file mode 100755 index 0000000..469c519 --- /dev/null +++ b/tests-ng/dynactions.sh @@ -0,0 +1,126 @@ +#!/usr/bin/env bash +# author: deadc0de6 (https://github.com/deadc0de6) +# Copyright (c) 2017, deadc0de6 +# +# test dynamic actions +# returns 1 in case of error +# + +# exit on first error +set -e + +# all this crap to get current path +rl="readlink -f" +if ! ${rl} "${0}" >/dev/null 2>&1; then + rl="realpath" + + if ! hash ${rl}; then + echo "\"${rl}\" not found !" && exit 1 + fi +fi +cur=$(dirname "$(${rl} "${0}")") + +#hash dotdrop >/dev/null 2>&1 +#[ "$?" != "0" ] && echo "install dotdrop to run tests" && exit 1 + +#echo "called with ${1}" + +# dotdrop path can be pass as argument +ddpath="${cur}/../" +[ "${1}" != "" ] && ddpath="${1}" +[ ! -d ${ddpath} ] && echo "ddpath \"${ddpath}\" is not a directory" && exit 1 + +export PYTHONPATH="${ddpath}:${PYTHONPATH}" +bin="python3 -m dotdrop.dotdrop" + +echo "dotdrop path: ${ddpath}" +echo "pythonpath: ${PYTHONPATH}" + +# get the helpers +source ${cur}/helpers + +echo -e "\e[96m\e[1m==> RUNNING $(basename $BASH_SOURCE) <==\e[0m" + +################################################################ +# this is the test +################################################################ + +# the action temp +tmpa=`mktemp -d` +# the dotfile source +tmps=`mktemp -d` +mkdir -p ${tmps}/dotfiles +# the dotfile destination +tmpd=`mktemp -d` + +# create the config file +cfg="${tmps}/config.yaml" + +cat > ${cfg} << _EOF +variables: + var1: "var1" + var2: "{{@@ var1 @@}} var2" + var3: "{{@@ var2 @@}} var3" + var4: "{{@@ dvar4 @@}}" +dynvariables: + dvar1: "echo dvar1" + dvar2: "{{@@ dvar1 @@}} dvar2" + dvar3: "{{@@ dvar2 @@}} dvar3" + dvar4: "echo {{@@ var3 @@}}" +actions: + pre: + preaction1: "echo {{@@ var3 @@}} > ${tmpa}/preaction1" + preaction2: "echo {{@@ dvar3 @@}} > ${tmpa}/preaction2" + post: + postaction1: "echo {{@@ var3 @@}} > ${tmpa}/postaction1" + postaction2: "echo {{@@ dvar3 @@}} > ${tmpa}/postaction2" + naked1: "echo {{@@ var3 @@}} > ${tmpa}/naked1" + naked2: "echo {{@@ dvar3 @@}} > ${tmpa}/naked2" +config: + backup: true + create: true + dotpath: dotfiles +dotfiles: + f_abc: + dst: ${tmpd}/abc + src: abc + actions: + - preaction1 + - preaction2 + - postaction1 + - postaction2 + - naked1 + - naked2 +profiles: + p1: + dotfiles: + - f_abc +_EOF +cat ${cfg} + +# create the dotfile +echo "test" > ${tmps}/dotfiles/abc + +# install +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 --verbose + +# checks +[ ! -e ${tmpa}/preaction1 ] && exit 1 +[ ! -e ${tmpa}/preaction2 ] && exit 1 +[ ! -e ${tmpa}/postaction1 ] && exit 1 +[ ! -e ${tmpa}/postaction2 ] && exit 1 +[ ! -e ${tmpa}/naked1 ] && exit 1 +[ ! -e ${tmpa}/naked2 ] && exit 1 + +grep 'var1 var2 var3' ${tmpa}/preaction1 >/dev/null +grep 'dvar1 dvar2 dvar3' ${tmpa}/preaction2 >/dev/null +grep 'var1 var2 var3' ${tmpa}/postaction1 >/dev/null +grep 'dvar1 dvar2 dvar3' ${tmpa}/postaction2 >/dev/null +grep 'var1 var2 var3' ${tmpa}/naked1 >/dev/null +grep 'dvar1 dvar2 dvar3' ${tmpa}/naked2 >/dev/null + +## CLEANING +rm -rf ${tmps} ${tmpd} ${tmpa} + +echo "OK" +exit 0