diff --git a/README.md b/README.md index b66a784..7995689 100644 --- a/README.md +++ b/README.md @@ -547,16 +547,21 @@ the following entries: - ... ``` -* **actions** entry: a list of action +* **actions** entry: a list of action (see [Use actions](#use-actions)) ``` : ``` -* **trans** entry: a list of transformations +* **trans** entry: a list of transformations (see [Use transformations](#use-transformations)) ``` : ``` +* **variables** entry: a list of template variables +``` + : +``` + ## All dotfiles for a profile To use all defined dotfiles for a profile, simply use @@ -621,6 +626,21 @@ Note that dotdrop uses different delimiters than * `{{@@ env['MY_VAR'] @@}}` contains environment variables (see [Environment variables](#environment-variables)). * `{{@@ header @@}}` insert dotdrop header (see [Dotdrop header](#dotdrop-header)). +Addionally to the above, variables can be added in the config yaml file under +the `variables` entry. The variables added there are directly reachable in +any templates. + +For example in the config file: +```yaml +variables: + var1: some variable content +``` + +Those can then be used in any template with +``` +{{@@ var1 @@}} +``` + ## Dotdrop header Dotdrop is able to insert a header in the generated dotfiles. This allows diff --git a/dotdrop/config.py b/dotdrop/config.py index 57acabe..6ab8870 100644 --- a/dotdrop/config.py +++ b/dotdrop/config.py @@ -38,6 +38,9 @@ class Cfg: # transformations keys key_trans = 'trans' + # template variables + key_variables = 'variables' + # dotfiles keys key_dotfiles = 'dotfiles' key_dotfiles_src = 'src' @@ -479,6 +482,11 @@ class Cfg: """return all defined settings""" return self.lnk_settings.copy() + def get_variables(self): + if self.key_variables in self.content: + return self.content[self.key_variables] + return {} + def dump(self): """return a dump of the config""" # temporary reset dotpath diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index a5ff0ed..bbadd09 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -81,7 +81,8 @@ def install(opts, conf): msg = 'no dotfiles defined for this profile (\"{}\")' LOG.err(msg.format(opts['profile'])) return False - t = Templategen(base=opts['dotpath'], debug=opts['debug']) + t = Templategen(base=opts['dotpath'], variables=opts['variables'], + debug=opts['debug']) inst = Installer(create=opts['create'], backup=opts['backup'], dry=opts['dry'], safe=opts['safe'], base=opts['dotpath'], diff=opts['installdiff'], debug=opts['debug']) @@ -184,7 +185,8 @@ def compare(opts, conf, tmp, focus=None, ignore=[]): if len(selected) < 1: return False - t = Templategen(base=opts['dotpath'], debug=opts['debug']) + t = Templategen(base=opts['dotpath'], variables=opts['variables'], + debug=opts['debug']) inst = Installer(create=opts['create'], backup=opts['backup'], dry=opts['dry'], base=opts['dotpath'], debug=opts['debug']) @@ -339,6 +341,7 @@ def main(): opts['installdiff'] = not args['--nodiff'] opts['link'] = args['--link'] opts['debug'] = args['--verbose'] + opts['variables'] = conf.get_variables() if opts['debug']: LOG.dbg('config file: {}'.format(args['--cfg'])) diff --git a/dotdrop/templategen.py b/dotdrop/templategen.py index 62618d2..db93459 100644 --- a/dotdrop/templategen.py +++ b/dotdrop/templategen.py @@ -22,7 +22,7 @@ COMMENT_END = '@@#}' class Templategen: - def __init__(self, base='.', debug=False): + def __init__(self, base='.', variables={}, debug=False): self.base = base.rstrip(os.sep) self.debug = debug loader = FileSystemLoader(self.base) @@ -36,6 +36,7 @@ class Templategen: comment_start_string=COMMENT_START, comment_end_string=COMMENT_END) self.env.globals['header'] = self._header + self.env.globals.update(variables) self.log = Logger() def generate(self, src, profile): diff --git a/tests-ng/variables.sh b/tests-ng/variables.sh new file mode 100755 index 0000000..2ef3c6b --- /dev/null +++ b/tests-ng/variables.sh @@ -0,0 +1,98 @@ +#!/usr/bin/env bash +# author: deadc0de6 (https://github.com/deadc0de6) +# Copyright (c) 2017, deadc0de6 +# +# test variables from yaml file +# 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 "RUNNING $(basename $BASH_SOURCE)" + +################################################################ +# this is the test +################################################################ + +# the dotfile source +tmps=`mktemp -d` +mkdir -p ${tmps}/dotfiles +#echo "dotfile source: ${tmps}" +# the dotfile destination +tmpd=`mktemp -d` +#echo "dotfile destination: ${tmpd}" + +# create the config file +cfg="${tmps}/config.yaml" + +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles +variables: + var1: "this is some test" + var2: 12 + var3: another test +dotfiles: + f_abc: + dst: ${tmpd}/abc + src: abc +profiles: + p1: + dotfiles: + - f_abc +_EOF +cat ${cfg} + +# create the dotfile +echo "{{@@ var1 @@}}" > ${tmps}/dotfiles/abc +echo "{{@@ var2 @@}}" >> ${tmps}/dotfiles/abc +echo "{{@@ var3 @@}}" >> ${tmps}/dotfiles/abc +echo "test" >> ${tmps}/dotfiles/abc + +# install +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 + +grep '^this is some test' ${tmpd}/abc >/dev/null +grep '^12' ${tmpd}/abc >/dev/null +grep '^another test' ${tmpd}/abc >/dev/null + +cat ${tmpd}/abc + +## CLEANING +rm -rf ${tmps} ${tmpd} + +echo "OK" +exit 0 diff --git a/tests/helpers.py b/tests/helpers.py index a89faac..b218c7c 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -73,6 +73,7 @@ def load_config(confpath, profile): opts['link'] = False opts['debug'] = True opts['dopts'] = '' + opts['variables'] = {} return conf, opts diff --git a/tests/test_install.py b/tests/test_install.py index 5be0877..29dd263 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -161,6 +161,7 @@ exec bspwm conf, opts = load_config(confpath, profile) opts['safe'] = False opts['debug'] = True + opts['variables'] = {} install(opts, conf) # now compare the generated files