| 1 | package Sweet::File::Template; | ||
| 2 | use latest; | ||
| 3 | use Moose; | ||
| 4 | |||
| 5 | use Carp; | ||
| 6 | use MooseX::AttributeShortcuts; | ||
| 7 | use MooseX::Types::Path::Class; | ||
| 8 | use Sweet::Types; | ||
| 9 | use Template; | ||
| 10 | |||
| 11 | use namespace::autoclean; | ||
| 12 | |||
| 13 | extends 'Sweet::File'; | ||
| 14 | |||
| 15 | has output => ( | ||
| 16 | default => sub { '' }, | ||
| 17 | init_arg => undef, | ||
| 18 | is => 'rw', | ||
| 19 | isa => 'Str', | ||
| 20 | required => 1, | ||
| 21 | ); | ||
| 22 | |||
| 23 | has include_path => ( | ||
| 24 | coerce => 1, | ||
| 25 | is => 'lazy', | ||
| 26 | isa => 'Path::Class::Dir', | ||
| 27 | ); | ||
| 28 | |||
| 29 | has _template => ( | ||
| 30 | builder => '_build_template', | ||
| 31 | is => 'lazy', | ||
| 32 | isa => 'Template' | ||
| 33 | ); | ||
| 34 | |||
| 35 | |||
| 36 | sub _build_template { | ||
| 37 | my $self = shift; | ||
| 38 | |||
| 39 | my $config = $self->_template_config; | ||
| 40 | |||
| 41 | my $template = Template->new($config) | ||
| 42 | or die $Template::ERROR, "\n"; | ||
| 43 | |||
| 44 | return $template; | ||
| 45 | } | ||
| 46 | |||
| 47 | has _template_config => ( | ||
| 48 | builder => '_build_template_config', | ||
| 49 | is => 'ro', | ||
| 50 | isa => 'HashRef', | ||
| 51 | lazy =>1, | ||
| 52 | ); | ||
| 53 | |||
| 54 | sub _build_template_config { | ||
| 55 | my $self = shift; | ||
| 56 | |||
| 57 | my $include_path = $self->include_path; | ||
| 58 | |||
| 59 | my $config = { | ||
| 60 | INCLUDE_PATH => $include_path, | ||
| 61 | POST_CHOMP => 1 | ||
| 62 | }; | ||
| 63 | |||
| 64 | return $config; | ||
| 65 | } | ||
| 66 | |||
| 67 | has template_name => ( | ||
| 68 | is => 'lazy', | ||
| 69 | isa => 'Str' | ||
| 70 | ); | ||
| 71 | |||
| 72 | sub _build_template_name { {} } | ||
| 73 | |||
| 74 | has template_vars => ( | ||
| 75 | is => 'lazy', | ||
| 76 | isa => 'HashRef' | ||
| 77 | ); | ||
| 78 | |||
| 79 | sub _build_template_vars { {} } | ||
| 80 | |||
| 81 | sub generate { | ||
| 82 | my $self = shift; | ||
| 83 | |||
| 84 | $self->process; | ||
| 85 | $self->write; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _build_lines { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | my $output = $self->output; | ||
| 92 | |||
| 93 | my @lines = split "\n", $output; | ||
| 94 | |||
| 95 | return \@lines; | ||
| 96 | } | ||
| 97 | |||
| 98 | use Data::Dumper; | ||
| 99 | sub process { | ||
| 100 | my $self = shift; | ||
| 101 | |||
| 102 | my $template = $self->_template; | ||
| 103 | my $output = $self->output; | ||
| 104 | my $template_name = $self->template_name; | ||
| 105 | my $template_vars = $self->template_vars; | ||
| 106 | |||
| 107 | say Dumper($template_vars); | ||
| 108 | $template->process( $template_name, $template_vars, \$output ) | ||
| 109 | or die $Template::ERROR, "\n"; | ||
| 110 | |||
| 111 | $self->output($output); | ||
| 112 | } | ||
| 113 | |||
| 114 | __PACKAGE__->meta->make_immutable; | ||
| 115 | |||
| 116 | 1; | ||
| 117 | __END__ |