Pipeline stuff

This commit is contained in:
Daniel Flanagan 2020-02-13 11:14:03 -06:00
parent 3f00303e97
commit 7fa86968e1
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
5 changed files with 34 additions and 47 deletions

View file

@ -1,11 +1,13 @@
scriptencoding utf-8
set fileencoding=utf8
let $vimdir = $HOME.'/.vim' let $vimdir = $HOME.'/.vim'
if has('nvim') if has('nvim')
let $vimdir = $XDG_CONFIG_HOME.'/nvim' let $vimdir = $XDG_CONFIG_HOME.'/nvim'
endif endif
" load a per-environment file if one exists if filereadable($ENV_PATH."/vim")
if filereadable("$ENV_PATH/vim") source $ENV_PATH."/vim"
source "$ENV_PATH/vim"
endif endif
call plug#begin($vimdir.'/bundle') call plug#begin($vimdir.'/bundle')

View file

@ -1,4 +1,4 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# TODO: needs fixing...? # TODO: needs fixing...?
[ -f "$HOME/.fzf.bash" ] && source "$HOME/.fzf.bash" && $(__fzf_cd__) cd "$(dirname "$(fzf)")"

22
bin/fsw
View file

@ -5,7 +5,7 @@ _VERSION="0.2.0"
function help() { function help() {
I=" " I=" "
cat << USAGE cat << USAGE
fsw - run a command when a file is modified fsw - run a command when a file is modified - v$_VERSION
Usage: Usage:
${I}fsw <command> [filter] [dirs...] ${I}fsw <command> [filter] [dirs...]
@ -41,11 +41,17 @@ USAGE
} }
dbg() { dbg() {
if [[ ! -z ${FSW_DEBUG+x} ]]; then if [[ -n ${FSW_DEBUG+x} ]]; then
echo -e "[debug] fsw: $@" echo -e "[debug] fsw: $*"
fi fi
} }
contains_element () {
local e match="$1"; shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
if [[ $1 = '-h' ]] || [[ $1 = '--help' ]] || [[ -z $1 ]]; then if [[ $1 = '-h' ]] || [[ $1 = '--help' ]] || [[ -z $1 ]]; then
help help
exit 0 exit 0
@ -64,16 +70,20 @@ if [[ -e $FILTER ]]; then
echo "It looks like your filter is an actual file. I'll just watch that for you." echo "It looks like your filter is an actual file. I'll just watch that for you."
DIRS=("${FILTER}") DIRS=("${FILTER}")
fi fi
while [[ ! -z $1 ]] && $(realpath $1) &> /dev/null; do while [[ -n $1 ]] && realpath "$1" &> /dev/null; do
dbg "Directory: ${1}" dbg "Directory: ${1}"
DIRS+=("$1"); shift DIRS+=("$1"); shift
done done
inotifywait -m -e "${FSW_EVENTS}" -r "${DIRS[@]}" 2>&1 \ inotifywait -m -e "${FSW_EVENTS},delete,delete_self" -r "${DIRS[@]}" 2>&1 \
| grep --line-buffered -v ' Beware: since -r was given, this may take a while!' \ | grep --line-buffered -v ' Beware: since -r was given, this may take a while!' \
| while read -r dir events filename; do | while read -r dir events filename; do
if contains_element "$dir" "${DIRS[@]}" && [[ $events =~ "delete" ]]; then
echo "One of the watched directories (\"$dir\") was deleted. Exiting..."
exit 75
fi
if [[ "$dir $events" = "Watches established." ]]; then if [[ "$dir $events" = "Watches established." ]]; then
echo "Ready." echo "Ready."
dbg "Directory: ${DIRS[@]}" dbg "Directory: ${DIRS[*]}"
else else
export FSW_FILENAME="$filename" export FSW_FILENAME="$filename"
export FSW_DIR="$dir" export FSW_DIR="$dir"

View file

@ -1,4 +1,4 @@
#!/usr/bin/env bash #!/usr/bin/env bash
eval "${1}" eval "${1}"
fsw "$@" exec fsw "$@"

View file

@ -8,7 +8,7 @@ if [ -n "${1+x}" ]; then
mkdir -p "$pdir" mkdir -p "$pdir"
if [ -z ${1+x} ]; then if [ -z ${1+x} ]; then
td="$(mktemp -p "$pdir" -d "tmp_pipeline.XXXXXXXX")" td="$(mktemp -p "$pdir" -d "tmp_pipeline.XXXXXXXX")"
cleanup_trap="rm -rf \"$td\"" trap "rm -rf '$td'" EXIT
else else
td="$pdir/$1"; shift td="$pdir/$1"; shift
mkdir -p "$td" mkdir -p "$td"
@ -17,48 +17,23 @@ if [ -n "${1+x}" ]; then
else else
td="$(mktemp --tmpdir -d pipeline.XXXXXXXX)" td="$(mktemp --tmpdir -d pipeline.XXXXXXXX)"
transform="$td/transform" transform="$td/transform"
cleanup_trap="rm -rf \"$td\"" trap "rm -rf '$td'" EXIT
fi fi
[ ! -e "$transform" ] && \ [ ! -e "$transform" ] && \
printf "#!/usr/bin/env bash\n# for %s\n\nbase64" "${td}" > "$transform" printf "#!/usr/bin/env bash\n# you must save this buffer manually\n# for %s\n
base64" "${td}" > "$transform"
in="$td/in" in="$td/in"; [ -n "${1+x}" ] && in="${1}" && shift
[ -n "${1+x}" ] && in="${1}" && shift out="$td/out"; [ -n "${1+x}" ] && out="${1}" && shift
out="$td/out"
[ -n "${1+x}" ] && out="${1}" && shift
# TODO: if no logging, log="/dev/null" # TODO: if no logging, log="/dev/null"
log="$td/log" log="$td/log"
chmod +x "$transform"
echo "Hello World" > "$in"
touch "$log" touch "$log"
fswi \ chmod +x "$transform"
"bash -c 'cd \"$td\" && < \"$in\" \"$transform\" | tee \"$out\"'" \
'in$|transform$' \
"$td" "$pdir" > "$log" 2>&1 &
watcher="$!" # TODO: saved inputs?
echo "Hello World" > "$in"
if [ -z "${cleanup_trap+x}" ]; then env PIPELINE_IN="$id" PIPELINE_TRANSFORM="$transform" PIPELINE_OUT="$out" nvim --cmd "source $DOTFILES_PATH/apps/neovim/scripts/pipeline.vim"
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"