Sidef/Types/Glob/Dir.pm untested

filter: Statementstmt 0.0% 0 / 184 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 80 MC/DCmcdc 0.0% 0 / 51 Subroutinesub 0.0% 0 / 38 Podpod 0.0% 0 / 32 totaltotal 0.0% 0 / 427 CC 22 SCAR 19.7
CC
22
Cov
0%
find
20.0
open
20.0
new
12.0
CRAP 7.2
1 package Sidef::Types::Glob::Dir;
2  
3 use utf8;
4 use 5.016;
5  
6 use parent qw(
7 Sidef::Types::Glob::File
8 );
9  
10 require Encode;
11 require File::Spec;
12  
13 sub new {
14 my (undef, $dir) = @_;
15 if (@_ > 2) {
16 shift(@_);
17 $dir = File::Spec->catdir(map { "$_" } @_);
18 }
19 elsif (ref($dir)) {
20 return $dir->to_dir;
21 }
22 bless \$dir, __PACKAGE__;
23 }
24  
25 *call = \&new;
26  
27 sub get_value { ${$_[0]} }
28 sub to_dir { $_[0] }
29  
30 sub root {
31 __PACKAGE__->new(File::Spec->rootdir);
32 }
33  
34 sub home {
35 my ($self) = @_;
36  
37 my $home = $ENV{HOME} || $ENV{LOGDIR};
38  
39 if (not $home and $^O ne 'MSWin32') {
40 $home = (getpwuid($<))[7];
41 }
42  
43 $home ? __PACKAGE__->new($home) : do {
44 state $x = require File::HomeDir;
45 __PACKAGE__->new(File::HomeDir->my_home);
46 };
47 }
48  
49 sub tmp {
50 __PACKAGE__->new(File::Spec->tmpdir);
51 }
52  
53 *temp = \&tmp;
54  
55 sub mktemp {
56 my ($self, %opts) = @_;
57 state $x = require File::Temp;
58 __PACKAGE__->new(File::Temp::tempdir(CLEANUP => 1, %opts));
59 }
60  
61 *make_tmp = \&mktemp;
62 *make_temp = \&mktemp;
63  
64 sub find {
65 my ($self, $arg) = @_;
66 state $x = require File::Find;
67  
68 my @files;
69 my $ref = ref($arg // '');
70 my $is_block = $ref eq 'Sidef::Types::Block::Block';
71  
72 File::Find::find(
73 {
74 no_chdir => 1,
75 wanted => sub {
76 my $file = Encode::decode_utf8($File::Find::name);
77  
78 if (-d $file) {
79 $file = __PACKAGE__->new($file);
80 }
81 else {
82 $file = Sidef::Types::Glob::File->new($file);
83 }
84  
85 $is_block ? $arg->run($file) : push(@files, $file);
86 }
87 },
88 $$self
89 );
90  
91 $is_block ? $self : Sidef::Types::Array::Array->new(\@files);
92 }
93  
94 *browse = \&find;
95 *walk = \&find;
96  
97 sub each {
98 ref($_[0]) || shift(@_);
99 my ($self, $block) = @_;
100 my $dh = $self->open() // return Sidef::Types::Bool::Bool::FALSE;
101 $dh->each($block);
102 $dh->close;
103 Sidef::Types::Bool::Bool::TRUE;
104 }
105  
106 sub glob {
107 ref($_[0]) || shift(@_);
108 my ($self, $pattern) = @_;
109 state $x = require File::Spec;
110 my @matches = CORE::glob(File::Spec->catfile("$self", "$pattern"));
111 Sidef::Types::Array::Array->new([map { (-d $_) ? __PACKAGE__->new($_) : Sidef::Types::Glob::File->new($_) } @matches]);
112 }
113  
114 sub cwd {
115 state $x = require Cwd;
116 __PACKAGE__->new(Encode::decode_utf8(Cwd::getcwd()));
117 }
118  
119 sub pwd {
120 __PACKAGE__->new(File::Spec->curdir);
121 }
122  
123 sub up {
124 my ($self) = @_;
125 __PACKAGE__->new(Encode::decode_utf8(File::Spec->catdir(ref($self) ? Encode::encode_utf8($$self) : (), File::Spec->updir)));
126 }
127  
128 sub split {
129 ref($_[0]) || shift(@_);
130 my ($self) = @_;
131 Sidef::Types::Array::Array->new([map { Sidef::Types::String::String->new($_) } File::Spec->splitdir("$self")]);
132 }
133  
134 # Returns the parent of the directory
135 sub parent {
136 ref($_[0]) || shift(@_);
137 my ($self) = @_;
138 state $x = require File::Basename;
139 __PACKAGE__->new(File::Basename::dirname("$self"));
140 }
141  
142 # Remove the directory (works only on empty dirs)
143 sub remove {
144 ref($_[0]) || shift(@_);
145 my ($self) = @_;
146 CORE::rmdir("$self") ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE);
147 }
148  
149 *delete = \&remove;
150 *unlink = \&remove;
151  
152 # Remove the directory with all its content
153 sub remove_tree {
154 ref($_[0]) || shift(@_);
155 my ($self) = @_;
156  
157 state $x = require File::Path;
158 (File::Path::remove_tree("$self")) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE);
159 }
160  
161 # Create directory without parents
162 sub create {
163 ref($_[0]) || shift(@_);
164 my ($self) = @_;
165 (CORE::mkdir("$self")) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE);
166 }
167  
168 *make = \&create;
169 *mkdir = \&create;
170  
171 # Create the directory (with parents, if needed)
172 sub create_tree {
173 ref($_[0]) || shift(@_);
174 my ($self) = @_;
175 state $x = require File::Path;
176 my $path = "$self";
177 -d $path ? (Sidef::Types::Bool::Bool::TRUE)
178 : (File::Path::make_path($path)) ? (Sidef::Types::Bool::Bool::TRUE)
179 : (Sidef::Types::Bool::Bool::FALSE);
180 }
181  
182 *make_tree = \&create_tree;
183 *mktree = \&create_tree;
184 *make_path = \&create_tree;
185 *mkpath = \&create_tree;
186  
187 sub open {
188 ref($_[0]) || shift(@_);
189 my ($self, $fh_ref, $err_ref) = @_;
190  
191 my $success = CORE::opendir(my $dir_h, "$self");
192 my $error = $!;
193 my $dir_obj = Sidef::Types::Glob::DirHandle->new($dir_h, $self);
194  
195 if (defined $fh_ref) {
196 ${$fh_ref} = $dir_obj;
197  
198 return $success
199 ? (Sidef::Types::Bool::Bool::TRUE)
200 : do {
201 defined($err_ref) && do { ${$err_ref} = Sidef::Types::String::String->new($error) };
202 (Sidef::Types::Bool::Bool::FALSE);
203 };
204 }
205  
206 $success ? $dir_obj : undef;
207 }
208  
209 *open_r = \&open;
210  
211 sub open_w { ... }
212 sub open_rw { ... }
213  
214 sub entries {
215 ref($_[0]) || shift(@_);
216 my ($self) = @_;
217 my $dh = $self->open() // return undef;
218 my $result = $dh->entries;
219 $dh->close;
220 $result;
221 }
222  
223 sub files {
224 ref($_[0]) || shift(@_);
225 my ($self) = @_;
226 my $dh = $self->open() // return undef;
227 my $result = $dh->files;
228 $dh->close;
229 $result;
230 }
231  
232 sub subdirs {
233 ref($_[0]) || shift(@_);
234 my ($self) = @_;
235 my $dh = $self->open() // return undef;
236 my $result = $dh->dirs;
237 $dh->close;
238 $result;
239 }
240  
241 sub chdir {
242 ref($_[0]) || shift(@_);
243 my ($self) = @_;
244 (chdir("$self")) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE);
245 }
246  
247 sub chroot {
248 ref($_[0]) || shift(@_);
249 my ($self) = @_;
250 (chroot("$self")) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE);
251 }
252  
253 sub concat {
254 ref($_[0]) || shift(@_);
255 my ($self, $file) = @_;
256  
257 ref($file) eq 'Sidef::Types::Glob::File'
258 ? $file->new(File::Spec->catfile("$self", "$file"))
259 : __PACKAGE__->new(File::Spec->catdir("$self", "$file"));
260 }
261  
262 *catfile = \&concat;
263  
264 sub is_empty {
265 ref($_[0]) || shift(@_);
266 my ($self) = @_;
267 CORE::opendir(my $dir_h, "$self") || return undef;
268 while (defined(my $file = CORE::readdir $dir_h)) {
269 next if ($file eq '.' or $file eq '..');
270 CORE::closedir($dir_h);
271 return (Sidef::Types::Bool::Bool::FALSE);
272 }
273 CORE::closedir($dir_h);
274 Sidef::Types::Bool::Bool::TRUE;
275 }
276  
277 sub size {
278 ref($_[0]) || shift(@_);
279 my ($self) = @_;
280 my $total = 0;
281 foreach my $item (@{$self->find}) {
282 $item->is_file and $total += (-s "$item" // 0);
283 }
284 Sidef::Types::Number::Number::_set_int($total);
285 }
286  
287 sub dump {
288 my ($self) = @_;
289 Sidef::Types::String::String->new('Dir(' . ${Sidef::Types::String::String->new("$self")->dump} . ')');
290 }
291  
292 sub to_str {
293 my ($self) = @_;
294 Sidef::Types::String::String->new($$self);
295 }
296  
297 *to_s = \&to_str;
298  
299 {
300 no strict 'refs';
301 *{__PACKAGE__ . '::' . '+'} = \&concat;
302 }
303  
304 1