diff --git a/README.md b/README.md index 2459254..9116130 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ why [dotdrop](https://github.com/deadc0de6/dotdrop) rocks. * [Templating](#templating) * [Available variables](#available-variables) + * [Available methods](#available-methods) * [Dotdrop header](#dotdrop-header) * [Example](#example) @@ -781,6 +782,22 @@ alias dotdrop='eval $(grep -v "^#" ~/dotfiles/.env) /usr/bin/dotdrop --cfg=~/dot The above aliases load all the variables from `~/dotfiles/.env` (while omitting lines starting with `#`) before calling dotdrop. +## Available methods + +Beside jinja2 global functions +(see [jinja2 global functions](http://jinja.pocoo.org/docs/2.10/templates/#list-of-global-functions) +the following functions are available and can be used within the templates: + +* `exists(path)`: return true when path exists +``` +{%@@ if exists('/dev/null') @@%} +it does exist +{%@@ endif @@%} +``` + +If you'd like a specific function to be available, either open an issue +or do a PR. + ## Dotdrop header Dotdrop is able to insert a header in the generated dotfiles. This allows diff --git a/dotdrop/jhelpers.py b/dotdrop/jhelpers.py new file mode 100644 index 0000000..05c5d73 --- /dev/null +++ b/dotdrop/jhelpers.py @@ -0,0 +1,13 @@ +""" +author: deadc0de6 (https://github.com/deadc0de6) +Copyright (c) 2018, deadc0de6 + +jinja2 helper methods +""" + +import os + + +def exists(path): + '''return true when path exists''' + return os.path.exists(path) diff --git a/dotdrop/templategen.py b/dotdrop/templategen.py index 1cae5fa..99bf970 100644 --- a/dotdrop/templategen.py +++ b/dotdrop/templategen.py @@ -11,6 +11,7 @@ from jinja2 import Environment, FileSystemLoader # local imports import dotdrop.utils as utils from dotdrop.logger import Logger +import dotdrop.jhelpers as jhelpers BLOCK_START = '{%@@' BLOCK_END = '@@%}' @@ -25,6 +26,7 @@ class Templategen: def __init__(self, profile, base='.', variables={}, debug=False): self.base = base.rstrip(os.sep) self.debug = debug + self.log = Logger() loader = FileSystemLoader(self.base) self.env = Environment(loader=loader, trim_blocks=True, lstrip_blocks=True, @@ -35,11 +37,14 @@ class Templategen: variable_end_string=VAR_END, comment_start_string=COMMENT_START, comment_end_string=COMMENT_END) - self.env.globals['header'] = self._header + # adding variables self.env.globals['env'] = os.environ self.env.globals['profile'] = profile self.env.globals.update(variables) - self.log = Logger() + # adding header method + self.env.globals['header'] = self._header + # adding helper methods + self.env.globals['exists'] = jhelpers.exists def generate(self, src): if not os.path.exists(src): diff --git a/tests-ng/jhelpers.sh b/tests-ng/jhelpers.sh new file mode 100755 index 0000000..059ab90 --- /dev/null +++ b/tests-ng/jhelpers.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env bash +# author: deadc0de6 (https://github.com/deadc0de6) +# Copyright (c) 2017, deadc0de6 +# +# test jinja2 helpers from jhelpers +# 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 dotfile source +tmps=`mktemp -d` +mkdir -p ${tmps}/dotfiles +# 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 +dotfiles: + f_abc: + dst: ${tmpd}/abc + src: abc +profiles: + p1: + dotfiles: + - f_abc +_EOF +cat ${cfg} + +# create the dotfile +echo "this is the test dotfile" > ${tmps}/dotfiles/abc + +# test exists +echo "{%@@ if exists('/dev/null') @@%}" >> ${tmps}/dotfiles/abc +echo "this should exist" >> ${tmps}/dotfiles/abc +echo "{%@@ endif @@%}" >> ${tmps}/dotfiles/abc + +echo "{%@@ if exists('/dev/abcdef') @@%}" >> ${tmps}/dotfiles/abc +echo "this should not exist" >> ${tmps}/dotfiles/abc +echo "{%@@ endif @@%}" >> ${tmps}/dotfiles/abc + +cat ${tmps}/dotfiles/abc + +# install +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V + +cat ${tmpd}/abc + +grep '^this should exist' ${tmpd}/abc >/dev/null +grep -v '^this should not exist' ${tmpd}/abc >/dev/null + +#cat ${tmpd}/abc + +## CLEANING +rm -rf ${tmps} ${tmpd} ${scr} + +echo "OK" +exit 0