more paths methods

This commit is contained in:
Graham Knop 2010-02-20 14:26:45 -06:00
parent 26bfeb772e
commit 45b84ec93f

View file

@ -15,6 +15,12 @@ package WebGUI::Paths;
=cut =cut
our $VERSION = '0.0.1'; our $VERSION = '0.0.1';
use Carp qw(croak);
use Cwd qw(realpath);
use File::Spec::Functions qw(catdir splitpath catpath splitpath updir catfile);
use Try::Tiny;
use namespace::clean;
=head1 NAME =head1 NAME
@ -30,67 +36,90 @@ These methods are available from this class:
=cut =cut
use File::Spec ();
use Cwd ();
my $root; my $root;
BEGIN { BEGIN {
$root = Cwd::realpath(File::Spec->catdir( $root = realpath(catdir(
File::Spec->catpath((File::Spec->splitpath(__FILE__))[0,1], ''), catpath((splitpath(__FILE__))[0,1], ''), (updir) x 2
(File::Spec->updir) x 2
)); ));
} }
use constant { use constant {
CONFIG_BASE => File::Spec->catdir($root, 'etc'), CONFIG_BASE => catdir($root, 'etc'),
LOG_CONFIG => File::Spec->catfile($root, 'etc', 'log.conf'), LOG_CONFIG => catfile($root, 'etc', 'log.conf'),
SPECTRE_CONFIG => File::Spec->catfile($root, 'etc', 'spectre.conf'), SPECTRE_CONFIG => catfile($root, 'etc', 'spectre.conf'),
UPGRADES_PATH => File::Spec->catfile($root, 'var', 'upgrades'), UPGRADES_PATH => catfile($root, 'var', 'upgrades'),
PRELOAD_CUSTOM => File::Spec->catfile($root, 'sbin', 'preload.custom'), PRELOAD_CUSTOM => catfile($root, 'sbin', 'preload.custom'),
PRELOAD_EXCLUSIONS => File::Spec->catfile($root, 'sbin', 'preload.exclude'), PRELOAD_EXCLUSIONS => catfile($root, 'sbin', 'preload.exclude'),
EXTRAS => File::Spec->catdir($root, 'www', 'extras'), EXTRAS => catdir($root, 'www', 'extras'),
DEFAULT_UPLOADS => File::Spec->catdir($root, 'www', 'uploads'), DEFAULT_UPLOADS => catdir($root, 'www', 'uploads'),
DEFAULT_SQL => File::Spec->catdir($root, 'var', 'create.sql'), DEFAULT_SQL => catdir($root, 'var', 'create.sql'),
}; };
sub siteConfigs { sub siteConfigs {
opendir my $dh, CONFIG_BASE; opendir my $dh, CONFIG_BASE;
my @configs; my @configs;
while (my $file = readdir $dh) { while ( my $file = readdir $dh ) {
my $fullPath = Cwd::realpath(File::Spec->catfile(CONFIG_BASE, $file)); my $fullPath = realpath( catfile( CONFIG_BASE, $file ) );
if (-d $fullPath if ( -d $fullPath
|| $file !~ /\.conf$/ || $file !~ /\.conf$/
|| $fullPath eq Cwd::realpath(LOG_CONFIG) || $fullPath eq realpath(LOG_CONFIG)
|| $fullPath eq Cwd::realpath(SPECTRE_CONFIG) || $fullPath eq realpath(SPECTRE_CONFIG) )
) { {
next; next;
} }
push @configs, $fullPath; push @configs, $fullPath;
} }
return @configs; return @configs;
} } ## end sub siteConfigs
sub preloadPaths { sub preloadPaths {
my @paths; my @paths;
if (open my $fh, '<', PRELOAD_CUSTOM) { try {
while (my $path = <$fh>) { @paths = grep {
$path =~ s/#.*//; -d ? 1 : do {
$path =~ s/^\s+//; warn "WARNING: Not adding lib directory '$path' from @{[PRELOAD_CUSTOM]}: Directory does not exist.\n";
$path =~ s/\s+$//; 0;
next
if !$path;
if (! -d $path) {
warn "WARNING: Not adding using lib directory '$path' from @{[PRELOAD_CUSTOM]}: Directory does not exist.\n";
} }
else { } _readTextLines(PRELOAD_CUSTOM);
push @paths, $path; };
} return @paths;
}
close $fh;
}
} }
sub includePreloads { sub includePreloads {
unshift @INC, preloadPaths(); unshift @INC, preloadPaths();
} }
sub preloadExclude {
my @excludes = _readTextLines(PRELOAD_EXCLUDE);
return @excludes;
}
sub preloadAll {
require WebGUI::Pluggable;
WebGUI::Pluggable::findAndLoad( 'WebGUI', {
exclude => \( preloadExclude() ),
onLoadFail => sub { warn sprintf 'Error loading %s: %s', @_ },
});
}
no namespace::clean;
sub _readTextLines {
my $file = shift;
my @lines;
open my $fh, '<', $file or croak "Cannot open $file: $!";
while (my $line = <$fh>) {
$line =~ s/#.*//;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
next
if !$line;
push @lines, $line;
}
return @lines;
}
use namespace::clean;
1; 1;