def main(): args = vars(parse_args()) if args.get('config'): return sdk_automation(args['config'][0], args['config'][1]) base_dir = os.path.abspath(os.path.dirname(sys.argv[0])) sdk_root = os.path.abspath(os.path.join(base_dir, SDK_ROOT)) api_specs_file = os.path.join(base_dir, API_SPECS_FILE) readme = args['readme'] match = re.match( 'specification/([^/]+)/resource-manager/readme.md', readme, re.IGNORECASE, ) if not match: spec = readme readme = 'specification/{0}/resource-manager/readme.md'.format(spec) else: spec = match.group(1) args['readme'] = readme args['spec'] = spec # update_parameters( # args.get('suffix') or get_suffix_from_api_specs(api_specs_file, spec)) update_parameters(args.get('suffix')) service = get_and_update_service_from_api_specs(api_specs_file, spec, args['service']) args['service'] = service module = ARTIFACT_FORMAT.format(service) stable_version, current_version = set_or_increase_version( sdk_root, GROUP_ID, module, **args) args['version'] = current_version succeeded = generate(sdk_root, **args) if succeeded: succeeded = compile_package(sdk_root, service) if succeeded: compare_with_maven_package(sdk_root, service, stable_version, current_version) if args.get('auto_commit_external_change') and args.get( 'user_name') and args.get('user_email'): pwd = os.getcwd() try: os.chdir(sdk_root) os.system( 'git add eng/versioning eng/mgmt pom.xml {0} {1}'. format(CI_FILE_FORMAT.format(service), POM_FILE_FORMAT.format(service))) os.system( 'git -c user.name={0} -c user.email={1} commit -m "[Automation] External Change"' .format(args['user_name'], args['user_email'])) finally: os.chdir(pwd) if not succeeded: raise RuntimeError('Failed to generate code or compile the package')
def sdk_automation(input_file: str, output_file: str): base_dir = os.path.abspath(os.path.dirname(sys.argv[0])) sdk_root = os.path.abspath(os.path.join(base_dir, SDK_ROOT)) api_specs_file = os.path.join(base_dir, API_SPECS_FILE) with open(input_file, 'r') as fin: config = json.load(fin) packages = [] for readme in config['relatedReadmeMdFiles']: match = re.search( 'specification/([^/]+)/resource-manager/readme.md', readme, re.IGNORECASE, ) if not match: logging.info( '[Skip] readme path does not format as specification/*/resource-manager/readme.md' ) else: spec = match.group(1) service = get_and_update_service_from_api_specs( api_specs_file, spec) pre_suffix = SUFFIX suffix = get_suffix_from_api_specs(api_specs_file, spec) update_parameters(suffix) # TODO: use specific function to detect tag in "resources" tag = None if service == 'resources': with open(os.path.join(config['specFolder'], readme)) as fin: tag_match = re.search('tag: (package-resources-[\S]+)', fin.read()) if tag_match: tag = tag_match.group(1) else: tag = 'package-resources-2020-10' module = ARTIFACT_FORMAT.format(service) stable_version, current_version = set_or_increase_version( sdk_root, GROUP_ID, module ) succeeded = generate( sdk_root, service, spec_root = config['specFolder'], readme = readme, autorest = AUTOREST_CORE_VERSION, use = AUTOREST_JAVA, tag = tag, ) if succeeded: compile_package(sdk_root, service) generated_folder = OUTPUT_FOLDER_FORMAT.format(service) packages.append({ 'packageName': '{0}'.format(ARTIFACT_FORMAT.format(service)), 'path': [ generated_folder, CI_FILE_FORMAT.format(service), POM_FILE_FORMAT.format(service), 'eng/versioning', 'pom.xml', ], 'readmeMd': [readme], 'artifacts': [ '{0}/pom.xml'.format(generated_folder), ] + [ jar for jar in glob.glob('{0}/target/*.jar'.format( generated_folder)) ], 'result': 'succeeded' if succeeded else 'failed', }) update_parameters(pre_suffix) if not packages: # try data-plane codegen packages = sdk_automation_data(config) with open(output_file, 'w') as fout: output = { 'packages': packages, } json.dump(output, fout)