Skip to content

Commit

Permalink
nix: Add a tool for checking Haskell imports and exports (#1768)
Browse files Browse the repository at this point in the history
  • Loading branch information
monacoremo committed Apr 6, 2021
1 parent 9f074ce commit 65e7f9e
Show file tree
Hide file tree
Showing 11 changed files with 516 additions and 32 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ __pycache__
*.tix
coverage
.hpc
imports.png
8 changes: 7 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,15 @@ rec {
style =
pkgs.callPackage nix/style.nix { };

# Tooling for analyzing Haskell imports and exports.
hsie =
pkgs.callPackage nix/hsie {
ghcWithPackages = pkgs.haskell.packages."${compiler}".ghcWithPackages;
};

# Development tools, including linting and styling scripts.
devtools =
pkgs.callPackage nix/devtools.nix { inherit tests style devCabalOptions; };
pkgs.callPackage nix/devtools.nix { inherit tests style devCabalOptions hsie; };

# Scripts for publishing new releases.
release =
Expand Down
75 changes: 48 additions & 27 deletions nix/devtools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
, devCabalOptions
, entr
, graphviz
, hsie
, silver-searcher
, style
, tests
Expand Down Expand Up @@ -98,40 +99,57 @@ let
${style}/bin/postgrest-style-check
'';

importsgraph =
dumpMinimalImports =
checkedShellScript
{
name = "postgrest-importsgraph";
docs =
''
Render the imports between PostgREST modules as a graph.
Output is written to 'imports.png' in the root directory of the project.
'';
name = "postgrest-dump-minimal-imports";
docs = "Dump minimal imports into given directory.";
inRootDir = true;
}
''
imports=$(
grep -rE 'import .*PostgREST\.' src main \
| sed -E \
-e 's|/|\.|g' \
-e 's/(src|main)\.(.*)\.hs:import .*(PostgREST\.\S+)( .*)?/"\2" -> "\3"/'
)
dumpdir="$1"
tmpdir="$(mktemp -d)"
mkdir -p "$dumpdir"
${cabal-install}/bin/cabal v2-build ${devCabalOptions} \
--builddir="$tmpdir" \
--ghc-option=-ddump-minimal-imports \
--ghc-option=-dumpdir="$dumpdir" \
1>&2
rm -rf "$tmpdir"
labels=$(
grep -rE '^(--)?\s*Description\s*:' src main \
| sed -E \
-e 's|/|\.|g' \
-e 's/^(src|main)\.(.*)\.hs:(--)?\s*Description\s*:\s*(.*)$/"\2" [label="\2\\n\4"]/'
)
# Fix OverloadedRecordFields imports
# shellcheck disable=SC2016
sed -E 's/\$sel:.*://g' -i "$dumpdir"/*
'';

cat <<EOF | ${graphviz}/bin/dot -Tpng > imports.png
digraph {
$imports
$labels
}
EOF
hsieMinimalImports =
checkedShellScript
{
name = "postgrest-hsie-minimal-imports";
docs = "Run hsie with a provided dump of minimal imports.";
}
''
tmpdir="$(mktemp -d)"
${dumpMinimalImports} "$tmpdir"
${hsie} "$tmpdir" "$@"
rm -rf "$tmpdir"
'';

hsieGraphModules =
checkedShellScript
{
name = "postgrest-hsie-graph-modules";
docs = "Create a PNG graph of modules imported within the codebase.";
}
''${hsie} graph-modules main src | ${graphviz}/bin/dot -Tpng -o "$1"'';

hsieGraphSymbols =
checkedShellScript
{
name = "postgrest-hsie-graph-symbols";
docs = "Create a PNG graph of symbols imported within the codebase.";
}
''${hsieMinimalImports} graph-symbols | ${graphviz}/bin/dot -Tpng -o "$1"'';
in
buildEnv {
name = "postgrest-devtools";
Expand All @@ -142,6 +160,9 @@ buildEnv {
run.bin
clean.bin
check.bin
importsgraph.bin
dumpMinimalImports.bin
hsieMinimalImports.bin
hsieGraphModules.bin
hsieGraphSymbols.bin
];
}
Loading

0 comments on commit 65e7f9e

Please sign in to comment.