| 1 | package MYDan::Util::DirConf; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MYDan::Util::DirConf - Interface module: directory configuration with a YAML file. | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use base MYDan::Util::DirConf; | ||
| 10 | |||
| 11 | sub define { qw( log code ) }; | ||
| 12 | |||
| 13 | my $conf = MYDan::Util::DirConf->new( '/conf/file' ); | ||
| 14 | my $conf->make() if ! $conf->check; | ||
| 15 | |||
| 16 | my $conf = $conf->path(); ## HASH ref | ||
| 17 | my $logdir = $conf->path( 'log' ); | ||
| 18 | my $logfile = $conf->path( log => 'foobar.log' ); | ||
| 19 | |||
| 20 | =cut | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | |||
| 24 | use Cwd; | ||
| 25 | use Carp; | ||
| 26 | use YAML::XS; | ||
| 27 | use File::Spec; | ||
| 28 | use FindBin qw( $Bin $RealBin $Script $RealScript ); | ||
| 29 | |||
| 30 | sub new | ||
| 31 | { | ||
| 32 | my ( $class, $conf, ) = splice @_; | ||
| 33 | my ( %conf, %path ) = ( abscent => {}, path => {} ); | ||
| 34 | |||
| 35 | confess "undefined config" unless $conf; | ||
| 36 | $conf = readlink $conf if -l $conf; | ||
| 37 | |||
| 38 | my $error = "invalid config $conf"; | ||
| 39 | confess "$error: not a regular file" unless -f $conf; | ||
| 40 | |||
| 41 | eval { $conf = YAML::XS::LoadFile( $conf ) }; | ||
| 42 | |||
| 43 | confess "$error: $@" if $@; | ||
| 44 | confess "$error: not HASH" if ref $conf ne 'HASH'; | ||
| 45 | |||
| 46 | for ( my $loop = keys %$conf; $loop; ) | ||
| 47 | { | ||
| 48 | for ( $loop = 0; my ( $name, $path ) = each %$conf; ) | ||
| 49 | { | ||
| 50 | $conf->{$name} = $path = $class->macro( $path ); | ||
| 51 | $loop = $path{$name} = delete $conf->{$name} if $path !~ /\$/; | ||
| 52 | } | ||
| 53 | |||
| 54 | while ( my ( $name, $path ) = each %path ) | ||
| 55 | { | ||
| 56 | for my $dir ( keys %$conf ) | ||
| 57 | { | ||
| 58 | $conf->{$dir} =~ s/\$$name\b/$path/g; | ||
| 59 | $conf->{$dir} =~ s/\$\{$name\}/$path/g; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | confess "$error: unresolved variable" if %$conf; | ||
| 65 | |||
| 66 | my $self = bless \%conf, ref $class || $class; | ||
| 67 | |||
| 68 | map { confess "$error: $_ not defined" | ||
| 69 | unless $conf{path}{$_} = Cwd::abs_path( $path{$_} ) } $self->define(); | ||
| 70 | |||
| 71 | return $self; | ||
| 72 | } | ||
| 73 | |||
| 74 | =head1 METHODS | ||
| 75 | |||
| 76 | =head3 check() | ||
| 77 | |||
| 78 | Inspect directories. Returns true if all directories exist, false otherwise. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | sub check | ||
| 82 | { | ||
| 83 | my $self = shift; | ||
| 84 | my %dir = reverse %{ $self->{path} }; | ||
| 85 | |||
| 86 | map { delete $dir{$_} if -d $_ || -l $_ } keys %dir; | ||
| 87 | $self->{abscent} = { reverse %dir }; | ||
| 88 | return ! keys %dir; | ||
| 89 | } | ||
| 90 | |||
| 91 | =head3 make() | ||
| 92 | |||
| 93 | Set up directories. Returns invoking object. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | sub make | ||
| 97 | { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | map { confess "cannot mkdir $_" if system( "rm -f $_ && mkdir -p $_" ) } | ||
| 101 | values %{ $self->{abscent} } unless $self->check(); | ||
| 102 | |||
| 103 | $self->{abscent} = {}; | ||
| 104 | return $self; | ||
| 105 | } | ||
| 106 | |||
| 107 | =head3 path( name => @name ) | ||
| 108 | |||
| 109 | Join a known path I<name> with @name. See File::Spec->join(). | ||
| 110 | |||
| 111 | =cut | ||
| 112 | sub path | ||
| 113 | { | ||
| 114 | my ( $self, $name ) = splice @_, 0, 2; | ||
| 115 | my $path = $self->{path}; | ||
| 116 | return $path unless defined $name; | ||
| 117 | return $path unless ( $path = $path->{$name} ) && @_; | ||
| 118 | File::Spec->join( $path, @_ ); | ||
| 119 | } | ||
| 120 | |||
| 121 | =head3 macro( $path ) | ||
| 122 | |||
| 123 | Replace $RealBin and $Bin in $path | ||
| 124 | |||
| 125 | =cut | ||
| 126 | sub macro | ||
| 127 | { | ||
| 128 | my ( $self, $path ) = splice @_; | ||
| 129 | if ( $path ) | ||
| 130 | { | ||
| 131 | $path =~ s/\$Bin\b/$Bin/g; $path =~ s/\$RealScript\b/$RealScript/g; | ||
| 132 | $path =~ s/\$\{Bin\}/$Bin/g; $path =~ s/\$\{RealScript\}/$RealScript/g; | ||
| 133 | $path =~ s/\$Script\b/$Script/g; $path =~ s/\$RealBin\b/$RealBin/g; | ||
| 134 | $path =~ s/\$\{Script\}/$Script/g; $path =~ s/\$\{RealBin\}/$RealBin/g; | ||
| 135 | } | ||
| 136 | return $path; | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; |