blob: e97668917f1a93a9df64bcb12f202104fff7047f (
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
|
(.module:
[lux #*
[control
["." monad (#+ Monad do)]
["ex" exception (#+ Exception exception:)]]
[data
["." maybe]
["." error (#+ Error)]
["." text
format]
[collection
["." array (#+ Array)]]]
[time
["." instant (#+ Instant)]
["." duration]]
[world
["." binary (#+ Binary)]]
["." io (#+ Process)]
[host (#+ import:)]
[compiler
["." host]]])
(type: #export File Text)
(type: #export Permission
#Read
#Write
#Execute)
(signature: #export (System !)
(: (Monad !)
&monad)
(: (All [e a] (-> (Exception e) e (! a)))
throw)
(: (All [a] (-> (! a) (! (Error a))))
try)
(: (All [a] (-> (Error a) (! a)))
lift)
(do-template [<name>]
[(: (-> Binary File (! Any))
<name>)]
[append]
[write])
(do-template [<name> <output>]
[(: (-> File (! <output>))
<name>)]
[read Binary]
[size Nat]
[files (List File)]
[last-modified Instant])
(do-template [<name>]
[(: (-> File (! Bit))
<name>)]
[file?]
[directory?]
)
(: (-> Permission File (! Bit))
can?)
(do-template [<name>]
[(: (-> File (! Any))
<name>)]
[make-directory]
[delete]
)
(: (-> File File (! Any))
move)
(: (-> Instant File (! Any))
modify)
(: Text
separator)
)
(def: #export (un-nest System<!> file)
(All [!] (-> (System !) File (Maybe [File 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 !) [File Text] File))
(format parent (:: System<!> separator) child))
(do-template [<name>]
[(exception: #export (<name> {file File})
(ex.report ["File" file]))]
[cannot-read-all-data]
[not-a-directory]
[cannot-make-directory]
[cannot-delete]
)
(exception: #export (cannot-move {target File} {source File})
(ex.report ["Source" source]
["Target" target]))
(exception: #export (cannot-modify {instant Instant} {file File})
(ex.report ["Instant" (%instant instant)]
["File" file]))
(`` (for {(~~ (static host.jvm))
(as-is (import: #long java/io/File
(new [String])
(~~ (do-template [<name>]
[(<name> [] #io #try boolean)]
[exists] [mkdirs] [delete]
[isFile] [isDirectory]
[canRead] [canWrite] [canExecute]))
(length [] #io #try long)
(listFiles [] #io #try #? (Array java/io/File))
(getAbsolutePath [] #io #try String)
(renameTo [java/io/File] #io #try boolean)
(lastModified [] #io #try long)
(setLastModified [long] #io #try boolean)
(#static separator String))
(import: java/lang/AutoCloseable
(close [] #io #try void))
(import: java/io/OutputStream
(write [(Array byte)] #io #try void)
(flush [] #io #try void))
(import: java/io/FileOutputStream
(new [java/io/File boolean] #io #try))
(import: java/io/InputStream
(read [(Array byte)] #io #try int))
(import: java/io/FileInputStream
(new [java/io/File] #io #try))
(structure: #export JVM@System (System Process)
(def: &monad io.Monad<Process>)
(def: throw io.throw)
(def: (try computation)
(do io.Monad<IO>
[outcome computation]
(:: io.Monad<Process> wrap outcome)))
(def: lift (:: io.Monad<IO> wrap))
(do-template [<name> <flag>]
[(def: (<name> data file)
(do io.Monad<Process>
[stream (FileOutputStream::new (java/io/File::new file) <flag>)
_ (OutputStream::write data stream)
_ (OutputStream::flush stream)]
(AutoCloseable::close stream)))]
[append #1]
[write #0]
)
(def: (read file)
(do io.Monad<Process>
[#let [file' (java/io/File::new file)]
size (java/io/File::length file')
#let [data (binary.create (.nat size))]
stream (FileInputStream::new file')
bytes-read (InputStream::read data stream)
_ (AutoCloseable::close stream)]
(if (i/= size bytes-read)
(wrap data)
(io.io (ex.throw cannot-read-all-data file)))))
(def: size
(|>> java/io/File::new
java/io/File::length
(:: io.Monad<Process> map .nat)))
(def: (files dir)
(do io.Monad<Process>
[?files (java/io/File::listFiles (java/io/File::new dir))]
(case ?files
(#.Some files)
(monad.map @ (|>> java/io/File::getAbsolutePath)
(array.to-list files))
#.None
(io.throw not-a-directory dir))))
(do-template [<name> <method>]
[(def: <name> (|>> java/io/File::new <method>))]
[file? java/io/File::isFile]
[directory? java/io/File::isDirectory]
)
(def: (can? permission file)
(let [jvm-file (java/io/File::new file)]
(case permission
#Read (java/io/File::canRead jvm-file)
#Write (java/io/File::canWrite jvm-file)
#Execute (java/io/File::canExecute jvm-file))))
(def: last-modified
(|>> java/io/File::new
(java/io/File::lastModified)
(:: io.Monad<Process> map (|>> duration.from-millis instant.absolute))))
(do-template [<name> <exception> <method>]
[(def: (<name> subject)
(do io.Monad<IO>
[outcome (<method> (java/io/File::new subject))]
(case outcome
(#error.Success #1)
(wrap (#error.Success []))
_
(io.throw <exception> [subject]))))]
[make-directory cannot-make-directory java/io/File::mkdirs]
[delete cannot-delete java/io/File::delete]
)
(do-template [<name> <exception> <method> <parameter-pre>]
[(def: (<name> parameter subject)
(do io.Monad<IO>
[outcome (<method> (|> parameter <parameter-pre>)
(java/io/File::new subject))]
(case outcome
(#error.Success #1)
(wrap (#error.Success []))
_
(io.throw <exception> [parameter subject]))))]
[move cannot-move java/io/File::renameTo java/io/File::new]
[modify cannot-modify java/io/File::setLastModified (<| duration.to-millis instant.relative)]
)
(def: separator (java/io/File::separator))
))
}))
(def: #export (exists? System<!> file)
(All [!] (-> (System !) File (! Bit)))
(do (:: System<!> &monad)
[??? (:: System<!> file? file)]
(if ???
(wrap ???)
(:: System<!> directory? file))))
|