mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-11 20:38:59 +00:00
adding dotfile specific variables for templating
This commit is contained in:
@@ -68,12 +68,12 @@ class Action(Cmd):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return 'action({})'.format(self.__str__())
|
return 'action({})'.format(self.__str__())
|
||||||
|
|
||||||
def execute(self, templater=None, newvars={}):
|
def execute(self, templater=None):
|
||||||
"""execute the action in the shell"""
|
"""execute the action in the shell"""
|
||||||
ret = 1
|
ret = 1
|
||||||
action = self.action
|
action = self.action
|
||||||
if templater:
|
if templater:
|
||||||
action = templater.generate_string(self.action, tmpvars=newvars)
|
action = templater.generate_string(self.action)
|
||||||
try:
|
try:
|
||||||
cmd = action.format(*self.args)
|
cmd = action.format(*self.args)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
|
|||||||
@@ -175,21 +175,23 @@ class Cfg:
|
|||||||
"""resolve dotfiles src/dst/actions templating for this profile"""
|
"""resolve dotfiles src/dst/actions templating for this profile"""
|
||||||
t = Templategen(variables=variables)
|
t = Templategen(variables=variables)
|
||||||
dotfiles = self._get_dotfiles(profile)
|
dotfiles = self._get_dotfiles(profile)
|
||||||
|
tvars = t.add_tmp_vars()
|
||||||
for d in dotfiles:
|
for d in dotfiles:
|
||||||
|
# add dotfile variables
|
||||||
|
t.restore_vars(tvars)
|
||||||
|
newvar = d.get_vars()
|
||||||
|
t.add_tmp_vars(newvars=newvar)
|
||||||
# src and dst path
|
# src and dst path
|
||||||
d.src = t.generate_string(d.src)
|
d.src = t.generate_string(d.src)
|
||||||
d.dst = t.generate_string(d.dst)
|
d.dst = t.generate_string(d.dst)
|
||||||
# pre actions
|
# pre actions
|
||||||
var = d.get_vars()
|
|
||||||
if self.key_actions_pre in d.actions:
|
if self.key_actions_pre in d.actions:
|
||||||
for action in d.actions[self.key_actions_pre]:
|
for action in d.actions[self.key_actions_pre]:
|
||||||
action.action = t.generate_string(action.action,
|
action.action = t.generate_string(action.action)
|
||||||
tmpvars=var)
|
|
||||||
# post actions
|
# post actions
|
||||||
if self.key_actions_post in d.actions:
|
if self.key_actions_post in d.actions:
|
||||||
for action in d.actions[self.key_actions_post]:
|
for action in d.actions[self.key_actions_post]:
|
||||||
action.action = t.generate_string(action.action,
|
action.action = t.generate_string(action.action)
|
||||||
tmpvars=var)
|
|
||||||
return dotfiles
|
return dotfiles
|
||||||
|
|
||||||
def _load_config(self, profile=None):
|
def _load_config(self, profile=None):
|
||||||
|
|||||||
@@ -45,8 +45,7 @@ def action_executor(o, dotfile, actions, defactions, templater, post=False):
|
|||||||
continue
|
continue
|
||||||
if o.debug:
|
if o.debug:
|
||||||
LOG.dbg('executing def-{}-action {}'.format(s, action))
|
LOG.dbg('executing def-{}-action {}'.format(s, action))
|
||||||
newvars = dotfile.get_vars()
|
ret = action.execute(templater=templater)
|
||||||
ret = action.execute(templater=templater, newvars=newvars)
|
|
||||||
if not ret:
|
if not ret:
|
||||||
err = 'def-{}-action \"{}\" failed'.format(s, action.key)
|
err = 'def-{}-action \"{}\" failed'.format(s, action.key)
|
||||||
LOG.err(err)
|
LOG.err(err)
|
||||||
@@ -59,8 +58,7 @@ def action_executor(o, dotfile, actions, defactions, templater, post=False):
|
|||||||
continue
|
continue
|
||||||
if o.debug:
|
if o.debug:
|
||||||
LOG.dbg('executing {}-action {}'.format(s, action))
|
LOG.dbg('executing {}-action {}'.format(s, action))
|
||||||
newvars = dotfile.get_vars()
|
ret = action.execute(templater=templater)
|
||||||
ret = action.execute(templater=templater, newvars=newvars)
|
|
||||||
if not ret:
|
if not ret:
|
||||||
err = '{}-action \"{}\" failed'.format(s, action.key)
|
err = '{}-action \"{}\" failed'.format(s, action.key)
|
||||||
LOG.err(err)
|
LOG.err(err)
|
||||||
@@ -93,7 +91,13 @@ def cmd_install(o):
|
|||||||
showdiff=o.install_showdiff,
|
showdiff=o.install_showdiff,
|
||||||
backup_suffix=o.install_backup_suffix)
|
backup_suffix=o.install_backup_suffix)
|
||||||
installed = 0
|
installed = 0
|
||||||
|
tvars = t.add_tmp_vars()
|
||||||
for dotfile in dotfiles:
|
for dotfile in dotfiles:
|
||||||
|
# add dotfile variables
|
||||||
|
t.restore_vars(tvars)
|
||||||
|
newvars = dotfile.get_vars()
|
||||||
|
t.add_tmp_vars(newvars=newvars)
|
||||||
|
|
||||||
preactions = []
|
preactions = []
|
||||||
if not o.install_temporary and dotfile.actions \
|
if not o.install_temporary and dotfile.actions \
|
||||||
and Cfg.key_actions_pre in dotfile.actions:
|
and Cfg.key_actions_pre in dotfile.actions:
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ class Dotfile:
|
|||||||
def get_vars(self):
|
def get_vars(self):
|
||||||
"""return this dotfile templating vars"""
|
"""return this dotfile templating vars"""
|
||||||
_vars = {}
|
_vars = {}
|
||||||
_vars['_dotfile_src'] = self.src
|
_vars['_dotfile_abs_src'] = self.src
|
||||||
_vars['_dotfile_dst'] = self.dst
|
_vars['_dotfile_abs_dst'] = self.dst
|
||||||
return _vars
|
return _vars
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|||||||
@@ -51,36 +51,28 @@ class Templategen:
|
|||||||
self.env.globals['exists'] = jhelpers.exists
|
self.env.globals['exists'] = jhelpers.exists
|
||||||
self.env.globals['exists_in_path'] = jhelpers.exists_in_path
|
self.env.globals['exists_in_path'] = jhelpers.exists_in_path
|
||||||
|
|
||||||
def generate(self, src, tmpvars={}):
|
def generate(self, src):
|
||||||
"""render template from path"""
|
"""render template from path"""
|
||||||
if not os.path.exists(src):
|
if not os.path.exists(src):
|
||||||
return ''
|
return ''
|
||||||
saved = self._patch_globals(tmpvars)
|
return self._handle_file(src)
|
||||||
ret = self._handle_file(src)
|
|
||||||
self._restore_globals(saved)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def generate_string(self, string, tmpvars={}):
|
def generate_string(self, string):
|
||||||
"""render template from string"""
|
"""render template from string"""
|
||||||
if not string:
|
if not string:
|
||||||
return ''
|
return ''
|
||||||
saved = self._patch_globals(tmpvars)
|
return self.env.from_string(string).render()
|
||||||
if self.debug:
|
|
||||||
self.log.dbg('new vars: {}'.format(tmpvars))
|
|
||||||
ret = self.env.from_string(string).render()
|
|
||||||
self._restore_globals(saved)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def _patch_globals(self, newvars={}):
|
def add_tmp_vars(self, newvars={}):
|
||||||
"""add vars to the globals, make sure to call _restore_globals"""
|
"""add vars to the globals, make sure to call restore_vars"""
|
||||||
saved_globals = self.env.globals.copy()
|
saved_globals = self.env.globals.copy()
|
||||||
if not newvars:
|
if not newvars:
|
||||||
return saved_globals
|
return saved_globals
|
||||||
self.env.globals.update(newvars)
|
self.env.globals.update(newvars)
|
||||||
return saved_globals
|
return saved_globals
|
||||||
|
|
||||||
def _restore_globals(self, saved_globals):
|
def restore_vars(self, saved_globals):
|
||||||
"""restore globals from _patch_globals"""
|
"""restore globals from add_tmp_vars"""
|
||||||
self.env.globals = saved_globals.copy()
|
self.env.globals = saved_globals.copy()
|
||||||
|
|
||||||
def update_variables(self, variables):
|
def update_variables(self, variables):
|
||||||
|
|||||||
@@ -59,10 +59,10 @@ cfg="${tmps}/config.yaml"
|
|||||||
cat > ${cfg} << _EOF
|
cat > ${cfg} << _EOF
|
||||||
actions:
|
actions:
|
||||||
pre:
|
pre:
|
||||||
preaction: "echo {{@@ _dotfile_src @@}} > {0}"
|
preaction: "echo {{@@ _dotfile_abs_src @@}} > {0}"
|
||||||
post:
|
post:
|
||||||
postaction: "echo {{@@ _dotfile_src @@}} > ${tmpa}/post"
|
postaction: "echo {{@@ _dotfile_abs_src @@}} > ${tmpa}/post"
|
||||||
nakedaction: "echo {{@@ _dotfile_src @@}} > ${tmpa}/naked"
|
nakedaction: "echo {{@@ _dotfile_abs_src @@}} > ${tmpa}/naked"
|
||||||
config:
|
config:
|
||||||
backup: true
|
backup: true
|
||||||
create: true
|
create: true
|
||||||
@@ -102,10 +102,10 @@ rm -f ${tmpa}/naked* ${tmpa}/pre* ${tmpa}/post* ${tmpd}/abc
|
|||||||
cat > ${cfg} << _EOF
|
cat > ${cfg} << _EOF
|
||||||
actions:
|
actions:
|
||||||
pre:
|
pre:
|
||||||
preaction: "echo {{@@ _dotfile_dst @@}} > ${tmpa}/pre"
|
preaction: "echo {{@@ _dotfile_abs_dst @@}} > ${tmpa}/pre"
|
||||||
post:
|
post:
|
||||||
postaction: "echo {{@@ _dotfile_dst @@}} > ${tmpa}/post"
|
postaction: "echo {{@@ _dotfile_abs_dst @@}} > ${tmpa}/post"
|
||||||
nakedaction: "echo {{@@ _dotfile_dst @@}} > ${tmpa}/naked"
|
nakedaction: "echo {{@@ _dotfile_abs_dst @@}} > ${tmpa}/naked"
|
||||||
config:
|
config:
|
||||||
backup: true
|
backup: true
|
||||||
create: true
|
create: true
|
||||||
|
|||||||
89
tests-ng/dotfile-variables.sh
Executable file
89
tests-ng/dotfile-variables.sh
Executable file
@@ -0,0 +1,89 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# author: deadc0de6 (https://github.com/deadc0de6)
|
||||||
|
# Copyright (c) 2017, deadc0de6
|
||||||
|
#
|
||||||
|
# test dotfile specific variables
|
||||||
|
# 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 --suffix='-dotdrop-tests'`
|
||||||
|
mkdir -p ${tmps}/dotfiles
|
||||||
|
# the dotfile destination
|
||||||
|
tmpd=`mktemp -d --suffix='-dotdrop-tests'`
|
||||||
|
|
||||||
|
# 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 'src:{{@@ _dotfile_abs_src @@}}' > ${tmps}/dotfiles/abc
|
||||||
|
echo 'dst:{{@@ _dotfile_abs_dst @@}}' >> ${tmps}/dotfiles/abc
|
||||||
|
|
||||||
|
# install
|
||||||
|
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V
|
||||||
|
|
||||||
|
# checks
|
||||||
|
[ ! -e ${tmpd}/abc ] && echo 'dotfile not installed' && exit 1
|
||||||
|
grep "src:${tmps}/dotfiles/abc" ${tmpd}/abc >/dev/null
|
||||||
|
grep "dst:${tmpd}/abc" ${tmpd}/abc >/dev/null
|
||||||
|
|
||||||
|
## CLEANING
|
||||||
|
rm -rf ${tmps} ${tmpd}
|
||||||
|
|
||||||
|
echo "OK"
|
||||||
|
exit 0
|
||||||
Reference in New Issue
Block a user