convert the resource classes to use the new Resource::Pack api
[gitmo/moose-website.git] / lib / Moose / Website.pm
1 package Moose::Website;
2 use Moose;
3 use MooseX::Types::Path::Class;
4
5 use Path::Class;
6 use Template;
7 use YAML::XS 'LoadFile';
8
9 use Moose::Website::I18N;
10 use Moose::Website::Resource::Templates;
11 use Moose::Website::Resource::WebFiles;
12
13 our $VERSION   = '0.01';
14 our $AUTHORITY = 'cpan:STEVAN';
15
16 with 'MooseX::Getopt';
17
18 has 'outdir' => (
19     is       => 'ro',
20     isa      => 'Path::Class::Dir',
21     coerce   => 1,
22     required => 1,
23 );
24
25 has 'locale' => (
26     is      => 'ro',
27     isa     => 'Str',
28     default => sub { 'en' },
29 );
30
31 has 'page_file' => (
32     is       => 'ro',
33     isa      => 'Path::Class::File',
34     coerce   => 1,
35     default  => sub {
36         file(__FILE__)->parent->parent->parent->file('data', 'pages.yml')
37     }
38 );
39
40 # ....
41
42 has 'template_resource' => (
43     traits  => [ 'NoGetopt' ],
44     is      => 'ro',
45     isa     => 'Moose::Website::Resource::Templates',
46     lazy    => 1,
47     default => sub {
48         Moose::Website::Resource::Templates->new
49     },
50 );
51
52 sub template_root {
53     shift->template_resource->fetch('template_dir')->install_from_absolute;
54 }
55
56 has 'web_file_resource' => (
57     traits  => [ 'NoGetopt' ],
58     is      => 'ro',
59     isa     => 'Moose::Website::Resource::WebFiles',
60     lazy    => 1,
61     default => sub {
62         Moose::Website::Resource::WebFiles->new(install_to => shift->outdir)
63     },
64 );
65
66 has 'i18n' => (
67     traits  => [ 'NoGetopt' ],
68     is      => 'ro',
69     isa     => 'Object',
70     lazy    => 1,
71     default => sub {
72         my $self = shift;
73         Moose::Website::I18N->get_handle( $self->locale )
74     }
75 );
76
77 has 'pages' => (
78     traits  => [ 'NoGetopt' ],
79     is      => 'ro',
80     isa     => 'ArrayRef[HashRef]',
81     lazy    => 1,
82     default => sub {
83         my $self = shift;
84         LoadFile( $self->page_file->stringify );
85     }
86 );
87
88 has 'tt' => (
89     traits  => [ 'NoGetopt' ],
90     is      => 'ro',
91     isa     => 'Template',
92     lazy    => 1,
93     default => sub {
94         my $self = shift;
95         Template->new(
96             INCLUDE_PATH => $self->template_root,
97             %{ $self->template_config }
98         )
99     }
100 );
101
102 has 'template_config' => (
103     traits  => [ 'NoGetopt' ],
104     is      => 'ro',
105     isa     => 'HashRef',
106     lazy    => 1,
107     default => sub { +{
108         ENCODING => 'UTF-8',
109     } },
110 );
111
112 sub log { shift; warn @_, "\n" }
113
114 sub run {
115     my $self = shift;
116
117     foreach my $page ( @{ $self->pages } ) {
118
119         my $outfile = $self->outdir->file( $page->{outfile} )->stringify;
120
121         $self->log( "Writing page " . $page->{name} . " to $outfile using template " . $page->{template} );
122
123         $self->tt->process(
124             $page->{template},
125             $self->build_template_params( current_page => $page ),
126             $outfile,
127             { binmode => ':encoding(UTF-8)' },
128         ) || confess $self->tt->error;
129     }
130
131     $self->log( "Copying web resources to " . $self->outdir );
132     $self->web_file_resource->install;
133 }
134
135 sub build_template_params {
136     my ($self, %params) = @_;
137
138     $params{ pages }  = $self->pages;
139     $params{ loc }    = sub { $self->i18n->loc( @_ ) };
140     $params{ locale } = $self->locale;
141
142     \%params;
143 }
144
145
146 __PACKAGE__->meta->make_immutable;
147
148 no Moose; 1;
149
150 __END__
151
152 =pod
153
154 =head1 NAME
155
156 Moose::Website - A Moosey solution to this problem
157
158 =head1 SYNOPSIS
159
160   use Moose::Website;
161
162 =head1 DESCRIPTION
163
164 =head1 METHODS
165
166 =over 4
167
168 =item B<>
169
170 =back
171
172 =head1 BUGS
173
174 All complex software has bugs lurking in it, and this module is no
175 exception. If you find a bug please either email me, or add the bug
176 to cpan-RT.
177
178 =head1 AUTHOR
179
180 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
181
182 =head1 COPYRIGHT AND LICENSE
183
184 Copyright 2010 Infinity Interactive, Inc.
185
186 L<http://www.iinteractive.com>
187
188 This library is free software; you can redistribute it and/or modify
189 it under the same terms as Perl itself.
190
191 =cut