1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
package Boxer::File::WithSkeleton;
=encoding UTF-8
=cut
use v5.20;
use utf8;
use Role::Commons -all;
use feature 'signatures';
use namespace::autoclean 0.16;
use Path::Tiny;
use Template::Tiny;
use File::ShareDir qw(dist_dir);
use Moo;
use MooX::StrictConstructor;
use Types::Standard qw(Maybe);
use Types::TypeTiny qw(HashLike);
use Types::Path::Tiny qw(Dir File Path);
use Boxer::Types qw(SkelDir Basename);
use strictures 2;
no warnings "experimental::signatures";
=head1 VERSION
Version v1.4.3
=cut
our $VERSION = "v1.4.3";
# permit callers to sloppily pass undefined values
sub BUILDARGS ( $class, %args )
{
delete @args{ grep !defined( $args{$_} ), keys %args };
return {%args};
}
has basename => (
is => 'ro',
isa => Basename,
);
has file => (
is => 'lazy',
isa => Basename,
default => sub ($self) {
if ( $self->basename ) {
return $self->basename;
}
elsif ( $self->skeleton_suffix ) {
return $self->skeleton_path->basename( $self->skeleton_suffix );
}
},
);
has file_path => (
is => 'lazy',
isa => Path,
required => 1,
default => sub ($self) {
if ( $self->file_dir and $self->file ) {
return $self->file_dir->child( $self->file );
}
},
);
has file_dir => (
is => 'lazy',
isa => Dir,
default => sub { path('.') },
);
has skeleton => (
is => 'lazy',
isa => Basename,
default => sub ($self) {
if ( $self->basename
and $self->skeleton_dir
and $self->skeleton_suffix )
{
return $self->skeleton_dir->child(
$self->basename . $self->skeleton_suffix )->basename;
}
},
);
has skeleton_path => (
is => 'lazy',
isa => File,
required => 1,
default => sub ($self) {
if ( $self->skeleton_dir and $self->skeleton ) {
return $self->skeleton_dir->child( $self->skeleton );
}
},
);
has skeleton_dir => (
is => 'lazy',
isa => SkelDir,
default => sub { path( dist_dir('Boxer'), 'skel' ) },
);
has skeleton_suffix => (
is => 'ro',
isa => Basename,
default => '.in',
);
sub create ( $self, $vars )
{
my $template = Template::Tiny->new(
TRIM => 1,
);
my $content = '';
$template->process(
\$self->skeleton_path->slurp,
$vars,
\$content
);
$self->file_path->spew( $content . "\n" );
}
=head1 AUTHOR
Jonas Smedegaard C<< <dr@jones.dk> >>.
=cut
our $AUTHORITY = 'cpan:JONASS';
=head1 COPYRIGHT AND LICENCE
Copyright © 2013-2016 Jonas Smedegaard
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
=cut
1;
|