Skip to content

Commit ba57c6c

Browse files
committed
Refactor cookiecutter replay file copying
The cleaning up of the replay file is now done in a separate function, as more cleaning up is coming soon. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 21c44aa commit ba57c6c

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

cookiecutter/hooks/post_gen_project.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,43 @@ def finish_setup() -> None:
131131
commit_git_changes(first_commit=was_repo_initialized)
132132

133133

134+
def clean_replay_file(
135+
*,
136+
replay_data: dict[str, Any],
137+
src: _pathlib.Path, # pylint: disable=unused-argument
138+
dst: _pathlib.Path,
139+
) -> dict[str, Any]:
140+
"""Clean the replay file.
141+
142+
Performs some cleaning of the replay file generated by cookiecutter to make it
143+
more suitable for the generated project.
144+
145+
Args:
146+
replay_data: The replay data to clean.
147+
src: The path of the original replay file.
148+
dst: The path of the cleaned replay file.
149+
150+
Returns:
151+
The cleaned replay data.
152+
"""
153+
# Remove the _output_dir key from the replay data because it is an absolute path
154+
# that depends on the current environment and we don't want to add it as part of
155+
# the generated project.
156+
replay_data["cookiecutter"].pop("_output_dir", None)
157+
158+
if template := replay_data["cookiecutter"].get("_template"):
159+
if not template.startswith(("gh:", "git@", "https://")):
160+
print(
161+
f"WARNING: The replay file's `_template` ({template}) doesn't seem "
162+
"to be a remote repository, it won't be saved to avoid storing a "
163+
"local template in the new repository. If this is incorrect, "
164+
f"please add it back manually to {dst}."
165+
)
166+
replay_data["cookiecutter"].pop("_template", None)
167+
168+
return replay_data
169+
170+
134171
def copy_replay_file() -> None:
135172
"""Copy the replay file to the project root."""
136173
src = _pathlib.Path("~/.cookiecutter_replay/cookiecutter.json").expanduser()
@@ -146,22 +183,9 @@ def copy_replay_file() -> None:
146183

147184
try:
148185
with src.open("r", encoding="utf8") as input_file:
149-
replay_data = _json.load(input_file)
150-
151-
# Remove the _output_dir key from the replay data because it is an absolute path
152-
# that depends on the current environment and we don't want to add it as part of
153-
# the generated project.
154-
replay_data["cookiecutter"].pop("_output_dir", None)
155-
156-
if template := replay_data["cookiecutter"].get("_template"):
157-
if not template.startswith(("gh:", "git@", "https://")):
158-
print(
159-
f"WARNING: The replay file's `_template` ({template}) doesn't seem "
160-
"to be a remote repository, it won't be saved to avoid storing a "
161-
"local template in the new repository. If this is incorrect, "
162-
f"please add it back manually to {dst}."
163-
)
164-
replay_data["cookiecutter"].pop("_template", None)
186+
replay_data = clean_replay_file(
187+
replay_data=_json.load(input_file), src=src, dst=dst
188+
)
165189

166190
with dst.open("w", encoding="utf8") as output_file:
167191
_json.dump(replay_data, output_file, indent=2)

0 commit comments

Comments
 (0)