|
1 | | -#lang typed/racket/base |
2 | | -(require racket/match) |
3 | | -(require/typed setup/dirs [get-doc-search-dirs (-> (Listof Path))]) |
4 | | -(require/typed racket/serialize [deserialize (Any -> Any)]) |
5 | | -(require/typed scribble/core [#:opaque Tag tag?]) |
6 | | -(require/typed scribble/tag |
7 | | - [#:opaque Method-Tag method-tag?] |
8 | | - [#:opaque Definition-Tag definition-tag?] |
9 | | - [#:opaque Class/Interface-Tag class/interface-tag?] |
10 | | - [class/interface-tag->constructor-tag (Class/Interface-Tag -> Tag)] |
11 | | - [definition-tag->class/interface-tag (Definition-Tag -> Class/Interface-Tag)] |
12 | | - [get-class/interface-and-method (Method-Tag -> (values Symbol Symbol))] |
13 | | - ) |
14 | | -(require/typed "valid-blueboxes-info.rkt" [valid-blueboxes-info? (Any -> Boolean)]) |
| 1 | +#lang racket/base |
| 2 | +(require racket/match |
| 3 | + racket/contract/base |
| 4 | + setup/dirs |
| 5 | + racket/serialize |
| 6 | + scribble/core |
| 7 | + scribble/tag |
| 8 | + "valid-blueboxes-info.rkt") |
15 | 9 |
|
16 | | -(provide fetch-blueboxes-strs |
17 | | - make-blueboxes-cache |
18 | | - blueboxes-cache? |
19 | | - fetch-blueboxes-method-tags |
20 | | - ) |
| 10 | +(provide |
| 11 | + blueboxes-cache? |
| 12 | + (contract-out |
| 13 | + [fetch-blueboxes-strs (->* (tag?) |
| 14 | + (#:blueboxes-cache blueboxes-cache?) |
| 15 | + (or/c #f (non-empty-listof string?)))] |
| 16 | + [make-blueboxes-cache (->* (boolean?) |
| 17 | + (#:blueboxes-dirs (listof path?)) |
| 18 | + blueboxes-cache?)] |
| 19 | + [fetch-blueboxes-method-tags (->* (symbol?) |
| 20 | + (#:blueboxes-cache blueboxes-cache?) |
| 21 | + (listof method-tag?))])) |
21 | 22 |
|
22 | | -(define-type Bluebox-Info bluebox-info) |
23 | | -(struct bluebox-info |
24 | | - ([blueboxes.rktd : Path-String] |
25 | | - [offset : (U Natural #f)] |
26 | | - [tag-ht : (U Blueboxes-Info-Hash #f)] ; (or/c valid-blueboxes-info? #f) |
27 | | - [mod-time : (U Natural #f)]) |
| 23 | +(struct bluebox-info (blueboxes.rktd |
| 24 | + offset |
| 25 | + tag-ht |
| 26 | + mod-time) |
28 | 27 | #:mutable) |
29 | 28 |
|
30 | | -(define-type Blueboxes-Cache blueboxes-cache) |
31 | | -(struct blueboxes-cache |
32 | | - ([info-or-paths : (U (Listof Path) (Listof Bluebox-Info))] |
33 | | - [method->tags : (U (HashTable Symbol (Listof Method-Tag)) #f)]) |
| 29 | +(struct blueboxes-cache (info-or-paths |
| 30 | + method->tags) |
34 | 31 | #:mutable) |
35 | 32 |
|
36 | | -(: make-blueboxes-cache : |
37 | | - Boolean |
38 | | - [#:blueboxes-dirs (Listof Path)] |
39 | | - -> |
40 | | - Blueboxes-Cache) |
41 | 33 | (define (make-blueboxes-cache |
42 | 34 | populate? |
43 | 35 | #:blueboxes-dirs |
44 | 36 | [blueboxes-dirs (for*/list ([d (in-list (get-doc-search-dirs))] |
45 | 37 | [c (in-list (if (directory-exists? d) |
46 | 38 | (directory-list d) |
47 | 39 | '()))]) |
48 | | - : (Listof Path) |
49 | 40 | (build-path d c))]) |
50 | 41 | (define cache (blueboxes-cache blueboxes-dirs #f)) |
51 | 42 | (when populate? (populate-cache! cache)) |
52 | 43 | cache) |
53 | 44 |
|
54 | | -(: fetch-blueboxes-strs : |
55 | | - Tag |
56 | | - [#:blueboxes-cache Blueboxes-Cache] |
57 | | - -> |
58 | | - (U #f (List* String (Listof String)))) |
59 | 45 | (define (fetch-blueboxes-strs tag #:blueboxes-cache [cache (make-blueboxes-cache #f)]) |
60 | 46 | (define plain-strs (fetch-strs-for-single-tag tag cache)) |
61 | 47 | (cond |
|
70 | 56 | (if constructor-strs (cdr constructor-strs) '()))] |
71 | 57 | [else plain-strs])) |
72 | 58 |
|
73 | | -(: fetch-strs-for-single-tag : Tag Blueboxes-Cache -> (U #f (List* String (Listof String)))) |
74 | 59 | (define (fetch-strs-for-single-tag tag cache) |
75 | 60 | (populate-cache! cache) |
76 | 61 | (for/or ([ent (in-list (blueboxes-cache-info-or-paths cache))]) |
77 | | - : (U #f (List* String (Listof String))) |
78 | 62 | (when (bluebox-info? ent) |
79 | 63 | (check-and-update-bluebox-info! ent)) |
80 | 64 | (match ent |
|
86 | 70 | (apply |
87 | 71 | append |
88 | 72 | (for/list ([offset+len (in-list offset+lens)]) |
89 | | - : (Listof (Listof (U String EOF))) |
90 | 73 | (call-with-input-file blueboxes.rktd |
91 | | - (λ ([port : Input-Port]) |
| 74 | + (λ (port) |
92 | 75 | (port-count-lines! port) |
93 | 76 | (file-position port (+ (car offset+len) (or offset 0))) |
94 | 77 | (for/list ([i (in-range (cdr offset+len))]) |
95 | | - : (Listof (U String EOF)) |
96 | 78 | (read-line port))))))) |
97 | 79 | (cond |
98 | 80 | [(not (andmap string? lines)) #f] |
|
102 | 84 | [_ (log-warning "expected bluebox-info?, given: ~v" ent) |
103 | 85 | #f]))) |
104 | 86 |
|
105 | | -(: fetch-blueboxes-method-tags : Symbol [#:blueboxes-cache Blueboxes-Cache] -> (Listof Method-Tag)) |
106 | 87 | (define (fetch-blueboxes-method-tags sym #:blueboxes-cache [cache (make-blueboxes-cache #f)]) |
107 | 88 | (populate-cache! cache) |
108 | 89 | (define ht (blueboxes-cache-method->tags cache)) |
109 | 90 | (or (and ht (hash-ref ht sym (λ () '()))) '())) |
110 | 91 |
|
111 | | -(define listof-path? (make-predicate (Listof Path))) |
112 | | - |
113 | | -(: populate-cache! : Blueboxes-Cache -> Void) |
114 | 92 | (define (populate-cache! cache) |
115 | 93 | (define cache-content (blueboxes-cache-info-or-paths cache)) |
116 | | - (when (listof-path? cache-content) |
| 94 | + (when (and (list? cache-content) (andmap path? cache-content)) |
117 | 95 | (define the-cache (build-blueboxes-cache cache-content)) |
118 | 96 | (define mtd-table (compute-methods-table the-cache)) |
119 | 97 | (set-blueboxes-cache-method->tags! cache mtd-table) |
120 | 98 | (set-blueboxes-cache-info-or-paths! cache the-cache))) |
121 | 99 |
|
122 | | -(: compute-methods-table : (Listof Bluebox-Info) -> (HashTable Symbol (Listof Method-Tag))) |
123 | 100 | (define (compute-methods-table lst) |
124 | | - (: meth-ht : (HashTable Symbol (Listof Method-Tag))) |
125 | 101 | (define meth-ht (make-hash)) |
126 | 102 | (for ([a-bluebox-info (in-list lst)]) |
127 | 103 | (match a-bluebox-info |
|
133 | 109 | (hash-set! meth-ht meth (cons tag (hash-ref meth-ht meth (λ () '())))))))])) |
134 | 110 | meth-ht) |
135 | 111 |
|
136 | | -(: build-blueboxes-cache : (Listof Path) -> (Listof Bluebox-Info)) |
137 | 112 | (define (build-blueboxes-cache blueboxes-dirs) |
138 | 113 | (filter |
139 | 114 | values |
140 | 115 | (for*/list ([doc-dir-name (in-list blueboxes-dirs)]) |
141 | | - : (Listof Bluebox-Info) |
142 | 116 | (define blueboxes.rktd (build-path doc-dir-name "blueboxes.rktd")) |
143 | 117 | (define a-bluebox-info (bluebox-info blueboxes.rktd #f #f #f)) |
144 | 118 | (populate-bluebox-info! a-bluebox-info) |
145 | 119 | a-bluebox-info))) |
146 | 120 |
|
147 | | -(: check-and-update-bluebox-info! : bluebox-info -> Void) |
148 | 121 | (define (check-and-update-bluebox-info! a-bluebox-info) |
149 | 122 | (match a-bluebox-info |
150 | 123 | [(bluebox-info blueboxes.rktd offset tag-ht mod-time) |
|
153 | 126 | (not (mod-time . = . (file-or-directory-modify-seconds blueboxes.rktd))))) |
154 | 127 | (populate-bluebox-info! a-bluebox-info))])) |
155 | 128 |
|
156 | | -(: populate-bluebox-info! : Bluebox-Info -> Void) |
157 | 129 | (define (populate-bluebox-info! a-bluebox-info) |
158 | 130 | (define blueboxes.rktd (bluebox-info-blueboxes.rktd a-bluebox-info)) |
159 | 131 | (cond |
160 | 132 | [(file-exists? blueboxes.rktd) |
161 | 133 | (call-with-input-file blueboxes.rktd |
162 | | - (λ ([port : Input-Port]) |
| 134 | + (λ (port) |
163 | 135 | (port-count-lines! port) |
164 | 136 | (define first-line (read-line port)) |
165 | 137 | (define pos (file-position port)) |
166 | 138 | (define desed |
167 | | - (with-handlers ([exn:fail? (λ ([x : exn:fail]) |
| 139 | + (with-handlers ([exn:fail? (λ (x) |
168 | 140 | (log-warning "Failed to deserialize ~a: ~a" |
169 | 141 | x |
170 | 142 | (exn-message x)) |
|
174 | 146 | (error 'build-blueboxes-cache |
175 | 147 | "blueboxes info didn't have the right shape: ~s" |
176 | 148 | candidate)) |
177 | | - (cast candidate Blueboxes-Info-Hash))) |
| 149 | + candidate)) |
178 | 150 | (define first-line-num (and (string? first-line) (string->number first-line))) |
179 | 151 | (cond |
180 | 152 | [(exact-nonnegative-integer? first-line-num) |
|
189 | 161 | (set-bluebox-info-offset! a-bluebox-info #f) |
190 | 162 | (set-bluebox-info-tag-ht! a-bluebox-info #f) |
191 | 163 | (set-bluebox-info-mod-time! a-bluebox-info #f)])) |
192 | | - |
193 | | -(define-type Blueboxes-Info-Hash |
194 | | - (HashTable |
195 | | - Tag |
196 | | - (Listof (Pairof Natural |
197 | | - Natural)))) |
198 | | - |
0 commit comments