blob: 6cd8022960373557905308f815226384471dd5aa (
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
|
(.module:
[lux #*
["." host (#+ import:)]
["@" target]
[abstract
["." monad (#+ Monad do)]]
[control
["." try (#+ Try) ("#@." functor)]
["." exception (#+ Exception exception:)]
["." io (#+ IO) ("#@." functor)]
[concurrency
["." promise (#+ Promise)]]
[security
["!" capability (#+ capability:)]]]
[data
["." product]
["." maybe]
["." binary (#+ Binary)]
["." text
["%" format (#+ format)]]
[number
["i" int]
["f" frac]]
[collection
["." array (#+ Array)]
["." list ("#@." functor)]]]
[time
["." instant (#+ Instant)]
["." duration]]
[macro
["." template]]])
(type: #export Path Text)
(capability: #export (Can-Open ! capability)
(can-open Path (! (Try (capability !)))))
(capability: #export (Can-See o)
(can-see [] o))
(capability: #export (Can-Query ! o)
(can-query [] (! (Try o))))
(capability: #export (Can-Modify ! i)
(can-modify [i] (! (Try Any))))
(capability: #export (Can-Delete !)
(can-delete [] (! (Try Any))))
(`` (signature: #export (File !)
(~~ (template [<name> <output>]
[(: (Can-See <output>)
<name>)]
[name Text]
[path Path]
))
(~~ (template [<name> <output>]
[(: (Can-Query ! <output>)
<name>)]
[size Nat]
[last-modified Instant]
[can-execute? Bit]
[content Binary]
))
(: (Can-Open ! File)
move)
(~~ (template [<name> <input>]
[(: (Can-Modify ! <input>)
<name>)]
[modify Instant]
[over-write Binary]
[append Binary]
))
(: (Can-Delete !)
delete)
))
(signature: #export (Directory !)
(: (Can-Query ! (List (File !)))
files)
(: (Can-Query ! (List (Directory !)))
directories)
(: (Can-Delete !)
discard))
(`` (signature: #export (System !)
(~~ (template [<name> <capability>]
[(: (Can-Open ! <capability>)
<name>)]
[file File]
[create-file File]
[directory Directory]
[create-directory Directory]
))
(: Text
separator)
))
(def: (async-file file)
(-> (File IO) (File Promise))
(`` (structure
(~~ (template [<forge> <name>+]
[(with-expansions [<rows> (template.splice <name>+)]
(template [<name>]
[(def: <name> (<forge> (|>> (!.use (:: file <name>)))))]
<rows>))]
[..can-see
[[name] [path]]]
))
(~~ (template [<forge> <name>+]
[(with-expansions [<rows> (template.splice <name>+)]
(template [<name>]
[(def: <name> (<forge> (|>> (!.use (:: file <name>)) promise.future)))]
<rows>))]
[..can-query
[[size] [last-modified] [can-execute?] [content]]]
[..can-modify
[[modify] [over-write] [append]]]
[..can-delete
[[delete]]]))
(def: move
(..can-open
(|>> (!.use (:: file move)) (io@map (try@map async-file)) promise.future))))))
(def: (async-directory directory)
(-> (Directory IO) (Directory Promise))
(`` (structure (~~ (template [<name> <async>]
[(def: <name> (..can-query
(|>> (!.use (:: directory <name>))
(io@map (try@map (list@map <async>)))
promise.future)))]
[files ..async-file]
[directories async-directory]))
(def: discard (..can-delete
(|>> (!.use (:: directory discard)) promise.future))))))
(def: #export (async system)
(-> (System IO) (System Promise))
(`` (structure
(~~ (template [<name> <async>]
[(def: <name> (..can-open
(|>> (!.use (:: system <name>)) (io@map (try@map <async>)) promise.future)))]
[file ..async-file]
[create-file ..async-file]
[directory ..async-directory]
[create-directory ..async-directory]))
(def: separator (:: system separator)))))
(def: #export (un-nest system file)
(All [!] (-> (System !) Path (Maybe [Path Text])))
(case (text.last-index-of (:: system separator) file)
#.None
#.None
(#.Some last-separator)
(let [[parent temp] (maybe.assume (text.split last-separator file))
[_ child] (maybe.assume (text.split (text.size (:: system separator)) temp))]
(#.Some [parent child]))))
(def: #export (nest system [parent child])
(All [!] (-> (System !) [Path Text] Path))
(format parent (:: system separator) child))
(template [<name>]
[(exception: #export (<name> {file Path})
(exception.report
["Path" file]))]
[cannot-create-file]
[cannot-find-file]
[cannot-delete-file]
[cannot-create-directory]
[cannot-find-directory]
[cannot-discard-directory]
[cannot-read-all-data]
[not-a-directory]
)
(with-expansions [<for-jvm> (as-is (exception: #export (cannot-move {target Path} {source Path})
(exception.report
["Source" source]
["Target" target]))
(exception: #export (cannot-modify {instant Instant} {file Path})
(exception.report
["Instant" (%.instant instant)]
["Path" file]))
(import: #long java/lang/String)
(import: #long java/io/File
(new [java/lang/String])
(~~ (template [<name>]
[(<name> [] #io #try boolean)]
[createNewFile] [mkdir]
[exists] [delete]
[isFile] [isDirectory]
[canRead] [canWrite] [canExecute]))
(getName [] java/lang/String)
(length [] #io #try long)
(listFiles [] #io #try #? [java/io/File])
(getAbsolutePath [] #io #try java/lang/String)
(renameTo [java/io/File] #io #try boolean)
(lastModified [] #io #try long)
(setLastModified [long] #io #try boolean)
(#static separator java/lang/String))
(template: (!delete path exception)
(do io.monad
[outcome (java/io/File::delete (java/io/File::new path))]
(case outcome
(#try.Success #1)
(wrap (#try.Success []))
_
(io.io (exception.throw exception [path])))))
(import: #long java/lang/AutoCloseable
(close [] #io #try void))
(import: #long java/io/OutputStream
(write [[byte]] #io #try void)
(flush [] #io #try void))
(import: #long java/io/FileOutputStream
(new [java/io/File boolean] #io #try))
(import: #long java/io/InputStream
(read [[byte]] #io #try int))
(import: #long java/io/FileInputStream
(new [java/io/File] #io #try))
(structure: (file path)
(-> Path (File IO))
(~~ (template [<name> <flag>]
[(def: <name>
(..can-modify
(function (<name> data)
(do (try.with io.monad)
[stream (java/io/FileOutputStream::new (java/io/File::new path) <flag>)
_ (java/io/OutputStream::write data stream)
_ (java/io/OutputStream::flush stream)]
(java/lang/AutoCloseable::close stream)))))]
[over-write #0]
[append #1]
))
(def: content
(..can-query
(function (content _)
(do (try.with io.monad)
[#let [file (java/io/File::new path)]
size (java/io/File::length file)
#let [data (binary.create (.nat size))]
stream (java/io/FileInputStream::new file)
bytes-read (java/io/InputStream::read data stream)
_ (java/lang/AutoCloseable::close stream)]
(if (i.= size bytes-read)
(wrap data)
(io.io (exception.throw cannot-read-all-data path)))))))
(def: name
(..can-see
(function (name _)
(|> path
java/io/File::new
java/io/File::getName))))
(def: path
(..can-see
(function (_ _)
path)))
(def: size
(..can-query
(function (size _)
(|> path
java/io/File::new
java/io/File::length
(:: (try.with io.monad) map .nat)))))
(def: last-modified
(..can-query
(function (last-modified _)
(|> path
java/io/File::new
(java/io/File::lastModified)
(:: (try.with io.monad) map (|>> duration.from-millis instant.absolute))))))
(def: can-execute?
(..can-query
(function (can-execute? _)
(|> path
java/io/File::new
java/io/File::canExecute))))
(def: move
(..can-open
(function (move destination)
(do io.monad
[outcome (java/io/File::renameTo (java/io/File::new destination)
(java/io/File::new path))]
(case outcome
(#try.Success #1)
(wrap (#try.Success (file destination)))
_
(io.io (exception.throw cannot-move [destination path])))))))
(def: modify
(..can-modify
(function (modify time-stamp)
(do io.monad
[outcome (java/io/File::setLastModified (|> time-stamp instant.relative duration.to-millis)
(java/io/File::new path))]
(case outcome
(#try.Success #1)
(wrap (#try.Success []))
_
(io.io (exception.throw cannot-modify [time-stamp path])))))))
(def: delete
(..can-delete
(function (delete _)
(!delete path cannot-delete-file)))))
(structure: (directory path)
(-> Path (Directory IO))
(~~ (template [<name> <method> <capability>]
[(def: <name>
(..can-query
(function (<name> _)
(do {@ (try.with io.monad)}
[?children (java/io/File::listFiles (java/io/File::new path))]
(case ?children
(#.Some children)
(|> children
array.to-list
(monad.filter @ (|>> <method>))
(:: @ map (monad.map @ (|>> java/io/File::getAbsolutePath (:: @ map <capability>))))
(:: @ join))
#.None
(io.io (exception.throw not-a-directory [path])))))))]
[files java/io/File::isFile file]
[directories java/io/File::isDirectory directory]
))
(def: discard
(..can-delete
(function (discard _)
(!delete path cannot-discard-directory)))))
(structure: #export system
(System IO)
(~~ (template [<name> <method> <capability> <exception>]
[(def: <name>
(..can-open
(function (<name> path)
(do io.monad
[#let [file (java/io/File::new path)]
outcome (<method> file)]
(case outcome
(#try.Success #1)
(wrap (#try.Success (<capability> path)))
_
(wrap (exception.throw <exception> [path])))))))]
[file java/io/File::isFile ..file cannot-find-file]
[create-file java/io/File::createNewFile ..file cannot-create-file]
[directory java/io/File::isDirectory ..directory cannot-find-directory]
[create-directory java/io/File::mkdir ..directory cannot-create-directory]
))
(def: separator (java/io/File::separator))
))]
(`` (for {(~~ (static @.old))
(as-is <for-jvm>)
(~~ (static @.jvm))
(as-is <for-jvm>)
(~~ (static @.js))
(as-is (import: Buffer
(#static from [Binary] ..Buffer))
(import: FileDescriptor)
(import: Stats
(size host.Number)
(mtimeMs host.Number)
(isFile [] #try host.Boolean)
(isDirectory [] #try host.Boolean))
(import: FsConstants
(F_OK host.Number)
(R_OK host.Number)
(W_OK host.Number)
(X_OK host.Number))
(import: Fs
(constants FsConstants)
(readFileSync [host.String] #try Binary)
(appendFileSync [host.String Buffer] #try Any)
(writeFileSync [host.String Buffer] #try Any)
(statSync [host.String] #try Stats)
(accessSync [host.String host.Number] #try Any)
(renameSync [host.String host.String] #try Any)
(utimesSync [host.String host.Number host.Number] #try Any)
(unlink [host.String] #try Any)
(readdirSync [host.String] #try (Array host.String))
(mkdirSync [host.String] #try Any)
(rmdirSync [host.String] #try Any))
(import: JsPath
(sep host.String)
(basename [host.String] host.String))
(import: (#static require [host.String] Any))
(template: (!fs)
(:coerce ..Fs (..require "fs")))
(structure: (file path)
(-> Path (File IO))
(~~ (template [<name> <method>]
[(def: <name>
(..can-modify
(function (<name> data)
(io.io (<method> [path (Buffer::from data)] (!fs))))))]
[over-write Fs::writeFileSync]
[append Fs::appendFileSync]
))
(def: content
(..can-query
(function (content _)
(io.io (Fs::readFileSync [path] (!fs))))))
(def: name
(..can-see
(function (name _)
(|> (..require "path")
(:coerce JsPath)
(JsPath::basename path)))))
(def: path
(..can-see
(function (_ _)
path)))
(def: size
(..can-query
(function (size _)
(|> (Fs::statSync [path] (!fs))
(:: try.monad map (|>> Stats::size frac-to-nat))
io.io))))
(def: last-modified
(..can-query
(function (last-modified _)
(|> (Fs::statSync [path] (!fs))
(:: try.monad map (|>> Stats::mtimeMs
f.int
duration.from-millis
instant.absolute))
io.io))))
(def: can-execute?
(..can-query
(function (can-execute? _)
(io.io (do try.monad
[_ (Fs::accessSync [path (|> (!fs) Fs::constants FsConstants::F_OK)] (!fs))]
(wrap (case (Fs::accessSync [path (|> (!fs) Fs::constants FsConstants::X_OK)] (!fs))
(#try.Success _)
true
(#try.Failure _)
false)))))))
(def: move
(..can-open
(function (move destination)
(io.io (do try.monad
[_ (Fs::renameSync [path destination] (!fs))]
(wrap (file destination)))))))
(def: modify
(..can-modify
(function (modify time-stamp)
(io.io (let [when (|> time-stamp instant.relative duration.to-millis i.frac)]
(Fs::utimesSync [path when when] (!fs)))))))
(def: delete
(..can-delete
(function (delete _)
(io.io (Fs::unlink [path] (!fs)))))))
(structure: (directory path)
(-> Path (Directory IO))
(~~ (template [<name> <method> <capability>]
[(def: <name>
(..can-query
(function (<name> _)
(io.io (let [fs (!fs)]
(do {@ try.monad}
[subs (Fs::readdirSync [path] fs)
subs (monad.map @ (function (_ sub)
(do @
[stats (Fs::statSync [sub] fs)
verdict (<method> [] stats)]
(wrap [verdict sub])))
(array.to-list subs))]
(wrap (|> subs
(list.filter product.left)
(list@map (|>> product.right <capability>))))))))))]
[files Stats::isFile ..file]
[directories Stats::isDirectory directory]
))
(def: discard
(..can-delete
(function (discard _)
(io.io (Fs::rmdirSync [path] (!fs)))))))
(structure: #export system (System IO)
(~~ (template [<name> <method> <capability> <exception>]
[(def: <name>
(..can-open
(function (<name> path)
(io.io (do try.monad
[stats (Fs::statSync [path] (!fs))
verdict (<method> [] stats)]
(if verdict
(wrap (<capability> path))
(exception.throw <exception> [path])))))))]
[file Stats::isFile ..file ..cannot-find-file]
[directory Stats::isDirectory ..directory ..cannot-find-directory]
))
(~~ (template [<name> <capability> <exception> <prep>]
[(def: <name>
(..can-open
(function (<name> path)
(io.io (let [fs (!fs)]
(case (Fs::accessSync [path (|> (!fs) Fs::constants FsConstants::F_OK)] fs)
(#try.Success _)
(exception.throw <exception> [path])
(#try.Failure _)
(do try.monad
[_ (|> fs <prep>)]
(wrap (<capability> path)))))))))]
[create-file ..file ..cannot-create-file (Fs::appendFileSync [path (Buffer::from (binary.create 0))])]
[create-directory ..directory ..cannot-create-directory (Fs::mkdirSync [path])]
))
(def: separator
(if host.on-node-js?
(|> (..require "path")
(:coerce JsPath)
JsPath::sep)
"/"))
)
)
})))
(template [<get> <signature> <create> <find> <exception>]
[(def: #export (<get> monad system path)
(All [!] (-> (Monad !) (System !) Path (! (Try (<signature> !)))))
(do monad
[outcome (!.use (:: system <create>) path)]
(case outcome
(#try.Success file)
(wrap (#try.Success file))
(#try.Failure error)
(if (exception.match? <exception> error)
(!.use (:: system <find>) path)
(wrap (#try.Failure error))))))]
[get-file File create-file file ..cannot-create-file]
[get-directory Directory create-directory directory ..cannot-create-directory]
)
(def: #export (exists? monad system path)
(All [!] (-> (Monad !) (System !) Path (! Bit)))
(do monad
[?file (!.use (:: system file) path)]
(case ?file
(#try.Success file)
(wrap true)
(#try.Failure _)
(do monad
[?directory (!.use (:: system directory) path)]
(case ?directory
(#try.Success directory)
(wrap true)
(#try.Failure _)
(wrap false))))))
|