aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/meta/io/archive.lux
blob: 346a05e567234ecd5d9036048a6d345cca17d6eb (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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
(.using
 [library
  [lux "*"
   [target {"+" Target}]
   [abstract
    [predicate {"+" Predicate}]
    ["[0]" monad {"+" do}]]
   [control
    ["[0]" try {"+" Try}]
    [concurrency
     ["[0]" async {"+" Async} ("[1]#[0]" monad)]]
    ["<>" parser
     ["<[0]>" binary {"+" Parser}]]]
   [data
    [binary {"+" Binary}]
    ["[0]" product]
    ["[0]" text ("[1]#[0]" equivalence)
     ["%" format {"+" format}]]
    [collection
     ["[0]" list ("[1]#[0]" functor mix)]
     ["[0]" dictionary {"+" Dictionary}]
     ["[0]" sequence {"+" Sequence}]
     ["[0]" set {"+" Set}]]]
   [math
    [number
     ["n" nat]]]
   [meta
    ["[0]" configuration {"+" Configuration}]
    ["[0]" version]]
   [world
    ["[0]" file]]]]
 ["[0]" //
  ["[1][0]" context]
  ["/[1]" //
   [import {"+" Import}]
   ["[0]" context {"+" Context}]
   ["[0]" archive {"+" Output Archive}
    ["[0]" registry {"+" Registry}]
    ["[0]" unit]
    ["[0]" artifact {"+" Artifact}
     ["[0]" category {"+" Category}]]
    ["[0]" module
     ["[0]" descriptor {"+" Descriptor}]
     ["[0]" document {"+" Document}]]]
   ["[0]" cache
    ["[1]/[0]" archive]
    ["[1]/[0]" module]
    ["[0]" dependency "_"
     ["[1]" module]]]
   ["/[1]" // {"+" Input}
    [language
     ["$" lux
      ["[0]" analysis]
      ["[0]" synthesis]
      ["[0]" generation]
      ["[0]" directive]
      ["[1]/[0]" program]]]]]])

(def: module_parser
  (Parser (module.Module .Module))
  ($_ <>.and
      <binary>.nat
      descriptor.parser
      (document.parser $.key $.parser)))

(def: parser
  (Parser [(module.Module .Module) Registry])
  ($_ <>.and
      ..module_parser
      registry.parser))

(def: (fresh_analysis_state host configuration)
  (-> Target Configuration .Lux)
  (analysis.state (analysis.info version.latest host configuration)))

(def: (analysis_state host configuration archive)
  (-> Target Configuration Archive (Try .Lux))
  (do [! try.monad]
    [modules (: (Try (List [descriptor.Module .Module]))
                (monad.each ! (function (_ module)
                                (do !
                                  [entry (archive.find module archive)
                                   content (|> entry
                                               (value@ [archive.#module module.#document])
                                               (document.content $.key))]
                                  (in [module content])))
                            (archive.archived archive)))]
    (in (with@ .#modules modules (fresh_analysis_state host configuration)))))

(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.empty text.hash)
   (dictionary.empty text.hash)
   (dictionary.empty text.hash)
   (dictionary.empty text.hash)])

(def: (loaded_document extension host @module expected actual document)
  (All (_ expression directive)
    (-> Text (generation.Host expression directive) module.ID (Sequence [Artifact (Set unit.ID)]) (Dictionary Text Binary) (Document .Module)
        (Try [(Document .Module) Bundles Output])))
  (do [! try.monad]
    [[definitions bundles] (: (Try [Definitions Bundles Output])
                              (loop [input (sequence.list expected)
                                     definitions (: Definitions
                                                    (dictionary.empty text.hash))
                                     bundles ..empty_bundles
                                     output (: Output sequence.empty)]
                                (let [[analysers synthesizers generators directives] bundles]
                                  (case input
                                    {.#Item [[[@artifact artifact_category mandatory_artifact?] artifact_dependencies] input']}
                                    (case (do !
                                            [data (try.of_maybe (dictionary.value (format (%.nat @artifact) extension) actual))
                                             .let [context [@module @artifact]
                                                   directive (# host ingest context data)]]
                                            (case artifact_category
                                              {category.#Anonymous}
                                              (do !
                                                [.let [output (sequence.suffix [@artifact {.#None} data] output)]
                                                 _ (# host re_learn context {.#None} directive)]
                                                (in [definitions
                                                     [analysers
                                                      synthesizers
                                                      generators
                                                      directives]
                                                     output]))
                                              
                                              {category.#Definition [name function_artifact]}
                                              (let [output (sequence.suffix [@artifact {.#None} data] output)]
                                                (if (text#= $/program.name name)
                                                  (in [definitions
                                                       [analysers
                                                        synthesizers
                                                        generators
                                                        directives]
                                                       output])
                                                  (do !
                                                    [value (# host re_load context {.#None} directive)]
                                                    (in [(dictionary.has name value definitions)
                                                         [analysers
                                                          synthesizers
                                                          generators
                                                          directives]
                                                         output]))))

                                              {category.#Analyser extension}
                                              (do !
                                                [.let [output (sequence.suffix [@artifact {.#None} data] output)]
                                                 value (# host re_load context {.#None} directive)]
                                                (in [definitions
                                                     [(dictionary.has extension (:as analysis.Handler value) analysers)
                                                      synthesizers
                                                      generators
                                                      directives]
                                                     output]))

                                              {category.#Synthesizer extension}
                                              (do !
                                                [.let [output (sequence.suffix [@artifact {.#None} data] output)]
                                                 value (# host re_load context {.#None} directive)]
                                                (in [definitions
                                                     [analysers
                                                      (dictionary.has extension (:as synthesis.Handler value) synthesizers)
                                                      generators
                                                      directives]
                                                     output]))

                                              {category.#Generator extension}
                                              (do !
                                                [.let [output (sequence.suffix [@artifact {.#None} data] output)]
                                                 value (# host re_load context {.#None} directive)]
                                                (in [definitions
                                                     [analysers
                                                      synthesizers
                                                      (dictionary.has extension (:as generation.Handler value) generators)
                                                      directives]
                                                     output]))

                                              {category.#Directive extension}
                                              (do !
                                                [.let [output (sequence.suffix [@artifact {.#None} data] output)]
                                                 value (# host re_load context {.#None} directive)]
                                                (in [definitions
                                                     [analysers
                                                      synthesizers
                                                      generators
                                                      (dictionary.has extension (:as directive.Handler value) directives)]
                                                     output]))

                                              {category.#Custom name}
                                              (do !
                                                [.let [output (sequence.suffix [@artifact {.#Some name} data] output)]
                                                 _ (# host re_learn context {.#Some name} directive)]
                                                (in [definitions
                                                     [analysers
                                                      synthesizers
                                                      generators
                                                      directives]
                                                     output]))))
                                      {try.#Success [definitions' bundles' output']}
                                      (again input' definitions' bundles' output')

                                      failure
                                      failure)
                                    
                                    {.#End}
                                    {try.#Success [definitions bundles output]}))))
     content (document.content $.key document)
     definitions (monad.each ! (function (_ [def_name def_global])
                                 (case def_global
                                   (^template [<tag>]
                                     [{<tag> payload}
                                      (in [def_name {<tag> payload}])])
                                   ([.#Alias]
                                    [.#Tag]
                                    [.#Slot])
                                   
                                   {.#Definition [exported? type _]}
                                   (|> definitions
                                       (dictionary.value def_name)
                                       try.of_maybe
                                       (# ! each (|>> [exported? type]
                                                      {.#Definition}
                                                      [def_name])))

                                   {.#Type [exported? _ labels]}
                                   (|> definitions
                                       (dictionary.value def_name)
                                       try.of_maybe
                                       (# ! each (function (_ def_value)
                                                   [def_name {.#Type [exported? (:as .Type def_value) labels]}])))))
                             (value@ .#definitions content))]
    (in [(document.document $.key (with@ .#definitions definitions content))
         bundles])))

(def: (load_definitions fs context @module host_environment entry)
  (All (_ expression directive)
    (-> (file.System Async) Context module.ID (generation.Host expression directive)
        (archive.Entry .Module)
        (Async (Try [(archive.Entry .Module) Bundles]))))
  (do (try.with async.monad)
    [actual (: (Async (Try (Dictionary Text Binary)))
               (cache/module.artifacts async.monad fs context @module))
     .let [expected (registry.artifacts (value@ archive.#registry entry))]
     [document bundles output] (|> (value@ [archive.#module module.#document] entry)
                                   (loaded_document (value@ context.#artifact_extension context) host_environment @module expected actual)
                                   async#in)]
    (in [(|> entry
             (with@ [archive.#module module.#document] document)
             (with@ archive.#output output))
         bundles])))

(def: (purge! fs context [module_name @module])
  (-> (file.System Async) Context [descriptor.Module module.ID] (Async (Try Any)))
  (do [! (try.with async.monad)]
    [.let [cache (cache/module.path fs context @module)]
     _ (|> cache
           (# fs directory_files)
           (# ! each (monad.each ! (# fs delete)))
           (# ! conjoint))]
    (# fs delete cache)))

(def: (valid_cache? expected actual)
  (-> Descriptor Input Bit)
  (and (text#= (value@ descriptor.#name expected)
               (value@ ////.#module actual))
       (text#= (value@ descriptor.#file expected)
               (value@ ////.#file actual))
       (n.= (value@ descriptor.#hash expected)
            (value@ ////.#hash actual))))

(type: Cache
  [descriptor.Module [module.ID [(module.Module .Module) Registry]]])

(type: Purge
  (Dictionary descriptor.Module module.ID))

(def: initial_purge
  (-> (List [Bit Cache])
      Purge)
  (|>> (list.all (function (_ [valid_cache? [module_name [@module _]]])
                   (if valid_cache?
                     {.#None}
                     {.#Some [module_name @module]})))
       (dictionary.of_list text.hash)))

(def: (full_purge caches load_order)
  (-> (List [Bit Cache])
      (dependency.Order .Module)
      Purge)
  (list#mix (function (_ [module_name [@module entry]] purge)
              (let [purged? (: (Predicate descriptor.Module)
                               (dictionary.key? purge))]
                (if (purged? module_name)
                  purge
                  (if (|> entry
                          (value@ [archive.#module module.#descriptor descriptor.#references])
                          set.list
                          (list.any? purged?))
                    (dictionary.has module_name @module purge)
                    purge))))
            (..initial_purge caches)
            load_order))

(def: pseudo_module
  Text
  "(Lux Caching System)")

(def: (valid_cache fs context import contexts [module_name @module])
  (-> (file.System Async) Context Import (List //.Context)
      [descriptor.Module module.ID]
      (Async (Try [Bit Cache])))
  (with_expansions [<cache> [module_name [@module [module registry]]]]
    (do [! (try.with async.monad)]
      [data (: (Async (Try Binary))
               (cache/module.cache fs context @module))
       [module registry] (async#in (<binary>.result ..parser data))]
      (if (text#= descriptor.runtime module_name)
        (in [true <cache>])
        (do !
          [input (//context.read fs ..pseudo_module import contexts (value@ context.#host_module_extension context) module_name)]
          (in [(..valid_cache? (value@ module.#descriptor module) input) <cache>]))))))

(def: (pre_loaded_caches fs context import contexts archive)
  (-> (file.System Async) Context Import (List //.Context) Archive
      (Async (Try (List [Bit Cache]))))
  (do [! (try.with async.monad)]
    [... TODO: Stop needing to wrap this expression in an unnecessary "do" expression.
     it (|> archive
            archive.reservations
            (monad.each !
                        (..valid_cache fs context import contexts)))]
    (in it)))

(def: (load_order archive pre_loaded_caches)
  (-> Archive (List [Bit Cache])
      (Try (dependency.Order .Module)))
  (|> pre_loaded_caches
      (monad.mix try.monad
                 (function (_ [_ [module [@module [|module| registry]]]] archive)
                   (archive.has module
                                [archive.#module |module|
                                 archive.#output (: Output sequence.empty)
                                 archive.#registry registry]
                                archive))
                 archive)
      (# try.monad each (dependency.load_order $.key))
      (# try.monad conjoint)))

(def: (loaded_caches host_environment fs context purge load_order)
  (All (_ expression directive)
    (-> (generation.Host expression directive) (file.System Async) Context
        Purge (dependency.Order .Module)
        (Async (Try (List [[descriptor.Module (archive.Entry .Module)] Bundles])))))
  (do [! (try.with async.monad)]
    [... TODO: Stop needing to wrap this expression in an unnecessary "do" expression.
     it (|> load_order
            (list.only (|>> product.left (dictionary.key? purge) not))
            (monad.each ! (function (_ [module_name [@module entry]])
                            (do !
                              [[entry bundles] (..load_definitions fs context @module host_environment entry)]
                              (in [[module_name entry]
                                   bundles])))))]
    (in it)))

(def: (load_every_reserved_module configuration host_environment fs context import contexts archive)
  (All (_ expression directive)
    (-> Configuration (generation.Host expression directive) (file.System Async) Context Import (List //.Context) Archive
        (Async (Try [Archive .Lux Bundles]))))
  (do [! (try.with async.monad)]
    [pre_loaded_caches (..pre_loaded_caches fs context import contexts archive)
     load_order (async#in (load_order archive pre_loaded_caches))
     .let [purge (..full_purge pre_loaded_caches load_order)]
     _ (|> purge
           dictionary.entries
           (monad.each ! (..purge! fs context)))
     loaded_caches (..loaded_caches host_environment fs context purge load_order)]
    (async#in
     (do [! try.monad]
       [archive (monad.mix !
                           (function (_ [[module entry] _bundle] archive)
                             (archive.has module entry archive))
                           archive
                           loaded_caches)
        analysis_state (..analysis_state (value@ context.#host context) configuration archive)]
       (in [archive
            analysis_state
            (list#mix (function (_ [_ [+analysers +synthesizers +generators +directives]]
                                   [analysers synthesizers generators directives])
                        [(dictionary.merged +analysers analysers)
                         (dictionary.merged +synthesizers synthesizers)
                         (dictionary.merged +generators generators)
                         (dictionary.merged +directives directives)])
                      ..empty_bundles
                      loaded_caches)])))))

(def: .public (thaw configuration host_environment fs context import contexts)
  (All (_ expression directive)
    (-> Configuration (generation.Host expression directive) (file.System Async) Context Import (List //.Context)
        (Async (Try [Archive .Lux Bundles]))))
  (do async.monad
    [binary (# fs read (cache/archive.descriptor fs context))]
    (case binary
      {try.#Success binary}
      (do (try.with async.monad)
        [archive (async#in (archive.import ///.version binary))]
        (..load_every_reserved_module configuration host_environment fs context import contexts archive))
      
      {try.#Failure error}
      (in {try.#Success [archive.empty
                         (fresh_analysis_state (value@ context.#host context) configuration)
                         ..empty_bundles]}))))