From 078ce2f5b882ee04f4060c4e4270b768ebca3718 Mon Sep 17 00:00:00 2001 From: James Westman Date: Fri, 23 Aug 2024 18:29:34 -0500 Subject: [PATCH] cli: Add decompile command This command converts .ui files to Blueprint, similar to the porting tool, but on individual files. --- blueprintcompiler/main.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/blueprintcompiler/main.py b/blueprintcompiler/main.py index 5b4cad8..1c3a1c6 100644 --- a/blueprintcompiler/main.py +++ b/blueprintcompiler/main.py @@ -25,6 +25,7 @@ import sys import typing as T from . import formatter, interactive_port, parser, tokenizer +from .decompiler import decompile_string from .errors import CompileError, CompilerBugError, PrintableError, report_bug from .gir import add_typelib_search_path from .lsp import LanguageServer @@ -103,6 +104,15 @@ class BlueprintApp: metavar="filenames", ) + decompile = self.add_subcommand( + "decompile", "Convert .ui XML files to blueprint", self.cmd_decompile + ) + decompile.add_argument("--output", dest="output", default="-") + decompile.add_argument("--typelib-path", nargs="?", action="append") + decompile.add_argument( + "input", metavar="filename", default=sys.stdin, type=argparse.FileType("r") + ) + port = self.add_subcommand("port", "Interactive porting tool", self.cmd_port) lsp = self.add_subcommand( @@ -300,6 +310,24 @@ class BlueprintApp: if panic: sys.exit(1) + def cmd_decompile(self, opts): + if opts.typelib_path != None: + for typelib_path in opts.typelib_path: + add_typelib_search_path(typelib_path) + + data = opts.input.read() + try: + decompiled = decompile_string(data) + + if opts.output == "-": + print(decompiled) + else: + with open(opts.output, "w") as file: + file.write(decompiled) + except PrintableError as e: + e.pretty_print(opts.input.name, data, stream=sys.stderr) + sys.exit(1) + def cmd_lsp(self, opts): langserv = LanguageServer() langserv.run()