Skip to content

Commit

Permalink
feat(cookiecutter): add .patch files support in cookiecutter feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Feb 2, 2021
1 parent 15938ec commit 3b20434
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
16 changes: 15 additions & 1 deletion ddb/feature/cookiecutter/actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import os
import patch
from typing import Union

from cookiecutter.config import DEFAULT_CONFIG
Expand Down Expand Up @@ -59,5 +60,18 @@ def _generate_template(template):
kwargs = {k: v for k, v in template.items() if v is not None}

ret = cookiecutter(**kwargs)
output_dir = os.path.relpath(ret, '.')

context.log.success(f"{template['template']} -> {os.path.relpath(ret, '.')}")
context.log.success(f"{template['template']} -> {output_dir}")

for patch_file in os.listdir(output_dir):
if patch_file.endswith('.patch'):
patch_file = os.path.join(output_dir, patch_file)
if os.path.isfile(patch_file):
context.log.notice("Applying patch %s in %s", patch_file, output_dir)
try:
pset = patch.fromfile(patch_file)
pset.apply(root=output_dir)
context.log.success("Patch %s applied in %s", patch_file, output_dir)
except Exception as exc: # pylint:disable=broad-except
context.log.error("Patch %s has failed to be applied in %s: %s", patch_file, output_dir, exc)
8 changes: 8 additions & 0 deletions docs/features/cookiecutter.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ This can be used to retrieve [djp packages](./jsonnet.md#djp-packages-ddb-jsonne
| `overwrite_if_exists` | boolean<br>`true` | |
| `config_file` | string | |
| `default_config` | dict[str, Any] | |

!!! tip "Generate `.patch` files to track your changes"

If you need to apply some modification on downloaded content, you should generate `.patch` files (unified diff)
of the output directory of the template.

Any `.patch` file available in the output directory will be automatically applied when invoking `ddb download`
command later, so you changes are kept while downloading a new version of the template.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ wcmatch
marshmallow-union
progress
ordered-set
patch

0 comments on commit 3b20434

Please sign in to comment.