|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 | set -eou pipefail
|
3 | 3 |
|
4 |
| -if [[ -n "${IN_NIX_SHELL+x}" ]]; then |
5 |
| - echo "buildAndWatch does not work inside of a nix shell. Please exit the nix-shell and retry." |
6 |
| - exit 1 |
7 |
| -fi |
| 4 | +function buildAndWatchWithNix() { |
| 5 | + if [[ -n "${IN_NIX_SHELL+x}" ]]; then |
| 6 | + echo "buildAndWatch does not work inside of a nix shell. Please exit the nix-shell and retry." |
| 7 | + exit 1 |
| 8 | + fi |
8 | 9 |
|
9 |
| -if [[ ! -x $(type -P "nix-build") ]]; then |
10 |
| - echo "nix-build is not available. Please install nix from https://nixos.org/download.html" |
11 |
| - exit 1 |
12 |
| -fi |
| 10 | + if [[ ! -x $(type -P "nix-build") ]]; then |
| 11 | + echo "nix-build is not available. Please install nix from https://nixos.org/download.html" |
| 12 | + exit 1 |
| 13 | + fi |
| 14 | + |
| 15 | + nix-build -A builder && \ |
| 16 | + ./result/bin/haskell-org-site clean && \ |
| 17 | + ./result/bin/haskell-org-site build && \ |
| 18 | + ./result/bin/haskell-org-site watch |
| 19 | + exit 0 |
| 20 | +} |
| 21 | + |
| 22 | +function buildAndWatchWithoutNix() { |
| 23 | + if [[ ! -x $(type -P "cabal") ]]; then |
| 24 | + echo "Please download and install a haskell environment. See: https://www.haskell.org/downloads/" |
| 25 | + exit 1 |
| 26 | + fi |
| 27 | + |
| 28 | + pushd builder |
| 29 | + cabal v2-build |
| 30 | + builderPath=$(find dist-newstyle -name 'haskell-org-site' -type f) |
| 31 | + popd |
| 32 | + builder="./builder/${builderPath}" |
| 33 | + |
| 34 | + if [[ ! -x "${builder}" ]]; then |
| 35 | + cat <<EOF |
| 36 | +After building 'haskell-org-site' I was unable to find the path to a |
| 37 | +runnable executable. This may be because of a bug in this script. You |
| 38 | +may want to try to build and run the builder manually, ask for help, |
| 39 | +or submit a bug report. |
| 40 | +EOF |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + |
| 44 | + "${builder}" clean |
| 45 | + "${builder}" build |
| 46 | + "${builder}" watch |
| 47 | + exit 0 |
| 48 | +} |
| 49 | + |
| 50 | +function buildAndWatchAuto() { |
| 51 | + if [[ -x $(type -P "nix-build") ]]; then |
| 52 | + if [[ -n "${IN_NIX_SHELL+x}" ]]; then |
| 53 | + cat <<EOF |
| 54 | +I found a 'nix' installation, but we're currently inside of a nix |
| 55 | +shell. I will try to build using cabal. If building with cabal fails, |
| 56 | +you should try to re-run this script when you are not inside of a nix |
| 57 | +shell. |
| 58 | +EOF |
| 59 | + buildAndWatchWithoutNix |
| 60 | + else |
| 61 | + buildAndWatchWithNix |
| 62 | + fi |
| 63 | + else |
| 64 | + buildAndWatchWithoutNix |
| 65 | + fi |
| 66 | +} |
13 | 67 |
|
14 |
| -nix-build -A builder && \ |
15 |
| - ./result/bin/haskell-org-site clean && \ |
16 |
| - ./result/bin/haskell-org-site build && \ |
17 |
| - ./result/bin/haskell-org-site watch |
| 68 | +function showHelp() { |
| 69 | + cat <<EOF |
| 70 | +Usage: buildAndWatch [buildMode] |
| 71 | +
|
| 72 | +Build the haskell.org site builder, compile the site, and start a |
| 73 | +local server on port 8000 to preview changes. |
| 74 | +
|
| 75 | +buildAndWatch takes a single optional argument, 'buildMode'. If it's |
| 76 | +omitted, buildAndWatch will attempt to select the best method for |
| 77 | +building the site based on your current environment. If you provide an |
| 78 | +argument, it may be one of the following: |
| 79 | +
|
| 80 | + auto (default) Automatically select between 'nix' or 'cabal' based on the |
| 81 | + current environment. |
| 82 | + nix Use nix to build the site |
| 83 | + cabal Use cabal to build the site. If you are in a nix shell, |
| 84 | + it will use your nix environment. Otherwise, it will use |
| 85 | + your system-wide cabal installation. |
| 86 | + help Show this help message and exit |
| 87 | +EOF |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +function buildAndWatchWithArgs() { |
| 92 | + case ${1} in |
| 93 | + "auto") |
| 94 | + buildAndWatchAuto |
| 95 | + ;; |
| 96 | + "nix") |
| 97 | + buildAndWatchWithNix |
| 98 | + ;; |
| 99 | + "cabal") |
| 100 | + buildAndWatchWithoutNix |
| 101 | + ;; |
| 102 | + "help") |
| 103 | + showHelp |
| 104 | + ;; |
| 105 | + *) |
| 106 | + echo "Unrecognized or missing arguments" |
| 107 | + showHelp |
| 108 | + exit 1 |
| 109 | + esac |
| 110 | +} |
| 111 | + |
| 112 | +if [[ $# -gt 0 ]]; then |
| 113 | + buildAndWatchWithArgs "${1}" |
| 114 | +else |
| 115 | + buildAndWatchWithArgs "auto" |
| 116 | +fi |
0 commit comments