@@ -44,6 +44,32 @@ has_command_or_error() {
4444 fi
4545}
4646
47+ # Output the global `ACTION` variable with a translation applied. A translation
48+ # has the form `action=translation`. The shortcut `action` is equivalent to
49+ # `action=action`. If no translation applies then a non-zero status code is
50+ # returned. The `tool` action is not subject to translations.
51+ translate_action () {
52+ if $IS_TOOL ; then
53+ echo tool
54+ return
55+ fi
56+ for arg in " $@ " ; do
57+ case " $arg " in
58+ $ACTION )
59+ echo " $arg "
60+ return 0
61+ ;;
62+ ${ACTION} =* )
63+ # Pick out the part after the `=`
64+ echo " $arg " | sed " s/${ACTION} =\(.*\)/\1/"
65+ return 0
66+ ;;
67+ esac
68+ done
69+ echo " "
70+ return 1
71+ }
72+
4773# When running the appropriate external tool this function is used which
4874# evaluates the given command while respecting $QUIET and $DRY_RUN.
4975# shellcheck disable=SC2086
@@ -131,11 +157,7 @@ try_nodejs() {
131157 fi
132158 tool=$( detect_nodejs_tool)
133159 has_command_or_error " $tool " package.json
134- node_action=" $ACTION "
135- # Only the "run" action need translation, the others match 1-to-1
136- if [ " $ACTION " = run ]; then
137- node_action=start
138- fi
160+ node_action=$( translate_action build run=start test)
139161 # We check if the package.json file contains an appropriate script. We use
140162 # grep for this. The check is not 100% bulletproof, but it's very close. We
141163 # could've used `npm run` to get the authorative list of the scripts but
@@ -164,14 +186,11 @@ try_cargo() {
164186
165187try_meson () {
166188 if [ -f meson.build ]; then
167- case $ACTION in
168- build) execute_command meson compile ;;
169- test | tool) execute_command meson " $ACTION " ;;
170- run)
171- echo " projectdo does not know how to run a project with Meson."
172- exit
173- ;;
174- esac
189+ has_command_or_error meson meson.build
190+ action=$( translate_action test build=compile)
191+ if [ $? -eq 0 ]; then
192+ execute_command meson " $action "
193+ fi
175194 fi
176195}
177196
@@ -215,15 +234,11 @@ try_cabal() {
215234
216235try_maven () {
217236 if [ -f pom.xml ]; then
218- has_command_or_error mvn pom.xml maven
219- case $ACTION in
220- build) execute_command mvn compile ;;
221- run)
222- echo " projectdo does not know how to run a project with Maven."
223- exit
224- ;;
225- test | tool) execute_command mvn " $ACTION " ;;
226- esac
237+ action=$( translate_action test build=compile)
238+ if [ $? -eq 0 ]; then
239+ has_command_or_error mvn pom.xml maven
240+ execute_command mvn " $action "
241+ fi
227242 fi
228243}
229244
@@ -303,11 +318,8 @@ try_makefile() {
303318try_nix_flake () {
304319 if [ -f flake.nix ]; then
305320 has_command_or_error nix flake.nix
306- if [ " $ACTION " = test ]; then
307- execute_command nix " flake check"
308- else
309- execute_command nix " $ACTION "
310- fi
321+ action=$( translate_action " test=flake check" build run)
322+ execute_command nix " $action "
311323 fi
312324 return 1
313325}
@@ -317,9 +329,8 @@ try_nix_flake() {
317329try_nix_package () {
318330 if [ -f default.nix ]; then
319331 has_command_or_error nix-build default.nix
320- if [ " $ACTION " = build ] || [ " $ACTION " = tool ]; then
321- execute_command nix-build
322- fi
332+ action=$( translate_action build=)
333+ execute_command nix-build " $action "
323334 fi
324335 return 1
325336}
@@ -329,23 +340,15 @@ try_nix_package() {
329340try_python () {
330341 if [ -f pyproject.toml ]; then
331342 if grep -q -m 1 " ^\[tool.poetry\]$" pyproject.toml; then
332- case $ACTION in
333- build | tool) execute_command poetry " $ACTION " ;;
334- test) execute_command poetry " run pytest" ;;
335- run)
336- echo " projectdo does not know how to run a project with Poetry."
337- exit
338- ;;
339- esac
343+ action=$( translate_action build " test=run pytest" )
344+ if [ $? -eq 0 ]; then
345+ execute_command poetry " $action "
346+ fi
340347 else
341348 if [ -f uv.lock ]; then
342349 case $ACTION in
343- build)
344- echo " projectdo does not know how to run a project with uv."
345- exit
346- ;;
347- test)
348- echo " projectdo does not know how to test a project with uv."
350+ build | test)
351+ echo " projectdo does not know how to $ACTION a project with uv."
349352 exit
350353 ;;
351354 run | tool) execute_command uv " $ACTION " ;;
@@ -362,24 +365,21 @@ try_python() {
362365# Go
363366
364367try_go () {
368+ # We detect Makefiles before we detect Go, so here we know that the Go project
369+ # is not tested by a Makefile.
365370 if [ -f go.mod ]; then
366- if [ " $ACTION " = test ]; then
367- # We detect Makefiles before we detect Go, so here we know that the Go
368- # project is _not_ tested by a Makefile.
369-
370- # Check if the project uses Mage. A magefile could in theory have any name,
371- # but `magefile.go` seems to be the convention.
372- if [ -f magefile.go ]; then
373- if grep -q -m 1 ' ^func Check(' magefile.go; then
374- execute_command mage check
375- elif grep -q -m 1 ' ^func Test(' magefile.go; then
376- execute_command mage test
377- fi
371+
372+ # Check if the project uses Mage. A magefile could in theory have any name,
373+ # but `magefile.go` seems to be the convention.
374+ if [ " $ACTION " = test ] && [ -f magefile.go ]; then
375+
376+ if grep -q -m 1 ' ^func Check(' magefile.go; then
377+ execute_command mage check
378+ elif grep -q -m 1 ' ^func Test(' magefile.go; then
379+ execute_command mage test
378380 fi
379- execute_command go test
380- else
381- execute_command go " $ACTION "
382381 fi
382+ execute_command go " $ACTION "
383383 fi
384384}
385385
0 commit comments