git.fiddlerwoaroof.com
Raw Blame History
#+TITLE: socket-based command runner
#+TAGS: utilities
#+AUTHOR: Ed Langley

** Introduction

Run a command everytime a line is written to a unix socket, passing
the line written as the last argument to the command. Useful in an
=after-save-hook= in emacs to run tests after the buffer is saved.
Unlike file-watchers, this allows more precise control of when the
tests are run, reducing the amount of time spent configuring ignore
files and waiting for spurious reruns.

** Building

Choose either building with zig:

#+BEGIN_SRC zsh :results output :post proc
  zig build --verbose
#+END_SRC

Or nix:

#+BEGIN_SRC zsh :results output :post proc
  nix build github:fiddlerwoaroof/sigexec
#+END_SRC

One can also run the flake's default app like:

#+BEGIN_SRC zsh :results output :post proc
  nix run github:fiddlerwoaroof/sigexec <socket> <command...>
#+END_SRC

** Usage

#+BEGIN_SRC zsh
  rm /tmp/jest.sock ; $OLDPWD/zig-out/bin/zig-test /tmp/jest.sock npx jest --
#+END_SRC


** Full-stack example:

*** In one terminal:

#+begin_src zsh
  % nix run github:fiddlerwoaroof/sigexec /tmp/bar.sock echo NOTICE ME: input:
  # output:
  # warning: listening at /tmp/bar.sock
  #
  # NOTICE ME: input: 4
#+end_src

*** In another terminal:

#+begin_src zsh
  # in another terminal:
  % echo 4 | socat - unix-connect:/tmp/bar.sock
  # ACK!
#+end_src

#+NAME: proc
#+BEGIN_SRC elisp :exports none
  (defun fix (fn inp)
    (cl-loop for old = inp then new
             for new = (funcall fn inp)
             until (equal new old)
             finally (cl-return new)))
  (s-join
   "\n"
   (funcall (fwoar/over (lambda (it)
                          (    (lambda (v)
                                 (s-replace-regexp " \\(--\\|#\\)" " \\\\\n  \\1" v))
                               (s-replace-regexp "/nix/store/[^/]*/\\(.* \\)"
                                                 "/nix/.../\\1"
                                                 (s-replace "/Users/edwlan" "~" it)))))
            (s-lines (s-trim *this*))))
#+END_SRC