Source

kink / src / main / kink / _enhance / java / nio / file / Path.kn

Full commit
  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
#!/usr/bin/env kink
# vim: et sw=4 sts=4

# Copyright (c) 2013 Miyakawa Taku
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

use('COMMON_PATTERN.instance_of')
use('java.lang.String')
use('java.nio.file.Path')
use('java.nio.file.Paths')
use('java.nio.file.Files')
use('java.nio.file.StandardOpenOption')
use('java.nio.file.SimpleFileVisitor')
use('java.nio.file.FileVisitResult')
use('java.io.File')

:enhance = { (:Proto)
    Proto.set_vars(*** Path_trait)
}

:_rmr_visitor = { ()
    SimpleFileVisitor.new_with(
        'postVisitDirectory' { (:Dirpath :Io_exception)
            Dirpath.delete
            FileVisitResult.CONTINUE
        }
        'visitFile' { (:File_path :Attribute)
            File_path.delete
            FileVisitResult.CONTINUE
        }
    )
}

:_tree_visitor = { (:Accum)
    SimpleFileVisitor.new_with(
        'preVisitDirectory' { (:Dirpath :Attribute)
            Accum.push(Dirpath)
            FileVisitResult.CONTINUE
        }
        'visitFile' { (:File_path :Attribute)
            Accum.push(File_path)
            FileVisitResult.CONTINUE
        }
    )
}

