From 6e695bd48c9736a019057c53639993cbf9282671 Mon Sep 17 00:00:00 2001 From: HendrikRauh <114620133+HendrikRauh@users.noreply.github.com> Date: Thu, 5 Mar 2026 23:29:02 +0100 Subject: [PATCH] refactor(format): enhance formatting tasks with tool existence checks and improved output --- flake.nix | 4 ++-- tasks.py | 52 +++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/flake.nix b/flake.nix index b4e20cd..0791d12 100644 --- a/flake.nix +++ b/flake.nix @@ -15,7 +15,6 @@ let system = "x86_64-linux"; pkgs = nixpkgs.legacyPackages.${system}; - # Use the same esp-idf-full as previously in .envrc esp-idf = esp-dev.packages.${system}.esp-idf-full; in { @@ -24,10 +23,11 @@ esp-idf pkgs.python3 pkgs.python3Packages.invoke + # Formatting tools pkgs.clang-tools pkgs.nodePackages.prettier - # pkgs.nodePackages.svgo + pkgs.nodePackages.svgo pkgs.black pkgs.nixfmt ]; diff --git a/tasks.py b/tasks.py index a6096f2..d782c10 100644 --- a/tasks.py +++ b/tasks.py @@ -83,37 +83,59 @@ def format(c): result = c.run( "find main components -name '*.c' -o -name '*.h' | xargs clang-format -i", warn=True, + hide=False, ) - if result and not result.ok: - missing_tools.append("clang-format") + if result.exited == 127 or (not result.ok and result.exited != 0): + # Check if tool exists + check = c.run("command -v clang-format", warn=True, hide=True) + if not check.ok: + missing_tools.append("clang-format") print("Formatting Python files...") - result = c.run("black tasks.py", warn=True) - if result and not result.ok: - missing_tools.append("black") + result = c.run("black tasks.py", warn=True, hide=False) + if result.exited == 127 or (not result.ok and result.exited != 0): + check = c.run("command -v black", warn=True, hide=True) + if not check.ok: + missing_tools.append("black") print("Formatting Nix files...") - result = c.run("nixfmt flake.nix", warn=True) - if result and not result.ok: - missing_tools.append("nixfmt") + result = c.run("nixfmt flake.nix", warn=True, hide=False) + if result.exited == 127 or (not result.ok and result.exited != 0): + check = c.run("command -v nixfmt", warn=True, hide=True) + if not check.ok: + missing_tools.append("nixfmt") print("Formatting SVG files...") result = c.run( - "find . -name '*.svg' -not -path './build/*' -not -path './managed_components/*' | xargs svgo", + "find . -name '*.svg' -not -path './build/*' -not -path './managed_components/*' | xargs -r svgo", warn=True, + hide=False, ) - if result and not result.ok: - missing_tools.append("svgo") + if result.exited == 127 or (not result.ok and result.exited != 0): + check = c.run("command -v svgo", warn=True, hide=True) + if not check.ok: + missing_tools.append("svgo") print("Formatting other files...") - result = c.run("prettier --write '**/*.{js,json,yaml,yml,md,html,css}'", warn=True) - if result and not result.ok: - missing_tools.append("prettier") + result = c.run( + "prettier --write '**/*.{js,json,yaml,yml,md,html,css}'", warn=True, hide=False + ) + if result.exited == 127 or (not result.ok and result.exited != 0): + check = c.run("command -v prettier", warn=True, hide=True) + if not check.ok: + missing_tools.append("prettier") if missing_tools: - print(f"\nāŒ ERROR: Missing formatting tools: {', '.join(missing_tools)}") + print("\n" + "=" * 60) + print(f"āŒ ERROR: Missing formatting tools: {', '.join(missing_tools)}") + print("=" * 60) print("Please install them or reload the nix-shell.") + print("=" * 60 + "\n") sys.exit(1) + else: + print("\n" + "=" * 60) + print("āœ… All files formatted successfully!") + print("=" * 60 + "\n") @task