|
| 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 | +} |
0 commit comments