App-Module-Template
view release on metacpan or search on metacpan
lib/App/Module/Template.pm view on Meta::CPAN
use Getopt::Std;
use POSIX qw(strftime);
use Template;
use Try::Tiny;
our (@EXPORT_OK, %EXPORT_TAGS);
@EXPORT_OK = qw(
run
_get_config
_get_config_path
_get_module_dirs
_get_module_fqfn
_get_template_path
_module_path_exists
_process_dirs
_process_file
_process_template
_validate_module_name
);
%EXPORT_TAGS = (
ALL => [ @EXPORT_OK ],
);
#-------------------------------------------------------------------------------
sub run {
my $class = shift;
my %opt;
# -c config file
# -m module name
# -t template dir, location of template files
getopts('c:m:t:', \%opt);
unless ( ( exists $opt{m} ) and ( defined $opt{m} ) ) {
croak "-m <Module::Name> is required. exiting...\n";
}
my $module = $opt{m};
my $dist = $module; $dist =~ s/::/-/gmsx;
my $file = $module; $file =~ s/.*:://msx; $file .= '.pm';
my $dist_dir = File::Spec->catdir( File::Spec->curdir, $dist );
my $tmpl_vars;
try {
_validate_module_name($module);
} catch {
croak "$_ module-template. exiting...";
};
if ( _module_path_exists($dist_dir) ) {
croak "Destination directory $dist_dir exists. exiting...";
}
my $template_dir = _get_template_path($opt{t});
my $config_file = _get_config_path($opt{c}, $template_dir);
my $cfg = _get_config($config_file);
# Setting this lets TT2 handle creating the destination files/directories
$cfg->{template_toolkit}{OUTPUT_PATH} = $dist_dir;
my $tt2 = Template->new( $cfg->{template_toolkit} );
# don't need this in the $tmpl_vars
delete $cfg->{template_toolkit};
my $dirs = _get_module_dirs( $module );
# Template Vars
$tmpl_vars = $cfg;
$tmpl_vars->{module} = $module;
$tmpl_vars->{today} = strftime('%Y-%m-%d', localtime());
$tmpl_vars->{year} = strftime('%Y', localtime());
$tmpl_vars->{module_path} = File::Spec->catfile( @{$dirs}, $file );
_process_dirs($tt2, $tmpl_vars, $template_dir, $template_dir);
# add the distribution dir to the front so our module ends up in the
# right place
unshift @{$dirs}, $dist_dir;
my $fqfn = _get_module_fqfn( $dirs, $file );
# create the module directory to receive the named module.pm
make_path( File::Spec->catdir( @{$dirs} ) );
# rename the template file with the module file name
move( File::Spec->catfile( $dist_dir, 'lib', 'Module.pm' ), $fqfn );
return 1;
}
#-------------------------------------------------------------------------------
sub _get_config {
my ($config_file) = @_;
my %cfg = Config::General->new(
-ConfigFile => $config_file,
-MergeDuplicateBlocks => 1,
-MergeDuplicateOptions => 1,
-AutoLaunder => 1,
-SplitPolicy => 'equalsign',
-InterPolateVars => 1,
-UTF8 => 1,
)->getall() or croak "Could not read configuration file $config_file";
return \%cfg;
}
#-------------------------------------------------------------------------------
sub _get_config_path {
my ($opt, $template_dir) = @_;
my $config_file;
if ( defined $opt ) {
$config_file = $opt;
}
else {
$config_file = File::Spec->catfile( $template_dir, '../config' );
( run in 0.894 second using v1.01-cache-2.11-cpan-5b529ec07f3 )