in scripts/sync_nullability_annotations.py [0:0]
def main():
base_dir = determine_base_dir()
os.chdir(base_dir)
all_h_files = get_files_with_extension("*.h")
all_m_files = get_files_with_extension("*.m")
print(f"Found {len(all_h_files)} '*.h' files and {len(all_m_files)} '*.m' files")
for h_file_path in all_h_files:
# key: method name with newlines and nullable removed
# value: the original method
method_map = {}
m_file_path = find_m_file_for_h_file(h_file_path, all_m_files)
if not m_file_path:
# Many .h files won't have a corresponding .m file, example bridging header
continue
if m_file_path in all_m_files:
all_m_files.remove(m_file_path)
else:
print(f"Path exists: {m_file_path} but was not found in 'all_m_files'")
h_file_text = read_text_from_file(h_file_path)
m_file_text = read_text_from_file(m_file_path)
# For each method definition in the .h file, add it to the method_map
for match in re.finditer(
r"^([+-].*?);$", h_file_text, flags=re.MULTILINE | re.DOTALL
):
method_declaration = match.group(1)
method_declaration = re.sub(
r"\n(NS_SWIFT_NAME|NS_SWIFT_UNAVAILABLE).*", "", method_declaration
)
key_for_method = key_for_method_declaration(method_declaration)
if method_declaration != key_for_method:
method_map[key_for_method] = method_declaration
# For each method definition in the .m file, replace it from the method_map
updated_m_file_text = m_file_text
for match in re.finditer(
r"^([+-].*?)\n{$", m_file_text, flags=re.MULTILINE | re.DOTALL
):
method_declaration = match.group(1)
key_for_method = key_for_method_declaration(method_declaration)
if key_for_method in method_map:
method_definition_from_header = method_map[key_for_method]
updated_m_file_text = updated_m_file_text.replace(
method_declaration, method_definition_from_header
)
if updated_m_file_text != m_file_text:
write_text_to_file(updated_m_file_text, m_file_path)