This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
dig.rkt
152 lines (118 loc) · 3.63 KB
/
dig.rkt
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#lang racket/base
(require racket/contract)
(define shovel/c
(-> any/c (subprogram/c artifact?)))
(provide
(contract-out
[install-found-artifact
(->* (any/c
path-string?)
(shovel/c)
(subprogram/c (cons/c path-record? path-record?)))]
[find-artifact
(->* (any/c)
(shovel/c)
(subprogram/c artifact?))]
[shovel/c
chaperone-contract?]
[broken-shovel
shovel/c]
[dig-failure
(-> (or/c symbol? string?)
any/c
subprogram?)]
[current-shovel
(parameter/c shovel/c)]
[shovel-cons
(-> shovel/c shovel/c shovel/c)]
[shovel-list
(->* () #:rest (listof shovel/c) shovel/c)]))
(require "artifact.rkt"
"monad.rkt"
"setting.rkt"
"state.rkt"
"subprogram.rkt"
"message.rkt"
"query.rkt")
(define+provide-message $dig ())
(define+provide-message $dig:no-artifact $dig (shovel-name hint))
(define+provide-setting DENXI_INSTALL_ARTIFACTS
(listof (list/c string? string?)) null)
(define (dig-failure name hint)
(subprogram-failure ($dig:no-artifact name hint)))
(define (broken-shovel v)
(dig-failure (object-name broken-shovel) v))
(define ((shovel-cons a b) k)
(subprogram (λ (m)
(define-values (r m*) (run-subprogram (a k) m))
(if (artifact? r)
(values r m*)
(run-subprogram (b k) m*)))))
(define (shovel-list . xs)
(if (null? xs)
broken-shovel
(shovel-cons
(car xs)
(apply shovel-list
(cdr xs)))))
(define current-shovel
(make-parameter broken-shovel))
(define (install-found-artifact plinth link-path [dig (current-shovel)])
(mdo arti := (find-artifact plinth dig)
records := (install-artifact arti link-path)
(subprogram-unit records)))
(define-subprogram (find-artifact plinth [dig (current-shovel)])
(if (artifact? plinth)
($use plinth)
($run! (dig plinth))))
(module+ test
(require rackunit
(submod "subprogram.rkt" test))
(test-case "Combine shovels"
(define ((bind-shovel v) k)
(if (eq? k v)
(subprogram-unit (make-artifact v))
(dig-failure v k)))
(define shovel
(shovel-list
(bind-shovel #"a")
(bind-shovel #"b")
(bind-shovel #"c")))
(define (check expected)
(check-equal? (artifact-source (get-subprogram-value (shovel expected)))
expected))
(check #"a")
(check #"b")
(check #"c")
(define-values (should-be-failure messages)
(run-subprogram (shovel #"d")))
(check-eq? should-be-failure FAILURE)
(check-equal? messages
(list ($dig:no-artifact 'broken-shovel #"d")
($dig:no-artifact #"c" #"d")
($dig:no-artifact #"b" #"d")
($dig:no-artifact #"a" #"d"))))
(test-subprogram-value
"Trivially find provided artifact"
(find-artifact (make-artifact #"") broken-shovel)
(λ (result)
(check-equal? (artifact-source result) #"")))
(test-subprogram
"Do not find an artifact by default when none is available"
(find-artifact #f)
(λ (result messages)
(check-equal? result FAILURE)
(check-equal? messages
(list ($dig:no-artifact 'broken-shovel #f)))))
(test-case "Find an artifact using the right shovel"
(define (indy req)
(subprogram-unit
(make-artifact
(if req #"t" #"f"))))
(define (check v expected)
(check-subprogram-value
(find-artifact v indy)
(λ (result)
(check-equal? (artifact-source result) expected))))
(check #t #"t")
(check #f #"f")))