๐Ÿ“ฆ Bogdanp / racket-chief

๐Ÿ“„ procfile.rkt ยท 65 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65#lang racket/base

(require racket/match)

(provide
 (struct-out procdef)
 read-procdef
 read-procdefs)

(define procdef-re
  #rx"(?m:([^:]+):[ ]+(.+))\n?")

(struct procdef (name command)
  #:transparent)

(define (read-procdef [in (current-input-port)])
  (define-values (line col pos)
    (port-next-location in))

  (match (regexp-match procdef-re in)
    [#f
     (if (and line col)
         (error 'read-procdef "invalid process definition at ~a:~a" line col)
         (error 'read-procdef "invalid process definition at pos ~a" pos))]
    [(list _ name command) (procdef (bytes->string/utf-8 name)
                                    (bytes->string/utf-8 command))]))

(define (read-procdefs [in (current-input-port)])
  (let loop ([procdefs null])
    (if (eof-object? (peek-byte in))
        (reverse procdefs)
        (loop (cons (read-procdef in) procdefs)))))

(module+ test
  (require rackunit)

  (check-exn
   exn:fail?
   (lambda _
     (read-procdef (open-input-string ""))))

  (check-exn
   exn:fail?
   (lambda _
     (read-procdef (open-input-string "web:"))))

  (check-equal?
   (read-procdef (open-input-string "web: npm start"))
   (procdef "web" "npm start"))

  (define multiline #<<PROCFILE
assets: npm start
web: raco koyo serve
PROCFILE
    )

  (let ([multiline-in (open-input-string multiline)])
    (check-equal? (read-procdef multiline-in) (procdef "assets" "npm start"))
    (check-equal? (read-procdef multiline-in) (procdef "web" "raco koyo serve")))

  (let ([multiline-in (open-input-string multiline)])
    (check-equal? (read-procdefs multiline-in)
                  (list (procdef "assets" "npm start")
                        (procdef "web" "raco koyo serve")))))