| 1 | package Tree::Simple::Visitor::LoadDirectoryTree; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.16'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use base qw(Tree::Simple::Visitor); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | my ($_class) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | my $class = ref($_class) || $_class; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $_class || $_class
MC/DC0%0/2
ref $_class || $_class
ref $_class
$_class
Truth table33%1/3
A || B
A= ref $_classB= $_class
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | my $visitor = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | bless($visitor, $class); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 4 | $visitor->_init(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | return $visitor; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | ▶ | sub _init { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | $self->{sort_function} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | $self->SUPER::_init(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # pre-built sort functions | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | sub SORT_FILES_FIRST { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
SORT_FILES_FIRST: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 172 | ▶ | return sub ($$$) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 172 | my ($path, $left, $right) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 172 | $left = File::Spec->catdir($path, $left); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 172 | $right = File::Spec->catdir($path, $right); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 172 | ▶ | return ((-d $left && -f $right) ? 1 : # file beats directory | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%6/6
-d $left and -f $right
-d $right and -f $left
MC/DC100%2/2
-d $left and -f $right
-d $left
-f $right
MC/DC100%2/2
-d $right and -f $left
-d $right
-f $left
Truth table100%3/3
A and B
A= -d $leftB= -f $right
Truth table100%3/3
A and B
A= -d $rightB= -f $left
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | (-d $right && -f $left) ? -1 : # file beats directory | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | (lc($left) cmp lc($right))) # otherwise just sort 'em | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | sub SORT_DIRS_FIRST { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
SORT_DIRS_FIRST: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 169 | ▶ | return sub ($$$) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 169 | my ($path, $left, $right) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 169 | $left = File::Spec->catdir($path, $left); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 169 | $right = File::Spec->catdir($path, $right); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 169 | ▶ | return ((-d $left && -f $right) ? -1 : # directory beats file | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%6/6
-d $left and -f $right
-d $right and -f $left
MC/DC100%2/2
-d $left and -f $right
-d $left
-f $right
MC/DC100%2/2
-d $right and -f $left
-d $right
-f $left
Truth table100%3/3
A and B
A= -d $leftB= -f $right
Truth table100%3/3
A and B
A= -d $rightB= -f $left
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | (-d $right && -f $left) ? 1 : # directory beats file | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | (lc($left) cmp lc($right))) # otherwise just sort 'em | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 5 | ▶ | sub setSortStyle { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
setSortStyle: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | my ($self, $sort_function) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 5 | ▶ | (defined($sort_function) && ref($sort_function) eq "CODE") | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $sort_function and ref $sort_function eq "CODE"
MC/DC100%2/2
defined $sort_function and ref $sort_function eq "CODE"
defined $sort_function
ref $sort_function eq "CODE"
Truth table100%3/3
A and B
A= defined $sort_functionB= ref $sort_function eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | || die "Insufficient Arguments : sort function argument must be a subroutine reference"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | $self->{sort_function} = $sort_function; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 9 | ▶ | sub visit { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
visit: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 9 | my ($self, $tree) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 9 | ▶ | (blessed($tree) && $tree->isa("Tree::Simple")) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($tree) and $tree->isa("Tree::Simple")
MC/DC100%2/2
builtin::blessed($tree) and $tree->isa("Tree::Simple")
builtin::blessed($tree)
$tree->isa("Tree::Simple")
Truth table100%3/3
A and B
A= builtin::blessed($tree)B= $tree->isa("Tree::Simple")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | || die "Insufficient Arguments : You must supply a valid Tree::Simple object"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # it must be a leaf | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | ▶ | ($tree->isLeaf()) || die "Illegal Operation : The tree must be a leaf node to load a directory"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # check that our directory is valid | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | my $root_dir = $tree->getNodeValue(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 4 | ▶ | (-e $root_dir && -d $root_dir) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-e $root_dir and -d $root_dir
MC/DC50%1/2
-e $root_dir and -d $root_dir
-e $root_dir
-d $root_dir
Truth table66%2/3
A and B
A= -e $root_dirB= -d $root_dir
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | || die "Incorrect Type : The tree's node value must be a valid directory"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # and load it recursively | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | $self->_recursiveLoad($tree, $root_dir); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 18 | ▶ | sub _recursiveLoad { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_recursiveLoad: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 18 | my ($self, $t, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # get a node filter if we have one | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 18 | my $filter = $self->getNodeFilter(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # get the contents of the directory | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 18 | ▶ | opendir(DIR, $path) || die "IO Error : Could not open directory : $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # avoid the . and .. symbolic links | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | my @dir_contents = grep { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 18 | ▶ | $_ ne File::Spec->curdir() && $_ ne File::Spec->updir() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$_ ne 'File::Spec'->curdir && $_ ne 'File::Spec'->updir
MC/DC100%2/2
$_ ne 'File::Spec'->curdir && $_ ne 'File::Spec'->updir
$_ ne 'File::Spec'->curdir
$_ ne 'File::Spec'->updir
Truth table100%3/3
A && B
A= $_ ne 'File::Spec'->curdirB= $_ ne 'File::Spec'->updir
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } readdir(DIR); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 18 | close(DIR); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # sort them if we need to with full paths | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | @dir_contents = sort { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 341 | $self->{sort_function}->($path, $a, $b) | |||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 18 | ▶ | } @dir_contents if $self->{sort_function}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # now traverse ... | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 18 | foreach my $item (@dir_contents) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # filter based on the item name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 177 | ▶ | $filter->($item) || next if defined($filter); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%3/4
defined $filter && (&$filter($item) || (next))
&$filter($item) or next
MC/DC0%0/2
defined $filter && (&$filter($item) || (next))
defined $filter
&$filter($item)
Truth table33%1/3
A && B
A= defined $filterB= &$filter($item)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # get the full path for checking | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # the item type and recursion | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 141 | my $full_path = File::Spec->catdir($path, $item); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 141 | ▶ | if (-d $full_path) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 15 | my $new_tree = $t->new($item); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 15 | $t->addChild($new_tree); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 15 | $self->_recursiveLoad($new_tree, $full_path); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | elsif (-f $full_path) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 126 | $t->addChild($t->new($item)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||