Skip to content

Instantly share code, notes, and snippets.

@vlOd2
Created December 6, 2025 15:36
Show Gist options
  • Select an option

  • Save vlOd2/bd172d5e1fa1b01c4b80c9fa82cab9d9 to your computer and use it in GitHub Desktop.

Select an option

Save vlOd2/bd172d5e1fa1b01c4b80c9fa82cab9d9 to your computer and use it in GitHub Desktop.
Wrapper for dotnet build intended for Kate
#!/bin/python3
import argparse
import subprocess
import os
import os.path
import re
import sys
PROJECT_PATH_PATTERN = re.compile(r"^(.+?) -> (.+)$")
def print_cmdline(l: list[str]) -> list[str]:
print(f"Running process: {" ".join(l)}")
return l
def build(proj_name: str, proj_dir: str, args: argparse.Namespace) -> tuple[str, str] | None:
proj_out_path: str | None = None
proj_out_dir: str | None = None
build_env = os.environ.copy()
build_env["DOTNET_CONSOLE_ANSI_COLOR"] = "1"
build_env["DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION"] = "1"
build_cmdline = ["dotnet", "publish" if args.publish else "build", "--nologo"]
if args.configuration:
build_cmdline.append(f"--configuration={args.configuration}")
if args.framework:
build_cmdline.append(f"--framework={args.framework}")
if args.runtime:
build_cmdline.append(f"--runtime={args.runtime}")
build_proc = subprocess.Popen(print_cmdline(build_cmdline), env=build_env, cwd=proj_dir, stdout=subprocess.PIPE, text=True)
for line in build_proc.stdout.readlines(): # type: ignore
print(line.strip())
match = PROJECT_PATH_PATTERN.match(line)
if match and match[1].strip() == proj_name:
proj_out_path = match[2].strip()
proj_out_dir = os.path.dirname(proj_out_path) # type: ignore
if build_proc.wait() != 0: # build failed
return None
if not proj_out_path or not proj_out_dir:
raise Exception("Failed to parse current project path")
return (proj_out_path, proj_out_dir)
def main():
args_parser = argparse.ArgumentParser(prog="DotnetBuildWrapper",
description="Wrapper for dotnet build intended for Kate")
args_parser.add_argument("-c", "--configuration", default="Debug")
args_parser.add_argument("-f", "--framework", default=None)
args_parser.add_argument("-r", "--runtime", default=None)
args_parser.add_argument("--build-only", default=False, action="store_true")
args_parser.add_argument("--publish", default=False, action="store_true")
args = args_parser.parse_args()
proj_dir = os.getcwd()
proj_name = os.path.basename(proj_dir)
proj_file = f"{proj_dir}/{proj_name}.csproj"
if not os.path.exists(proj_file):
print(f"Could not find project file: {proj_file}", file=sys.stderr)
return
build_result = build(proj_name, proj_dir, args)
if not build_result:
return
(_, proj_out_dir) = build_result
if args.build_only or args.publish:
return
# app run command
run_script = f"dotnet run --no-build --no-restore --project \"{proj_file}\""
if args.configuration:
run_script += f" --configuration=\"{args.configuration}\""
if args.framework:
run_script += f" --framework=\"{args.framework}\""
if args.runtime:
run_script += f" --runtime=\"{args.runtime}\""
# process status prompt
run_script += ";__RUN_EXIT_CODE=$?; echo; echo \"Process has quit with exit code: $__RUN_EXIT_CODE\""
run_script += ";echo \"Press any key to exit...\"; read"
subprocess.Popen(print_cmdline([
"konsole", "--hide-tabbar", "--hide-menubar", "--nofork",
f"-p tabtitle={proj_name}",
"-e", "bash", "-c", run_script
]), cwd=proj_out_dir)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment