|
| 1 | +# Using output_header_mapping.json convert all imports in specified source folder location from godot imports to godot-compat imports |
| 2 | + |
| 3 | + |
| 4 | +import json |
| 5 | +import os |
| 6 | +import sys |
| 7 | + |
| 8 | +from compat_generator import map_header_files |
| 9 | +from header_matcher import match_headers |
| 10 | + |
| 11 | +if __name__ == "__main__": |
| 12 | + if len(sys.argv) > 2: |
| 13 | + current_directory = os.path.join(os.getcwd(), sys.argv[1]) |
| 14 | + godot_cpp_directory = os.path.join(os.getcwd(), sys.argv[2]) |
| 15 | + # Load the godot mappings |
| 16 | + with open(f"{godot_cpp_directory}/output_header_mapping.json", "r") as file: |
| 17 | + godot_mappings = json.load(file) |
| 18 | + |
| 19 | + # Generate mappings for godot-cpp |
| 20 | + godot_cpp_mappings = map_header_files(godot_cpp_directory) |
| 21 | + matches = match_headers(godot_mappings, godot_cpp_mappings) |
| 22 | + # Save matches to a file |
| 23 | + with open("header_matches.json", "w") as outfile: |
| 24 | + json.dump(matches, outfile, indent=4) |
| 25 | + current_directory = os.getcwd() |
| 26 | + # Go through folder specified through all files with .cpp, .h or .hpp |
| 27 | + for root, dirs, files in os.walk(current_directory): |
| 28 | + for file in files: |
| 29 | + if file.endswith(".cpp") or file.endswith(".h") or file.endswith(".hpp"): |
| 30 | + with open(os.path.join(root, file), "r") as f: |
| 31 | + content = f.read() |
| 32 | + |
| 33 | + # Replace imports to godot imports with godot_compat imports |
| 34 | + for match in matches: |
| 35 | + generate_imports = matches[match] |
| 36 | + godot_compat_imports = "" |
| 37 | + for generate_import in generate_imports: |
| 38 | + godot_compat_import = generate_import.replace("gen/include/godot_cpp/", "godot_compat/") |
| 39 | + godot_compat_import = godot_compat_import.replace("include/godot_cpp/", "godot_compat/") |
| 40 | + godot_compat_imports += f"#include <{godot_compat_import}>\n" |
| 41 | + # Remove last 'n from imports |
| 42 | + godot_compat_imports = godot_compat_imports[:-1] |
| 43 | + content = content.replace(f"#include \"{match}\"", godot_compat_imports) |
| 44 | + # Write the modified content back to the file |
| 45 | + with open(os.path.join(root, file), "w") as f: |
| 46 | + f.write(content) |
0 commit comments