Skip to content

Commit e27445b

Browse files
committed
Updated hooks pre_prompt/post_gen_project
Prompting for GitHub is removed if gh tool is not found.
1 parent 711f907 commit e27445b

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

hooks/post_gen_project.uv

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,25 @@ def post_gen_project() -> int:
5959
try:
6060
direnv_allow = sh.direnv.bake("allow")
6161
except sh.CommandNotFound:
62+
logger.optional("direnv is not available, skipping direnv allow.")
6263
direnv_allow = lambda *args, **kwargs: None
6364

64-
try:
65-
gh_repo_create = sh.gh.repo.create(
66-
"{{ cookiecutter.package_name }}",
67-
# {% if cookiecutter.make_github_repo_private %}
68-
"--private",
69-
# {% else %}
70-
"--public",
71-
# {% endif %}
72-
"--push",
73-
"--source=.",
74-
"--remote=upstream",
75-
)
76-
except sh.CommandNotFound:
77-
gh_repo_create = lambda *args, **kwargs, None
78-
65+
# {% if cookiecutter.create_github_repo is defined and cookiecutter.create_github_repo %}
66+
gh_repo_create = sh.gh.repo.create.bake(
67+
"{{ cookiecutter.package_name }}",
68+
# {% if cookiecutter.make_github_repo_private is defined and cookiecutter.make_github_repo_private %}
69+
"--private",
70+
# {% else %}
71+
"--public",
72+
# {% endif %}
73+
"--push",
74+
"--source=.",
75+
"--remote=upstream",
76+
)
77+
# {% else %}
78+
gh_repo_create = lambda *args, **kwargs: None
79+
# {% endif %}
80+
7981
cmds = [
8082
(True, sh.uv.python.install.bake("{{ cookiecutter.python_version_dev }}")),
8183
(False, direnv_allow),
@@ -84,9 +86,7 @@ def post_gen_project() -> int:
8486
(True, sh.git.init.bake("--quiet", "--initial-branch", "main")),
8587
(True, sh.git.add.bake(".")),
8688
(True, sh.git.commit.bake("-m", "initial commit")),
87-
# {% if cookiecutter.create_github_repo %}
88-
(False, gh_repo_create)
89-
# {% endif %}
89+
(False, gh_repo_create),
9090
]
9191

9292
for required, cmd in cmds:

hooks/pre_prompt.uv

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def pre_prompt(config_file: str | Path | None = None) -> int:
8585
- Fetch GitHub user.name from git config.
8686
- Fetch GitHub user.email from git config.
8787
- Find available Python versions excluding pre-releases.
88+
- Check for optional tool gh and disable GitHub prompting if not found.
8889
8990
Discovered values are then merged into the cookiecutter context.
9091
"""
@@ -107,7 +108,19 @@ def pre_prompt(config_file: str | Path | None = None) -> int:
107108

108109
config_file = Path(config_file or "cookiecutter.json")
109110
cookiecutter = json.loads(config_file.read_text())
111+
112+
# Optional tool checks and modifications to cookiecutter JSON
113+
try:
114+
sh.gh("--version")
115+
except sh.CommandNotFound:
116+
logger.optional("gh is not available, disabling GitHub prompts.")
117+
del cookiecutter["create_github_repo"]
118+
del cookiecutter["make_github_repo_private"]
119+
del cookiecutter["__prompts__"]["create_github_repo"]
120+
del cookiecutter["__prompts__"]["make_github_repo_private"]
121+
110122
cookiecutter |= discovered
123+
111124
json.dump(cookiecutter, config_file.open("w"), indent=4)
112125

113126
return 0

0 commit comments

Comments
 (0)