@@ -24,8 +24,21 @@ class NewTemplateScaffold(FileTemplateScaffold):
2424 unwanted_chars = "" .join (["-" , ";" , "!" , "*" , ":" , " " ])
2525
2626 def __init__ (self , project_name : str = None , ** kwargs : t .Any ) -> None :
27+ super (NewTemplateScaffold , self ).__init__ (
28+ working_project_name = project_name , ** kwargs
29+ )
2730 self ._project_name = project_name
28- super (NewTemplateScaffold , self ).__init__ (** kwargs )
31+
32+ def create_file (self , base_path : str , file_name : str , content : t .Any ) -> None :
33+ _path = os .path .join (base_path , file_name .replace (".ellar" , ".py" ))
34+ if os .path .exists (_path ):
35+ content = self .read_file_content (path = _path )
36+
37+ with open (
38+ os .path .join (base_path , file_name .replace (".ellar" , ".py" )), mode = "w"
39+ ) as fw :
40+ refined_content = self ._ctx .environment .from_string (content ).render ()
41+ fw .writelines (refined_content )
2942
3043 def on_scaffold_completed (self ) -> None :
3144 popen_res = subprocess .run (
@@ -36,9 +49,18 @@ def on_scaffold_completed(self) -> None:
3649 )
3750 if popen_res .returncode == 0 :
3851 project_working_project_name = self .get_project_name ()
52+
53+ log_1 = f"- cd { self ._working_project_name } "
54+ if self ._specified_directory :
55+ log_1 = (
56+ f"- cd { self ._specified_directory .lower ()} "
57+ if self ._specified_directory != "."
58+ else ""
59+ )
60+
3961 print (
40- f"`{ self . _working_project_name } ` project created successfully.\n "
41- f"- cd { self . _working_project_name } "
62+ f"`{ project_working_project_name } ` project created successfully.\n "
63+ f"{ log_1 } "
4264 )
4365 print ("To start your server, run the command below" )
4466 print (
@@ -47,13 +69,37 @@ def on_scaffold_completed(self) -> None:
4769 else :
4870 print (popen_res .stderr .decode ("utf8" ))
4971
72+ def is_directory_empty (self ) -> bool :
73+ """
74+ Check if the given directory is empty.
75+ """
76+ # Get the list of files and directories in the directory
77+ working_project_dir = os .path .join (
78+ self ._working_directory , self ._working_project_name
79+ )
80+ if os .path .isdir (working_project_dir ):
81+ items = os .listdir (working_project_dir )
82+ exclude = ["poetry.lock" , "pyproject.toml" ]
83+ items = [item for item in items if item not in exclude ]
84+ return len (items ) == 0
85+ return True
86+
5087 def validate_project_name (self ) -> None :
5188 if os .path .exists (self ._working_project_name ):
5289 message = "A folder with same name exist '{name}' " .format (
5390 name = self ._working_project_name
5491 )
5592 raise EllarCLIException (message )
5693
94+ if not self .is_directory_empty ():
95+ working_project_dir = os .path .join (
96+ self ._working_directory , self ._working_project_name
97+ )
98+ message = (
99+ f"Scaffolding Project Directory is not empty. - { working_project_dir } "
100+ )
101+ raise EllarCLIException (message )
102+
57103 project_working_project_name = self .get_project_name ()
58104 if not project_working_project_name .isidentifier ():
59105 message = (
@@ -64,9 +110,10 @@ def validate_project_name(self) -> None:
64110 raise EllarCLIException (message )
65111
66112 def get_scaffolding_context (self , working_project_name : str ) -> t .Dict :
67- template_context = dict (
68- folder_name = working_project_name , project_name = self .get_project_name ()
69- )
113+ template_context = {
114+ "folder_name" : working_project_name ,
115+ "project_name" : self .get_project_name (),
116+ }
70117 return template_context
71118
72119 def get_project_name (self ) -> str :
@@ -78,10 +125,14 @@ def get_project_cwd(self) -> str:
78125
79126
80127def new_command (
81- folder_name : str ,
82- project_name : t .Optional [str ] = typer .Option (
128+ project_name : str = typer .Argument (
129+ None ,
130+ help = "Project Module Name. Defaults to `project-name` if not set" ,
131+ show_default = False ,
132+ ),
133+ directory : t .Optional [str ] = typer .Argument (
83134 None ,
84- help = "Project Module Name. Defaults to `folder-name` if not set " ,
135+ help = "The name of a new directory to scaffold the project into. Scaffolding into an existing directory is only allowed if the directory is empty " ,
85136 show_default = False ,
86137 ),
87138):
@@ -91,7 +142,7 @@ def new_command(
91142 schema = schema ,
92143 working_directory = os .getcwd (),
93144 scaffold_ellar_template_root_path = root_scaffold_template_path ,
94- working_project_name = folder_name .lower (),
95145 project_name = project_name ,
146+ specified_directory = directory ,
96147 )
97148 init_template_scaffold .scaffold ()
0 commit comments