| 13 | from typing import Optional |
| 14 | |
| 15 | def process(filepath: str, skip_protocols: bool = False, |
| 16 | output_path: Optional[str] = None) -> int: |
| 17 | target = Path(filepath) |
| 18 | if not target.is_file(): |
| 19 | print(f"'{target}' is not a valid file", file=sys.stderr) |
| 20 | return 1 |
| 21 | |
| 22 | macho = lief.MachO.parse(target) |
| 23 | if macho is None: |
| 24 | print(f"Can't parse Mach-O file: {target}", file=sys.stderr) |
| 25 | metadata = macho.at(0).objc_metadata |
| 26 | |
| 27 | if metadata is None: |
| 28 | print(f"Can't parse ObjC metadata in {target}'", file=sys.stderr) |
| 29 | return 1 |
| 30 | |
| 31 | if skip_protocols: |
| 32 | output = "" |
| 33 | for cls in metadata.classes: |
| 34 | output += cls.to_decl() |
| 35 | else: |
| 36 | output = metadata.to_decl() |
| 37 | print(output) |
| 38 | |
| 39 | if output_path is not None: |
| 40 | out = Path(output_path) |
| 41 | if out.is_dir(): |
| 42 | out /= f"{target.name}_objc.h" |
| 43 | out.write_text(output) |
| 44 | print(f"Saved in {out}") |
| 45 | else: |
| 46 | print(f"Saved in {out}") |
| 47 | out.write_text(output) |
| 48 | return 0 |
| 49 | |
| 50 | |
| 51 | def main() -> int: |