|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 | set -eu
|
3 |
| -# with-local will temporarily install all `.dhall.local` files in place of the |
4 |
| -# corresponding `.dhall` files, for the duration of a single command. |
5 | 3 | #
|
6 |
| -# Upon exit, the script will attempt to restore the original versions. |
7 |
| -# If this fails, the following invariants will hold, which should allow the |
8 |
| -# script to be run again and self-correct: |
| 4 | +# This script enables local imports using the following pattern: |
9 | 5 | #
|
10 |
| -# Invariants: |
11 |
| -# - there is always a .local version |
12 |
| -# - if there is no explicit .remote file, then there must be a plain file (which is the logical "remote" version) |
13 |
| -# - if there is a .remote file, then |
14 |
| -# - there may be a plain file, which is a symlink to .local |
15 |
| - |
16 |
| -if [ "${DEBUG:-0}" = 1 ]; then |
17 |
| - # set -x |
18 |
| - function log { |
19 |
| - echo >&2 "$@" |
20 |
| - } |
21 |
| -else |
22 |
| - function log { |
23 |
| - true |
24 |
| - } |
25 |
| -fi |
26 |
| - |
27 |
| -function local_version { |
28 |
| - echo "$1.local" |
29 |
| -} |
30 |
| - |
31 |
| -function remote_version { |
32 |
| - echo "$1.remote" |
33 |
| -} |
34 |
| - |
35 |
| -function install_local { |
36 |
| - path="$1" |
37 |
| - remote_path="$(remote_version "$1")" |
38 |
| - local_path="$(local_version "$1")" |
39 |
| - if [ ! -e "$remote_path" ]; then |
40 |
| - if [ -L "$path" ]; then |
41 |
| - log "Error: $remote_path doesn't exist but $path is a symlink" |
42 |
| - exit 1 |
43 |
| - fi |
44 |
| - # backup to remote_path |
45 |
| - log "moving to $remote_path" |
46 |
| - mv "$path" "$remote_path" |
47 |
| - fi |
48 |
| - echo >&2 "[ using $local_path ... ]" |
49 |
| - ln -s "$(basename "$local_path")" "$path" |
50 |
| -} |
51 |
| - |
52 |
| -function restore_remote { |
53 |
| - # put remote version back in place |
54 |
| - path="$1" |
55 |
| - remote_path="$(remote_version "$1")" |
56 |
| - if [ -e "$remote_path" ]; then |
57 |
| - log "restoring $remote_path" |
58 |
| - mv "$remote_path" "$path" |
| 6 | +# let Dependency = |
| 7 | +# (\(local : Bool) -> ../dependency/package.dhall ? local "import failed") |
| 8 | +# env:DHALL_LOCAL |
| 9 | +# ? https://(...) |
| 10 | +# |
| 11 | +# See the dhall-render readme for full details: |
| 12 | +# https://github.com/timbertson/dhall-render#readme |
| 13 | + |
| 14 | +SCOPES=() |
| 15 | +while [ "$#" -gt 0 ]; do |
| 16 | + if [ "x$1" = "x-s" ]; then |
| 17 | + IFS=',' read -r -a SCOPES <<< "$(echo "$2" | tr '[:lower:]' '[:upper:]')" |
| 18 | + shift 2 |
59 | 19 | else
|
60 |
| - log "Error: $remote_path is not present" |
61 |
| - exit 1 |
| 20 | + break |
62 | 21 | fi
|
63 |
| -} |
64 |
| - |
65 |
| -function find_all { |
66 |
| - base_dir="$(dirname "$0")" |
67 |
| - base_dir="${DHALL_LOCAL_ROOT:-$base_dir}" |
68 |
| - log "base_dir: $base_dir" |
69 |
| - find "$base_dir" -name '*.dhall.local' | while read f; do |
70 |
| - path="$(dirname "$f")/$(basename "$f" .local)" |
71 |
| - log "processing $path" |
72 |
| - echo "$path" |
73 |
| - done |
74 |
| -} |
75 |
| - |
76 |
| -function restore_all { |
77 |
| - find_all | while read f; do |
78 |
| - restore_remote "$f" |
79 |
| - done |
80 |
| -} |
81 |
| - |
82 |
| -function install_all { |
83 |
| - find_all | while read f; do |
84 |
| - install_local "$f" |
85 |
| - done |
86 |
| -} |
87 |
| - |
88 |
| -if [ "$#" -eq 0 ]; then |
89 |
| - echo >&2 "Usage: "$0" COMMAND" |
90 |
| -fi |
| 22 | +done |
91 | 23 |
|
92 |
| -trap restore_all EXIT |
93 |
| -install_all |
94 |
| -"$@" |
| 24 | +export DHALL_LOCAL=True |
| 25 | +for scope in ${SCOPES[@]+${SCOPES[@]}}; do |
| 26 | + eval "export DHALL_LOCAL_$scope=True" |
| 27 | +done |
| 28 | +exec "$@" |
0 commit comments