Skip to content

Commit d0cc243

Browse files
committed
treefmt: add config, tooling and pre-commit hook
Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
1 parent 7d6a80c commit d0cc243

4 files changed

Lines changed: 77 additions & 6 deletions

File tree

.scripts/pre-commit.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
# pre-commit hook script
3+
4+
echo "Running pre-commit hook with treefmt"
5+
6+
# Run treefmt to format the code and check if there are any changes after formatting
7+
if ! just fmt --fail-on-change; then
8+
echo -e "\nCode may have been formatted. Please review the changes and commit again."
9+
exit 1
10+
fi

justfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ default:
99

1010
transferDir := `if [ -d "$HOME/NextcloudPrivate/Transfer" ]; then echo "$HOME/NextcloudPrivate/Transfer"; else echo "$HOME/Nextcloud/Transfer"; fi`
1111

12+
# Aliases
13+
14+
alias fmt := format
15+
1216
# Apply the patch to the qownnotes-scripts repository
1317
[group('patch')]
1418
git-apply-patch:
@@ -35,3 +39,8 @@ just-format:
3539
echo "Formatting $file"
3640
just --fmt --unstable -f "$file"
3741
done
42+
43+
# Format all files
44+
[group('linter')]
45+
format args='':
46+
nix-shell -p treefmt nodePackages.prettier shfmt nixfmt-rfc-style statix taplo kdePackages.qtdeclarative --run "treefmt {{ args }}"

shell.nix

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
{ pkgs ? import <nixpkgs> {} }:
2-
pkgs.mkShell {
3-
nativeBuildInputs = with pkgs; [
4-
php
5-
just
6-
];
1+
{
2+
pkgs ? import <nixpkgs> { },
3+
}:
4+
pkgs.mkShell {
5+
nativeBuildInputs = with pkgs; [
6+
php
7+
just
8+
];
9+
10+
shellHook = ''
11+
# Determine the repository root
12+
REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
13+
14+
# Check if we are in the repository root
15+
if [ "$REPO_ROOT" = "$(pwd)" ]; then
16+
# Symlink the pre-commit hook into the .git/hooks directory
17+
ln -sf ../../.scripts/pre-commit.sh .git/hooks/pre-commit
18+
fi
19+
'';
720
}

treefmt.toml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# https://github.com/numtide/treefmt
2+
# https://github.com/numtide/treefmt-nix
3+
4+
on-unmatched = "info"
5+
6+
[formatter.prettier]
7+
command = "prettier"
8+
options = ["--write"]
9+
includes = ["*.js", "*.md", "*.yaml", "*.yml", "*.json"]
10+
11+
[formatter.shfmt]
12+
command = "shfmt"
13+
includes = ["*.sh", "*.bash", "*.envrc", "*.envrc.*"]
14+
options = ["-s", "-w", "-i", "2"]
15+
16+
[formatter.just]
17+
command = "just"
18+
includes = ["*.just"]
19+
20+
[formatter.taplo]
21+
command = "taplo"
22+
includes = ["*.toml"]
23+
options = ["format"]
24+
25+
[formatter.nixfmt-rfc-style]
26+
command = "nixfmt"
27+
includes = ["*.nix"]
28+
29+
# Statix doesn't support formatting single files, see https://github.com/oppiliappan/statix/issues/69
30+
# Workaround: https://github.com/numtide/treefmt/issues/241#issuecomment-1614563462
31+
[formatter.statix]
32+
command = "bash"
33+
options = ["-euc", "for file in \"$@\"; do statix fix \"$file\"; done"]
34+
includes = ["*.nix"]
35+
36+
[formatter.qmlformat]
37+
includes = ["*.qml"]
38+
command = "qmlformat"
39+
options = ["--inplace", "--normalize"]

0 commit comments

Comments
 (0)