| 1 | package Toader::findToaderDirs; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Toader::isaToaderDir; | ||
| 6 | use base 'Error::Helper'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Toader::findToaderDirs - Finds all Toader directories under a specified Toader directory. | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | Version 0.1.0 | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | our $VERSION = '0.1.0'; | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | use Toader::findToaderDirs; | ||
| 23 | |||
| 24 | my $foo = Toader::findToaderDirs->new(); | ||
| 25 | |||
| 26 | my @dirs=$foo->findToaderDirs($directory); | ||
| 27 | if($foo->error){ | ||
| 28 | warn('Error '.$foo->error.': '.$foo->errorString); | ||
| 29 | }else{ | ||
| 30 | print join("\n", @dirs)."\n"; | ||
| 31 | } | ||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | =head2 new | ||
| 36 | |||
| 37 | This initiates the object. | ||
| 38 | |||
| 39 | my $foo = Toader::findToaderDirs->new(); | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub new{ | ||
| 44 | my $self={ | ||
| 45 | error=>undef, | ||
| 46 | errorString=>'', | ||
| 47 | isatd=>Toader::isaToaderDir->new, | ||
| 48 | errorExtra=>{ | ||
| 49 | flags=>{ | ||
| 50 | 1=>'noDirSpecified', | ||
| 51 | 2=>'notAdir', | ||
| 52 | 3=>'notAtoaderDir', | ||
| 53 | 4=>'isaToaderDirErrored', | ||
| 54 | 5=>'rootIsAtoaderDir', | ||
| 55 | 6=>'dirOpenFailed', | ||
| 56 | }, | ||
| 57 | }, | ||
| 58 | }; | ||
| 59 | bless $self; | ||
| 60 | |||
| 61 | return $self; | ||
| 62 | } | ||
| 63 | |||
| 64 | =head2 findToaderDirs | ||
| 65 | |||
| 66 | This returns all found L<Toader> directories under the path. | ||
| 67 | |||
| 68 | One argument is taken and it a L<Toader> directory. | ||
| 69 | |||
| 70 | The returned array will also include the one it started in. | ||
| 71 | |||
| 72 | my @dirs=$foo->findToaderDirs($directory); | ||
| 73 | if($foo->error){ | ||
| 74 | warn('Error '.$foo->error.': '.$foo->errorString); | ||
| 75 | }else{ | ||
| 76 | print join("\n", @dirs)."\n"; | ||
| 77 | } | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub findToaderDirs{ | ||
| 82 | my $self=$_[0]; | ||
| 83 | my $dir=$_[1]; | ||
| 84 | my $recursive=$_[2]; | ||
| 85 | |||
| 86 | #blank any previous errors | ||
| 87 | if(!$self->errorblank){ | ||
| 88 | return undef; | ||
| 89 | } | ||
| 90 | |||
| 91 | # Makes sure a directory is specified. | ||
| 92 | if (!defined( $dir )) { | ||
| 93 | $self->{error}=1; | ||
| 94 | $self->{errorString}='No directory defined'; | ||
| 95 | $self->warn; | ||
| 96 | return undef; | ||
| 97 | } | ||
| 98 | |||
| 99 | # Make sure the what is a directory. | ||
| 100 | if (! -d $dir ) { | ||
| 101 | $self->{error}=2; | ||
| 102 | $self->{errorString}='The specified item is not a directory'; | ||
| 103 | $self->warn; | ||
| 104 | return undef; | ||
| 105 | } | ||
| 106 | |||
| 107 | #make sure the directory we were passed is a Toader directory | ||
| 108 | my $returned=$self->{isatd}->isaToaderDir($dir); | ||
| 109 | if ($self->{isatd}->error) { | ||
| 110 | $self->{error}=4; | ||
| 111 | $self->{errorString}='isaToaderDir returned "'.$self->{isatd}->error.'", "'.$self->{isatd}->errorString.'"'; | ||
| 112 | $self->warn; | ||
| 113 | return undef; | ||
| 114 | } | ||
| 115 | if (!$returned) { | ||
| 116 | $self->{error}=3; | ||
| 117 | $self->{errorString}='"'.$dir.'" is not a Toader directory'; | ||
| 118 | $self->warn; | ||
| 119 | return undef; | ||
| 120 | } | ||
| 121 | |||
| 122 | #this returns a list of found directories | ||
| 123 | my @dirs; | ||
| 124 | |||
| 125 | #gets the subdirs to start | ||
| 126 | my @sdirs=$self->findToaderSubDirs($dir); | ||
| 127 | |||
| 128 | #puts together the first one | ||
| 129 | my $int=0; | ||
| 130 | while( defined( $sdirs[$int] ) ){ | ||
| 131 | push( @dirs, $dir.'/'.$sdirs[$int] ); | ||
| 132 | |||
| 133 | $int++; | ||
| 134 | } | ||
| 135 | #prevents duplicates | ||
| 136 | if( ! $recursive ){ | ||
| 137 | push(@dirs, $dir); | ||
| 138 | } | ||
| 139 | |||
| 140 | #process each subdir | ||
| 141 | $int=0; | ||
| 142 | while( defined( $sdirs[$int] ) ){ | ||
| 143 | my $newdir=$dir.'/'.$sdirs[$int]; | ||
| 144 | |||
| 145 | #set the recursive arg to true as we don't want to add it twice | ||
| 146 | my @newdirs=$self->findToaderDirs( $newdir, '1' ); | ||
| 147 | |||
| 148 | push( @dirs, @newdirs ); | ||
| 149 | |||
| 150 | $int++; | ||
| 151 | } | ||
| 152 | |||
| 153 | return @dirs; | ||
| 154 | } | ||
| 155 | |||
| 156 | =head2 findToaderSubDirs | ||
| 157 | |||
| 158 | This lists all sub L<Toader> directories under a specified L<Toader> directory. | ||
| 159 | |||
| 160 | This only returns the found directory names under the directory. | ||
| 161 | |||
| 162 | my @sub=$foo->findToaderSubDirs($dir); | ||
| 163 | if($foo->error){ | ||
| 164 | warn('Error '.$foo->error.': '.$foo->errorString); | ||
| 165 | }else{ | ||
| 166 | print join("\n", @dirs)."\n"; | ||
| 167 | } | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub findToaderSubDirs{ | ||
| 172 | my $self=$_[0]; | ||
| 173 | my $dir=$_[1]; | ||
| 174 | |||
| 175 | #blank any previous errors | ||
| 176 | if(!$self->errorblank){ | ||
| 177 | return undef; | ||
| 178 | } | ||
| 179 | |||
| 180 | # Makes sure a directory is specified. | ||
| 181 | if (!defined( $dir )) { | ||
| 182 | $self->{error}=1; | ||
| 183 | $self->{errorString}='No directory defined'; | ||
| 184 | $self->warn; | ||
| 185 | return undef; | ||
| 186 | } | ||
| 187 | |||
| 188 | # Make sure the what is a directory. | ||
| 189 | if (! -d $dir ) { | ||
| 190 | $self->{error}=2; | ||
| 191 | $self->{errorString}='The specified item is not a directory'; | ||
| 192 | $self->warn; | ||
| 193 | return undef; | ||
| 194 | } | ||
| 195 | |||
| 196 | #initiates the directory checker | ||
| 197 | my $isatd=Toader::isaToaderDir->new; | ||
| 198 | |||
| 199 | #make sure the directory we were passed is a Toader directory | ||
| 200 | my $returned=$isatd->isaToaderDir($dir); | ||
| 201 | if ($isatd->error) { | ||
| 202 | $self->{error}=4; | ||
| 203 | $self->{errorString}='isaToaderDir returned "'.$isatd->error.'", "'.$isatd->errorString.'"'; | ||
| 204 | $self->warn; | ||
| 205 | return undef; | ||
| 206 | } | ||
| 207 | if (!$returned) { | ||
| 208 | $self->{error}=3; | ||
| 209 | $self->{errorString}='"'.$dir.'" is not a Toader directory'; | ||
| 210 | $self->warn; | ||
| 211 | return undef; | ||
| 212 | } | ||
| 213 | |||
| 214 | my @dirs; | ||
| 215 | |||
| 216 | #opens the directory handle | ||
| 217 | my $dh; | ||
| 218 | if(!opendir($dh,$dir)){ | ||
| 219 | $self->{error}=6; | ||
| 220 | $self->{errorString}='Unable to open the specified directory'; | ||
| 221 | $self->warn; | ||
| 222 | return undef; | ||
| 223 | } | ||
| 224 | #process each item | ||
| 225 | while(readdir($dh)){ | ||
| 226 | if ( | ||
| 227 | ( $_ ne '.' ) && | ||
| 228 | ( $_ ne '..' ) && | ||
| 229 | ( -d $dir.'/'.$_.'/.toader' ) | ||
| 230 | ) { | ||
| 231 | push(@dirs, $_); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | #done with the directory handle | ||
| 235 | closedir($dh); | ||
| 236 | |||
| 237 | return @dirs; | ||
| 238 | } | ||
| 239 | |||
| 240 | =head1 ERROR CODES | ||
| 241 | |||
| 242 | =head2 1, noDirSpecified | ||
| 243 | |||
| 244 | No directory specified. | ||
| 245 | |||
| 246 | =head2 2, notAdir | ||
| 247 | |||
| 248 | Not a directory. | ||
| 249 | |||
| 250 | =head2 3, notAtoaderDir | ||
| 251 | |||
| 252 | Not a L<Toader> directory. | ||
| 253 | |||
| 254 | =head2 4, isaToaderDirErrored | ||
| 255 | |||
| 256 | L<Toader::isaToaderDir>->isaToaderDir errored. | ||
| 257 | |||
| 258 | =head2 5, rootIsAtoaderDir | ||
| 259 | |||
| 260 | "/" is the directory and it appears to be a L<Toader> directory. | ||
| 261 | |||
| 262 | This is a major WTF and should not be even if '/.toader' exists. | ||
| 263 | |||
| 264 | =head2 6, dirOpenFailed | ||
| 265 | |||
| 266 | Could not open one of the directories. | ||
| 267 | |||
| 268 | =head1 AUTHOR | ||
| 269 | |||
| 270 | Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >> | ||
| 271 | |||
| 272 | =head1 BUGS | ||
| 273 | |||
| 274 | Please report any bugs or feature requests to C<bug-toader at rt.cpan.org>, or through | ||
| 275 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Toader>. I will be notified, and then you'll | ||
| 276 | automatically be notified of progress on your bug as I make changes. | ||
| 277 | |||
| 278 | =head1 SUPPORT | ||
| 279 | |||
| 280 | You can find documentation for this module with the perldoc command. | ||
| 281 | |||
| 282 | perldoc Toader::findToaderDirs | ||
| 283 | |||
| 284 | |||
| 285 | You can also look for information at: | ||
| 286 | |||
| 287 | =over 4 | ||
| 288 | |||
| 289 | =item * RT: CPAN's request tracker | ||
| 290 | |||
| 291 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Toader> | ||
| 292 | |||
| 293 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 294 | |||
| 295 | L<http://annocpan.org/dist/Toader> | ||
| 296 | |||
| 297 | =item * CPAN Ratings | ||
| 298 | |||
| 299 | L<http://cpanratings.perl.org/d/Toader> | ||
| 300 | |||
| 301 | =item * Search CPAN | ||
| 302 | |||
| 303 | L<http://search.cpan.org/dist/Toader/> | ||
| 304 | |||
| 305 | =back | ||
| 306 | |||
| 307 | |||
| 308 | =head1 ACKNOWLEDGEMENTS | ||
| 309 | |||
| 310 | |||
| 311 | =head1 LICENSE AND COPYRIGHT | ||
| 312 | |||
| 313 | Copyright 2013 Zane C. Bowers-Hadley. | ||
| 314 | |||
| 315 | This program is free software; you can redistribute it and/or modify it | ||
| 316 | under the terms of either: the GNU General Public License as published | ||
| 317 | by the Free Software Foundation; or the Artistic License. | ||
| 318 | |||
| 319 | See http://dev.perl.org/licenses/ for more information. | ||
| 320 | |||
| 321 | |||
| 322 | =cut | ||
| 323 | |||
| 324 | 1; # End of Toader::findToaderDirs |