1 package Catalyst::Helper;
13 use Catalyst::Exception;
14 use Path::Class qw/dir file/;
15 use File::ShareDir qw/dist_dir/;
16 use namespace::autoclean;
18 with 'MooseX::Emulate::Class::Accessor::Fast';
20 # Change Catalyst/Devel.pm also
21 our $VERSION = '1.22';
27 Catalyst::Helper - Bootstrap a Catalyst application
31 catalyst.pl <myappname>
35 sub get_sharedir_file {
36 my ($self, @filename) = @_;
38 if (-d "inc/.author" && -f "lib/Catalyst/Helper.pm"
39 ) { # Can't use sharedir if we're in a checkout
40 # this feels horrible, better ideas?
44 $dist_dir = dist_dir('Catalyst-Devel');
46 my $file = file( $dist_dir, @filename);
47 Carp::confess("Cannot find $file") unless -r $file;
48 my $contents = $file->slurp;
52 # Do not touch this method, *EVER*, it is needed for back compat.
54 my ( $self, $class, $file ) = @_;
55 unless ( $cache{$class} ) {
57 $cache{$class} = eval "package $class; <DATA>";
59 my $data = $cache{$class};
60 Carp::confess("Could not get data from __DATA__ segment for $class")
62 my @files = split /^__(.+)__\r?\n/m, $data;
65 my ( $name, $content ) = splice @files, 0, 2;
66 return $content if $name eq $file;
73 my ( $self, $name ) = @_;
75 # Needs to be here for PAR
78 if ( $name =~ /[^\w:]/ || $name =~ /^\d/ || $name =~ /\b:\b|:{3,}/) {
79 warn "Error: Invalid application name.\n";
82 $self->{name } = $name;
83 $self->{dir } = $name;
84 $self->{dir } =~ s/\:\:/-/g;
85 $self->{script } = dir( $self->{dir}, 'script' );
86 $self->{appprefix } = Catalyst::Utils::appprefix($name);
87 $self->{appenv } = Catalyst::Utils::class2env($name);
88 $self->{startperl } = -r '/usr/bin/env'
89 ? '#!/usr/bin/env perl'
90 : "#!$Config{perlpath} -w";
91 $self->{scriptgen } = $Catalyst::Devel::CATALYST_SCRIPT_GEN;
92 $self->{catalyst_version} = $Catalyst::VERSION;
93 $self->{author } = $self->{author} = $ENV{'AUTHOR'}
94 || eval { @{ [ getpwuid($<) ] }[6] }
95 || 'Catalyst developer';
97 my $gen_scripts = ( $self->{makefile} ) ? 0 : 1;
98 my $gen_makefile = ( $self->{scripts} ) ? 0 : 1;
99 my $gen_app = ( $self->{scripts} || $self->{makefile} ) ? 0 : 1;
102 for ( qw/ _mk_dirs _mk_config _mk_appclass _mk_rootclass _mk_readme
103 _mk_changes _mk_apptest _mk_images _mk_favicon/ ) {
112 for ( qw/ _mk_cgi _mk_fastcgi _mk_server
113 _mk_test _mk_create _mk_information
121 ## not much of this can really be changed, mk_compclass must be left for
127 $self->{author} = $self->{author} = $ENV{'AUTHOR'}
128 || eval { @{ [ getpwuid($<) ] }[6] }
130 $self->{base} ||= dir( $FindBin::Bin, '..' );
131 unless ( $_[0] =~ /^(?:model|view|controller)$/i ) {
134 my $class = "Catalyst::Helper::$helper";
135 eval "require $class";
138 Catalyst::Exception->throw(
139 message => qq/Couldn't load helper "$class", "$@"/ );
142 if ( $class->can('mk_stuff') ) {
143 return 1 unless $class->mk_stuff( $self, @args );
148 my $name = shift || "Missing name for model/view/controller";
151 return 0 if $name =~ /[^\w\:]/;
153 $self->{long_type} = ucfirst $type;
154 $type = 'M' if $type =~ /model/i;
155 $type = 'V' if $type =~ /view/i;
156 $type = 'C' if $type =~ /controller/i;
157 my $appdir = dir( split /\:\:/, $app );
159 dir( $self->{base}, 'lib', $appdir, 'C' );
160 $type = $self->{long_type} unless -d $test_path;
161 $self->{type} = $type;
162 $self->{name} = $name;
163 $self->{class} = "$app\::$type\::$name";
167 dir( $self->{base}, 'lib', $appdir, $type );
169 if ( $name =~ /\:/ ) {
170 my @path = split /\:\:/, $name;
172 $path = dir( $path, @path );
174 $self->mk_dir($path);
175 $file = file( $path, "$file.pm" );
176 $self->{file} = $file;
179 $self->{test_dir} = dir( $self->{base}, 't' );
180 $self->{test} = $self->next_test;
184 my $comp = $self->{long_type};
185 my $class = "Catalyst::Helper::$comp\::$helper";
186 eval "require $class";
189 Catalyst::Exception->throw(
190 message => qq/Couldn't load helper "$class", "$@"/ );
193 if ( $class->can('mk_compclass') ) {
194 return 1 unless $class->mk_compclass( $self, @args );
197 return 1 unless $self->_mk_compclass
200 if ( $class->can('mk_comptest') ) {
201 $class->mk_comptest( $self, @args );
210 return 1 unless $self->_mk_compclass;
218 my ( $self, $dir ) = @_;
220 print qq/ exists "$dir"\n/;
223 if ( mkpath [$dir] ) {
224 print qq/created "$dir"\n/;
228 Catalyst::Exception->throw( message => qq/Couldn't create "$dir", "$!"/ );
232 my ( $self, $file, $content ) = @_;
233 if ( -e $file && -s _ ) {
234 print qq/ exists "$file"\n/;
236 unless ( $self->{'.newfiles'}
238 || $self->{makefile} );
239 if ( $self->{'.newfiles'} ) {
240 if ( my $f = IO::File->new("< $file") ) {
241 my $oldcontent = join( '', (<$f>) );
242 return 0 if $content eq $oldcontent;
248 if ( my $f = IO::File->new("> $file") ) {
251 print qq/created "$file"\n/;
255 Catalyst::Exception->throw( message => qq/Couldn't create "$file", "$!"/ );
259 my ( $self, $tname ) = @_;
260 if ($tname) { $tname = "$tname.t" }
262 my $name = $self->{name};
266 $tname = $prefix . '.t';
267 $self->{prefix} = $prefix;
268 $prefix = lc $prefix;
270 $self->{uri} = "/$prefix";
272 my $dir = $self->{test_dir};
273 my $type = lc $self->{type};
275 return file( $dir, "$type\_$tname" );
278 # Do not touch this method, *EVER*, it is needed for back compat.
279 ## addendum: we had to split this method so we could have backwards
280 ## compatability. otherwise, we'd have no way to pass stuff from __DATA__
283 my ( $self, $file, $path, $vars ) = @_;
284 my $template = $self->get_file( ( caller(0) )[0], $file );
285 $self->render_file_contents($template, $path, $vars);
288 sub render_sharedir_file {
289 my ( $self, $file, $path, $vars ) = @_;
290 my $template = $self->get_sharedir_file( $file );
291 die("Cannot get template from $file for $self\n") unless $template;
292 $self->render_file_contents($template, $path, $vars);
295 sub render_file_contents {
296 my ( $self, $template, $path, $vars ) = @_;
298 my $t = Template->new;
299 return 0 unless $template;
301 $t->process( \$template, { %{$self}, %$vars }, \$output )
302 || Catalyst::Exception->throw(
303 message => qq/Couldn't process "$template", / . $t->error() );
304 $self->mk_file( $path, $output );
307 sub _mk_information {
309 print qq/Change to application directory and Run "perl Makefile.PL" to make sure your install is complete\n/;
314 $self->mk_dir( $self->{dir} );
315 $self->mk_dir( $self->{script} );
316 $self->{lib} = dir( $self->{dir}, 'lib' );
317 $self->mk_dir( $self->{lib} );
318 $self->{root} = dir( $self->{dir}, 'root' );
319 $self->mk_dir( $self->{root} );
320 $self->{static} = dir( $self->{root}, 'static' );
321 $self->mk_dir( $self->{static} );
322 $self->{images} = dir( $self->{static}, 'images' );
323 $self->mk_dir( $self->{images} );
324 $self->{t} = dir( $self->{dir}, 't' );
325 $self->mk_dir( $self->{t} );
327 $self->{class} = dir( split( /\:\:/, $self->{name} ) );
328 $self->{mod} = dir( $self->{lib}, $self->{class} );
329 $self->mk_dir( $self->{mod} );
331 if ( $self->{short} ) {
332 $self->{m} = dir( $self->{mod}, 'M' );
333 $self->mk_dir( $self->{m} );
334 $self->{v} = dir( $self->{mod}, 'V' );
335 $self->mk_dir( $self->{v} );
336 $self->{c} = dir( $self->{mod}, 'C' );
337 $self->mk_dir( $self->{c} );
340 $self->{m} = dir( $self->{mod}, 'Model' );
341 $self->mk_dir( $self->{m} );
342 $self->{v} = dir( $self->{mod}, 'View' );
343 $self->mk_dir( $self->{v} );
344 $self->{c} = dir( $self->{mod}, 'Controller' );
345 $self->mk_dir( $self->{c} );
347 my $name = $self->{name};
349 $self->{short} ? "$name\::C::Root" : "$name\::Controller::Root";
350 $self->{base} = dir( $self->{dir} )->absolute;
355 my $mod = $self->{mod};
356 $self->render_sharedir_file( file('lib', 'MyApp.pm.tt'), "$mod.pm" );
361 $self->render_sharedir_file( file('lib', 'MyApp', 'Controller', 'Root.pm.tt'),
362 file( $self->{c}, "Root.pm" ) );
367 $self->{path} = dir( 'lib', split( '::', $self->{name} ) );
368 $self->{path} .= '.pm';
369 my $dir = $self->{dir};
370 $self->render_sharedir_file( 'Makefile.PL.tt', file($dir, "Makefile.PL") );
372 if ( $self->{makefile} ) {
374 # deprecate the old Build.PL file when regenerating Makefile.PL
375 $self->_deprecate_file(
376 file( $self->{dir}, 'Build.PL' ) );
382 my $dir = $self->{dir};
383 my $appprefix = $self->{appprefix};
384 $self->render_sharedir_file( 'myapp.conf.tt',
385 file( $dir, "$appprefix.conf" ) );
390 my $dir = $self->{dir};
391 $self->render_sharedir_file( 'README.tt', file($dir, "README") );
396 my $dir = $self->{dir};
397 my $time = strftime('%Y-%m-%d %H:%M:%S', localtime time);
398 $self->render_sharedir_file( 'Changes.tt', file($dir, "Changes"), { time => $time } );
404 $self->render_sharedir_file( file('t', '01app.t.tt'), file($t, "01app.t") );
405 $self->render_sharedir_file( file('t', '02pod.t.tt'), file($t, "02pod.t") );
406 $self->render_sharedir_file( file('t', '03podcoverage.t.tt'), file($t, "03podcoverage.t") );
411 my $script = $self->{script};
412 my $appprefix = $self->{appprefix};
413 $self->render_sharedir_file( file('script', 'myapp_cgi.pl.tt'), file($script,"$appprefix\_cgi.pl") );
414 chmod 0700, file($script,"$appprefix\_cgi.pl");
419 my $script = $self->{script};
420 my $appprefix = $self->{appprefix};
421 $self->render_sharedir_file( file('script', 'myapp_fastcgi.pl.tt'), file($script, "$appprefix\_fastcgi.pl") );
422 chmod 0700, file($script, "$appprefix\_fastcgi.pl");
427 my $script = $self->{script};
428 my $appprefix = $self->{appprefix};
429 $self->render_sharedir_file( file('script', 'myapp_server.pl.tt'), file($script, "$appprefix\_server.pl") );
430 chmod 0700, file($script, "$appprefix\_server.pl");
435 my $script = $self->{script};
436 my $appprefix = $self->{appprefix};
437 $self->render_sharedir_file( file('script', 'myapp_test.pl.tt'), file($script, "$appprefix\_test.pl") );
438 chmod 0700, file($script, "$appprefix\_test.pl");
443 my $script = $self->{script};
444 my $appprefix = $self->{appprefix};
445 $self->render_sharedir_file( file('script', 'myapp_create.pl.tt'), file($script, "$appprefix\_create.pl") );
446 chmod 0700, file($script, "$appprefix\_create.pl");
451 my $file = $self->{file};
452 return $self->render_sharedir_file( file('lib', 'Helper', 'compclass.pm.tt'), $file );
457 my $test = $self->{test};
458 $self->render_sharedir_file( file('t', 'comptest.tt'), $test ); ## wtf do i rename this to?
463 my $images = $self->{images};
465 qw/catalyst_logo btn_120x50_built btn_120x50_built_shadow
466 btn_120x50_powered btn_120x50_powered_shadow btn_88x31_built
467 btn_88x31_built_shadow btn_88x31_powered btn_88x31_powered_shadow/;
468 for my $name (@images) {
469 my $image = $self->get_sharedir_file("root", "static", "images", "$name.png.bin");
470 $self->mk_file( file( $images, "$name.png" ), $image );
476 my $root = $self->{root};
477 my $favicon = $self->get_sharedir_file( 'root', 'favicon.ico.bin' );
478 my $dest = dir( $root, "favicon.ico" );
479 $self->mk_file( $dest, $favicon );
483 sub _deprecate_file {
484 my ( $self, $file ) = @_;
486 my ($f, $oldcontent);
487 if ( $f = IO::File->new("< $file") ) {
488 $oldcontent = join( '', (<$f>) );
490 my $newfile = $file . '.deprecated';
491 if ( $f = IO::File->new("> $newfile") ) {
493 print $f $oldcontent;
494 print qq/created "$newfile"\n/;
496 print qq/removed "$file"\n/;
499 Catalyst::Exception->throw(
500 message => qq/Couldn't create "$file", "$!"/ );
506 This module is used by B<catalyst.pl> to create a set of scripts for a
507 new catalyst application. The scripts each contain documentation and
508 will output help on how to use them if called incorrectly or in some
509 cases, with no arguments.
511 It also provides some useful methods for a Helper module to call when
512 creating a component. See L</METHODS>.
518 Used to create new components for a catalyst application at the
523 The catalyst test server, starts an HTTPD which outputs debugging to
528 A script for running tests from the command-line.
532 Run your application as a CGI.
536 Run the application as a fastcgi app. Either by hand, or call this
537 from FastCgiServer in your http server config.
541 The L</_create.pl> script creates application components using Helper
542 modules. The Catalyst team provides a good number of Helper modules
543 for you to use. You can also add your own.
545 Helpers are classes that provide two methods.
547 * mk_compclass - creates the Component class
548 * mk_comptest - creates the Component test
550 So when you call C<scripts/myapp_create.pl view MyView TT>, create
551 will try to execute Catalyst::Helper::View::TT->mk_compclass and
552 Catalyst::Helper::View::TT->mk_comptest.
554 See L<Catalyst::Helper::View::TT> and
555 L<Catalyst::Helper::Model::DBIC::Schema> for examples.
557 All helper classes should be under one of the following namespaces.
559 Catalyst::Helper::Model::
560 Catalyst::Helper::View::
561 Catalyst::Helper::Controller::
563 =head2 COMMON HELPERS
569 L<Catalyst::Helper::Model::DBIC::Schema> - DBIx::Class models
573 L<Catalyst::Helper::View::TT> - Template Toolkit view
577 L<Catalyst::Helper::Model::LDAP>
581 L<Catalyst::Helper::Model::Adaptor> - wrap any class into a Catalyst model
587 The helpers will read author name from /etc/passwd by default.
588 To override, please export the AUTHOR variable.
594 This method in your Helper module is called with C<$helper>
595 which is a L<Catalyst::Helper> object, and whichever other arguments
596 the user added to the command-line. You can use the $helper to call methods
599 If the Helper module does not contain a C<mk_compclass> method, it
600 will fall back to calling L</render_file>, with an argument of
605 This method in your Helper module is called with C<$helper>
606 which is a L<Catalyst::Helper> object, and whichever other arguments
607 the user added to the command-line. You can use the $helper to call methods
610 If the Helper module does not contain a C<mk_compclass> method, it
611 will fall back to calling L</render_file>, with an argument of
616 This method is called if the user does not supply any of the usual
617 component types C<view>, C<controller>, C<model>. It is passed the
618 C<$helper> object (an instance of L<Catalyst::Helper>), and any other
619 arguments the user typed.
621 There is no fallback for this method.
623 =head1 INTERNAL METHODS
625 These are the methods that the Helper classes can call on the
626 <$helper> object passed to them.
628 =head2 render_file ($file, $path, $vars)
630 Render and create a file from a template in DATA using Template
631 Toolkit. $file is the relevent chunk of the __DATA__ section, $path is
632 the path to the file and $vars is the hashref as expected by
633 L<Template Toolkit|Template>.
635 =head2 get_file ($class, $file)
637 Fetch file contents from the DATA section. This is used internally by
638 L</render_file>. $class is the name of the class to get the DATA
639 section from. __PACKAGE__ or ( caller(0) )[0] might be sensible
644 Create the main application skeleton. This is called by L<catalyst.pl>.
646 =head2 mk_component ($app)
648 This method is called by L<create.pl> to make new components
649 for your application.
651 =head2 mk_dir ($path)
653 Surprisingly, this function makes a directory.
655 =head2 mk_file ($file, $content)
657 Writes content to a file. Called by L</render_file>.
659 =head2 next_test ($test_name)
661 Calculates the name of the next numbered test file and returns it.
662 Don't give the number or the .t suffix for the test name.
666 =head2 get_sharedir_file
668 Method for getting a file out of share/
672 =head2 render_file_contents
674 Process a L<Template::Toolkit> template.
678 =head2 render_sharedir_file
680 Render a template/image file from our share directory
686 The helpers will read author name from /etc/passwd by default.
687 To override, please export the AUTHOR variable.
691 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
692 L<Catalyst::Response>, L<Catalyst>
696 Catalyst Contributors, see Catalyst.pm
700 This library is free software. You can redistribute it and/or modify
701 it under the same terms as Perl itself.