@@ -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
3838format () {
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
4954idempotency () {
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
6468main () {
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