def copy_template(template_path: Path, path: Path, variables: dict):
for d in template_path.iterdir():
target_path = path / d.relative_to(template_path)
if d.is_dir():
copy_template(d, target_path, variables)
elif target_path.exists():
# better not overwrite any existing files!
raise click.UsageError('Target file "{}" already exists. Aborting!'.format(target_path))
else:
with Action('Writing {}..'.format(target_path)):
target_path.parent.mkdir(parents=True, exist_ok=True)
with d.open() as fd:
contents = fd.read()
template = string.Template(contents)
contents = template.safe_substitute(variables)
with target_path.open('w') as fd:
fd.write(contents)
评论列表
文章目录