aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/world/file.lux
blob: 6630f8a607fda01d66c766505c1255a2e7f5a9b5 (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
(.module:
  [lux #*
   [control
    [monad (#+ Monad do)]
    ["ex" exception (#+ Exception exception:)]
    pipe]
   [data
    [error (#+ Error)]
    text/format
    [collection [array]]]
   [time
    [instant (#+ Instant)]
    [duration]]
   [world [blob (#+ Blob)]]
   [io (#+ Process)]
   [host (#+ import:)]
   [language/host]])

(type: #export File Text)

(type: #export Permission
  #Read
  #Write
  #Execute)

(signature: #export (System m)
  (: (Monad m)
     &monad)

  (: (All [e a] (-> (Exception e) e (m a)))
     throw)

  (: (All [a] (-> (m a) (m (Error a))))
     try)

  (: (All [a] (-> (Error a) (m a)))
     lift)
  
  (do-template [<name>]
    [(: (-> Blob File (m Any))
        <name>)]

    [append] [write])

  (do-template [<name> <output>]
    [(: (-> File (m <output>))
        <name>)]

    [read Blob]
    [size Nat]
    [files (List File)]
    [last-modified Instant])

  (do-template [<name>]
    [(: (-> File (m Bit))
        <name>)]

    [file?]
    [directory?]
    )

  (: (-> Permission File (m Bit))
     can?)

  (do-template [<name>]
    [(: (-> File (m Any))
        <name>)]

    [make-directory]
    [delete]
    )

  (: (-> File File (m Any))
     move)

  (: (-> Instant File (m Any))
     modify)

  (: Text
     separator)
  )

(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 language/host.jvm))
          (as-is (import: #long java/io/File
                   (new [String])
                   (exists [] #io #try boolean)
                   (mkdirs [] #io #try boolean)
                   (delete [] #io #try boolean)
                   (length [] #io #try long)
                   (listFiles [] #io #try #? (Array java/io/File))
                   (getAbsolutePath [] #io #try String)
                   (renameTo [java/io/File] #io #try boolean)
                   (isFile [] #io #try boolean)
                   (isDirectory [] #io #try boolean)
                   (lastModified [] #io #try long)
                   (setLastModified [long] #io #try boolean)
                   (canRead [] #io #try boolean)
                   (canWrite [] #io #try boolean)
                   (canExecute [] #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 true]
                     [write  false]
                     )

                   (def: (read file)
                     (do io.Monad<Process>
                       [#let [file' (java/io/File::new file)]
                        size (java/io/File::length [] file')
                        #let [data (blob.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 true)
                            (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 true)
                            (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<m> file)
  (All [m] (-> (System m) File (m Bit)))
  (|> file
      (do> (:: System<m> &monad)
           [(:: System<m> file?)]
           [(if> [(wrap true)]
                 [(:: System<m> directory? file)])])))