| 1 | package XT::Files; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | our $VERSION = '0.001'; | ||
| 8 | |||
| 9 | use Class::Tiny 1; | ||
| 10 | |||
| 11 | use Role::Tiny::With (); | ||
| 12 | |||
| 13 | Role::Tiny::With::with 'XT::Files::Role::Logger'; | ||
| 14 | |||
| 15 | use Carp (); | ||
| 16 | use File::Basename (); | ||
| 17 | use File::Find (); | ||
| 18 | use Module::Load (); | ||
| 19 | use Scalar::Util (); | ||
| 20 | use version 0.77 (); | ||
| 21 | |||
| 22 | use XT::Files::File; | ||
| 23 | |||
| 24 | use constant MODULE_NAME_RX => qr{ ^ [A-Za-z_] [0-9A-Za-z_]* (?: :: [0-9A-Za-z_]+ )* $ }xs; ## no critic (RegularExpressions::RequireLineBoundaryMatching) | ||
| 25 | |||
| 26 | # | ||
| 27 | # CLASS METHODS | ||
| 28 | # | ||
| 29 | |||
| 30 | sub BUILD { | ||
| 31 | my ( $self, $args ) = @_; | ||
| 32 | |||
| 33 | $self->{_excludes} = []; | ||
| 34 | $self->{_file} = {}; | ||
| 35 | |||
| 36 | if ( exists $args->{'-config'} ) { | ||
| 37 | if ( defined $args->{'-config'} ) { | ||
| 38 | $self->_load_config( $args->{'-config'} ); | ||
| 39 | } | ||
| 40 | |||
| 41 | # -config exists but is not defined, no configuration requested | ||
| 42 | } | ||
| 43 | else { | ||
| 44 | # We did not get "config => undef" and therefore try to load the | ||
| 45 | # default config file | ||
| 46 | $self->_load_default_config(); | ||
| 47 | } | ||
| 48 | |||
| 49 | return; | ||
| 50 | } | ||
| 51 | |||
| 52 | { | ||
| 53 | # The XT::Files singleton | ||
| 54 | my $xtf; | ||
| 55 | |||
| 56 | sub initialize { | ||
| 57 | my $class = shift; | ||
| 58 | |||
| 59 | Carp::croak( __PACKAGE__ . q{ is already initialized} ) if $class->_is_initialized; | ||
| 60 | |||
| 61 | $xtf = $class->new(@_); | ||
| 62 | return $xtf; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub instance { | ||
| 66 | my ($class) = @_; | ||
| 67 | |||
| 68 | if ( !$class->_is_initialized ) { | ||
| 69 | |||
| 70 | # ignore args | ||
| 71 | $class->initialize; | ||
| 72 | } | ||
| 73 | |||
| 74 | return $xtf; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _is_initialized { | ||
| 78 | my ($class) = @_; | ||
| 79 | |||
| 80 | return 1 if defined $xtf; | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | # | ||
| 86 | # OBJECT METHODS | ||
| 87 | # | ||
| 88 | |||
| 89 | sub plugin { | ||
| 90 | my ( $self, $plugin_name, $plugin_version, $keyvals_ref ) = @_; | ||
| 91 | |||
| 92 | my $plugin_pkg = $self->_expand_config_plugin_name($plugin_name); | ||
| 93 | |||
| 94 | Module::Load::load($plugin_pkg); | ||
| 95 | |||
| 96 | if ( defined $plugin_version ) { | ||
| 97 | $self->log_fatal("Not a valid version '$plugin_version'") if !version::is_lax($plugin_version); | ||
| 98 | $self->log_fatal( "$plugin_pkg version $plugin_version required--this is only version " . $plugin_pkg->VERSION ) if version->parse( $plugin_pkg->VERSION ) < version->parse($plugin_version); | ||
| 99 | } | ||
| 100 | |||
| 101 | $self->log_fatal("$plugin_pkg doesn't have a run method") if !$plugin_pkg->can('run'); | ||
| 102 | $self->log_fatal("$plugin_pkg doesn't have a new method") if !$plugin_pkg->can('new'); | ||
| 103 | |||
| 104 | my $plugin = $plugin_pkg->new( xtf => $self ); | ||
| 105 | |||
| 106 | $plugin->run($keyvals_ref); | ||
| 107 | |||
| 108 | return; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub files { | ||
| 112 | my ($self) = @_; | ||
| 113 | |||
| 114 | my $exclude_regex; | ||
| 115 | my @excludes = @{ $self->{_excludes} }; | ||
| 116 | if (@excludes) { | ||
| 117 | $exclude_regex = join q{|}, @excludes; | ||
| 118 | } | ||
| 119 | |||
| 120 | my @result; | ||
| 121 | RESULT_FILE: | ||
| 122 | for my $name ( sort keys %{ $self->{_file} } ) { | ||
| 123 | |||
| 124 | # skip ignored files | ||
| 125 | next RESULT_FILE if !defined $self->{_file}->{$name}; | ||
| 126 | |||
| 127 | # skip excluded files | ||
| 128 | next RESULT_FILE if defined $exclude_regex && File::Basename::fileparse($name) =~ $exclude_regex; | ||
| 129 | |||
| 130 | # skip non-existing files | ||
| 131 | next RESULT_FILE if !-e $name; | ||
| 132 | |||
| 133 | push @result, $self->{_file}->{$name}; | ||
| 134 | } | ||
| 135 | |||
| 136 | return @result; | ||
| 137 | } | ||
| 138 | |||
| 139 | # | ||
| 140 | # File | ||
| 141 | # | ||
| 142 | |||
| 143 | sub bin_file { | ||
| 144 | my ( $self, $name ) = @_; | ||
| 145 | |||
| 146 | my $file = XT::Files::File->new( name => $name, is_script => 1 ); | ||
| 147 | $self->file( $name, $file ); | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub file { | ||
| 152 | my ( $self, $name, $file ) = @_; | ||
| 153 | |||
| 154 | if ( @_ > 2 ) { | ||
| 155 | if ( defined $file ) { | ||
| 156 | $self->log_fatal(q{File is not of class 'XT::Files::File'}) if !defined Scalar::Util::blessed($file) || !$file->isa('XT::Files::File'); | ||
| 157 | } | ||
| 158 | |||
| 159 | $self->{_file}->{$name} = $file; | ||
| 160 | } | ||
| 161 | |||
| 162 | return $self->{_file}->{$name}; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub ignore_file { | ||
| 166 | my ( $self, $name ) = @_; | ||
| 167 | |||
| 168 | $self->file( $name, undef ); | ||
| 169 | return; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub module_file { | ||
| 173 | my ( $self, $name ) = @_; | ||
| 174 | |||
| 175 | my $file = XT::Files::File->new( name => $name, is_module => 1 ); | ||
| 176 | $self->file( $name, $file ); | ||
| 177 | return; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub pod_file { | ||
| 181 | my ( $self, $name ) = @_; | ||
| 182 | |||
| 183 | my $file = XT::Files::File->new( name => $name, is_pod => 1 ); | ||
| 184 | $self->file( $name, $file ); | ||
| 185 | return; | ||
| 186 | } | ||
| 187 | |||
| 188 | sub remove_file { | ||
| 189 | my ( $self, $name ) = @_; | ||
| 190 | |||
| 191 | delete $self->{_file}->{$name}; | ||
| 192 | return; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub test_file { | ||
| 196 | my ( $self, $name ) = @_; | ||
| 197 | |||
| 198 | my $file = XT::Files::File->new( name => $name, is_test => 1, is_script => 1 ); | ||
| 199 | $self->file( $name, $file ); | ||
| 200 | return; | ||
| 201 | } | ||
| 202 | |||
| 203 | # | ||
| 204 | # Directory | ||
| 205 | # | ||
| 206 | |||
| 207 | sub bin_dir { | ||
| 208 | my ( $self, $name ) = @_; | ||
| 209 | |||
| 210 | for my $file ( $self->_find_new_files($name) ) { | ||
| 211 | $self->bin_file( $file, $name ); | ||
| 212 | } | ||
| 213 | |||
| 214 | return; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub module_dir { | ||
| 218 | my ( $self, $name ) = @_; | ||
| 219 | |||
| 220 | for my $file ( $self->_find_new_files($name) ) { | ||
| 221 | if ( $file =~ m{ [.] pm $ }xsm ) { | ||
| 222 | $self->module_file( $file, $name ); | ||
| 223 | } | ||
| 224 | elsif ( $file =~ m{ [.] pod $ }xsm ) { | ||
| 225 | $self->pod_file( $file, $name ); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | return; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub test_dir { | ||
| 233 | my ( $self, $name ) = @_; | ||
| 234 | |||
| 235 | for my $file ( $self->_find_new_files($name) ) { | ||
| 236 | if ( $file =~ m{ [.] t $ }xsm ) { | ||
| 237 | $self->test_file( $file, $name ); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | return; | ||
| 242 | } | ||
| 243 | |||
| 244 | # | ||
| 245 | # Excludes | ||
| 246 | # | ||
| 247 | |||
| 248 | sub exclude { | ||
| 249 | my ( $self, $exclude ) = @_; | ||
| 250 | |||
| 251 | push @{ $self->{_excludes} }, $exclude; | ||
| 252 | return; | ||
| 253 | } | ||
| 254 | |||
| 255 | # | ||
| 256 | # PRIVATE METHODS | ||
| 257 | # | ||
| 258 | |||
| 259 | sub _expand_config_plugin_name { | ||
| 260 | my ( $self, $plugin_name ) = @_; | ||
| 261 | |||
| 262 | my $package_name = $plugin_name; | ||
| 263 | if ( $package_name !~ s{ ^ = }{}xsm ) { | ||
| 264 | $package_name = "XT::Files::Plugin::$plugin_name"; | ||
| 265 | } | ||
| 266 | |||
| 267 | $self->log_fatal("'$plugin_name' is not a valid plugin name") if $package_name !~ MODULE_NAME_RX; | ||
| 268 | |||
| 269 | return $package_name; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub _find_new_files { | ||
| 273 | my ( $self, $dir ) = @_; | ||
| 274 | |||
| 275 | my @files; | ||
| 276 | |||
| 277 | if ( !-d $dir ) { | ||
| 278 | $self->log_debug("Directory $dir does not exist or is not a directory"); | ||
| 279 | return; | ||
| 280 | } | ||
| 281 | |||
| 282 | File::Find::find( | ||
| 283 | { | ||
| 284 | no_chdir => 1, | ||
| 285 | wanted => sub { | ||
| 286 | return if -l $File::Find::name || !-f _; | ||
| 287 | push @files, $File::Find::name; | ||
| 288 | }, | ||
| 289 | }, | ||
| 290 | $dir, | ||
| 291 | ); | ||
| 292 | |||
| 293 | @files = grep { !exists $self->{_file}->{$_} } @files; | ||
| 294 | |||
| 295 | return @files; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub _global_keyval { ## no critic (Subroutines::RequireFinalReturn) | ||
| 299 | my ( $self, $key, $value ) = @_; | ||
| 300 | |||
| 301 | if ( $key eq ':version' ) { | ||
| 302 | $self->log_fatal("Not a valid version '$value'") if !version::is_lax($value); | ||
| 303 | $self->log_fatal( __PACKAGE__ . " version $value required--this is only version " . __PACKAGE__->VERSION ) if version->parse( __PACKAGE__->VERSION ) < version->parse($value); | ||
| 304 | return; | ||
| 305 | } | ||
| 306 | |||
| 307 | $self->log_fatal("Invalid entry '$key = $value'"); | ||
| 308 | } | ||
| 309 | |||
| 310 | sub _load_config { | ||
| 311 | my ( $self, $config ) = @_; | ||
| 312 | |||
| 313 | my $type_section = 1; | ||
| 314 | my $type_keyval = 2; | ||
| 315 | |||
| 316 | open my $fh, '<', $config or $self->log_fatal("Cannot read file '$config': $!"); | ||
| 317 | |||
| 318 | my $in_global_section = 1; | ||
| 319 | my @lines; | ||
| 320 | my $line_counter = 0; | ||
| 321 | LINE: | ||
| 322 | while ( defined( my $line = <$fh> ) ) { | ||
| 323 | $line_counter++; | ||
| 324 | |||
| 325 | # skip empty lines or comment | ||
| 326 | next LINE if $line =~ m{ ^ \s* (?: [#;] | $ ) }xsm; | ||
| 327 | |||
| 328 | # remove leading whitespace | ||
| 329 | $line =~ s{ ^ \s* }{}xsm; | ||
| 330 | |||
| 331 | # remove tailing whitespace | ||
| 332 | $line =~ s{ \s* $ }{}xsm; | ||
| 333 | |||
| 334 | if ( $line =~ m{ ^ \[ }xsm ) { | ||
| 335 | $self->log_fatal("Syntax error in config on line $line_counter") if $line !~ m{ ^ \[ ( .+ ) \] $ }xsm; | ||
| 336 | my $section = $1; ## no critic (RegularExpressions::ProhibitCaptureWithoutTest) | ||
| 337 | push @lines, [ $type_section, $line_counter, $section ]; | ||
| 338 | $in_global_section = 0; | ||
| 339 | next LINE; | ||
| 340 | } | ||
| 341 | |||
| 342 | my ( $key, $value ) = split /\s*=\s*/xsm, $line, 2; | ||
| 343 | $self->log_fatal("Syntax error in config on line $line_counter") if !defined $key || !defined $value || $key eq q{} || $value eq q{}; | ||
| 344 | # | ||
| 345 | if ($in_global_section) { | ||
| 346 | $self->_global_keyval( $key, $value ); | ||
| 347 | next LINE; | ||
| 348 | } | ||
| 349 | |||
| 350 | push @lines, [ $type_keyval, $line_counter, $key, $value ]; | ||
| 351 | } | ||
| 352 | close $fh or $self->logger->log_fatal("Cannot read file '$config': $!"); | ||
| 353 | |||
| 354 | while (@lines) { | ||
| 355 | my $section = ${ $lines[0] }[2]; | ||
| 356 | shift @lines; | ||
| 357 | |||
| 358 | my @keyvals; | ||
| 359 | my $plugin_version; | ||
| 360 | |||
| 361 | LINE_KEYVAL: | ||
| 362 | while (@lines) { | ||
| 363 | last LINE_KEYVAL if $lines[0][0] == $type_section; | ||
| 364 | |||
| 365 | if ( $lines[0][2] eq ':version' ) { | ||
| 366 | $plugin_version = $lines[0][3]; | ||
| 367 | } | ||
| 368 | else { | ||
| 369 | push @keyvals, [ @{ $lines[0] }[ 2, 3 ] ]; | ||
| 370 | } | ||
| 371 | |||
| 372 | shift @lines; | ||
| 373 | } | ||
| 374 | |||
| 375 | $self->plugin( $section, $plugin_version, \@keyvals ); | ||
| 376 | } | ||
| 377 | |||
| 378 | return; | ||
| 379 | } | ||
| 380 | |||
| 381 | sub _load_default_config { | ||
| 382 | my ($self) = @_; | ||
| 383 | |||
| 384 | my $config; | ||
| 385 | FILE: | ||
| 386 | for my $file ( '.xtfilesrc', 'xtfiles.ini' ) { | ||
| 387 | next FILE if !-e $file; | ||
| 388 | $self->log_fatal("Multiple default config files found: '$config' and '$file'") if defined $config; | ||
| 389 | $config = $file; | ||
| 390 | } | ||
| 391 | |||
| 392 | if ( !defined $config ) { | ||
| 393 | my $default_config = '[Default]'; | ||
| 394 | $config = \$default_config; | ||
| 395 | } | ||
| 396 | |||
| 397 | return $self->_load_config($config); | ||
| 398 | } | ||
| 399 | |||
| 400 | 1; | ||
| 401 | |||
| 402 | __END__ |