get_template works

This commit is contained in:
Zane C. B-H 2021-11-10 00:35:50 -06:00
parent 36b36b6429
commit ad158ce4da
4 changed files with 113 additions and 22 deletions

View File

@ -17,21 +17,48 @@ my $templates_dir;
my $output_dir;
my $module;
my $action;
my $template;
GetOptions(
's=s' => \$stoaster_dir,
't=s' => \$templates_dir,
'T=s' => \$templates_dir,
'o=s' => \$output_dir,
'm=s' => \$module,
'a=s' => \$action,
't=s' => \$template,
);
# make sure we have an action
if ( !defined($action) ) {
die 'Np action defined via -a';
}
my $stoaster = Server::Toaster->new(
dir => $stoaster_dir,
templates => $templates_dir,
output => $output_dir,
);
if ($action eq 'get_files') {
my %returned=$stoaster->get_files($module);
print Dumper( \%returned );
if ( $action eq 'get_files' ) {
my %returned = $stoaster->get_files($module);
foreach my $template ( keys(%returned) ) {
print $template
. "\n use => "
. $returned{$template}{use}
. "\n share => "
. $returned{$template}{share} . "\n\n";
}
exit 0;
}
if ( $action eq 'get_template' ) {
my $template_data = $stoaster->get_template( $module, $template );
print $template_data;
exit 0;
}
# no action defined
die 'No action matched';

View File

@ -194,6 +194,39 @@ sub get_files {
return %returned;
}
=head2 get_template
=cut
sub get_template {
my ( $self, $module, $template ) = @_;
# make sure we have a module defined
if ( !defined($module) ) {
die 'No module specified';
}
my $to_return;
my $to_eval
= 'use Server::Toaster::Modules::'
. $module . '; '
. 'my $st_module=Server::Toaster::Modules::'
. $module
. '->new('.
'dir=>$self->{dir}, '
. 'output=>$self->{output}, '
. 'templates=>$self->{templates} );'
. '$to_return=$st_module->get_template( $template );';
eval($to_eval);
if ($@) {
print "Evaled... ".$to_eval."\n";
die "Eval failed... ".$@;
}
return $to_return;
}
=head1 AUTHOR
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>

View File

@ -76,10 +76,7 @@ and the value being the full path to the file.
=cut
sub get_files_module {
return (
'Apache/domain.tt',
'Apache/httpd.conf.tt'
);
return ( 'Apache/domain.tt', 'Apache/httpd.conf.tt' );
}
=head1 AUTHOR

View File

@ -65,12 +65,12 @@ sub new {
# set the default output dir if non is specified
if ( !defined( $opts{output} ) ) {
$opts{output} = catdir( $opts{dir}, 'output' );
$opts{output} = catdir( $opts{dir}, 'output' );
}
# set the default template dir if non is specified
if ( !defined( $opts{templates} ) ) {
$opts{templates} = catdir($opts{dir} , 'templates');
$opts{templates} = catdir( $opts{dir}, 'templates' );
}
# init the object
@ -85,7 +85,7 @@ sub new {
return $self;
}
=head2 get_files_module
=head2 get_files
Fetches a list of templates a module uses.
@ -97,29 +97,63 @@ and the value being the full path to the file.
sub get_files {
my ($self) = @_;
my @files=$self->get_files_module;
my @files = $self->get_files_module;
my $share_dir = dist_dir('Server-Toaster');
my %to_return;
foreach my $file (@files) {
my @split_path=split(/\//, $file);
$to_return{$file}={
rel=>catfile(@split_path),
share=>catfile($share_dir, @split_path),
};
my @split_path = split( /\//, $file );
$to_return{$file} = {
rel => catfile(@split_path),
share => catfile( $share_dir, @split_path ),
};
my $use_test=catfile( $self->{templates}, @split_path );
if (-f $use_test ) {
$to_return{$file}{use}=$use_test;
}else {
$to_return{$file}{use}=$to_return{$file}{share};
my $use_test = catfile( $self->{templates}, @split_path );
if ( -f $use_test ) {
$to_return{$file}{use} = $use_test;
}
else {
$to_return{$file}{use} = $to_return{$file}{share};
}
}
return %to_return;
}
=head2 get_template
Fetches a specified template.
=cut
sub get_template{
my ($self, $template) = @_;
if (!defined( $template ) ) {
die( 'No template specified' );
}
my %files=$self->get_files;
if (!defined( $files{$template} )) {
die( '"'.$template.'" is not a known template' );
}
my $to_use=$files{$template}{use};
my $template_data='';
my $fh;
open( $fh, '<', $to_use) or die( $! );
while (<$fh>) {
$template_data=$template_data.$_;
}
close( $fh );
return $template_data;
}
=head1 AUTHOR
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>