Skip to content

Commit 3433ad4

Browse files
committed
feat: write .env at project creation
1 parent db7e71a commit 3433ad4

3 files changed

Lines changed: 39 additions & 29 deletions

File tree

docs/the_cli/usage.rst

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,27 @@ Each **entry** represents a journal entry within the **myjourney** app.
3333
3434
If necessary, adjust the python_version value in the ``.pre-commit-config.yaml`` file.
3535

36-
**4. Create a new .env file**
37-
38-
.. code-block:: bash
39-
40-
falco sync-dotenv
41-
42-
**5. Fill in some values for the admin user**
36+
**4. Fill in some values for the admin user**
4337

4438
.. code-block:: text
4539
:caption: .env
4640
4741
DJANGO_SUPERUSER_EMAIL=admin@mail.com
4842
DJANGO_SUPERUSER_PASSWORD=admin
4943
50-
**6. Migrate, and create the admin user**
44+
**5. Migrate, and create the admin user**
5145

5246
.. code-block:: bash
5347
5448
hatch run migrate && falco setup-admin
5549
56-
**7. Create the new app, entries**
50+
**6. Create the new app, entries**
5751

5852
.. code-block:: bash
5953
6054
falco start-app entries
6155
62-
**8. Add some fields to your Entry model**
56+
**7. Add some fields to your Entry model**
6357

6458
.. code-block:: python
6559
@@ -98,7 +92,7 @@ If necessary, adjust the python_version value in the ``.pre-commit-config.yaml``
9892
However, please note that this approach may not be the most advisable.
9993

10094

101-
**9. Make migrations for the new model and run them**
95+
**8. Make migrations for the new model and run them**
10296

10397
.. code-block:: bash
10498
@@ -116,21 +110,21 @@ If necessary, adjust the python_version value in the ``.pre-commit-config.yaml``
116110
[tool.falco.crud]
117111
always-migrate = true
118112
119-
**10. Generate CRUD views for the Entry model**
113+
**9. Generate CRUD views for the Entry model**
120114

121115
.. code-block:: bash
122116
123117
falco crud entries.entry --entry-point --skip-git-check
124118
125-
**11. Run the project**
119+
**10. Run the project**
126120

127121
.. code-block:: bash
128122
129123
falco work
130124
131125
Now, check out http://127.0.0.1:8000/entries to see your running app.
132126

133-
This process currently requires 11 commands. Considering the outcome, it's not too shabby! However, I'm confident there's still plenty of room for improvement.
127+
This process currently requires 10 commands. Considering the outcome, it's not too shabby! However, I'm confident there's still plenty of room for improvement.
134128
If you have any suggestions on how to improve this workflow, feel free to open a discussion at https://github.com/Tobi-De/falco/discussions.
135129

136130
.. todo::

src/falco/commands/start_project.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from falco.commands.crud.utils import run_html_formatters
1818
from falco.commands.htmx import get_latest_tag as htmx_latest_tag
1919
from falco.commands.htmx import Htmx
20+
from falco.commands.sync_dotenv import get_updated as get_updated_dotenv
21+
from falco.commands.sync_dotenv import SyncDotenv
2022
from falco.config import read_falco_config
2123
from falco.config import write_falco_config
2224
from falco.utils import clean_project_name
@@ -27,6 +29,7 @@
2729
from rich import print as rich_print
2830
from rich.prompt import Prompt
2931

32+
3033
DEFAULT_SKIP = [
3134
"playground.ipynb",
3235
"README.md",
@@ -96,7 +99,18 @@ def __call__(self) -> None:
9699
with change_directory(project_dir):
97100
pyproject_path = Path("pyproject.toml")
98101
falco_config = read_falco_config(pyproject_path)
102+
99103
crud_utils = InstallCrudUtils().install(project_name=self.project_name, falco_config=falco_config)
104+
105+
env_file = Path(".env")
106+
env_file.touch()
107+
env_file_content = env_file.read_text()
108+
env_template_file = Path(".env.template")
109+
env_config = SyncDotenv().get_config(
110+
env_content=env_file_content, env_template_content=env_template_file.read_text()
111+
)
112+
env_file.write_text(get_updated_dotenv(env_file_content, env_config))
113+
100114
config = {
101115
"crud": {"utils-path": str(crud_utils)},
102116
**self.cruft_file_to_falco_config(git_installed),

src/falco/commands/sync_dotenv.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,7 @@ def __call__(self, project_name: Annotated[str, cappa.Dep(get_project_name)]):
3838
dotenv_content = dotenv_file.read_text() if dotenv_file.exists() else ""
3939
dotenv_template_content = dotenv_template_file.read_text() if dotenv_template_file.exists() else ""
4040

41-
default_values = {
42-
"DJANGO_DEBUG": True,
43-
"DJANGO_ENV": "dev",
44-
"DJANGO_SECRET_KEY": secrets.token_urlsafe(64),
45-
"DJANGO_ALLOWED_HOSTS": "*",
46-
"DATABASE_URL": "sqlite:///db.sqlite3",
47-
"DJANGO_SUPERUSER_EMAIL": "",
48-
"DJANGO_SUPERUSER_PASSWORD": "",
49-
}
50-
51-
config = {
52-
**parse(dotenv_template_content),
53-
**default_values,
54-
**parse(dotenv_content),
55-
}
41+
config = self.get_config(dotenv_content, dotenv_template_content)
5642

5743
if self.fill_missing:
5844
for key, value in config.items():
@@ -78,6 +64,22 @@ def __call__(self, project_name: Annotated[str, cappa.Dep(get_project_name)]):
7864

7965
rich_print(f"[green] {dotenv_file} and {dotenv_template_file} synchronised [/green]")
8066

67+
def get_config(self, env_content: str, env_template_content: str) -> dict:
68+
default_values = {
69+
"DJANGO_DEBUG": True,
70+
"DJANGO_ENV": "dev",
71+
"DJANGO_SECRET_KEY": secrets.token_urlsafe(64),
72+
"DJANGO_ALLOWED_HOSTS": "*",
73+
"DATABASE_URL": "sqlite:///db.sqlite3",
74+
"DJANGO_SUPERUSER_EMAIL": "",
75+
"DJANGO_SUPERUSER_PASSWORD": "",
76+
}
77+
return {
78+
**parse(env_template_content),
79+
**default_values,
80+
**parse(env_content),
81+
}
82+
8183

8284
def parse(env_content: str) -> dict:
8385
result = {}

0 commit comments

Comments
 (0)