Skip to content

Commit

Permalink
generator.py: Use cargo run instead of guessing path
Browse files Browse the repository at this point in the history
Currently, the script just guesses a target path based on the default config.
This fails if the project or the user has configured a different target
directory.

This code is shorter and more correct.
  • Loading branch information
sophie-h committed Sep 5, 2024
1 parent 5c4134d commit c6ba79e
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

DEFAULT_GIR_FILES_DIRECTORY = Path("./gir-files")
DEFAULT_GIR_DIRECTORY = Path("./gir/")
DEFAULT_GIR_PATH = DEFAULT_GIR_DIRECTORY / "target/release/gir"


def run_command(command, folder=None):
Expand All @@ -30,8 +29,13 @@ async def spawn_process(exe, args):
return stdout, stderr


async def spawn_gir(gir_exe, args):
stdout, stderr = await spawn_process(gir_exe, args)
async def spawn_gir(conf, args):
if conf.gir_path:
stdout, stderr = await spawn_process(conf.gir_path, args)
else:
cargo_args = ["run", "--release", "--quiet", "--manifest-path", DEFAULT_GIR_DIRECTORY / "Cargo.toml", "--"]
stdout, stderr = await spawn_process("cargo", cargo_args + args)

# Gir doesn't print anything to stdout. If it does, this is likely out of
# order with stderr, unless the printer/logging flushes in between.
assert not stdout, "`gir` printed unexpected stdout: {}".format(stdout)
Expand All @@ -40,10 +44,6 @@ async def spawn_gir(gir_exe, args):
return ""


def update_workspace():
return run_command(["cargo", "build", "--release"], "gir")


def ask_yes_no_question(question, conf):
question = "{} [y/N] ".format(question)
if conf.yes:
Expand Down Expand Up @@ -71,12 +71,6 @@ def update_submodule(submodule_path, conf):
return False


def build_gir():
print("=> Building gir...")
update_workspace()
print("<= Done!")


async def regenerate_crate_docs(conf, crate_dir, base_gir_args):
doc_path = "docs.md"
# Generate into docs.md instead of the default vendor.md
Expand All @@ -99,7 +93,7 @@ async def regenerate_crate_docs(conf, crate_dir, base_gir_args):
logs += "==> Regenerating documentation for `{}` into `{}`...\n".format(
crate_dir, doc_path
)
logs += await spawn_gir(conf.gir_path, doc_args)
logs += await spawn_gir(conf, doc_args)

logs += "==> Embedding documentation from `{}` into `{}`...\n".format(
doc_path, crate_dir
Expand Down Expand Up @@ -139,7 +133,7 @@ def regen_crates(path, conf):

async def regenerate_crate(path, args):
return "==> Regenerating `{}`...\n".format(path) + await spawn_gir(
conf.gir_path, args
conf, args
)

processes.append(regenerate_crate(path, args))
Expand Down Expand Up @@ -194,7 +188,6 @@ def parse_args():
)
parser.add_argument(
"--gir-path",
default=DEFAULT_GIR_PATH,
type=file_path,
help="Path of the gir executable to run",
)
Expand Down Expand Up @@ -228,9 +221,8 @@ async def main():
if not conf.gir_files_paths:
update_submodule(DEFAULT_GIR_FILES_DIRECTORY, conf)

if conf.gir_path == DEFAULT_GIR_PATH:
if conf.gir_path is None:
update_submodule(DEFAULT_GIR_DIRECTORY, conf)
build_gir()

print("=> Regenerating crates...")
for path in conf.path:
Expand Down

0 comments on commit c6ba79e

Please sign in to comment.