From 5c0fd46ebecb44960a8b419d8b513f2b76cd4630 Mon Sep 17 00:00:00 2001 From: James Westman Date: Wed, 24 Nov 2021 21:29:18 -0600 Subject: [PATCH] Add input directory argument to batch-compile This allows it to maintain the directory structure of the input files. --- docs/setup.rst | 2 +- gtkblueprinttool/main.py | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/docs/setup.rst b/docs/setup.rst index dcf0c2d..a742133 100644 --- a/docs/setup.rst +++ b/docs/setup.rst @@ -35,7 +35,7 @@ gtk-blueprint-tool works as a meson subproject. # LIST YOUR BLUEPRINT FILES HERE ), output: '.', - command: [find_program('gtk-blueprint-tool'), 'batch-compile', '@OUTPUT@', '@INPUT@'], + command: [find_program('gtk-blueprint-tool'), 'batch-compile', '@OUTPUT@', '@CURRENT_SOURCE_DIR@', '@INPUT@'], ) #. In the same ``meson.build`` file, add this argument to your ``gnome.compile_resources`` command: diff --git a/gtkblueprinttool/main.py b/gtkblueprinttool/main.py index 0fe632b..9e73ecd 100644 --- a/gtkblueprinttool/main.py +++ b/gtkblueprinttool/main.py @@ -41,6 +41,7 @@ class BlueprintApp: batch_compile = self.add_subcommand("batch-compile", "Compile many blueprint files at once", self.cmd_batch_compile) batch_compile.add_argument("output_dir", metavar="output-dir") + batch_compile.add_argument("input_dir", metavar="input-dir") batch_compile.add_argument("inputs", nargs="+", metavar="filenames", default=sys.stdin, type=argparse.FileType('r')) lsp = self.add_subcommand("lsp", "Run the language server (for internal use by IDEs)", self.cmd_lsp) @@ -87,10 +88,21 @@ class BlueprintApp: data = file.read() try: + if not os.path.commonpath([file.name, opts.input_dir]): + print(f"{Colors.RED}{Colors.BOLD}error: input file '{file.name}' is not in input directory '{opts.input_dir}'{Colors.CLEAR}") + sys.exit(1) + xml = self._compile(data) - name = os.path.splitext(os.path.basename(file.name))[0] + ".ui" - with open(os.path.join(opts.output_dir, name), "w") as file: + path = os.path.join( + opts.output_dir, + os.path.relpath( + os.path.splitext(file.name)[0] + ".ui", + opts.input_dir + ) + ) + os.makedirs(os.path.dirname(path), exist_ok=True) + with open(path, "w") as file: file.write(xml) except PrintableError as e: e.pretty_print(file.name, data)