This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/bin/pipeline

65 lines
1.6 KiB
Bash
Executable file

#!/usr/bin/env sh
SAVED_PIPELINE_DIR="$DOTFILES_PATH/bin/pipelines"
mkdir -p "$SAVED_PIPELINE_DIR"
if [ -n "${1+x}" ]; then
pdir="$SAVED_PIPELINE_DIR/$1"; shift
mkdir -p "$pdir"
if [ -z ${1+x} ]; then
td="$(mktemp -p "$pdir" -d "tmp_pipeline.XXXXXXXX")"
cleanup_trap="rm -rf \"$td\""
else
td="$pdir/$1"; shift
mkdir -p "$td"
fi
transform="$pdir/transform"
else
td="$(mktemp --tmpdir -d pipeline.XXXXXXXX)"
transform="$td/transform"
cleanup_trap="rm -rf \"$td\""
fi
[ ! -e "$transform" ] && \
printf "#!/usr/bin/env bash\n# for %s\n\nbase64" "${td}" > "$transform"
in="$td/in"
[ -n "${1+x}" ] && in="${1}" && shift
out="$td/out"
[ -n "${1+x}" ] && out="${1}" && shift
# TODO: if no logging, log="/dev/null"
log="$td/log"
chmod +x "$transform"
echo "Hello World" > "$in"
touch "$log"
fswi \
"bash -c 'cd \"$td\" && < \"$in\" \"$transform\" | tee \"$out\"'" \
'in$|transform$' \
"$td" "$pdir" > "$log" 2>&1 &
watcher="$!"
if [ -z "${cleanup_trap+x}" ]; then
trap "kill \"$watcher\"" EXIT
else
trap "${cleanup_trap}; kill \"$watcher\"" EXIT
fi
job="$(echo "let g:pipeline_job_id = jobstart(['inotifywait', '-m', '-e', 'close_write', '${out}'], {'detach':1,'on_stdout':{j,d,e->execute('checktime')}})" | tr "'" '"')"
echo $job >> "$log"
# this will only autosave (and therefore autoreload) for the out buffer (since
# it was opened last) - autosaving (and therefore autorunning) is potentially
# very scary
nvim -V9"$td/vimlog" \
-o "$in" "$transform" "$out" \
--cmd "$job" \
--cmd 'set updatetime=250' \
--cmd 'autocmd CursorHold,CursorHoldI <buffer> silent write' \
--cmd 'autocmd VimLeave call jobstop(g:pipeline_job_id)' \
--cmd "$job"