blib/lib/Catalyst/Helper/View/Bootstrap.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 145 CC 10 SCAR 32.0
CC
10
Cov
0%
mk_templates
56.0
get_sharedir_file
20.0
mk_compclass
2.0
CRAP 24.5
1 package Catalyst::Helper::View::Bootstrap;
2  
3 our $VERSION = '0.0051';
4 $VERSION = eval $VERSION;
5  
6 use strict;
7 use warnings;
8 use File::Spec;
9 use Path::Class qw/dir file/;
10 use File::ShareDir qw/dist_dir/;
11  
12 my @template_files = ();
13 my @javascript_files = ();
14 my @image_files = ();
15  
16 sub get_sharedir_file {
17 my ($self, @filename) = @_;
18 my $dist_dir;
19 if (exists $ENV{CATALYST_DEVEL_SHAREDIR}) {
20 $dist_dir = $ENV{CATALYST_DEVEL_SHAREDIR};
21 }
22 elsif (-d "inc/.author" && -f "lib/Catalyst/Helper/View/Bootstrap.pm"
23 ) { # Can't use sharedir if we're in a checkout
24 # this feels horrible, better ideas?
25 $dist_dir = 'share';
26 }
27 else {
28 $dist_dir = dist_dir('Catalyst-Helper-View-Bootstrap');
29 }
30 my $file = file( $dist_dir, @filename);
31 Carp::confess("Cannot find $file") unless -r $file;
32 my $contents = $file->slurp(iomode => "<:raw");
33 return $contents;
34 }
35  
36 sub mk_compclass {
37 my ( $self, $helper, @args ) = @_;
38 my $file = $helper->{file};
39 $helper->render_file( 'compclass', $file );
40 $self->mk_templates( $helper, @args );
41 $helper->{root} = dir( $helper->{base}, 'root' );
42 $helper->mk_dir( $helper->{root} );
43 $helper->{static} = dir( $helper->{root}, 'static' );
44 $helper->mk_dir( $helper->{static} );
45 $helper->{images} = dir( $helper->{static}, 'images' );
46 $helper->mk_dir( $helper->{images} );
47 $self->_mk_images($helper);
48 }
49  
50 sub mk_templates {
51 my ( $self, $helper ) = @_;
52 my $base = $helper->{base},;
53 my $ldir = File::Spec->catfile( $base, 'root', 'lib' );
54 my $sdir = File::Spec->catfile( $base, 'root', 'src' );
55 my $jsdir = File::Spec->catfile( $base, 'root', 'static', 'js' );
56 my $cssdir = File::Spec->catfile( $base, 'root', 'static', 'css' );
57  
58 $helper->mk_dir($ldir);
59 $helper->mk_dir($sdir);
60 $helper->mk_dir($jsdir);
61 $helper->mk_dir($cssdir);
62  
63 my $dir = File::Spec->catfile( $ldir, 'config' );
64 $helper->mk_dir($dir);
65  
66 foreach my $file (qw( main url )) {
67 $helper->render_file( "config_$file",
68 File::Spec->catfile( $dir, $file ) );
69 }
70  
71 $dir = File::Spec->catfile( $ldir, 'site' );
72 $helper->mk_dir($dir);
73  
74 foreach my $file (qw( wrapper layout html header footer )) {
75 $helper->render_file( "site_$file",
76 File::Spec->catfile( $dir, $file ) );
77 }
78  
79 foreach my $file (qw( welcome.tt2 message.tt2 error.tt2 ttsite.css )) {
80 $helper->render_file( $file, File::Spec->catfile( $sdir, $file ) );
81 }
82
83 my $jsMinifier = 0;
84 eval {
85 $SIG{__DIE__} = 'IGNORE';
86 if(require 'JavaScript::Minifier::XS') {
87 $jsMinifier = 1;
88 }
89 elsif(require 'JavaScript::Minifier') {
90 $jsMinifier = 2;
91 }
92  
93 };
94
95 foreach my $file (qw( respondjs.min.js )) {
96 my $jsfile = File::Spec->catfile( $jsdir, $file );
97 $helper->render_file( $file, $jsfile );
98
99 if(substr($jsfile, -3) eq '.js' && substr($jsfile, -7) ne '.min.js') {
100 my $minJSFile = substr($jsfile, 0, -3) . '.min.js';
101  
102 if($jsMinifier) {
103 my $minJSRef = file($minJSFile);
104 my $jsfileRef = file($jsfile);
105 my $MINIFIEDJS = $minJSRef->open('w');
106 my $contents = $jsfileRef->slurp(iomode => "<:raw");
107  
108 if($jsMinifier == 1) {
109 print $MINIFIEDJS JavaScript::Minifier::XS::minify($contents);
110 }
111
112 elsif($jsMinifier == 2) {
113 JavaScript::Minifier::minify(
114 input=>$contents,
115 output=>$MINIFIEDJS,
116 stripDebug=>1,
117 );
118 }
119 $MINIFIEDJS->close;
120 }
121 else {
122 unlink($minJSFile);
123 symlink($jsfile, $minJSFile);
124 }
125
126 my $gzipJSFile = $jsfile . '.gz';
127 system("gzip -9c $minJSFile > $gzipJSFile");
128 }
129 }
130
131 foreach my $file (qw( )) {
132 $helper->render_file( $file, File::Spec->catfile( $cssdir, $file ) );
133 }
134 }
135  
136 sub _mk_images {
137 my $self = shift;
138 my $helper = shift;
139 my $images = $helper->{images};
140 my @images =
141 qw/catalyst_logo/;
142 for my $name (@images) {
143 my $image = $self->get_sharedir_file("root", "static", "images", "$name.png.bin");
144 rename file( $images, "$name.png" ), file( $images, "$name.png" ).'.orig';
145 $helper->mk_file( file( $images, "$name.png" ), $image );
146 }
147 }
148  
149  
150 =head1 NAME
151  
152 Catalyst::Helper::View::Bootstrap - Helper for Twitter Bootstrap 5 and TT view which builds a skeleton web site
153  
154 =head1 SYNOPSIS
155  
156 # use the helper to create the view module and templates
157  
158 $ script/myapp_create.pl view HTML Bootstrap
159  
160 # add something like the following to your main application module
161  
162 sub message : Global {
163 my ( $self, $c ) = @_;
164 $c->stash->{template} = 'message.tt2';
165 $c->stash->{message} ||= $c->req->param('message') || 'No message';
166 }
167  
168 sub index : Path : Args(0) {
169 my ( $self, $c ) = @_;
170 $c->stash->{template} = 'welcome.tt2';
171 }
172  
173 sub end : Private { # Or use Catalyst::Action::RenderView
174 my ( $self, $c ) = @_;
175 $c->forward( $c->view('HTML') );
176 }
177  
178 =head1 DESCRIPTION
179  
180 This helper module creates a TT View module. It goes further than
181 Catalyst::Helper::View::TT in that it additionally creates a simple
182 set of templates to get you started with your web site presentation
183 using Bootstrap 5 from a CDN (Content Delivery Network).
184  
185 It creates the templates in F<root/> directory underneath your
186 main project directory. In here two further subdirectories are
187 created: F<root/src> which contains the main page templates, and F<root/lib>
188 containing a library of other template components (header, footer,
189 etc.) that the page templates use.
190  
191 The view module that the helper creates is automatically configured
192 to locate these templates.
193  
194 It sets character encoding to utf-8 and it delivers HTML5 pages.
195  
196 =head2 Default Rendering
197  
198 To render a template the following process is applied:
199  
200 The configuration template F<root/lib/config/main> is rendered. This is
201 controlled by the C<PRE_PROCESS> configuration variable set in the controller
202 generated by Catalyst::Helper::View::Bootstrap. Additionally, templates referenced by
203 the C<PROCESS> directive will then be rendered.
204  
205 Next, the template defined by the C<WRAPPER> config variable is called. The default
206 wrapper template is located in F<root/lib/site/wrapper>. The wrapper template
207 passes files with C<.css/.js/.txt> extensions through as text OR processes
208 the templates defined after the C<WRAPPER> directive: C<site/html> and C<site/layout>.
209  
210 Based on the default value of the C<WRAPPER> directive in F<root/lib/site/wrapper>,
211 the following templates are processed in order:
212  
213 =over 4
214  
215 =item * F<root/src/your_template.tt2>
216  
217 =item * F<root/lib/site/footer>
218  
219 =item * F<root/lib/site/header>
220  
221 =item * F<root/lib/site/sidemenu>
222  
223 =item * F<root/lib/site/layout>
224  
225 =item * F<root/lib/site/html>
226  
227 =back
228  
229 Finally, the rendered content is returned to the bowser.
230  
231 =head1 METHODS
232  
233 =head2 mk_compclass
234  
235 Generates the component class.
236  
237 =head2 mk_templates
238  
239 Generates the templates.
240  
241 =cut
242  
243 =head1 SEE ALSO
244  
245 L<Catalyst>, L<Catalyst::View::TT>, L<Catalyst::Helper>,
246 L<Catalyst::Helper::View::TT>
247 L<Bootstrap|https://getbootstrap.com/>
248  
249 =head1 AUTHORS
250  
251 Ferruccio Zamuner <nonsolosoft@diff.org>
252 Juan Paredes
253 Colin Keith <colinmkeith@gmail.com>
254  
255 =head1 LICENSE
256  
257 This library is free software. You can redistribute it and/or modify
258 it under the same terms as perl itself.
259  
260 =cut
261  
262 1;
263  
264 __DATA__