navi icon indicating copy to clipboard operation
navi copied to clipboard

Multiple commands in a single 'recipe' get their variables set to zero.

Open i30817 opened this issue 3 years ago • 3 comments

Describe the bug I suspect that this can be worked around by making a script file taking the variables as a argument, so they appear like a single command to navi, but basically if i (ab)use navi 'support' for multiple calls of different executables/scripts ending up with something like:

$ use_debug: echo 'false true' | tr ' ' '\n' --- --prevent-extra --map "[[ $0 == t* ]] && echo -n 1 || echo -n 0"

; this will be empty on failure
$ retroarch_branch: git -C /home/i3/Documents/Projects/RetroArch fetch origin &>/dev/null && { git branch --no-column --format '%(refname)' -a | sed '/HEAD detached at/d' | sed 's/^refs\/\(heads\|remotes\)\///g'; } --- --prevent-extra

; this will be empty on failure
$ retroarch_pr: git -C /home/i3/Documents/Projects/RetroArch ls-remote -q origin 'pull/*/head' 2>/dev/null | cut -f2 | sed -E 's/[^[:digit:]]*([[:digit:]]+)[^[:digit:]]+/\1/' | sort -rn --- --prevent-extra

$ discord: echo "--disable-discord --enable-discord" | tr ' ' '\n' --- --prevent-extra
$ qt: echo "--disable-qt --enable-qt" | tr ' ' '\n' --- --prevent-extra
$ cdrom: echo "--disable-cdrom --enable-cdrom" | tr ' ' '\n' --- --prevent-extra
$ caca: echo "--disable-caca --enable-caca" | tr ' ' '\n' --- --prevent-extra
$ sdl: echo "--disable-sdl --enable-sdl" | tr ' ' '\n' --- --prevent-extra
$ opengl1: echo "--disable-opengl1 --enable-opengl1" | tr ' ' '\n' --- --prevent-extra
$ ffmpeg: echo "--disable-ffmpeg --enable-ffmpeg" | tr ' ' '\n' --- --prevent-extra
$ flac: echo "--disable-flac --enable-flac" | tr ' ' '\n' --- --prevent-extra
$ wayland: echo "--disable-wayland --enable-wayland" | tr ' ' '\n' --- --prevent-extra
$ kms: echo "--disable-kms --enable-kms" | tr ' ' '\n' --- --prevent-extra
$ sdl2: echo "--enable-sdl2 --disable-sdl2" | tr ' ' '\n' --- --prevent-extra
$ x11: echo "--enable-x11 --disable-x11" | tr ' ' '\n' --- --prevent-extra
$ opengl: echo "--enable-opengl --disable-opengl" | tr ' ' '\n' --- --prevent-extra
$ vulkan: echo "--enable-vulkan --disable-vulkan" | tr ' ' '\n' --- --prevent-extra

$ retroarch_cfg: echo '<discord> <qt> <cdrom> <caca> <sdl> <opengl1> <ffmpeg> <flac> <wayland> <kms> <sdl2> <x11> <opengl> <vulkan>'

# retroarch
( \
cd /home/i3/Documents/Projects/RetroArch; \
BRCH='<retroarch_branch>'; \
[[ "$BRCH" != "" ]] || { echo "Failed connecting to git origin" >&2; printf "\a"; exit 1; }; \
OUTPUT=$( { git checkout "$BRCH" ; } 2>&1 ) || { echo "Failed checking out branch" >&2; printf "\a"; exit 1; }; \
[[ "$OUTPUT" == *"Your branch "* ]] && { git pull && git submodule update && make clean || { echo "Failed pulling, updating or cleaning branch" >&2; printf "\a"; exit 1; }; }; \
./configure <retroarch_cfg>  && make -j$(nproc) DEBUG=<use_debug> GL_DEBUG=<use_debug> TARGET=retroarch; \
printf "\a"; \
)

The variables at every step ask for the possible values, but don't show them on top.

To Reproduce Eh... just fill the variables there? In fact, let me just get them for you quick.

Expected behavior At least during a single 'recipe' that navi would remember display the chosen variables, not have = 0 all the time except when choosing one of them.

Screenshots If applicable, add screenshots to help explain your problem.

Versions:

  • OS: [e.g. macOS, WSL ubuntu, ubuntu]
  • Shell Version [replace this text with the output of sh --version]

Additional context Add any other context about the problem here.

i30817 avatar Nov 21 '21 15:11 i30817

Right, this was caused my the --map on a debug false or true for some reason.

$ use_debug: echo 'false true' | tr ' ' '\n' --- --prevent-extra

showed the other options,

$ use_debug: echo 'false true' | tr ' ' '\n' --- --prevent-extra --map "[[ $0 == t* ]] && echo -n 1 || echo -n 0"

as soon as it appeared appeared to 'reset' them.

