about summary refs log tree commit diff
path: root/tools/depotfmt.nix
blob: d1a1cb425ae3569e86d8002013e89d96c45a2100 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Builds treefmt for depot, with a hardcoded configuration that
# includes the right paths to formatters.
{ depot, pkgs, ... }:

let
  config = pkgs.writeText "depot-treefmt-config" ''
    [formatter.go]
    command = "${pkgs.go}/bin/gofmt"
    options = [ "-w" ]
    includes = ["*.go"]

    [formatter.tf]
    command = "${pkgs.terraform}/bin/terraform"
    options = [ "fmt" ]
    includes = [ "*.tf" ]
  '';

  # helper tool for formatting the depot interactively
  depotfmt = pkgs.writeShellScriptBin "depotfmt" ''
    exec ${pkgs.treefmt}/bin/treefmt ''${@} \
      --config-file ${config} \
      --tree-root $(${pkgs.git}/bin/git rev-parse --show-toplevel)
  '';

  # wrapper for running formatting checks in CI
  check = pkgs.runCommandNoCC "depotfmt-check" {} ''
    ${pkgs.git}/bin/git clone ${depot.path.origSrc} depot
    export HOME="$(${pkgs.coreutils}/bin/realpath .)"
    ${pkgs.treefmt}/bin/treefmt \
      --fail-on-change \
      --config-file ${config} \
      --tree-root depot && : > $out
  '';
in depotfmt // depot.nix.readTree.drvTargets { inherit check; }