Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
54 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,60 @@ | ||
use v6; | ||
use File::Spec::Unix; | ||
class File::Spec::OS2 is File::Spec::Unix; | ||
use File::Spec::Win32; | ||
class File::Spec::OS2 is File::Spec::Win32; | ||
|
||
my $module = "File::Spec::Unix"; | ||
#require $module; | ||
# Some regexes we use for path splitting | ||
my $driveletter = regex { <[a..z A..Z]> ':' } | ||
my $slash = regex { '/' | '\\' } | ||
|
||
method canonpath { ::($module).canonpath() } | ||
method catdir { ::($module).catdir() } | ||
method catfile { ::($module).catfile() } | ||
method tmpdir { ::($module).tmpdir() } | ||
method no_upwards { ::($module).no_upwards() } | ||
|
||
#method curdir { ::($module).curdir() } | ||
#method updir { ::($module).updir() } | ||
method devnull { '/dev/nul' } | ||
#method rootdir { ::($module).rootdir() } | ||
method case_tolerant { True } | ||
method default_case_tolerant { True } | ||
method file_name_is_absolute { ::($module).file_name_is_absolute() } | ||
method path { ::($module).path() } | ||
method join { ::($module).join() } | ||
method splitpath { ::($module).splitpath() } | ||
method splitdir ($dirs) { $dirs.split: /<[\\\/]>/ } | ||
method catpath { ::($module).catpath() } | ||
method abs2rel { ::($module).abs2rel() } | ||
method rel2abs { ::($module).rel2abs() } | ||
|
||
method file_name_is_absolute ($file) { so $file ~~ /^ <$driveletter>? <$slash>/ } | ||
|
||
method path { | ||
%*ENV<PATH>\ | ||
.subst(:global, '\\', '/')\ | ||
.split(';')\ | ||
.map: { $_ eq '' ?? '.' !! $_ }; | ||
} | ||
|
||
|
||
method tmpdir { | ||
state $tmpdir; | ||
return $tmpdir if $tmpdir.defined; | ||
return $tmpdir = self._tmpdir( | ||
|%*ENV<TMPDIR TEMP TMP>, | ||
|< /tmp / >, | ||
self.curdir | ||
); | ||
} | ||
|
||
method catdir (*@dirs) { | ||
my $path = @dirs\ | ||
.subst(:global, '\\', '/')\ | ||
.map( { $_ ~~ m❚'/' $❚ ?? $_ ~ '/' !! $_ } )\ | ||
.join; | ||
self.canonpath($path); | ||
} | ||
|
||
sub canonpath ($path) { | ||
return unless defined $path; | ||
|
||
$path ~~ s/^<[a..z]>:/{ uc $() }/; | ||
$path ~~ s:g❚\\❚/❚; #:: | ||
#$path =~ s| ([^/])/+ |$1/|g; # xx////xx -> xx/xx | ||
#$path ~~ s:g❚'/'** 2..*❚/❚; #:: # xx////xx -> xx/xx | ||
#$path ~~ s:g❚'/.'+ '/'❚/❚; #:: # xx/././xx -> xx/xx | ||
$path ~~ s:g❚'/' ['.'? '/']+ ❚/❚; #:: # xx///xx/././xx -> xx/xx/xx | ||
$path ~~ s❚^ './'+ <?before <-[/]>>❚❚; # ./xx -> xx | ||
$path ~~ s❚'/' $❚❚ | ||
unless $path ~~ m❚^ <$driveletter>? '/' $❚; # xx/ -> xx | ||
$path ~~ s❚^ '/..' $❚/❚; # /.. -> / | ||
$path ~~ s❚^ '/..'+ ❚❚; # /../xx -> /xx | ||
return $path; | ||
} | ||
|
||
|
||
|
||
|