-
Notifications
You must be signed in to change notification settings - Fork 1
/
pathname.cr
653 lines (517 loc) · 12.4 KB
/
pathname.cr
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
644
645
646
647
648
649
650
651
652
653
struct Pathname
include Comparable(self)
VERSION = "0.1.6"
def self.cwd
self.new(Dir.current)
end
def self.glob(*patterns)
Dir.glob(patterns) do |path|
yield self.new(path)
end
end
def self.glob(*patterns)
Dir.glob(patterns).map { |path| self.new(path) }
end
def self.glob(patterns : Enumerable(String))
Dir.glob(patterns) do |path|
yield self.new(path)
end
end
def self.glob(patterns : Enumerable(String))
Dir.glob(patterns).map { |path| self.new(path) }
end
@path : String
def initialize(path : String)
path = path.sub(/#{File::SEPARATOR_STRING}+$/, "") unless path == File::SEPARATOR_STRING
@path = path
end
def initialize(path : Pathname)
initialize(path.to_s)
end
def +(other : Pathname) : Pathname
if other.absolute?
other
else
Pathname.new("#{self}#{File::SEPARATOR_STRING}#{other}")
end
end
def +(other : String) : Pathname
self + Pathname.new(other)
end
def /(other : Pathname) : Pathname
self + other.to_s
end
def /(other : String) : Pathname
self + other
end
def <=>(other : Pathname)
@path <=> other.@path
end
def ===(other : Pathname)
cleanpath == other.cleanpath
end
def absolute? : Bool
@path.starts_with?(File::SEPARATOR_STRING)
end
def ascend
mfnames = filenames
0.upto(mfnames.size - 1).each do |i|
new_path = mfnames[0..i].join(File::SEPARATOR_STRING)
new_path = "#{File::SEPARATOR_STRING}#{new_path}" if new_path.empty?
yield Pathname.new(new_path)
end
end
def atime : Time
File.stat(@path).atime
end
def basename : Pathname
Pathname.new(File.basename(@path))
end
def basename(suffix : String) : Pathname
pattern = "#{suffix.gsub(/\./, "\\.").gsub(/\*/, "[^\\.]+")}$"
new_path = File.basename(@path)
new_path = new_path.sub(Regex.new(pattern), "") if new_path =~ /[^\.]\.[^\.]/
Pathname.new(new_path)
end
# def binread
# def binwrite
# def birthtime
def blockdev? : Bool
File.stat(@path).blockdev?
end
def chardev? : Bool
File.stat(@path).chardev?
end
def children(with_directory : Bool = true) : Array(Pathname)
paths = [] of Pathname
Dir.entries(@path).each do |entry|
next if entry == "." || entry == ".."
new_path = self + entry
next if !with_directory && new_path.directory?
paths << new_path
end
paths
end
# def chmod
# def chown
def cleanpath(consider_symlink : Bool = false) : Pathname
new_filenames = [] of String
filenames.each do |node|
case node
when "", "."
when ".."
if new_filenames.empty?
new_filenames << node if relative?
else
if new_filenames.last == ".."
new_filenames << node
else
new_filenames.pop
end
end
else
new_filenames << node
end
end
new_path = new_filenames.join(File::SEPARATOR_STRING)
if new_path.empty?
new_path = (absolute? ? "/" : ".")
end
new_path = "/#{new_path}" if absolute? && new_path !~ /^#{File::SEPARATOR_STRING}/
Pathname.new(new_path)
end
def ctime : Time
File.stat(@path).ctime
end
def delete
File.delete(@path)
end
def descend
mfnames = filenames
(mfnames.size - 1).downto(0).each do |i|
new_path = mfnames[0..i].join(File::SEPARATOR_STRING)
new_path = "#{File::SEPARATOR_STRING}#{new_path}" if new_path.empty?
yield Pathname.new(new_path)
end
end
def directory? : Bool
File.directory?(@path)
end
def dirname : Pathname
Pathname.new(File.dirname(@path))
end
def each_child(with_directory : Bool = true)
children(with_directory).each do |child|
yield child
end
end
def each_entry
entries.each do |entry|
yield entry
end
end
def each_filename
mfnames = filenames
mfnames.shift if absolute?
mfnames.each do |filename|
yield filename
end
end
def each_line
File.each_line(@path) do |line|
yield line
end
end
def entries : Array[Pathname]
ents = [] of Pathname
Dir.entries(@path).each do |entry|
ents << Pathname.new(entry)
end
ents
end
def executable? : Bool
File.executable?(@path)
end
def exists? : Bool
File.exists?(@path)
end
def expand_path(default_dir : Pathname) : Pathname
if absolute?
self
else
Pathname.new("#{default_dir}#{File::SEPARATOR_STRING}#{@path}").cleanpath
end
end
def expand_path(default_dir : String = Dir.current)
expand_path(Pathname.new(default_dir))
end
def extname : String
File.extname(@path)
end
def file? : Bool
stat.file?
end
def filenames : Array(String)
@path.split(File::SEPARATOR_STRING)
end
# def find
# def fnmatch
# def fnmatch?
# def ftype
# def grpowned?
def inspect(io)
io << "#<Pathname:" << @path << ">"
end
def join(*args) : Pathname
new_pathname = self
args.each do |arg|
new_pathname += arg
end
new_pathname
end
# def lchmod
# def lchown
def lstat
File.lstat(@path)
end
def make_link(old)
File.link(old, @path)
end
def make_symlink(old)
File.symlink(old, @path)
end
def mkdir(mode = 0o511)
Dir.mkdir(@path, mode)
end
def mkdir_p(mode = 0o511)
Dir.mkdir_p(@path, mode)
end
# def mountpoint?
def mtime : Time
stat.mtime
end
def open(mode = "r", perm = DEFAULT_CREATE_MODE)
File.open(@path, mode, perm)
end
def open(mode = "r", perm = DEFAULT_CREATE_MODE)
File.open(@path, mode, perm) do |io|
yield io
end
end
def opendir
Dir.open(@path)
end
def opendir
Dir.open(@path) do |dir|
yield dir
end
end
def parent
dirname
end
# def pipe?
# def owned?
def read(encoding = nil, invalid = nil) : String
File.read(@path, encoding, invalid)
end
def readable? : Bool
File.readable?(@path)
end
# def readable_real?
# def readlink
# def realdirpath
def real_path : Pathname
Pathname.new(File.real_path(@path))
end
def relative? : Bool
!absolute?
end
def relative_path_from(base_directory : String) : Pathname
relative_path_from(Pathname.new(base_directory))
end
def relative_path_from(base_directory : Pathname) : Pathname
if absolute? && base_directory.absolute?
mfnames = cleanpath.filenames
bfnames = base_directory.cleanpath.filenames
while mfnames.first? == bfnames.first?
mfnames.shift
bfnames.shift
end
until bfnames.empty?
bfnames.shift
mfnames.unshift("..")
end
Pathname.new(mfnames.join(File::SEPARATOR_STRING))
elsif relative? && base_directory.relative?
Pathname.new("#{base_directory}#{File::SEPARATOR_STRING}#{@path}").cleanpath
else
raise "relative and absolute path."
end
end
def rename(to)
File.rename(@path, to)
end
def rmdir
Dir.rmdir(@path)
end
# def rmtree
def root?
cleanpath.path == "/"
end
def setgid?
stat.setgid?
end
def setuid?
stat.setuid?
end
def size
stat.size
end
def size?
return nil unless exists?
size == 0 ? nil : size
end
def socket?
stat.socket?
end
def split
{dirname, basename}
end
def stat
File.stat(@path)
end
def sticky?
stat.sticky?
end
def sub(pattern, replace) : Pathname
Pathname.new(@path.sub(pattern, replace))
end
def sub(pattern)
Pathname.new(@path.sub(pattern) { |lmatched| yield lmatched })
end
def sub_ext(replace : String)
striped_path = (@path == "/" ? "" : basename(".*"))
fnames = filenames
fnames.pop
fnames.push("#{striped_path}#{replace}")
Pathname.new(fnames.join(File::SEPARATOR_STRING))
end
def symlink?
File.symlink?(@path)
end
# def sysopen
# def to_path
def to_s(io)
io << @path
end
def truncate(size = 0)
File.open(@path, "r+") do |file|
file.truncate(size)
end
end
def utime
stat.utime
end
def world_readable?
((stat.mode & LibC::S_IRWXO) & LibC::S_IROTH > 0)
end
def world_writable?
((stat.mode & LibC::S_IRWXO) & LibC::S_IWOTH > 0)
end
def writable?
File.writable?(@path)
end
def write(content, perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil)
File.write(@path, content, perm, encoding, invalid)
end
def zero?
return false unless exists?
size == 0
end
end
class File
def self.basename(path : Pathname)
self.basename(path.to_s)
end
def self.basename(path : Pathname, suffix)
self.basename(path.to_s, suffix)
end
def self.delete(path : Pathname)
self.delete(path.to_s)
end
def self.directory?(path : Pathname)
self.directory?(path.to_s)
end
def self.dirname(path : Pathname)
self.dirname(path.to_s)
end
def self.each_line(path : Pathname)
self.each_line(path.to_s) do |line|
yield line
end
end
def self.executable?(path : Pathname)
self.executable?(path.to_s)
end
def self.exists?(path : Pathname)
self.exists?(path.to_s)
end
def self.expand_path(path : Pathname, dir = nil)
self.expand_path(path.to_s, dir)
end
def self.extname(path : Pathname)
self.extname(path.to_s)
end
def self.file?(path : Pathname)
self.file?(path.to_s)
end
def self.link(old_path : Pathname, new_path)
self.link(old_path.to_s, new_path)
end
def self.link(old_path, new_path : Pathname)
self.link(old_path, new_path.to_s)
end
def self.link(old_path : Pathname, new_path : Pathname)
self.link(old_path.to_s, new_path.to_s)
end
def self.lstat(path : Pathname)
self.lstat(path.to_s)
end
def self.open(path : Pathname, mode = "r", perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil)
self.open(path.to_s, mode, perm, encoding, invalid)
end
def self.open(path : Pathname, mode = "r", perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil)
self.open(path.to_s, mode, perm, encoding, invalid) do |fd|
yield fd
end
end
def self.read(path : Pathname, encoding = nil, invalid = nil)
self.read(path.to_s, encoding, invalid)
end
def self.read_lines(path : Pathname, encoding = nil, invalid = nil)
self.read_lines(path.to_s, encoding, invalid)
end
def self.readable?(path : Pathname)
self.readable?(path.to_s)
end
def self.real_path(path : Pathname)
self.real_path(path.to_s)
end
def self.rename(old_path : Pathname, new_path)
self.rename(old_path.to_s, new_path)
end
def self.rename(old_path, new_path : Pathname)
self.rename(old_path, new_path.to_s)
end
def self.rename(old_path : Pathname, new_path : Pathname)
self.rename(old_path.to_s, new_path.to_s)
end
def self.size(path : Pathname)
self.size(path.to_s)
end
def self.stat(path : Pathname)
self.stat(path.to_s)
end
def self.symlink(old_path : Pathname, new_path)
self.symlink(old_path.to_s, new_path)
end
def self.symlink(old_path, new_path : Pathname)
self.symlink(old_path, new_path.to_s)
end
def self.symlink(old_path : Pathname, new_path : Pathname)
self.symlink(old_path.to_s, new_path.to_s)
end
def self.symlink?(path : Pathname)
self.symlink?(path.to_s)
end
def self.writable?(path : Pathname)
self.writable?(path.to_s)
end
def self.write(path : Pathname, content, perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil)
self.write(path.to_s, content, perm, encoding, invalid)
end
def self.new(path : Pathname, mode = "r", perm = DEFAULT_CREATE_MODE, encoding = nil, invalid = nil)
self.new(path.to_s, mode, perm, encoding, invalid)
end
end
class Dir
def self.cd(path : Pathname)
self.cd(path.to_s)
end
def self.cd(path : Pathname)
self.cd(path.to_s) do
yield
end
end
def self.entries(path : Pathname)
self.entries(path.to_s)
end
def self.exists?(path : Pathname)
self.exists?(path.to_s)
end
def self.foreach(path : Pathname)
self.foreach(path.to_s) do |filename|
yield filename
end
end
def self.mkdir(path : Pathname, mode = 0o777)
self.mkdir(path.to_s, mode)
end
def self.mkdir_p(path : Pathname, mode = 0o777)
self.mkdir_p(path.to_s, mode)
end
def self.new(path : Pathname)
self.new(path.to_s)
end
def self.open(path : Pathname)
self.open(path.to_s)
end
def self.open(path : Pathname)
self.open(path.to_s) do |dir|
yield dir
end
end
def self.rmdir(path : Pathname)
self.rmdir(path.to_s)
end
end