1717from ch_cli_tools .manifest import get_manifest
1818
1919from . import HERE
20- from .utils import copymergedir , replaceindir , to_python_module
20+ from .utils import confirm , copymergedir , replace_in_file , replaceindir , to_python_module
2121
2222CODEGEN = os .path .join (HERE , 'bin' , 'openapi-generator-cli.jar' )
2323APPLICATIONS_SRC_PATH = os .path .join ('applications' )
@@ -129,33 +129,60 @@ def generate_python_client(module, openapi_file, client_src_path, lib_name=LIB_N
129129 os .system (command )
130130
131131
132- def generate_ts_client (openapi_file ):
132+ def generate_ts_client (openapi_file , app_name = "" ):
133133 get_dependencies ()
134- out_dir = f"{ os .path .dirname (os .path .dirname (openapi_file ))} /frontend/src/rest"
134+ out_dir = f"{ os .path .dirname (os .path .dirname (openapi_file ))} /frontend/src/rest/ { app_name } "
135135 command = f"java -jar { CODEGEN } generate " \
136136 f"-i { openapi_file } " \
137137 f"-g typescript-fetch " \
138- f"-o { out_dir } "
138+ f"-o { out_dir } " \
139+ f"--additional-properties=prefixParameterInterfaces=false"
139140 os .system (command )
140141
141142 replaceindir (out_dir , "http://localhost" , '' )
142143
143144
145+ def json2yaml (json_filename , yaml_file = None ):
146+ import yaml
147+ if yaml_file is None :
148+ yaml_file = str (json_filename ).replace ('.json' , '.yaml' )
149+ with open (json_filename , 'r' ) as json_filename :
150+ data = json .load (json_filename )
151+ with open (yaml_file , 'w' ) as yaml_file :
152+ yaml .dump (data , yaml_file )
153+
154+
144155def generate_openapi_from_ninja_schema (app_name : str , app_path : pathlib .Path ) -> None :
145- subprocess .check_call (["sh" , "dev-setup.sh" ], cwd = app_path )
146- out_path = app_path / 'api' / 'openapi.yaml'
156+ # check if cloudharness_django python library is installed
157+ python_module = to_python_module (app_name )
158+ try :
159+ import cloudharness_django
160+ # dynamicall import python_module
161+ __import__ (f'{ python_module } ' )
162+ except ImportError :
163+ if confirm ('Runtime env is not installed. Do you want to install it? [Y/n]' ):
164+ subprocess .check_call (["sh" , "dev-setup.sh" ], cwd = app_path )
165+ else :
166+ logging .error ('Runtime env is not installed. Cound not generate openapi files for Django Ninja.' )
167+ return
168+ logging .info (f"Generating openapi files for Django Ninja for application { app_name } " )
169+ out_path = app_path / 'api' / 'openapi.json'
147170
148171 manage_path = app_path / 'backend' / 'manage.py'
149172 command = [
150173 'python' , manage_path , 'export_openapi_schema' ,
151174 '--settings' , 'django_baseapp.settings' ,
152- '--api' , f'{ to_python_module ( app_name ) } .api.api' ,
175+ '--api' , f'{ python_module } .api.api' ,
153176 '--output' , out_path ,
154177 '--indent' , '2' ,
155178 ]
156179
157180 subprocess .run (command )
158181
182+ replace_in_file (out_path , f'{ app_name } _api_' , '' )
183+
184+ json2yaml (out_path )
185+
159186
160187def get_dependencies ():
161188 """
@@ -235,6 +262,8 @@ def generate_clients(
235262 if not should_generate ('client libraries' ):
236263 return
237264
265+ logging .info ('Generating client libraries for %s' , str (client_types ))
266+
238267 client_src_path = root_path / 'libraries' / 'client' / client_lib_name
239268 apps_path = root_path / 'applications'
240269 apps = (app for app in apps_path .iterdir () if app .is_dir ())
@@ -253,7 +282,7 @@ def generate_clients(
253282 generate_python_client (manifest .app_name , openapi_file , client_src_path , lib_name = client_lib_name )
254283
255284 if TemplateType .WEBAPP in manifest .templates and ClientType .TS_CLIENT in client_types :
256- generate_ts_client (openapi_file )
285+ generate_ts_client (openapi_file , app_name )
257286
258287 aggregate_packages (client_src_path , client_lib_name )
259288
0 commit comments