import argparse
def updated_version(file_path: str, new_version: int):
version_define = '#define OH_API_VERSION_'
version_current_define = '#define OH_CURRENT_API_VERSION OH_API_VERSION_'
new_content = ''
insert_after_line = '#define SDK_VERSION_9 9'
old_version = 9
for i in range(new_version - old_version):
new_content += '{}{} {}\n'.format(version_define, old_version + i + 1, old_version + i + 1)
new_content_tag = '{}{}\n'.format(version_current_define, new_version)
new_content += new_content_tag
with open(file_path, 'r') as fp:
lines = fp.readlines()
insert_position = None
for i, line in enumerate(lines):
if insert_after_line in line:
insert_position = i + 1
break
if (insert_position is not None) and (new_content_tag not in lines):
lines.insert(insert_position, new_content)
with open(file_path, 'w') as fp:
fp.writelines(lines)
elif insert_position is None:
raise Exception('未找到插入位置: {},写入失败'.format(insert_after_line))
def process_target_version():
try:
parser = argparse.ArgumentParser(description='Updated version')
parser.add_argument('-p', '--path', type=str, required=True, help='Path to the input file')
parser.add_argument('-v', '--version', type=str, required=True, help='Version of the api')
args = parser.parse_args()
input_file = args.path
api_version = int(args.version.split('.')[0])
updated_version(input_file, api_version)
except Exception as e:
raise e
if __name__ == '__main__':
process_target_version()