aoc2020/scripts/watch

43 lines
1.1 KiB
Racket
Executable File

#!/usr/bin/env racket
#lang racket
(require racket/rerequire racket/runtime-path)
(define (reinvoke file)
(displayln "invoking module")
(define p (build-path file))
(dynamic-rerequire p)
(dynamic-require (list 'submod p 'main) #f)
(displayln "invocation complete"))
(define (watch-forever file)
(sleep 0.2)
(define chg-evt (filesystem-change-evt file (lambda ()
(displayln "error watching file...")
(sleep 1) (watch-forever file))))
(define runner (thread (lambda () (reinvoke file))))
(define (handle-break _)
(cond
[(sync/timeout 0 runner)
(displayln "exiting")
(exit)]
[else
(displayln "killing module")
(kill-thread runner)
(filesystem-change-evt-cancel chg-evt)]))
(with-handlers ([exn:break? handle-break])
(sync chg-evt)
(kill-thread runner))
(watch-forever file))
(define-runtime-path aoc "aoc.rkt")
(command-line
#:program "watch"
#:args (day)
(displayln "warming up")
(dynamic-require aoc #f)
(displayln "ready to go!")
(watch-forever (format "~a.rkt" day)))