|
6 | 6 | import argparse |
7 | 7 | import re |
8 | 8 | import glob |
| 9 | +import subprocess |
| 10 | +import yaml |
9 | 11 | from typing import List |
10 | 12 |
|
11 | 13 | from parameters import * |
12 | 14 | from utils import set_or_increase_version |
13 | 15 | from utils import update_service_ci_and_pom |
14 | 16 | from utils import update_root_pom |
15 | 17 | from utils import update_version |
| 18 | +from utils import ListIndentDumper |
16 | 19 |
|
17 | 20 |
|
| 21 | +GROUP_ID = 'com.azure' |
18 | 22 | LLC_ARGUMENTS = '--java --low-level-client --sdk-integration --generate-samples' |
19 | 23 |
|
20 | 24 |
|
@@ -48,8 +52,7 @@ def sdk_automation(config: dict) -> List[dict]: |
48 | 52 | generated_folder = 'sdk/{0}/{1}'.format(service, module) |
49 | 53 |
|
50 | 54 | if succeeded: |
51 | | - install_build_tools(sdk_root) |
52 | | - compile_package(os.path.join(sdk_root, generated_folder)) |
| 55 | + compile_package(sdk_root, GROUP_ID, module) |
53 | 56 |
|
54 | 57 | artifacts = [ |
55 | 58 | '{0}/pom.xml'.format(generated_folder) |
@@ -94,54 +97,116 @@ def generate( |
94 | 97 | shutil.rmtree(os.path.join(output_dir, 'src/samples/java', namespace.replace('.', '/'), 'generated'), |
95 | 98 | ignore_errors=True) |
96 | 99 |
|
97 | | - credential_arguments = '--java.credential-types={0}'.format(credential_types) |
98 | | - if credential_scopes: |
99 | | - credential_arguments += ' --java.credential-scopes={0}'.format(credential_scopes) |
| 100 | + readme_relative_path = update_readme(output_dir, input_file, credential_types, credential_scopes, title) |
| 101 | + if readme_relative_path: |
| 102 | + logging.info('[GENERATE] Autorest from README {}'.format(readme_relative_path)) |
100 | 103 |
|
101 | | - input_arguments = '--input-file={0}'.format(input_file) |
102 | | - |
103 | | - artifact_arguments = '--artifact-id={0}'.format(module) |
104 | | - if title: |
105 | | - artifact_arguments += ' --title={0}'.format(title) |
106 | | - |
107 | | - command = 'autorest --version={0} --use={1} --java.azure-libraries-for-java-folder={2} --java.output-folder={3} --java.namespace={4} {5}'.format( |
108 | | - autorest, |
109 | | - use, |
110 | | - os.path.abspath(sdk_root), |
111 | | - os.path.abspath(output_dir), |
112 | | - namespace, |
113 | | - ' '.join((LLC_ARGUMENTS, input_arguments, credential_arguments, artifact_arguments, autorest_options)), |
114 | | - ) |
115 | | - logging.info(command) |
116 | | - if os.system(command) != 0: |
117 | | - logging.error('[GENERATE] Autorest fail') |
118 | | - return False |
119 | | - |
120 | | - group = "com.azure" |
121 | | - set_or_increase_version(sdk_root, group, module) |
122 | | - update_service_ci_and_pom(sdk_root, service, group, module) |
123 | | - update_root_pom(sdk_root, service) |
124 | | - update_version(sdk_root, output_dir) |
| 104 | + command = 'autorest --version={0} --use={1} {2}'.format( |
| 105 | + autorest, |
| 106 | + use, |
| 107 | + readme_relative_path |
| 108 | + ) |
| 109 | + logging.info(command) |
| 110 | + try: |
| 111 | + subprocess.run(command, shell=True, cwd=output_dir, check=True) |
| 112 | + except subprocess.CalledProcessError: |
| 113 | + logging.error('[GENERATE] Autorest fail') |
| 114 | + return False |
| 115 | + else: |
| 116 | + logging.info('[GENERATE] Autorest from JSON {}'.format(input_file)) |
| 117 | + |
| 118 | + credential_arguments = '--java.credential-types={0}'.format(credential_types) |
| 119 | + if credential_scopes: |
| 120 | + credential_arguments += ' --java.credential-scopes={0}'.format(credential_scopes) |
| 121 | + |
| 122 | + input_arguments = '--input-file={0}'.format(input_file) |
| 123 | + |
| 124 | + artifact_arguments = '--artifact-id={0}'.format(module) |
| 125 | + if title: |
| 126 | + artifact_arguments += ' --title={0}'.format(title) |
| 127 | + |
| 128 | + command = 'autorest --version={0} --use={1} ' \ |
| 129 | + '--java.azure-libraries-for-java-folder={2} --java.output-folder={3} ' \ |
| 130 | + '--java.namespace={4} {5}'\ |
| 131 | + .format( |
| 132 | + autorest, |
| 133 | + use, |
| 134 | + os.path.abspath(sdk_root), |
| 135 | + os.path.abspath(output_dir), |
| 136 | + namespace, |
| 137 | + ' '.join((LLC_ARGUMENTS, input_arguments, credential_arguments, artifact_arguments, autorest_options)) |
| 138 | + ) |
| 139 | + logging.info(command) |
| 140 | + if os.system(command) != 0: |
| 141 | + logging.error('[GENERATE] Autorest fail') |
| 142 | + return False |
| 143 | + |
| 144 | + set_or_increase_version(sdk_root, GROUP_ID, module) |
| 145 | + update_service_ci_and_pom(sdk_root, service, GROUP_ID, module) |
| 146 | + update_root_pom(sdk_root, service) |
| 147 | + update_version(sdk_root, output_dir) |
125 | 148 |
|
126 | 149 | return True |
127 | 150 |
|
128 | 151 |
|
129 | | -def install_build_tools(sdk_root: str): |
130 | | - command = 'mvn --no-transfer-progress clean install -f {0} -pl com.azure:sdk-build-tools'.format(os.path.join(sdk_root, 'pom.xml')) |
| 152 | +def compile_package(sdk_root: str, group_id: str, module: str): |
| 153 | + command = 'mvn --no-transfer-progress clean verify package -f {0}/pom.xml -pl {1}:{2} -am'.format( |
| 154 | + sdk_root, group_id, module) |
131 | 155 | logging.info(command) |
132 | 156 | if os.system(command) != 0: |
133 | | - logging.error('[COMPILE] Maven build fail for sdk-build-tools') |
| 157 | + logging.error('[COMPILE] Maven build fail') |
134 | 158 | return False |
135 | 159 | return True |
136 | 160 |
|
137 | 161 |
|
138 | | -def compile_package(output_dir: str): |
139 | | - command = 'mvn --no-transfer-progress clean verify package -f {0}'.format(os.path.join(output_dir, 'pom.xml')) |
140 | | - logging.info(command) |
141 | | - if os.system(command) != 0: |
142 | | - logging.error('[COMPILE] Maven build fail') |
143 | | - return False |
144 | | - return True |
| 162 | +def update_readme(output_dir: str, input_file: str, credential_types: str, credential_scopes: str, title: str) -> str: |
| 163 | + readme_relative_path = '' |
| 164 | + |
| 165 | + swagger_dir = os.path.join(output_dir, 'swagger') |
| 166 | + if os.path.isdir(swagger_dir): |
| 167 | + for filename in os.listdir(swagger_dir): |
| 168 | + if filename.lower().startswith('readme') and filename.lower().endswith('.md'): |
| 169 | + readme_yaml_found = False |
| 170 | + readme_path = os.path.join(swagger_dir, filename) |
| 171 | + with open(readme_path, 'r', encoding='utf-8') as f_in: |
| 172 | + content = f_in.read() |
| 173 | + if content: |
| 174 | + yaml_blocks = re.findall(r'```\s?(?:yaml|YAML)\n(.*?)```', content, re.DOTALL) |
| 175 | + for yaml_str in yaml_blocks: |
| 176 | + yaml_json = yaml.safe_load(yaml_str) |
| 177 | + if 'low-level-client' in yaml_json and yaml_json['low-level-client']: |
| 178 | + # yaml block found, update |
| 179 | + yaml_json['input-file'] = [input_file] |
| 180 | + if title: |
| 181 | + yaml_json['title'] = title |
| 182 | + if credential_types: |
| 183 | + yaml_json['credential-types'] = credential_types |
| 184 | + if credential_scopes: |
| 185 | + yaml_json['credential-scopes'] = credential_scopes |
| 186 | + |
| 187 | + # write updated yaml |
| 188 | + updated_yaml_str = yaml.dump(yaml_json, |
| 189 | + sort_keys=False, |
| 190 | + Dumper=ListIndentDumper) |
| 191 | + |
| 192 | + if not yaml_str == updated_yaml_str: |
| 193 | + # update readme |
| 194 | + updated_content = content.replace(yaml_str, updated_yaml_str, 1) |
| 195 | + with open(readme_path, 'w', encoding='utf-8') as f_out: |
| 196 | + f_out.write(updated_content) |
| 197 | + |
| 198 | + logging.info('[GENERATE] YAML block in README updated from\n{0}\nto\n{1}'.format( |
| 199 | + yaml_str, updated_yaml_str |
| 200 | + )) |
| 201 | + |
| 202 | + readme_yaml_found = True |
| 203 | + break |
| 204 | + |
| 205 | + if readme_yaml_found: |
| 206 | + readme_relative_path = 'swagger/{}'.format(filename) |
| 207 | + break |
| 208 | + |
| 209 | + return readme_relative_path |
145 | 210 |
|
146 | 211 |
|
147 | 212 | def parse_args() -> argparse.Namespace: |
@@ -206,14 +271,9 @@ def main(): |
206 | 271 | base_dir = os.path.abspath(os.path.dirname(sys.argv[0])) |
207 | 272 | sdk_root = os.path.abspath(os.path.join(base_dir, SDK_ROOT)) |
208 | 273 |
|
209 | | - generate(sdk_root, **args) |
210 | | - |
211 | | - output_dir = os.path.join( |
212 | | - sdk_root, |
213 | | - 'sdk', args['service'], args['module'] |
214 | | - ) |
215 | | - install_build_tools(sdk_root) |
216 | | - compile_package(output_dir) |
| 274 | + succeeded = generate(sdk_root, **args) |
| 275 | + if succeeded: |
| 276 | + compile_package(sdk_root, GROUP_ID, args['module']) |
217 | 277 |
|
218 | 278 |
|
219 | 279 | if __name__ == '__main__': |
|
0 commit comments