A workaround is only doing the 'mapping' in a back section of the script, not inside the variable.

i30817 avatar Nov 21 '21 17:11 i30817

I'm sorry, I'm afraid I don't fully understand what you mean. Could you please share a minimal example?

denisidoro avatar Nov 22 '21 11:11 denisidoro

Sure. In the following the script -m arguments are arguments to make. In fact, fuck it, i'll post it at the end.

This:

$ use_debug: echo 'false true' | tr ' ' '\n' --- --prevent-extra
$ ags_build_dir: echo "/home/i3/Documents/Projects/ags/Engine" --- --prevent-extra
$ ags_branch: git -C <ags_build_dir> fetch origin &>/dev/null; git -C <ags_build_dir> branch --no-column --format '%(refname)' -a | sed '/HEAD detached at/d' | sed 's/^refs\/\(heads\|remotes\)\///g' --- --prevent-extra

# ags
( \
[[ <use_debug> == t* ]] && DBG="debug" || DBG="ags"; \
cd <ags_build_dir>; github-branch-pull -m $DBG -m USE_BUILT_IN_LIBSRC=1 -m TARGET=ags <ags_branch>; printf "\a"; \
)

Looks like this at the start: Screenshot from 2021-11-22 12-09-23

While if you change that to this:

$ use_debug: echo 'false true' | tr ' ' '\n' ---  --prevent-extra --map "[[ $0 == t* ]] && echo -n debug || echo -n ags"
$ ags_build_dir: echo "/home/i3/Documents/Projects/ags/Engine" --- --prevent-extra
$ ags_branch: git -C <ags_build_dir> fetch origin &>/dev/null; git -C <ags_build_dir> branch --no-column --format '%(refname)' -a | sed '/HEAD detached at/d' | sed 's/^refs\/\(heads\|remotes\)\///g' --- --prevent-extra

# ags
( \
cd <ags_build_dir>; github-branch-pull -m <use_debug> -m USE_BUILT_IN_LIBSRC=1 -m TARGET=ags <ags_branch>; printf "\a"; \
)

Starts like this: Screenshot from 2021-11-22 12-26-54

either map is kind of broken, to be spilling to other variables or (quite possibly) i was using it wrong.

Mind you i like the way i'm using it now more, since it's more flexible and allows me to use it in multiple rules (for instance retroarch requires passing 'DEBUG=1' or 'DEBUG=0'.

Silly fragile script for pulling branches or prs from github (actually the fetch origin is in the navi script and the pr name fetch too, that's the reason it pulls the pr here. I should probably extract that to the navi script too, so the script is purely local) only and that only builds maybe correctly in a 'configure / makefile' context:

#!/bin/bash

ME=$(basename "$0")

function usage(){
    cat >&2 <<ENDOFHELP
Usage: $ME [-h] [-p] [-c OPT1 -c OPT2...] [-m OPT1 -m OPT2...] BRANCH_NAME
$ME pulls a branch or pr from a github repository
and assuming that there is at least one configure
or make argument, calls calls make or configure.
    -h show this help
    -p the name is from a github pull request not a regular remote branch
    -c configure option string (will call configure)
    -m make option string (will call make)
ENDOFHELP
}

CONF=()
MAKE=()
while getopts "hpc:m:" opt; do
  case $opt in
    h) usage; exit 0; ;;
    p) PR=1; ;;
    c) CONF+=("${OPTARG}"); ;;
    m) MAKE+=("${OPTARG}"); ;;
    *) echo >&2 "$ME: unknown option ${OPTARG}. Exit"; usage; exit 1; ;;
  esac
done
shift $((OPTIND-1))

(( "$#" != 1 ))   && { echo  >&2 "$ME: requires a branch or pull name. Exit"; exit 1; }
[[ ! -d "$PWD" ]] && { echo  >&2 "$ME: could not retrieve current directory. Exit"; exit 1; }
NAME="$1"


[[ -v PR ]] && { git fetch origin pull/$NAME/head:$NAME   || { echo "Failed connecting to git origin" >&2; exit 1; }; }
OUTPUT=$( { git checkout "$NAME" ; } 2>&1 ) || { echo "Failed checking out branch" >&2; exit 1; }
[[ "$OUTPUT" == *"Your branch "* ]] && { git pull && git submodule update && make clean || { echo "Failed pulling, updating or cleaning branch" >&2; exit 1; }; }

#empty arrays fail -v
if [[ -v CONF ]] && [[ -v MAKE ]]; then
	./configure "${CONF[@]}" && make -j$(nproc) "${MAKE[@]}"
elif [[ -v CONF ]]; then
	./configure "${CONF[@]}"
elif [[ -v MAKE ]]; then
	make -j$(nproc) "${MAKE[@]}"
fi

i30817 avatar Nov 22 '21 12:11 i30817