Skip to content

Commit 2bc92bb

Browse files
committed
Use settings to get BASE_DIR instead of checking for manage.py. Handle existing components. Add some ascii art and an ask for a GitHub star.
1 parent c836bf1 commit 2bc92bb

File tree

2 files changed

+84
-20
lines changed

2 files changed

+84
-20
lines changed

django_unicorn/management/commands/startunicorn.py

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from pathlib import Path
22
from webbrowser import open
33

4+
from django.conf import settings
45
from django.core.management.base import BaseCommand, CommandError
56

67
from django_unicorn.components.unicorn_view import (
@@ -9,14 +10,14 @@
910
)
1011

1112

12-
COMPONENT_FILE = """from django_unicorn.components import UnicornView
13+
COMPONENT_FILE_CONTENT = """from django_unicorn.components import UnicornView
1314
1415
1516
class {pascal_case_component_name}View(UnicornView):
1617
pass
1718
"""
1819

19-
TEMPLATE_FILE = """<div>
20+
TEMPLATE_FILE_CONTENT = """<div>
2021
<!-- put component code here -->
2122
</div>
2223
"""
@@ -29,16 +30,20 @@ def add_arguments(self, parser):
2930
parser.add_argument("component_names", nargs="+", type=str)
3031

3132
def handle(self, *args, **options):
32-
if not Path("manage.py").exists():
33-
raise CommandError("Can't find manage.py in current path.")
33+
if not hasattr(settings, "BASE_DIR"):
34+
raise CommandError("Can't find BASE_DIR for this project.")
3435

36+
if "component_names" not in options:
37+
raise CommandError("Pass in at least one component name.")
38+
39+
base_path = Path(settings.BASE_DIR)
3540
first_component = False
3641

37-
if not Path("unicorn").exists():
38-
Path("unicorn").mkdir()
42+
if not (base_path / Path("unicorn")).exists():
43+
(base_path / Path("unicorn")).mkdir()
3944
self.stdout.write(
4045
self.style.SUCCESS(
41-
"Create unicorn directory for your first component! ✨"
46+
"Created unicorn directory for your first component! ✨\n"
4247
)
4348
)
4449

@@ -49,26 +54,58 @@ def handle(self, *args, **options):
4954
pascal_case_component_name = convert_to_pascal_case(component_name)
5055

5156
# Create component
52-
if not Path("unicorn/components").exists():
53-
Path("unicorn/components").mkdir()
57+
component_base_path = base_path / Path("unicorn") / Path("components")
58+
59+
if not component_base_path.exists():
60+
component_base_path.mkdir()
61+
62+
component_path = component_base_path / Path(
63+
f"{snake_case_component_name}.py"
64+
)
65+
66+
if component_path.exists():
67+
self.stdout.write(
68+
self.style.ERROR(
69+
f"The component for {snake_case_component_name}.py already exists."
70+
)
71+
)
72+
return
5473

55-
component_path = Path(f"unicorn/components/{snake_case_component_name}.py")
5674
component_path.write_text(
57-
COMPONENT_FILE.format(
75+
COMPONENT_FILE_CONTENT.format(
5876
**{"pascal_case_component_name": pascal_case_component_name}
5977
)
6078
)
6179
self.stdout.write(self.style.SUCCESS(f"Created {component_path}."))
6280

6381
# Create template
64-
if not Path("unicorn/templates/unicorn").exists():
65-
if not Path("unicorn/templates").exists():
66-
Path("unicorn/templates").mkdir()
82+
template_base_path = (
83+
base_path / Path("unicorn") / Path("templates") / Path("unicorn")
84+
)
85+
86+
if not template_base_path.exists():
87+
if not (base_path / Path("unicorn") / Path("templates")).exists():
88+
(base_path / Path("unicorn") / Path("templates")).mkdir()
89+
90+
template_base_path.mkdir()
6791

68-
Path("unicorn/templates/unicorn").mkdir()
92+
template_path = (
93+
base_path
94+
/ Path("unicorn")
95+
/ Path("templates")
96+
/ Path("unicorn")
97+
/ Path(f"{component_name}.html")
98+
)
6999

70-
template_path = Path(f"unicorn/templates/unicorn/{component_name}.html")
71-
template_path.write_text(TEMPLATE_FILE)
100+
if template_path.exists():
101+
self.stdout.write(
102+
self.style.ERROR(
103+
f"The template for {component_name}.html already exists."
104+
)
105+
)
106+
return
107+
108+
template_path.write_text(TEMPLATE_FILE_CONTENT)
72109
self.stdout.write(self.style.SUCCESS(f"Created {template_path}."))
73110

74111
if first_component:
@@ -77,6 +114,12 @@ def handle(self, *args, **options):
77114
)
78115

79116
if input_value.strip().lower() in ("y", "yes"):
117+
self.stdout.write(
118+
self.style.SUCCESS(
119+
"Thank you for helping spread the word about Unicorn!"
120+
)
121+
)
122+
80123
self.stdout.write(
81124
"""
82125
,/
@@ -97,12 +140,14 @@ def handle(self, *args, **options):
97140
\\
98141
"""
99142
)
143+
144+
open("https://github.com/adamghill/django-unicorn", new=2)
145+
else:
100146
self.stdout.write(
101-
self.style.SUCCESS(
102-
"Thank you for helping spread the word about Unicorn!"
147+
self.style.ERROR(
148+
"Ok, bummer. I hope you will star it for me at https://github.com/adamghill/django-unicorn at some point!"
103149
)
104150
)
105-
open("https://github.com/adamghill/django-unicorn", new=2)
106151

107152
self.stdout.write(
108153
self.style.WARNING(
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
3+
from django.core.management.base import CommandError
4+
5+
import pytest
6+
7+
from django_unicorn.management.commands.startunicorn import Command
8+
9+
10+
def test_handle_no_base_dir():
11+
with pytest.raises(CommandError):
12+
Command().handle()
13+
14+
15+
def test_handle_no_args(settings):
16+
settings.BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
with pytest.raises(CommandError):
19+
Command().handle()

0 commit comments

Comments
 (0)