aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/meta/io/archive.lux
blob: eef5907d2cfb3f70fc648150dcc77db7d5d929b1 (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
(.module:
  [lux (#- Module)
   ["@" target (#+ Host)]
   [abstract
    ["." monad (#+ do)]]
   [control
    ["." try (#+ Try)]
    ["." exception (#+ exception:)]
    [concurrency
     ["." promise (#+ Promise) ("#@." monad)]]
    [security
     ["!" capability (#+ capability:)]]
    ["<>" parser
     ["<b>" binary (#+ Parser)]]]
   [data
    [binary (#+ Binary)]
    ["." product]
    ["." text ("#@." equivalence)
     ["%" format (#+ format)]]
    [collection
     ["." list ("#@." functor fold)]
     ["." dictionary (#+ Dictionary)]
     ["." row (#+ Row)]]]
   [world
    ["." file (#+ Path File Directory)]]]
  [program
   [compositor
    ["." static (#+ Static)]]]
  ["." //
   ["/#" //
    ["." archive (#+ Archive)
     ["." artifact (#+ Artifact)]
     ["." descriptor (#+ Module Descriptor)]
     ["." document (#+ Document)]]
    [cache
     ["." dependency]]
    [//
     [language
      ["$" lux
       ["." version]
       ["." analysis]
       ["." synthesis]
       ["." generation]
       ["." directive]]]]]])

(exception: #export (cannot-prepare {archive Path}
                                    {module-id archive.ID}
                                    {error Text})
  (exception.report
   ["Archive" archive]
   ["Module ID" (%.nat module-id)]
   ["Error" error]))

(def: (archive system static)
  (All [!] (-> (file.System !) Static Path))
  (format (get@ #static.target static)
          (:: system separator)
          (get@ #static.host static)))

(def: (unversioned-lux-archive system static)
  (All [!] (-> (file.System !) Static Path))
  (format (..archive system static)
          (:: system separator)
          //.lux-context))

(def: (versioned-lux-archive system static)
  (All [!] (-> (file.System !) Static Path))
  (format (..unversioned-lux-archive system static)
          (:: system separator)
          (%.nat ///.version)))

(def: (module system static module-id)
  (All [!] (-> (file.System !) Static archive.ID Path))
  (format (..versioned-lux-archive system static)
          (:: system separator)
          (%.nat module-id)))

(def: #export (artifact system static module-id name)
  (All [!] (-> (file.System !) Static archive.ID Text Path))
  (format (..module system static module-id)
          (:: system separator)
          name
          (get@ #static.artifact-extension static)))

(def: #export (prepare system static module-id)
  (-> (file.System Promise) Static archive.ID (Promise (Try Any)))
  (do {@ promise.monad}
    [#let [module (..module system static module-id)]
     module-exists? (file.exists? promise.monad system module)]
    (if module-exists?
      (wrap (#try.Success []))
      (do @
        [_ (file.get-directory @ system (..unversioned-lux-archive system static))
         _ (file.get-directory @ system (..versioned-lux-archive system static))
         outcome (!.use (:: system create-directory) module)]
        (case outcome
          (#try.Success output)
          (wrap (#try.Success []))

          (#try.Failure error)
          (wrap (exception.throw ..cannot-prepare [(..archive system static)
                                                   module-id
                                                   error])))))))

(def: #export (write system static module-id name content)
  (-> (file.System Promise) Static archive.ID Text Binary (Promise (Try Any)))
  (do (try.with promise.monad)
    [artifact (: (Promise (Try (File Promise)))
                 (file.get-file promise.monad system
                                (..artifact system static module-id name)))]
    (!.use (:: artifact over-write) content)))

(def: #export (enable system static)
  (-> (file.System Promise) Static (Promise (Try Any)))
  (do (try.with promise.monad)
    [_ (: (Promise (Try (Directory Promise)))
          (file.get-directory promise.monad system (get@ #static.target static)))
     _ (: (Promise (Try (Directory Promise)))
          (file.get-directory promise.monad system (..archive system static)))]
    (wrap [])))

(def: (general-descriptor system static)
  (-> (file.System Promise) Static Path)
  (format (..archive system static)
          (:: system separator)
          "general-descriptor"))

(def: #export (freeze system static archive)
  (-> (file.System Promise) Static Archive (Promise (Try Any)))
  (do (try.with promise.monad)
    [file (: (Promise (Try (File Promise)))
             (file.get-file promise.monad system (..general-descriptor system static)))]
    (!.use (:: file over-write) (archive.export ///.version archive))))

(def: module-descriptor-file
  "module-descriptor")

(def: (module-descriptor system static module-id)
  (-> (file.System Promise) Static archive.ID Path)
  (format (..module system static module-id)
          (:: system separator)
          ..module-descriptor-file))

(def: #export (cache system static module-id content)
  (-> (file.System Promise) Static archive.ID Binary (Promise (Try Any)))
  (do (try.with promise.monad)
    [file (: (Promise (Try (File Promise)))
             (file.get-file promise.monad system
                            (..module-descriptor system static module-id)))]
    (!.use (:: file over-write) content)))

(def: (read-module-descriptor system static module-id)
  (-> (file.System Promise) Static archive.ID (Promise (Try Binary)))
  (do (try.with promise.monad)
    [file (: (Promise (Try (File Promise)))
             (file.get-file promise.monad system
                            (..module-descriptor system static module-id)))]
    (!.use (:: file content) [])))

(def: parser
  (Parser [Descriptor (Document .Module)])
  (<>.and descriptor.parser
          (document.parser $.parser)))

(def: (fresh-analysis-state host)
  (-> Host .Lux)
  (analysis.state (analysis.info version.version host)))

(def: (analysis-state host archive)
  (-> Host Archive (Try .Lux))
  (do {@ try.monad}
    [modules (: (Try (List [Module .Module]))
                (monad.map @ (function (_ module)
                               (do @
                                 [[descriptor document] (archive.find module archive)
                                  content (document.read $.key document)]
                                 (wrap [module content])))
                           (archive.archived archive)))]
    (wrap (set@ #.modules modules (fresh-analysis-state host)))))

(def: (cached-artifacts system static module-id)
  (-> (file.System Promise) Static archive.ID (Promise (Try (Dictionary Text Binary))))
  (do {@ (try.with promise.monad)}
    [module-dir (!.use (:: system directory) (..module system static module-id))
     cached-files (!.use (:: module-dir files) [])]
    (|> cached-files
        (list@map (function (_ file)
                    [(!.use (:: file name) [])
                     (!.use (:: file path) [])]))
        (list.filter (|>> product.left (text@= ..module-descriptor-file) not))
        (monad.map @ (function (_ [name path])
                       (do @
                         [file (: (Promise (Try (File Promise)))
                                  (!.use (:: system file) path))
                          data (: (Promise (Try Binary))
                                  (!.use (:: file content) []))]
                         (wrap [name data]))))
        (:: @ map (dictionary.from-list text.hash)))))

(type: Definitions (Dictionary Text Any))
(type: Analysers (Dictionary Text analysis.Handler))
(type: Synthesizers (Dictionary Text synthesis.Handler))
(type: Generators (Dictionary Text generation.Handler))
(type: Directives (Dictionary Text directive.Handler))

(type: Bundles
  [Analysers
   Synthesizers
   Generators
   Directives])

(def: empty-bundles
  Bundles
  [(dictionary.new text.hash)
   (dictionary.new text.hash)
   (dictionary.new text.hash)
   (dictionary.new text.hash)])

(def: (loaded-document extension host module-id expected actual document)
  (All [expression directive]
    (-> Text (generation.Host expression directive) archive.ID (Row Artifact) (Dictionary Text Binary) (Document .Module)
        (Try [(Document .Module) Bundles])))
  (do {@ try.monad}
    [[definitions bundles] (: (Try [Definitions Bundles])
                              (loop [input (row.to-list expected)
                                     definitions (: Definitions
                                                    (dictionary.new text.hash))
                                     bundles ..empty-bundles]
                                (let [[analysers synthesizers generators directives] bundles]
                                  (case input
                                    (#.Cons [[artifact-id artifact-category] input'])
                                    (case (do @
                                            [data (try.from-maybe (dictionary.get (format (%.nat artifact-id) extension) actual))
                                             #let [context [module-id artifact-id]
                                                   directive (:: host ingest context data)]]
                                            (case artifact-category
                                              #artifact.Anonymous
                                              (do @
                                                [_ (:: host re-learn context directive)]
                                                (wrap [definitions
                                                       [analysers
                                                        synthesizers
                                                        generators
                                                        directives]]))
                                              
                                              (#artifact.Definition name)
                                              (do @
                                                [value (:: host re-load context directive)]
                                                (wrap [(dictionary.put name value definitions)
                                                       [analysers
                                                        synthesizers
                                                        generators
                                                        directives]]))

                                              (#artifact.Analyser extension)
                                              (do @
                                                [value (:: host re-load context directive)]
                                                (wrap [definitions
                                                       [(dictionary.put extension (:coerce analysis.Handler value) analysers)
                                                        synthesizers
                                                        generators
                                                        directives]]))

                                              (#artifact.Synthesizer extension)
                                              (do @
                                                [value (:: host re-load context directive)]
                                                (wrap [definitions
                                                       [analysers
                                                        (dictionary.put extension (:coerce synthesis.Handler value) synthesizers)
                                                        generators
                                                        directives]]))

                                              (#artifact.Generator extension)
                                              (do @
                                                [value (:: host re-load context directive)]
                                                (wrap [definitions
                                                       [analysers
                                                        synthesizers
                                                        (dictionary.put extension (:coerce generation.Handler value) generators)
                                                        directives]]))

                                              (#artifact.Directive extension)
                                              (do @
                                                [value (:: host re-load context directive)]
                                                (wrap [definitions
                                                       [analysers
                                                        synthesizers
                                                        generators
                                                        (dictionary.put extension (:coerce directive.Handler value) directives)]]))))
                                      (#try.Success [definitions' bundles'])
                                      (recur input' definitions' bundles')

                                      failure
                                      failure)
                                    
                                    #.None
                                    (#try.Success [definitions bundles])))))
     content (document.read $.key document)
     definitions (monad.map @ (function (_ [def-name def-global])
                                (case def-global
                                  (#.Alias alias)
                                  (wrap [def-name (#.Alias alias)])
                                  
                                  (#.Definition [exported? type annotations _])
                                  (do @
                                    [value (try.from-maybe (dictionary.get def-name definitions))]
                                    (wrap [def-name (#.Definition [exported? type annotations value])]))))
                            (get@ #.definitions content))]
    (wrap [(document.write $.key (set@ #.definitions definitions content))
           bundles])))

(def: (load-definitions system static module-id host-environment [descriptor document])
  (All [expression directive]
    (-> (file.System Promise) Static archive.ID (generation.Host expression directive)
        [Descriptor (Document .Module)]
        (Promise (Try [[Descriptor (Document .Module)]
                       Bundles]))))
  (do (try.with promise.monad)
    [actual (cached-artifacts system static module-id)
     #let [expected (|> descriptor (get@ #descriptor.registry) artifact.artifacts)]
     [document bundles] (promise@wrap (loaded-document (get@ #static.artifact-extension static) host-environment module-id expected actual document))]
    (wrap [[descriptor document] bundles])))

(def: (load-every-reserved-module host-environment system static archive)
  (All [expression directive]
    (-> (generation.Host expression directive) (file.System Promise) Static Archive
        (Promise (Try [Archive
                       .Lux
                       Bundles]))))
  (do {@ (try.with promise.monad)}
    [pre-loaded-caches (|> archive
                           archive.reservations
                           (monad.map @ (function (_ [module-name module-id])
                                          (do @
                                            [data (..read-module-descriptor system static module-id)
                                             descriptor,document (promise@wrap (<b>.run ..parser data))]
                                            (wrap [module-name [module-id descriptor,document]])))))
     load-order (|> pre-loaded-caches
                    (monad.fold try.monad
                                (function (_ [module [module-id descriptor,document]] archive)
                                  (archive.add module descriptor,document archive))
                                archive)
                    (:: try.monad map (dependency.load-order $.key))
                    (:: try.monad join)
                    promise@wrap)
     loaded-caches (monad.map @ (function (_ [module-name [module-id descriptor,document]])
                                  (do @
                                    [[descriptor,document bundles] (..load-definitions system static module-id host-environment descriptor,document)]
                                    (wrap [[module-name descriptor,document]
                                           bundles])))
                              load-order)]
    (promise@wrap
     (do try.monad
       [archive (monad.fold try.monad
                            (function (_ [[module descriptor,document] _bundle] archive)
                              (archive.add module descriptor,document archive))
                            archive
                            loaded-caches)
        analysis-state (..analysis-state (get@ #static.host static) archive)]
       (wrap [archive
              analysis-state
              (list@fold (function (_ [_ [+analysers +synthesizers +generators +directives]]
                                      [analysers synthesizers generators directives])
                           [(dictionary.merge +analysers analysers)
                            (dictionary.merge +synthesizers synthesizers)
                            (dictionary.merge +generators generators)
                            (dictionary.merge +directives directives)])
                         ..empty-bundles
                         loaded-caches)])))))

(def: #export (thaw host-environment system static)
  (All [expression directive]
    (-> (generation.Host expression directive) (file.System Promise) Static
        (Promise (Try [Archive .Lux Bundles]))))
  (do promise.monad
    [file (!.use (:: system file) (..general-descriptor system static))]
    (case file
      (#try.Success file)
      (do (try.with promise.monad)
        [binary (!.use (:: file content) [])
         archive (promise@wrap (archive.import ///.version binary))]
        (..load-every-reserved-module host-environment system static archive))
      
      (#try.Failure error)
      (wrap (#try.Success [archive.empty
                           (fresh-analysis-state (get@ #static.host static))
                           ..empty-bundles])))))