aoc2022/scripts/watch

45 lines
1.1 KiB
Plaintext
Raw Normal View History

2022-12-01 05:11:06 +00:00
#!/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)
2022-12-01 06:02:24 +00:00
(match (filesystem-change-evt file (λ () #f))
[#f
(displayln "error watching file...")
(sleep 1)]
[chg-evt
(define runner (thread (lambda () (reinvoke file))))
2022-12-01 05:11:06 +00:00
2022-12-01 06:02:24 +00:00
(define (handle-break _)
(cond
[(sync/timeout 0 runner)
(displayln "exiting")
(exit)]
[else
(displayln "killing module")
(kill-thread runner)
(filesystem-change-evt-cancel chg-evt)]))
2022-12-01 05:11:06 +00:00
2022-12-01 06:02:24 +00:00
(with-handlers ([exn:break? handle-break])
(sync chg-evt)
(kill-thread runner))])
2022-12-01 05:11:06 +00:00
(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)))