Compare commits

...

2 Commits

Author SHA1 Message Date
Zane C. B-H d88b2099f9 more work 2021-11-11 05:32:40 -06:00
Zane C. B-H ad158ce4da get_template works 2021-11-10 00:35:50 -06:00
5 changed files with 223 additions and 26 deletions

View File

@ -22,6 +22,10 @@ my %WriteMakefileArgs = (
'Template' => '3.009',
'Template::Plugin::JSON' => '0.08',
'File::ShareDir' => '0',
'Data::Dumper' => '0',
'JSON' => '0',
'YAML' => '0',
'TOML' => '0',
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => 'Server-Toaster-*' },

View File

@ -11,27 +11,146 @@ use warnings;
use Server::Toaster;
use Getopt::Long qw(:config pass_through);
use Data::Dumper;
use TOML qw(from_toml to_toml);;
use JSON;
use YAML qw(Dump);
my $version = '0.0.1';
# make the output consistent
$Data::Dumper::Sortkeys = 1;
my $stoaster_dir;
my $templates_dir;
my $output_dir;
my $module;
my $action;
my $template;
my $help;
# fuck JSON, but in this case it does print everything in a easier to read fashion :(
my $val_print = 'json';
GetOptions(
's=s' => \$stoaster_dir,
't=s' => \$templates_dir,
'o=s' => \$output_dir,
'm=s' => \$module,
'a=s' => \$action,
's=s' => \$stoaster_dir,
'T=s' => \$templates_dir,
'o=s' => \$output_dir,
'm=s' => \$module,
'a=s' => \$action,
't=s' => \$template,
'vp=s' => \$val_print,
'h' => \$help,
'help' => \$help,
);
#make sure val_print, --vp, is something valid
my %val_print_test=(
toml=>1,
json=>1,
dump=>1,
yaml=>1,
);
if (!defined( $val_print_test{$val_print} )) {
die('"'.$val_print.'" is not a valid value print type');
}
# print help info if asked
if ($help) {
print "stoaster v" . $version . '
-s <dir> Server Toaster directory.
-T <dir> Template directory.
-o <dir> Output directory.
-m <mod> Module to use.
-a <action> Action to perform.
-h Print this.
--help Print this.
Default Directories
-s stoaster
-T $opts{s}/Templates
-T $opts{o}/output
';
exit 255;
}
# make sure we have an action
if ( !defined($action) ) {
die 'Np action defined via -a';
}
# init the stoaster object
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 );
# print the requested template if asked
if ( $action eq 'get_files' ) {
# make sure we have a module specified
if (!defined($module)) {
die('No module specified. Please use the -m switch');
}
# get the varius templates a module has
my %returned = $stoaster->get_files($module);
# print the templates a module uses
foreach my $template ( keys(%returned) ) {
print $template
. "\n use => "
. $returned{$template}{use}
. "\n share => "
. $returned{$template}{share} . "\n\n";
}
exit 0;
}
# print a specified template
if ( $action eq 'get_template' ) {
# make sure we have a module specified
if (!defined($module)) {
die('No module specified. Please use the -m switch');
}
# make sure we have a template specified
if ( !defined($template) ) {
die('Please specify a template using -t');
}
# get the template and print it
my $template_data = $stoaster->get_template( $module, $template );
print $template_data;
exit 0;
}
# get the default settings and print them
if ( $action eq 'get_defaults' ) {
my $defaults = $stoaster->get_defaults;
$Data::Dumper::Sortkeys = 1;
# check the specified value print method and print it using that
# make sure all keys are sorted for the sake of sanity
if ($val_print eq 'dump') {
print Dumper($defaults);
}elsif ($val_print eq 'toml') {
print to_toml($defaults);
}elsif ($val_print eq 'json') {
my $json=JSON->new;
$json->pretty(1);
$json->canonical(1);
print $json->encode( $defaults );
}elsif ($val_print eq 'yaml') {
print Dump($defaults);
}
exit 0;
}
# no action defined
die 'No action matched';

View File

@ -151,6 +151,16 @@ sub fill_in {
}
=head2 get_defaults
=cut
sub get_defaults{
my ( $self ) = @_;
return $self->{defaults}
}
=head2 get_files
Fetches a list of templates a module uses.
@ -194,6 +204,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> >>