Skip to content

Commit 85b375c

Browse files
authored
Fix terminal playground to work with new CLI (#637)
* Fix terminal playground to work with new CLI * Update README
1 parent 8299a04 commit 85b375c

File tree

2 files changed

+61
-27
lines changed

2 files changed

+61
-27
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,11 +1424,11 @@ terminal, it will format the given source input with the requested query
14241424
file, updating the output on any inotify event against those files.
14251425

14261426
```
1427-
Usage: playground.sh (LANGUAGE | QUERY_FILE) [INPUT_SOURCE]
1427+
Usage: ${PROGNAME} LANGUAGE [QUERY_FILE] [INPUT_SOURCE]
14281428
14291429
LANGUAGE can be one of the supported languages (e.g., "ocaml", "rust",
1430-
etc.); alternatively, give the path to the query file itself, as
1431-
QUERY_FILE.
1430+
etc.). The packaged formatting queries for this language can be
1431+
overridden by specifying a QUERY_FILE.
14321432
14331433
The INPUT_SOURCE is optional. If not specified, it defaults to trying
14341434
to find the bundled integration test input file for the given language.

playground.sh

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ fail() {
1414
cat >&2 <<-EOF
1515
Error: ${error}
1616
17-
Usage: ${PROGNAME} (LANGUAGE | QUERY_FILE) [INPUT_SOURCE]
17+
Usage: ${PROGNAME} LANGUAGE [QUERY_FILE] [INPUT_SOURCE]
1818
1919
LANGUAGE can be one of the supported languages (e.g., "ocaml", "rust",
20-
etc.); alternatively, give the path to the query file itself, as
21-
QUERY_FILE.
20+
etc.). The packaged formatting queries for this language can be
21+
overridden by specifying a QUERY_FILE.
2222
2323
The INPUT_SOURCE is optional. If not specified, it defaults to trying
2424
to find the bundled integration test input file for the given language.
@@ -36,40 +36,73 @@ get_sample_input() {
3636
}
3737

3838
format() {
39-
local query="$1"
40-
local input="$2"
41-
local skip_idempotence="${3-1}"
39+
local language="$1"
40+
local query="$2"
41+
local input="$3"
42+
local skip_idempotence="${4-1}"
43+
44+
local -a topiary_args=(
45+
--language "${language}"
46+
--query "${query}"
47+
)
4248

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

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

4954
idempotency() {
50-
local query="$1"
51-
local input="$2"
55+
local language="$1"
56+
local query="$2"
57+
local input="$3"
5258

53-
if format "${query}" "${input}" 0 >/dev/null 2>&1; then
59+
if format "${language}" "${query}" "${input}" 0 >/dev/null 2>&1; then
5460
printf "Yes"
61+
elif (( $? == 7 )); then
62+
printf "No"
5563
else
56-
if (( $? == 7 )); then
57-
printf "No"
58-
else
59-
printf "n/a"
60-
fi
64+
printf "n/a"
6165
fi
6266
}
6367

6468
main() {
65-
local query="${1-}"
66-
if ! [[ -e "${query}" ]]; then
67-
query="queries/${query}.scm"
68-
[[ -e "${query}" ]] || fail "Couldn't find language query file '${query}'"
69-
fi
69+
local language
70+
local query
71+
local input
72+
73+
case $# in
74+
1)
75+
language="$1"
76+
query="queries/${language}.scm"
77+
input="$(get_sample_input "${language}")"
78+
;;
79+
80+
2)
81+
language="$1"
82+
83+
if [[ "$2" =~ \.scm$ ]]; then
84+
query="$2"
85+
input="$(get_sample_input "${language}")"
86+
else
87+
query="queries/${language}.scm"
88+
input="$2"
89+
fi
90+
;;
91+
92+
3)
93+
language="$1"
94+
query="$2"
95+
input="$3"
96+
;;
97+
98+
*)
99+
fail "Invalid command line arguments"
100+
;;
101+
esac
102+
103+
local language="$1"
70104

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

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

82115
hr
83116
cat <<-EOF
117+
Language ${language}
84118
Query File ${query}
85119
Input Source ${input}
86120
EOF
87121
hr
88122

89-
format "${query}" "${input}" || true
123+
format "${language}" "${query}" "${input}" || true
90124
hr
91-
printf "Idempotent %s\n" "$(idempotency "${query}" "${input}")"
125+
printf "Idempotent %s\n" "$(idempotency "${language}" "${query}" "${input}")"
92126

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

0 commit comments

Comments
 (0)