Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix terminal playground to work with new CLI #637

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1424,11 +1424,11 @@ terminal, it will format the given source input with the requested query
file, updating the output on any inotify event against those files.

```
Usage: playground.sh (LANGUAGE | QUERY_FILE) [INPUT_SOURCE]
Usage: ${PROGNAME} LANGUAGE [QUERY_FILE] [INPUT_SOURCE]

LANGUAGE can be one of the supported languages (e.g., "ocaml", "rust",
etc.); alternatively, give the path to the query file itself, as
QUERY_FILE.
etc.). The packaged formatting queries for this language can be
overridden by specifying a QUERY_FILE.

The INPUT_SOURCE is optional. If not specified, it defaults to trying
to find the bundled integration test input file for the given language.
Expand Down
82 changes: 58 additions & 24 deletions playground.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ fail() {
cat >&2 <<-EOF
Error: ${error}

Usage: ${PROGNAME} (LANGUAGE | QUERY_FILE) [INPUT_SOURCE]
Usage: ${PROGNAME} LANGUAGE [QUERY_FILE] [INPUT_SOURCE]

LANGUAGE can be one of the supported languages (e.g., "ocaml", "rust",
etc.); alternatively, give the path to the query file itself, as
QUERY_FILE.
etc.). The packaged formatting queries for this language can be
overridden by specifying a QUERY_FILE.

The INPUT_SOURCE is optional. If not specified, it defaults to trying
to find the bundled integration test input file for the given language.
Expand All @@ -36,40 +36,73 @@ get_sample_input() {
}

format() {
local query="$1"
local input="$2"
local skip_idempotence="${3-1}"
local language="$1"
local query="$2"
local input="$3"
local skip_idempotence="${4-1}"

local -a topiary_args=(
--language "${language}"
--query "${query}"
)

local -a topiary_args=(--query "${query}")
(( skip_idempotence )) && topiary_args+=(--skip-idempotence)

cargo run --quiet -- fmt "${topiary_args[@]}" < "${input}"
}

idempotency() {
local query="$1"
local input="$2"
local language="$1"
local query="$2"
local input="$3"

if format "${query}" "${input}" 0 >/dev/null 2>&1; then
if format "${language}" "${query}" "${input}" 0 >/dev/null 2>&1; then
printf "Yes"
elif (( $? == 7 )); then
printf "No"
else
if (( $? == 7 )); then
printf "No"
else
printf "n/a"
fi
printf "n/a"
fi
}

main() {
local query="${1-}"
if ! [[ -e "${query}" ]]; then
query="queries/${query}.scm"
[[ -e "${query}" ]] || fail "Couldn't find language query file '${query}'"
fi
local language
local query
local input

case $# in
1)
language="$1"
query="queries/${language}.scm"
input="$(get_sample_input "${language}")"
;;

2)
language="$1"

if [[ "$2" =~ \.scm$ ]]; then
query="$2"
input="$(get_sample_input "${language}")"
else
query="queries/${language}.scm"
input="$2"
fi
;;

3)
language="$1"
query="$2"
input="$3"
;;

*)
fail "Invalid command line arguments"
;;
esac

local language="$1"

local language="$(basename --suffix=.scm "${query}")"
local input="${2-$(get_sample_input "${language}")}"
[[ -e "${query}" ]] || fail "Couldn't find language query file '${query}'"
[[ -e "${input}" ]] || fail "Couldn't find input source file '${input}'"

# Horizontal rule (this is a function because executing it in a TTY-
Expand All @@ -81,14 +114,15 @@ main() {

hr
cat <<-EOF
Language ${language}
Query File ${query}
Input Source ${input}
EOF
hr

format "${query}" "${input}" || true
format "${language}" "${query}" "${input}" || true
hr
printf "Idempotent %s\n" "$(idempotency "${query}" "${input}")"
printf "Idempotent %s\n" "$(idempotency "${language}" "${query}" "${input}")"

# NOTE Different editors have different strategies for modifying
# files, so we wait on multiple events. This *may* not be an
Expand Down
Loading