11from pathlib import Path
22from webbrowser import open
33
4+ from django .conf import settings
45from django .core .management .base import BaseCommand , CommandError
56
67from django_unicorn .components .unicorn_view import (
910)
1011
1112
12- COMPONENT_FILE = """from django_unicorn.components import UnicornView
13+ COMPONENT_FILE_CONTENT = """from django_unicorn.components import UnicornView
1314
1415
1516class {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 (
0 commit comments