Skip to content

Commit 3bb8c42

Browse files
committed
Add check-weeds script
This script also gets run as part of `scripts/check.sh`. Since `check-weeds` has to do much of the same work as `check-formatting`, I extracted the common code to a separate Bash file. Also made it so the scripts don’t have to be run from the repo root directory.
1 parent e046c32 commit 3bb8c42

File tree

4 files changed

+94
-24
lines changed

4 files changed

+94
-24
lines changed

scripts/check-formatting

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
3+
SCRIPTDIR="$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")"
34

4-
mode=${1:-check}
5-
6-
## This would ideally be read from ./nix/versions.nix, but we can’t assume the user has Nix, so we hardcode this, then
7-
## at the end of the script, if the user _does_ have Nix, we check if this hardcoded version is the one we want.
8-
ormolu_version="0.7.2.0"
5+
source "$SCRIPTDIR/check-version.bash"
96

10-
if command -v ormolu >/dev/null; then
11-
if ormolu --version | grep -q "${ormolu_version}"; then
12-
find . -name '*.hs' -not -path "*/dist-newstyle/*" -not -path "*/.stack-work/*" -print0 \
13-
| xargs -0 ormolu --check-idempotence --mode "${mode}"
14-
else
15-
>&2 echo "Found Ormolu, but the version isn’t ${ormolu_version}:"
16-
>&2 ormolu --version
17-
>&2 echo "skipping format check"
18-
fi
19-
else
20-
>&2 echo "Ormolu not found on PATH, skipping format check"
21-
fi
7+
mode=${1:-check}
228

23-
## When the user has Nix, ensure that `ormolu_version` matches what we require for Nix.
24-
if command -v nix >/dev/null; then
25-
expected_version="$(nix eval --file ./nix/versions.nix ormolu --raw)"
26-
if [[ "$expected_version" != "$ormolu_version" ]]; then
27-
>&2 echo "⚠️ Please update ‘ormolu_version’ in $0 to ${expected_version} and open a PR."
28-
fi
9+
if version-equals ormolu "0.7.2.0"; then
10+
## We `cd` so Ormolu can find its .ormolu configuration.
11+
cd "$SCRIPTDIR/.."
12+
find . -name '*.hs' -not -path "*/dist-newstyle/*" -not -path "*/.stack-work/*" -print0 \
13+
| xargs -0 ormolu --check-idempotence --mode "${mode}"
2914
fi

scripts/check-version.bash

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
SCRIPTNAME="$(readlink -f -- "${BASH_SOURCE[0]}")"
2+
SCRIPTDIR="$(dirname -- "$SCRIPTNAME")"
3+
4+
# shellcheck disable=SC2120
5+
# because the argument is optional
6+
function indent() {
7+
local indentSize=2
8+
local indent=${1:-1}
9+
pr -to $((indent * indentSize))
10+
}
11+
12+
## This function checks
13+
## 1. that the command is on PATH,
14+
## 2. that the version of the command found matches our expectation (command must support `--version`), and
15+
## 3. if the user has Nix, that our expectation matches the version specified by our Nix config.
16+
##
17+
## Failing either of the first two checks causes an error, while the third is only a warning (because the third
18+
## indicates a code issue, not a user issue).
19+
function version-equals {
20+
local command="$1"
21+
## This would ideally be read from ./nix/versions.nix, but we can’t assume the user has Nix, so we hardcode this,
22+
## then at the end of the script, if the user _does_ have Nix, we check if this hardcoded version is the one we
23+
## want.
24+
local expected_version="$2"
25+
26+
if command -v "$command" >/dev/null; then
27+
if ! "$command" --version | grep -q "${expected_version}"; then
28+
>&2 echo "Skipping $command check – found, but the version isn’t ${expected_version}:"
29+
"$command" --version | indent >&2
30+
exit 78
31+
fi
32+
else
33+
>&2 echo "Skipping $command check – not found on PATH"
34+
exit 127
35+
fi
36+
37+
## When the user has Nix, check that `expected_version` matches what we require for Nix.
38+
if command -v nix >/dev/null; then
39+
nix_reported_version="$(nix eval --file $SCRIPTDIR/../nix/versions.nix "$command" --raw)"
40+
if [[ "$expected_version" != "$nix_reported_version" ]]; then
41+
>&2 echo "⚠️ Running $command check with unexpected version."
42+
>&2 echo " Please update ‘version-equals ${command} \"$expected_version\"’ called from"
43+
>&2 echo " $SCRIPTNAME"
44+
>&2 echo " to use \"$nix_reported_version\" and open a PR."
45+
fi
46+
fi
47+
}

scripts/check-weeds

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
SCRIPTDIR="$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")"
4+
5+
source "$SCRIPTDIR/check-version.bash"
6+
7+
if version-equals weeder "2.8.0"; then
8+
## We `cd` so Weeder can find all the files.
9+
cd "$SCRIPTDIR/.."
10+
## Weeder gets confused by HIE files in the Direnv cache, but doesn’t let us exclude a directory from HIE search
11+
## (ocharles/weeder#201). So this enumerates the directories we _do_ want to search.
12+
##
13+
## TODO: Populate the `--hie-directory` list with something like
14+
## `find . -type d -name ".stack-work" -exec dirname "{}" \;` to keep it from going stale.
15+
weeder \
16+
--require-hs-files --no-default-fields \
17+
--hie-directory ./codebase2 \
18+
--hie-directory ./lib \
19+
--hie-directory ./parser-typechecker \
20+
--hie-directory ./unison-cli \
21+
--hie-directory ./unison-cli-integration \
22+
--hie-directory ./unison-cli-main \
23+
--hie-directory ./unison-core \
24+
--hie-directory ./unison-hashing-v2 \
25+
--hie-directory ./unison-merge \
26+
--hie-directory ./unison-runtime \
27+
--hie-directory ./unison-share-api \
28+
--hie-directory ./unison-share-projects-api \
29+
--hie-directory ./unison-syntax \
30+
--hie-directory ./yaks
31+
fi

scripts/check.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
SCRIPTDIR="$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")"
4+
5+
## TODO: `transcripts` should be able to take a search path, but since it currently looks for files relative to its run
6+
## path, we `cd` to where it can find them.
7+
cd "$SCRIPTDIR/.."
28

39
true \
410
&& stack build --fast --test \
@@ -7,4 +13,5 @@ true \
713
&& stack exec unison transcript unison-src/transcripts-manual/rewrites.md \
814
&& stack exec unison transcript unison-src/transcripts-manual/docs.to-html.md \
915
&& stack exec cli-integration-tests \
16+
&& ./scripts/check-weeds \
1017
&& ./scripts/check-formatting

0 commit comments

Comments
 (0)