77from InquirerPy import inquirer # type: ignore
88from InquirerPy .validator import EmptyInputValidator # type: ignore
99from click import Command , Option
10+ from importlib_resources import files # type: ignore
1011from yaspin import yaspin # type: ignore
1112
1213from ..core .copy_files import copy_files # type: ignore
1617from .custom_group import CustomGroup
1718from .menu import Menus
1819
20+ dicts = dict [str , Any ]
21+
1922
2023class Builder (CustomGroup ):
2124
@@ -31,9 +34,9 @@ def create(self) -> Command:
3134 def callback (network : bool = False ) -> None :
3235 clear (0 )
3336
34- services : set [dict [ str , Any ] ] = set ([])
37+ services : set [dicts ] = set ([])
3538 networks : set [str ] = set ([])
36- envs : set [dict [ str , Any ] ] = set ([])
39+ envs : set [dicts ] = set ([])
3740
3841 if not network :
3942 menu = Menus ()
@@ -96,18 +99,18 @@ def callback(
9699 ) -> None :
97100 clear (0 )
98101
99- path : Path = Path ( )
102+ path : Path = self . cwd . joinpath ( "data.json" )
100103
101104 if not path .exists ():
102105 print ("Missing JSON file for services. Use 'create' first." )
103106 return
104107
105- data : dict [ str , Any ] = read_json (path ) or {}
106- compose : dict [ str , Any ] = data .get ("compose" , {}) or {}
108+ data : dicts = read_json (path ) or {}
109+ compose : dicts = data .get ("compose" , {}) or {}
107110
108- services : set [dict [ str , Any ] ] = set (compose .get ("services" , []))
111+ services : set [dicts ] = set (compose .get ("services" , []))
109112 networks : set [str ] = set (compose .get ("networks" , []))
110- envs : set [dict [ str , Any ] ] = set (data .get ("envs" , []))
113+ envs : set [dicts ] = set (data .get ("envs" , []))
111114
112115 if networks :
113116 pass
@@ -126,21 +129,21 @@ def callback(
126129 )
127130
128131 def build (self ) -> Command :
129- help = "Build the files for the containerization in case create failed ."
132+ help = "Build the files for the containerization."
130133 options : list [Option ] = []
131134
132135 def callback (
133136 service : str | None = None , add : bool = False , remove : bool = False
134137 ) -> None :
135138 clear (0 )
136139
137- path : Path = Path ( )
140+ path : Path = self . cwd . joinpath ( "data.json" )
138141
139142 if not path .exists ():
140143 print ("Missing JSON file for services. Use 'create' first." )
141144 return
142145
143- data : dict [ str , Any ] = read_json (Path () ) or {}
146+ data : dicts = read_json (path ) or {}
144147
145148 if not data :
146149 print ("JSON file is empty. Use 'create' first." )
@@ -159,7 +162,7 @@ def callback(
159162
160163 def __get_data (
161164 self , menu : Menus , get_service : bool = True , get_env : bool = True
162- ) -> tuple [dict [ str , Any ], dict [ str , Any ] ]:
165+ ) -> tuple [dicts , dicts ]:
163166 clear (0.5 )
164167
165168 name = self .__get_name (message = "Enter the name of the service: " )
@@ -187,14 +190,26 @@ def __get_name(self, message: str) -> str:
187190 return name
188191
189192 @yaspin (text = "Creating files..." , color = "cyan" )
190- def __save_files (self , data : dict [str , Any ], build : bool = False ) -> None :
193+ def __save_files (self , data : dicts , build : bool = False ) -> None :
194+ tmps_path = files ("minecraft-docker-cli.assets.templates" )
195+ composer_template = tmps_path .joinpath ("docker-compose.yml.j2" )
196+ env_template = tmps_path .joinpath (".env.j2" )
197+
191198 if not build :
192- write_json (Path ( ), data )
199+ write_json (self . cwd . joinpath ( "data.json" ), data )
193200
194- composer : dict [str , Any ] = data .get ("composer" ) or {}
195- template_to_file (Path (), composer , Path ())
201+ composer : dicts = data .get ("composer" ) or {}
202+ template_to_file (
203+ composer_template , composer , self .cwd .joinpath ("docker-compose.yml" )
204+ )
196205
197- envs : list [dict [str , Any ]] = data .get ("envs" ) or []
206+ services : list [dicts ] = composer .get ("services" , []) or []
207+ names : list [str ] = [service .get ("name" ) for service in services ] # type: ignore
208+ copy_files (self .cwd , names )
209+
210+ envs : list [dicts ] = data .get ("envs" ) or []
198211 for env in envs :
199212 relative_path = f"servers/{ env .get ("CONTAINER_NAME" )} /.env" # type: ignore
200- template_to_file (Path (), env , Path ())
213+ template_to_file (
214+ env_template , env , self .cwd .joinpath (relative_path )
215+ )
0 commit comments