Skip to content

Commit 28a795c

Browse files
ci: update copier config and script
1 parent dba4cc5 commit 28a795c

File tree

2 files changed

+15
-38
lines changed

2 files changed

+15
-38
lines changed

.copier/update_dotenv.py

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,29 @@
1-
#!/usr/bin/env python3
2-
"""
3-
This script updates the .env file with values from the Copier answers.
4-
It allows the .env file to be a regular file (not a template) that works with or without Copier.
5-
"""
6-
71
import json
8-
import yaml
92
from pathlib import Path
103

114
# Update the .env file with the answers from the .copier-answers.yml file
125
# without using Jinja2 templates in the .env file, this way the code works as is
136
# without needing Copier, but if Copier is used, the .env file will be updated
147
root_path = Path(__file__).parent.parent
15-
answers_path = root_path / ".copier-answers.yml"
16-
17-
# Load the answers from YAML format
18-
with open(answers_path, "r") as f:
19-
answers_yaml = f.read()
20-
# Convert YAML to Python dict
21-
answers = yaml.safe_load(answers_yaml)
22-
8+
answers_path = Path(__file__).parent / ".copier-answers.yml"
9+
answers = json.loads(answers_path.read_text())
2310
env_path = root_path / ".env"
24-
if not env_path.exists():
25-
env_example_path = root_path / ".env.example"
26-
if env_example_path.exists():
27-
with open(env_example_path, "r") as f:
28-
env_content = f.read()
29-
else:
30-
env_content = ""
31-
else:
32-
with open(env_path, "r") as f:
33-
env_content = f.read()
11+
env_content = env_path.read_text()
3412

35-
# Update the .env file
3613
lines = []
14+
3715
for line in env_content.splitlines():
38-
line_updated = False
3916
for key, value in answers.items():
4017
upper_key = key.upper()
4118
if line.startswith(f"{upper_key}="):
42-
content = f'{upper_key}="{value}"' if isinstance(value, str) and " " in value else f"{upper_key}={value}"
43-
lines.append(content)
44-
line_updated = True
19+
if " " in value:
20+
content = f"{upper_key}={value!r}"
21+
else:
22+
content = f"{upper_key}={value}"
23+
new_line = line.replace(line, content)
24+
lines.append(new_line)
4525
break
46-
if not line_updated:
26+
else:
4727
lines.append(line)
4828

49-
# Write the updated .env file
50-
with open(env_path, "w") as f:
51-
f.write("\n".join(lines))
52-
53-
print(f"Updated {env_path} with values from Copier answers.")
29+
env_path.write_text("\n".join(lines))

copier.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ _jinja_extensions:
1313
- extensions.py:SlugifyExtension
1414
_skip_if_exists:
1515
- CHANGELOG.md
16+
_answers_file: .copier/.copier-answers.yml
17+
_tasks:
18+
- ["{{ _copier_python }}", .copier/update_dotenv.py]
1619

1720
# PROMPT --------------------------------
1821
project_name:
@@ -22,7 +25,6 @@ project_name:
2225
project_description:
2326
type: str
2427
help: Your project description
25-
default: "Minimum Viable Python Template"
2628

2729
author_fullname:
2830
type: str
@@ -37,7 +39,6 @@ author_email:
3739
author_username:
3840
type: str
3941
help: Your username (e.g. on GitHub)
40-
default: "pythoninthegrass"
4142

4243
repository_provider:
4344
type: str

0 commit comments

Comments
 (0)