From a7547585c994931aabdc82c2f6cb41e569ca311d Mon Sep 17 00:00:00 2001
From: zowoq <59103226+zowoq@users.noreply.github.com>
Date: Mon, 13 May 2024 21:03:49 +1000
Subject: [PATCH] treefmt: refactor, treefmt2, pipelines

also move editorconfig to a separate check as it isn't currently compatible with treefmt pipelines
---
 .editorconfig   |  4 +++
 .gitignore      |  4 ---
 dev/treefmt.nix | 85 ++++++++++++++++++++++++++-----------------------
 flake.nix       |  8 +++++
 4 files changed, 57 insertions(+), 44 deletions(-)

diff --git a/.editorconfig b/.editorconfig
index afb1fc7..7a561b4 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -5,3 +5,7 @@ end_of_line = lf
 insert_final_newline = true
 trim_trailing_whitespace = true
 charset = utf-8
+
+[*.age]
+end_of_line = unset
+insert_final_newline = unset
diff --git a/.gitignore b/.gitignore
index eb68800..1058929 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,9 +6,5 @@ result-*
 .terraform
 .terraform.*
 
-# needed for treefmt
-!.github
-!.sops.yaml
-
 # direnv
 .direnv/
diff --git a/dev/treefmt.nix b/dev/treefmt.nix
index 9619a76..458bfcb 100644
--- a/dev/treefmt.nix
+++ b/dev/treefmt.nix
@@ -2,14 +2,24 @@
   # Used to find the project root
   projectRootFile = ".git/config";
 
-  programs.hclfmt.enable = true;
+  package = pkgs.treefmt2;
+
+  programs = {
+    deadnix.enable = true;
+    hclfmt.enable = true;
+    nixpkgs-fmt.enable = true;
+    prettier.enable = true;
+    ruff.check = true;
+    ruff.format = true;
+    statix.enable = true;
+  };
 
   programs.mypy = {
     enable = true;
     directories = {
       "tasks" = {
         directory = ".";
-        files = [ "**/tasks.py" ];
+        files = [ "*tasks.py" ];
         modules = [ ];
         extraPythonPackages = [
           pkgs.python3.pkgs.deploykit
@@ -19,41 +29,47 @@
     };
   };
 
-  programs.prettier.enable = true;
+  settings.global.excludes = [
+    # vendored from external source
+    "hosts/build02/packages-with-update-script.nix"
+  ];
 
   settings.formatter = {
     actionlint = {
       command = pkgs.actionlint;
       includes = [ ".github/workflows/*.yml" ];
+      pipeline = "yaml";
+      priority = 1;
     };
 
-    editorconfig-checker = {
-      command = pkgs.editorconfig-checker;
-      includes = [ "*" ];
-      excludes = [ "*.age" ];
+    deadnix = {
+      pipeline = "nix";
+      priority = 1;
     };
 
-    nix = {
-      command = "sh";
-      options = [
-        "-eucx"
-        ''
-          ${pkgs.lib.getExe pkgs.deadnix} --edit "$@"
+    statix = {
+      pipeline = "nix";
+      priority = 2;
+    };
 
-          for i in "$@"; do
-            ${pkgs.lib.getExe pkgs.statix} fix "$i"
-          done
+    nixpkgs-fmt = {
+      pipeline = "nix";
+      priority = 3;
+    };
 
-          ${pkgs.lib.getExe pkgs.nixpkgs-fmt} "$@"
-        ''
-        "--"
-      ];
-      includes = [ "*.nix" ];
-      excludes = [
-        "nix/sources.nix"
-        # vendored from external source
-        "hosts/build02/packages-with-update-script.nix"
-      ];
+    ruff-check = {
+      pipeline = "python";
+      priority = 1;
+    };
+
+    ruff-format = {
+      pipeline = "python";
+      priority = 2;
+    };
+
+    mypy-tasks = {
+      pipeline = "python";
+      priority = 3;
     };
 
     prettier = {
@@ -63,21 +79,10 @@
         "never"
       ];
       excludes = [
-        "secrets.yaml"
+        "*secrets.yaml"
       ];
-    };
-
-    python = {
-      command = "sh";
-      options = [
-        "-eucx"
-        ''
-          ${pkgs.lib.getExe pkgs.ruff} check --fix "$@"
-          ${pkgs.lib.getExe pkgs.ruff} format "$@"
-        ''
-        "--" # this argument is ignored by bash
-      ];
-      includes = [ "*.py" ];
+      pipeline = "yaml";
+      priority = 2;
     };
   };
 }
diff --git a/flake.nix b/flake.nix
index fe85c97..5857491 100644
--- a/flake.nix
+++ b/flake.nix
@@ -99,6 +99,14 @@
                 cd ${self}
                 mkdocs build --strict --site-dir $out
               '';
+              editorconfig = pkgs.runCommand "editorconfig"
+                {
+                  buildInputs = [ pkgs.editorconfig-checker ];
+                } ''
+                cd ${self}
+                editorconfig-checker
+                touch $out
+              '';
             };
           };