aoc2023/scripts/watch

45 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)
(match (filesystem-change-evt file (λ () #f))
[#f
(displayln "error watching file...")
(sleep 1)]
[chg-evt
(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)))