-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
back to using env for local dependencies
This supercedes the changes in f53b604 Thanks to dhall-lang/dhall-haskell#2203 (to be released in dhall 1.40.0), the `?` operator now only falls back on import not found, but not type errors etc. There's still some chance for accidental fallback, but that can now be worked around by using an intentional type error to prevent unwanted fallbacks.
- Loading branch information
1 parent
58fb84e
commit 34befa0
Showing
10 changed files
with
205 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,28 @@ | ||
#!/usr/bin/env bash | ||
set -eu | ||
# with-local will temporarily install all `.dhall.local` files in place of the | ||
# corresponding `.dhall` files, for the duration of a single command. | ||
# | ||
# Upon exit, the script will attempt to restore the original versions. | ||
# If this fails, the following invariants will hold, which should allow the | ||
# script to be run again and self-correct: | ||
# This script enables local imports using the following pattern: | ||
# | ||
# Invariants: | ||
# - there is always a .local version | ||
# - if there is no explicit .remote file, then there must be a plain file (which is the logical "remote" version) | ||
# - if there is a .remote file, then | ||
# - there may be a plain file, which is a symlink to .local | ||
|
||
if [ "${DEBUG:-0}" = 1 ]; then | ||
# set -x | ||
function log { | ||
echo >&2 "$@" | ||
} | ||
else | ||
function log { | ||
true | ||
} | ||
fi | ||
|
||
function local_version { | ||
echo "$1.local" | ||
} | ||
|
||
function remote_version { | ||
echo "$1.remote" | ||
} | ||
|
||
function install_local { | ||
path="$1" | ||
remote_path="$(remote_version "$1")" | ||
local_path="$(local_version "$1")" | ||
if [ ! -e "$remote_path" ]; then | ||
if [ -L "$path" ]; then | ||
log "Error: $remote_path doesn't exist but $path is a symlink" | ||
exit 1 | ||
fi | ||
# backup to remote_path | ||
log "moving to $remote_path" | ||
mv "$path" "$remote_path" | ||
fi | ||
echo >&2 "[ using $local_path ... ]" | ||
ln -s "$(basename "$local_path")" "$path" | ||
} | ||
|
||
function restore_remote { | ||
# put remote version back in place | ||
path="$1" | ||
remote_path="$(remote_version "$1")" | ||
if [ -e "$remote_path" ]; then | ||
log "restoring $remote_path" | ||
mv "$remote_path" "$path" | ||
# let Dependency = | ||
# (\(local : Bool) -> ../dependency/package.dhall ? local "import failed") | ||
# env:DHALL_LOCAL | ||
# ? https://(...) | ||
# | ||
# See the dhall-render readme for full details: | ||
# https://github.com/timbertson/dhall-render#readme | ||
|
||
SCOPES=() | ||
while [ "$#" -gt 0 ]; do | ||
if [ "x$1" = "x-s" ]; then | ||
IFS=',' read -r -a SCOPES <<< "$(echo "$2" | tr '[:lower:]' '[:upper:]')" | ||
shift 2 | ||
else | ||
log "Error: $remote_path is not present" | ||
exit 1 | ||
break | ||
fi | ||
} | ||
|
||
function find_all { | ||
base_dir="$(dirname "$0")" | ||
base_dir="${DHALL_LOCAL_ROOT:-$base_dir}" | ||
log "base_dir: $base_dir" | ||
find "$base_dir" -name '*.dhall.local' | while read f; do | ||
path="$(dirname "$f")/$(basename "$f" .local)" | ||
log "processing $path" | ||
echo "$path" | ||
done | ||
} | ||
|
||
function restore_all { | ||
find_all | while read f; do | ||
restore_remote "$f" | ||
done | ||
} | ||
|
||
function install_all { | ||
find_all | while read f; do | ||
install_local "$f" | ||
done | ||
} | ||
|
||
if [ "$#" -eq 0 ]; then | ||
echo >&2 "Usage: "$0" COMMAND" | ||
fi | ||
done | ||
|
||
trap restore_all EXIT | ||
install_all | ||
"$@" | ||
export DHALL_LOCAL=True | ||
for scope in ${SCOPES[@]+${SCOPES[@]}}; do | ||
eval "export DHALL_LOCAL_$scope=True" | ||
done | ||
exec "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,35 @@ | ||
# itty bitty test libby | ||
def assert_equal(a,b) | ||
|
||
require 'ostruct' | ||
require 'open3' | ||
|
||
def assert_equal(a, b, desc = nil) | ||
if a != b | ||
raise "AssertionError, expected: #{b.inspect}, got: #{a.inspect}" | ||
suffix = desc ? " (#{desc})" : "" | ||
raise "AssertionError, expected: #{b.inspect}, got: #{a.inspect}#{suffix}" | ||
end | ||
end | ||
|
||
def assert_matches(a,b) | ||
if !b.match?(a) | ||
raise "AssertionError, expected: #{a.inspect} to match #{b.inspect}" | ||
end | ||
end | ||
|
||
def test(desc) | ||
puts("# #{desc} ...") | ||
yield | ||
end | ||
|
||
def run(*cmd) | ||
run?(*cmd).success or raise "Command failed: #{cmd.join(' ')}" | ||
end | ||
|
||
def run?(*cmd) | ||
puts(" + #{cmd.join(' ')}") | ||
Open3.popen2e(*cmd) do |stdin, out_and_err, wait| | ||
output = out_and_err.read | ||
code = wait.value | ||
OpenStruct.new({ output: output, success: wait.value == 0 }) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
\(x : Natural) -> "number: ${Natural/show x}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
let show = | ||
( \(local : Bool) -> | ||
./local-show-impl-not-present.dhall ? local "import failed" | ||
) | ||
env:DHALL_LOCAL_SHOW | ||
? https://prelude.dhall-lang.org/v20.0.0/Natural/show.dhall sha256:684ed560ad86f438efdea229eca122c29e8e14f397ed32ec97148d578ca5aa21 | ||
|
||
in show 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
let show = | ||
-- This is a bit arduous, but it is possible to have a local implementation | ||
-- which is attempted if `DHALL_LOCAL` is set, but required if DHALL_LOCAL_SHOW | ||
-- is set. It's probably too verbose unless you want maximal flexibility. | ||
(\(local : Bool) -> ./local-show-impl-not-present.dhall) env:DHALL_LOCAL | ||
? ( \(local : Bool) -> | ||
./local-show-impl-not-present.dhall ? local "import failed" | ||
) | ||
env:DHALL_LOCAL_SHOW | ||
? https://prelude.dhall-lang.org/v20.0.0/Natural/show.dhall sha256:684ed560ad86f438efdea229eca122c29e8e14f397ed32ec97148d578ca5aa21 | ||
|
||
in show 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let show = | ||
(\(local : Bool) -> ./local-show-impl-not-present.dhall) env:DHALL_LOCAL | ||
? https://prelude.dhall-lang.org/v20.0.0/Natural/show.dhall sha256:684ed560ad86f438efdea229eca122c29e8e14f397ed32ec97148d578ca5aa21 | ||
|
||
in show 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
let show = | ||
(\(local : Bool) -> ./local-show.dhall ? local "import failed") | ||
env:DHALL_LOCAL_SHOW | ||
? https://prelude.dhall-lang.org/v20.0.0/Natural/show.dhall sha256:684ed560ad86f438efdea229eca122c29e8e14f397ed32ec97148d578ca5aa21 | ||
|
||
in show 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let show = | ||
(\(local : Bool) -> ./local-show.dhall) env:DHALL_LOCAL | ||
? https://prelude.dhall-lang.org/v20.0.0/Natural/show.dhall sha256:684ed560ad86f438efdea229eca122c29e8e14f397ed32ec97148d578ca5aa21 | ||
|
||
in show 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/usr/bin/env ruby | ||
require_relative 'lib/utest' | ||
|
||
load "maintenance/fix" | ||
|
||
[ | ||
{ | ||
desc: "local", | ||
file: 'test/local/present.dhall', | ||
output: "number: 1" | ||
}, | ||
{ | ||
desc: "scope selected", | ||
args: ['-s', 'show'], | ||
file: 'test/local/present-scoped.dhall', | ||
output: "number: 1" | ||
}, | ||
{ | ||
desc: "scope not selected", | ||
file: 'test/local/present-scoped.dhall', | ||
output: "1" | ||
}, | ||
|
||
{ | ||
desc: "missing: lax", | ||
file: 'test/local/missing.dhall', | ||
output: "1" | ||
}, | ||
|
||
{ | ||
desc: "missing: scope not selected", | ||
file: 'test/local/missing-scoped.dhall', | ||
output: "1" | ||
}, | ||
|
||
{ | ||
desc: "missing: semi-scope not selected", | ||
file: 'test/local/missing-semi-scoped.dhall', | ||
output: "1" | ||
}, | ||
|
||
{ | ||
desc: "missing: scope selected", | ||
args: ['-s', 'show'], | ||
file: 'test/local/missing-scoped.dhall', | ||
success: false, | ||
output: /local "import failed"/ | ||
}, | ||
|
||
{ | ||
desc: "missing: semi-scope selected", | ||
args: ['-s', 'show'], | ||
file: 'test/local/missing-semi-scoped.dhall', | ||
success: false, | ||
output: /local "import failed"/ | ||
}, | ||
].each do |test_case| | ||
local_args = test_case.fetch(:args, []) | ||
file = test_case.fetch(:file) | ||
full_args = local_args + [file] | ||
test("#{test_case[:desc]} (#{full_args})") do | ||
result = run?( | ||
'./maintenance/local', *local_args, | ||
'dhall', 'text', '--file', file | ||
) | ||
assert_equal(result.success, test_case.fetch(:success, true), "process with output:\n#{result.output}") | ||
expected_output = test_case.fetch(:output) | ||
if expected_output.is_a?(Regexp) | ||
assert_matches(result.output, expected_output) | ||
else | ||
assert_equal(result.output, expected_output) | ||
end | ||
end | ||
end |