| 1 | package Seis::IO::Path; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | use 5.010_001; | ||
| 6 | |||
| 7 | package IO::Path; | ||
| 8 | use File::Basename (); | ||
| 9 | use File::Path (); | ||
| 10 | use File::Spec (); | ||
| 11 | |||
| 12 | use overload ( | ||
| 13 | q{""} => sub { | ||
| 14 | $_[0]->{fullpath} | ||
| 15 | }, | ||
| 16 | fallback => 1, | ||
| 17 | ); | ||
| 18 | |||
| 19 | sub _file_spec { 'File::Spec' } | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my $class = shift; | ||
| 23 | my $fullpath = shift; | ||
| 24 | bless { | ||
| 25 | fullpath => $fullpath | ||
| 26 | }, $class; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub basename { File::Basename::basename($_[0]->{fullpath}) } | ||
| 30 | |||
| 31 | sub perl { | ||
| 32 | local $Data::Dumper::Terse = 1; | ||
| 33 | local $Data::Dumper::Useqq = 1; | ||
| 34 | local $Data::Dumper::Purity = 1; | ||
| 35 | local $Data::Dumper::Indent = 0; | ||
| 36 | Data::Dumper::Dumper($_[0]->{fullpath}); | ||
| 37 | } | ||
| 38 | sub gist { goto &perl } | ||
| 39 | sub volume { | ||
| 40 | my $self = shift; | ||
| 41 | (my $volume, ) = $self->_file_spec->splitpath($self->{fullpath}); | ||
| 42 | $volume; | ||
| 43 | } | ||
| 44 | sub directory { | ||
| 45 | my $self = shift; | ||
| 46 | (undef, my $volume, ) = $self->_file_spec->splitpath($self->{fullpath}); | ||
| 47 | $volume =~ s!/\z!!r; | ||
| 48 | } | ||
| 49 | sub parent { | ||
| 50 | require Path::Tiny; | ||
| 51 | IO::Path->new(Path::Tiny->new($_[0])->parent->stringify); | ||
| 52 | } | ||
| 53 | { | ||
| 54 | no strict 'refs'; | ||
| 55 | *{"isーabsolute"} = sub { | ||
| 56 | substr( $_[0]->directory, 0, 1 ) eq '/' ? Bool::True() : Bool::False() | ||
| 57 | }; | ||
| 58 | *{"isーrelative"} = sub { | ||
| 59 | substr( $_[0]->directory, 0, 1 ) ne '/' ? Bool::True() : Bool::False() | ||
| 60 | }; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub path { $_[0] } | ||
| 64 | sub IO { | ||
| 65 | Seis::IO::Handle->_new($_[0]->{fullpath}); | ||
| 66 | } | ||
| 67 | sub cleanup { | ||
| 68 | my $self = shift; | ||
| 69 | $self->_file_spec->canonpath($self->{fullpath}); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub isa { | ||
| 73 | my ($self, $stuff) = @_; | ||
| 74 | return UNIVERSAL::isa($self, $stuff->{name}) if UNIVERSAL::isa($stuff, 'Seis::Class'); | ||
| 75 | return UNIVERSAL::isa($self, $stuff); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub Str { $_[0]->{fullpath} } | ||
| 79 | |||
| 80 | sub absolute { | ||
| 81 | my $self = shift; | ||
| 82 | (ref $self)->new($self->_file_spec->rel2abs($self->{fullpath}, @_)); | ||
| 83 | } | ||
| 84 | sub relative { | ||
| 85 | my $self = shift; | ||
| 86 | (ref $self)->new($self->_file_spec->rel2abs($self->{fullpath}, @_)); | ||
| 87 | } | ||
| 88 | |||
| 89 | package IO::Path::Unix; | ||
| 90 | BEGIN { our @ISA = qw(IO::Path); } | ||
| 91 | |||
| 92 | use File::Spec::Unix; | ||
| 93 | sub _file_spec { 'File::Spec::Unix' } | ||
| 94 | |||
| 95 | package IO::Path::Win32; | ||
| 96 | BEGIN { our @ISA = qw(IO::Path); } | ||
| 97 | |||
| 98 | sub _file_spec { | ||
| 99 | require File::Spec::Win32; | ||
| 100 | 'File::Spec::Win32' | ||
| 101 | } | ||
| 102 | |||
| 103 | sub resolve { | ||
| 104 | ... # NYI | ||
| 105 | } | ||
| 106 | |||
| 107 | package IO::Path::Cygwin; | ||
| 108 | BEGIN { our @ISA = qw(IO::Path); } | ||
| 109 | |||
| 110 | sub _file_spec { | ||
| 111 | require File::Spec::Cygwin; | ||
| 112 | 'File::Spec::Cygwin' | ||
| 113 | } | ||
| 114 | sub resolve { | ||
| 115 | ... # NYI | ||
| 116 | } | ||
| 117 | |||
| 118 | package IO::Spec::Win32; | ||
| 119 | sub canonpath { | ||
| 120 | require File::Spec::Win32; | ||
| 121 | File::Spec::Win32->canonpath($_[1]); | ||
| 122 | } | ||
| 123 | |||
| 124 | package IO::Spec::Cygwin; | ||
| 125 | sub canonpath { | ||
| 126 | require File::Spec::Cygwin; | ||
| 127 | File::Spec::Cygwin->canonpath($_[1]); | ||
| 128 | } | ||
| 129 | |||
| 130 | package IO::Spec::Unix; | ||
| 131 | sub canonpath { | ||
| 132 | File::Spec::Unix->canonpath($_[1]); | ||
| 133 | } | ||
| 134 | |||
| 135 | 1; | ||
| 136 |