# Trait for the paths
:Path_trait = [
    # Parent path of the path
    'parent_path' { > :File_path ()
        File_path.getParent
    }

    # Child path of the path
    # ex. path('/usr/local') + 'bin' -> path('/usr/local/bin')
    # ex. path('/usr/local') + path('bin') -> path('/usr/local/bin')
    'op_add' { > :File_path (:Other)
        File_path.resolve(Other)
    }

    # Relative path of the receiver from the argument
    # ex. path('/usr/local/bin') - '/usr' -> path('local/bin')
    # ex. path('/usr/local/bin') - path('/usr') -> path('local/bin')
    'op_sub' { > :One_path (:Other)
        Other.switch(
            (instance_of(Path)) { Other.relativize(One_path) }
            (instance_of(String)) { Paths.get(Other).relativize(One_path) }
        )
    }

    # Sibling path
    # ex. path('/usr/local/bin').sibling('lib') -> path('/usr/local/lib')
    # ex. path('/usr/local/bin').sibling(path('lib')) -> path('/usr/local/lib')
    'sibling' { > :File_path (:Other)
        File_path.resolveSibling(Other)
    }

    # Leaf of the path
    # ex. path('/usr/local/bin').leaf -> path('bin')
    'leaf' { > :File_path ()
        File_path.getFileName
    }

    # Decides if the path has a root or not
    # ex. path('/usr/local/bin').has_root? -> true
    # ex. path('bin/kink').has_root? -> false
    'has_root?' { > :File_path ()
        File_path.getRoot.object??
    }

    # Root path
    # ex. path('/usr/local/bin').root -> path('/')
    'root' { > :File_path ()
        File_path.getRoot
    }

    # Normalizes a path
    # ex. path('/usr/local/bin/../lib') -> path('/usr/local/lib')
    'normalize' { > :File_path ()
        File_path.call_method('normalize')
    }

    # Determines the path is absolute or not
    # ex. path('/usr/local/bin').absolute? -> true
    # ex. path('bin').absolute? -> false
    'absolute?' { > :File_path ()
        File_path.isAbsolute
    }

    # Returns the absolute path
    # ex. path('some.txt').absolute -> path('/home/me/work/some.txt')
    'absolute' { > :File_path ()
        File_path.toAbsolutePath
    }

    # Returns the real path
    # ex. path('.' LinkOption.NOFOLLOW_LINKS) -> path('/home/me')
    # ex. path('/bin/sh') -> path('/usr/bin/bash') # Follows the link
    'real' { > :File_path (* :Args)
        File_path.toRealPath(*** Args)
    }

    # Determines if the path is a directory or not
    # ex. path('/usr/bin').dir? -> true
    # ex. path('/usr/bin/ls').dir? -> false
    'dir?' { > :File_path (* :Link_options)
        Files.isDirectory(File_path *** Link_options)
    }

    # Determines if the path is a file or not
    # ex. path('/usr/bin/ls').file? -> true
    # ex. path('/usr/bin').file? -> false
    'file?' { > :File_path (* :Link_options)
        Files.isRegularFile(File_path *** Link_options)
    }

    # Determines if the path is a symbolic link or not
    'symlink?' { > :File_path ()
        Files.isSymbolicLink(File_path)
    }

    # Determines if the path exists
    'exists?' { > :File_path (* :Link_options)
        Files.exists(File_path *** Link_options)
    }

    # Determines if the path does not exist
    'not_exists?' { > :File_path (* :Link_options)
        Files.notExists(File_path *** Link_options)
    }

    #
    # Readable/Writable/Executable
    #

    # Determines if the file is readable or not
    'readable?' { > :File_path ()
        Files.isReadable(File_path)
    }

    # Determines if the file is writable or not
    'writable?' { > :File_path ()
        Files.isWritable(File_path)
    }

    # Determines if the file is executable or not
    'executable?' { > :File_path ()
        Files.isExecutable(File_path)
    }

    # Determines if the two paths are same or not
    'same_path_as?' { > :Path_1 (:Path_2)
        Files.isSameFile(Path_1 Path_2)
    }

    # Determines if the path is hidden
    'hidden?' { > :File_path ()
        Files.isHidden(File_path)
    }

    # Returns a target of the symbolic link (readlink operation)
    'symlink_target' { > :Symlink_path ()
        Files.readSymbolicLink(Symlink_path)
    }

    # Makes a temporary directory
    # ex. path('/tmp').make_temp_dir('workdir') -> path('/tmp/workdir0123')
    'make_temp_dir' { > :Base_path (* [:Prefix = ''].rest(:Attributes))
        Files.createTempDirectory(Base_path Prefix *** Attributes)
    }

    # Makes a temporary file
    # ex. path('/tmp').make_temp_file('workfile' '.log') -> path('/tmp/workfile0123.log')
    'make_temp_file' { > :Base_path (* [:Prefix = '' :Suffix = '.tmp'].rest(:Attributes))
        Files.createTempFile(Base_path Prefix Suffix *** Attributes)
    }

    # Makes a directory
    # ex. path('/home/me/dir').make_dir
    'make_dir' { > :Dirpath (* :Attributes)
        Files.createDirectory(Dirpath *** Attributes)
        ()
    }

    # Makes directories
    # ex. path('/home/me/parent/child').make_dir_recursively
    'make_dir_recursively' { > :Dirpath (* :Attributes)
        Files.createDirectories(Dirpath *** Attributes)
        ()
    }

    # Makes an empty file
    # ex. path('/home/me/empty_file')
    'make_file' { > :File_path (* :Attributes)
        Files.createFile(File_path *** Attributes)
        ()
    }

    # Makes a hard link
    'make_link' { > :Link_path (:Target_path)
        Files.createLink(Link_path Target_path)
        ()
    }

    # Makes a symbolic link
    'make_symlink' { > :Symlink_path (* [:Target_path].rest(:Attributes))
        Files.createSymbolicLink(Symlink_path Target_path *** Attributes)
        ()
    }

    # Removes a file or a directory
    'delete' { > :File_path ()
        Files.delete(File_path)
    }

    # Removes a directory recursively
    'delete_recursively' { > :File_path ()
        Files.walkFileTree(File_path _rmr_visitor)
        ()
    }

    #
    # Methods for file contents
    #

    # Output stream
    'output_stream' { > :File_path (* :Open_options)
        Files.newOutputStream(File_path *** Open_options)
    }

    # Output stream for append
    'output_stream_appending' { > :File_path ()
        File_path.output_stream(
            StandardOpenOption.WRITE
            StandardOpenOption.CREATE
            StandardOpenOption.APPEND
        )
    }

    # Input stream
    'input_stream' { > :File_path (* :Open_options)
        Files.newInputStream(File_path *** Open_options)
    }

    # Writes bytes to the file
    'write_bytes' { > :File_path (* [:Bytes].rest(:Open_options))
        Files.write(File_path Bytes Open_options)
        File_path
    }

    # Appends bytes to the file
    'write_bytes_appending' { > :File_path (:Bytes)
        File_path.write_bytes(
            Bytes
            StandardOpenOption.WRITE
            StandardOpenOption.CREATE
            StandardOpenOption.APPEND
        )
    }

    # Writes a text to the file
    'print' { > :File_path (:Text :Charset)
        use('USING.using')
        using(File_path.output_stream) { (:Out)
            :Charset.any?.then { Out.print(Text Charset) } { Out.print(Text) }
        }
    }

    # Appends a text to the file
    'print_appending' { > :File_path (:Text :Charset)
        use('USING.using')
        using(File_path.output_stream_appending) { (:Out)
            :Charset.any?.then { Out.print(Text Charset) } { Out.print(Text) }
        }
    }

    # Reads a text from the file
    'text' { > :File_path (:Charset)
        use('USING.using')
        using(File_path.input_stream) { (:In)
            :Charset.any?.then { In.text(Charset) } { In.text }
        }
    }

    # Reads bytes from the file
    'bytes' { > :File_path (:Size)
        use('USING.using')
        using(File_path.input_stream) { (:In)
            :Size.any?.then { In.bytes(Size) } { In.bytes }
        }
    }

    # Reads a program from the file
    'program' { > :File_path (:Charset)
        File_path.bytes.program(File_path.name)
    }

    # Writes a line to the file
    'print_line' { > :File_path (:Text :Charset)
        use('USING.using')
        using(File_path.output_stream) { (:Out)
            :Charset.any?.then { Out.print_line(Text Charset) } { Out.print_line(Text) }
        }
    }

    # Appends a line to the file
    'print_line_appending' { > :File_path (:Text :Charset)
        use('USING.using')
        using(File_path.output_stream_appending) { (:Out)
            :Charset.any?.then { Out.print_line(Text Charset) } { Out.print_line(Text) }
        }
    }

    # Reads lines from the file
    'lines' { > :File_path (:Charset)
        use('USING.using')
        using(File_path.input_stream) { (:In)
            :Charset.any?.then { In.lines(Charset) } { In.lines }
        }
    }

    # Returns paths of children of the directory
    'child_paths' { > :File_path ()
        use('USING.using')
        using(Files.newDirectoryStream(File_path)) { (:Stream)
            Stream.list
        }
    }

    # Returns paths of descendant files and directories
    'tree' { > :File_path ()
        :Accum = []
        Files.walkFileTree(File_path _tree_visitor(Accum))
        Accum
    }

    # `true` if the class is a superclass of Path or the class is File
    # ex. path('/bin').can_into?(Path) -> true
    # ex. path('/bin').can_into?(Object) -> true
    # ex. path('/bin').can_into?(File) -> true
    # ex. path('/bin').can_into?(String) -> false
    'can_into?' { > :File_path (:Class)
        File_path.object?? && (File_path.instance_of?(Class) || Class == File)
    }

    # Convert the path to a path or a file
    # ex. path('/bin').into(Path) -> path('/bin')
    # ex. path('/bin').into(Object) -> path('/bin')
    # ex. path('/bin').into(File) -> File.new('/bin')
    'into' { > :File_path (:Class)
        (Class == File).then { File_path.toFile } { File_path }
    }

    # Returns the path name
    # ex. path('/bin').name -> '/bin'
    'name' { > :File_path ()
        File_path.toString
    }

    # Returns the string representation of the path
    # ex. path('/bin').show -> path("/bin")
    'show' { > :File_path ()
        File_path.object??.then { 'path(' + File_path.name.show + ')' } { 'null-path' }
    }

    # Returns true if the receiver is a path
    # ex. path('/bin').path?? -> true
    'path??' { > :File_path ()
        File_path.object??
    }

]