6
6
7
7
from azure_devtools .ci_tools .git_tools import get_add_diff_file_list
8
8
from pathlib import Path
9
- from subprocess import check_call
9
+ from subprocess import check_call , getoutput
10
10
from typing import List , Dict , Any
11
11
from glob import glob
12
12
import yaml
@@ -52,26 +52,32 @@ def init_new_service(package_name, folder_name):
52
52
53
53
def update_servicemetadata (sdk_folder , data , config , folder_name , package_name , spec_folder , input_readme ):
54
54
55
- readme_file = str (Path (spec_folder , input_readme ))
56
- global_conf = config ["meta" ]
57
- local_conf = config .get ("projects" , {}).get (readme_file , {})
58
-
59
- if "resource-manager" in input_readme :
60
- cmd = ["autorest" , input_readme ]
61
- else :
62
- # autorest for DPG will be executed in package folder like: sdk/deviceupdate/azure-iot-deviceupdate/swagger
63
- cmd = ["autorest" , _DPG_README ]
64
- cmd += build_autorest_options (global_conf , local_conf )
65
-
66
- # metadata
67
55
metadata = {
68
- "autorest" : global_conf ["autorest_options" ]["version" ],
69
- "use" : global_conf ["autorest_options" ]["use" ],
70
56
"commit" : data ["headSha" ],
71
57
"repository_url" : data ["repoHttpsUrl" ],
72
- "autorest_command" : " " .join (cmd ),
73
- "readme" : input_readme ,
74
58
}
59
+ if "meta" in config :
60
+ readme_file = str (Path (spec_folder , input_readme ))
61
+ global_conf = config ["meta" ]
62
+ local_conf = config .get ("projects" , {}).get (readme_file , {})
63
+
64
+ if "resource-manager" in input_readme :
65
+ cmd = ["autorest" , input_readme ]
66
+ else :
67
+ # autorest for DPG will be executed in package folder like: sdk/deviceupdate/azure-iot-deviceupdate/swagger
68
+ cmd = ["autorest" , _DPG_README ]
69
+ cmd += build_autorest_options (global_conf , local_conf )
70
+
71
+ # metadata
72
+ metadata .update ({
73
+ "autorest" : global_conf ["autorest_options" ]["version" ],
74
+ "use" : global_conf ["autorest_options" ]["use" ],
75
+ "autorest_command" : " " .join (cmd ),
76
+ "readme" : input_readme ,
77
+ })
78
+ else :
79
+ metadata ["cadl_src" ] = input_readme
80
+ metadata .update (config )
75
81
76
82
_LOGGER .info ("Metadata json:\n {}" .format (json .dumps (metadata , indent = 2 )))
77
83
@@ -322,3 +328,40 @@ def format_samples(sdk_code_path) -> None:
322
328
fw .write (file_content )
323
329
324
330
_LOGGER .info (f"format generated_samples successfully" )
331
+
332
+ def gen_cadl (cadl_relative_path : str , spec_folder : str ) -> Dict [str , Any ]:
333
+ # update config file
334
+ cadl_python = "@azure-tools/cadl-python"
335
+ project_yaml_path = Path (spec_folder ) / cadl_relative_path / "cadl-project.yaml"
336
+ with open (project_yaml_path , "r" ) as file_in :
337
+ project_yaml = yaml .safe_load (file_in )
338
+ if not project_yaml .get ("emitters" , {}).get (cadl_python ):
339
+ return
340
+ if not project_yaml ["emitters" ][cadl_python ].get ("sdk-folder" ):
341
+ raise Exception ("no sdk-folder is defined" )
342
+ output_path = Path (os .getcwd ()) / project_yaml ["emitters" ][cadl_python ]["sdk-folder" ]
343
+ if not output_path .exists ():
344
+ os .makedirs (output_path )
345
+
346
+ project_yaml ["emitters" ][cadl_python ].pop ("sdk-folder" )
347
+ project_yaml ["emitters" ][cadl_python ]["output-path" ] = str (output_path )
348
+ with open (project_yaml_path , "w" ) as file_out :
349
+ yaml .safe_dump (project_yaml , file_out )
350
+
351
+ # npm install tool
352
+ origin_path = os .getcwd ()
353
+ os .chdir (Path (spec_folder ) / cadl_relative_path )
354
+ check_call ("npm install" , shell = True )
355
+
356
+ # generate code
357
+ check_call (f"npx cadl compile . --emit { cadl_python } " , shell = True )
358
+ if Path (output_path / "output.yaml" ).exists ():
359
+ os .remove (Path (output_path / "output.yaml" ))
360
+
361
+ # get version of @azure-tools/cadl-python used in generation
362
+ cadl_python_version = getoutput (f"npm view { cadl_python } version" ).split ('\n ' )[- 1 ]
363
+
364
+ # return to original folder
365
+ os .chdir (origin_path )
366
+
367
+ return {cadl_python : cadl_python_version }
0 commit comments