1 package Catalyst::Helper;
3 use Moose::Util::TypeConstraints;
14 use Catalyst::Exception;
15 use Path::Class qw/dir file/;
16 use File::ShareDir qw/dist_dir/;
17 use namespace::autoclean;
23 Catalyst::Helper - Bootstrap a Catalyst application
27 catalyst.pl <myappname>
31 sub get_sharedir_file {
32 my ($self, @filename) = @_;
34 if (-d "inc/.author" && -f "lib/Catalyst/Helper.pm"
35 ) { # Can't use sharedir if we're in a checkout
36 # this feels horrible, better ideas?
40 $dist_dir = dist_dir('Catalyst-Devel');
42 my $file = file( $dist_dir, @filename);
43 Carp::confess("Cannot find $file") unless -r $file;
44 my $contents = $file->slurp;
48 # Do not touch this method, *EVER*, it is needed for back compat.
50 my ( $self, $class, $file ) = @_;
51 unless ( $cache{$class} ) {
53 $cache{$class} = eval "package $class; <DATA>";
55 my $data = $cache{$class};
56 Carp::confess("Could not get data from __DATA__ segment for $class")
58 my @files = split /^__(.+)__\r?\n/m, $data;
61 my ( $name, $content ) = splice @files, 0, 2;
62 return $content if $name eq $file;
66 my $appname = subtype 'Str',
67 where { not (/[^\w:]/ or /^\d/ or /\b:\b|:{3,}/) },
68 message { "Error: Invalid application name '$_'." };
70 has name => ( is => 'ro', isa => $appname, writer => '_set_name', lazy => 1, isa => 'Str', default => sub { confess("no name") } );
72 my @lazy_strs = qw/ dir appprefix author rootname /;
73 foreach my $name (@lazy_strs) {
74 has $name => ( is => 'ro', isa => 'Str', init_arg => undef, lazy => 1, builder => "_build_$name" );
77 class_type 'Path::Class::Dir';
78 my $coerced_dir = subtype 'Str', where { 1 };
79 coerce $coerced_dir, from 'Path::Class::Dir', via { '' . $_ };
81 my @lazy_dirs = qw/ class base script /;
82 foreach my $name (@lazy_dirs) {
83 has $name => ( is => 'ro', isa => $coerced_dir, coerce => 1, init_arg => undef, lazy => 1, builder => "_build_$name" );
86 foreach my $wrap (qw/mk_app/) {
89 $self->$_ for @lazy_strs, @lazy_dirs;
93 sub _build_dir_locations {
95 my ($script, $lib, $root, $static, $mod);
97 script => do { $script = dir( $self->dir, 'script' ) },
98 lib => do { $lib = dir( $self->dir, 'lib' ) },
99 root => do { $root = dir( $self->dir, 'root' ) },
100 static => do {$static = dir( $root, 'static' ) },
101 images => dir( $static, 'images' ),
102 t => dir( $self->dir, 't' ),
103 mod => do { $mod = dir( $self->lib, $self->class ) },
104 m => dir( $mod, 'Model' ),
105 v => dir( $mod, 'View' ),
106 c => dir( $mod, 'Controller' ),
110 sub _build_class { dir( split( /\:\:/, shift->name ) ) }
114 sub _build_base { dir( shift->dir )->absolute }
115 sub _build_dir { my $dir = shift->name; $dir =~ s/\:\:/-/g; return $dir; }
116 sub _build_appprefix { Catalyst::Utils::appprefix(shift->name) }
119 || eval { @{ [ getpwuid($<) ] }[6] }
120 || 'Catalyst developer';
122 sub _build_rootname { shift->name . '::Controller::Root' }
124 has _app_template_data => ( isa => 'HashRef', is => 'ro', lazy => 1, builder => '_build_app_template_data' );
125 sub _build_app_template_data {
128 $self->_build_dir_locations,
136 # Needs to be here for PAR
139 $self->{startperl } = -r '/usr/bin/env'
140 ? '#!/usr/bin/env perl'
141 : "#!$Config{perlpath} -w";
142 $self->{scriptgen } = $Catalyst::Devel::CATALYST_SCRIPT_GEN;
143 $self->{catalyst_version} = $Catalyst::VERSION;
145 my $gen_scripts = ( $self->{makefile} ) ? 0 : 1;
146 my $gen_makefile = ( $self->{scripts} ) ? 0 : 1;
147 my $gen_app = ( $self->{scripts} || $self->{makefile} ) ? 0 : 1;
150 for ( qw/ _mk_dirs _mk_config _mk_appclass _mk_rootclass _mk_readme
151 _mk_changes _mk_apptest _mk_images _mk_favicon/ ) {
153 $self->$_($self->_app_template_data);
160 for ( qw/ _mk_cgi _mk_fastcgi _mk_server
161 _mk_test _mk_create _mk_information
163 $self->$_($self->_app_template_data);
169 ## not much of this can really be changed, mk_compclass must be left for
175 $self->{base} ||= dir( $FindBin::Bin, '..' ); # FIXME!
176 unless ( $_[0] =~ /^(?:model|view|controller)$/i ) {
179 my $class = "Catalyst::Helper::$helper";
180 eval "require $class";
183 Catalyst::Exception->throw(
184 message => qq/Couldn't load helper "$class", "$@"/ );
187 if ( $class->can('mk_stuff') ) {
188 return 1 unless $class->mk_stuff( $self, @args );
193 my $name = shift || "Missing name for model/view/controller";
196 return 0 if $name =~ /[^\w\:]/;
198 $self->{long_type} = ucfirst $type;
199 $type = 'M' if $type =~ /model/i;
200 $type = 'V' if $type =~ /view/i;
201 $type = 'C' if $type =~ /controller/i;
202 my $appdir = dir( split /\:\:/, $app );
204 dir( $self->{base}, 'lib', $appdir, 'C' );
205 $type = $self->{long_type} unless -d $test_path;
206 $self->{type} = $type;
207 $self->{name} = $name;
208 $self->{class} = "$app\::$type\::$name";
212 dir( $self->{base}, 'lib', $appdir, $type );
214 if ( $name =~ /\:/ ) {
215 my @path = split /\:\:/, $name;
217 $path = dir( $path, @path );
219 $self->mk_dir($path);
220 $file = file( $path, "$file.pm" );
221 $self->{file} = $file;
224 $self->{test_dir} = dir( $self->{base}, 't' );
225 $self->{test} = $self->next_test;
229 my $comp = $self->{long_type};
230 my $class = "Catalyst::Helper::$comp\::$helper";
231 eval "require $class";
234 Catalyst::Exception->throw(
235 message => qq/Couldn't load helper "$class", "$@"/ );
238 if ( $class->can('mk_compclass') ) {
239 return 1 unless $class->mk_compclass( $self, @args );
242 return 1 unless $self->_mk_compclass
245 if ( $class->can('mk_comptest') ) {
246 $class->mk_comptest( $self, @args );
255 return 1 unless $self->_mk_compclass;
263 my ( $self, $dir ) = @_;
265 print qq/ exists "$dir"\n/;
268 if ( mkpath [$dir] ) {
269 print qq/created "$dir"\n/;
273 Catalyst::Exception->throw( message => qq/Couldn't create "$dir", "$!"/ );
277 my ( $self, $file, $content ) = @_;
278 Carp::confess("No file") unless $file;
279 if ( -e $file && -s _ ) {
280 print qq/ exists "$file"\n/;
282 unless ( $self->{'.newfiles'}
284 || $self->{makefile} );
285 if ( $self->{'.newfiles'} ) {
286 if ( my $f = IO::File->new("< $file") ) {
287 my $oldcontent = join( '', (<$f>) );
288 return 0 if $content eq $oldcontent;
294 if ( my $f = IO::File->new("> $file") ) {
297 print qq/created "$file"\n/;
301 Catalyst::Exception->throw( message => qq/Couldn't create "$file", "$!"/ );
305 my ( $self, $tname ) = @_;
306 if ($tname) { $tname = "$tname.t" }
308 my $name = $self->{name};
312 $tname = $prefix . '.t';
313 $self->{prefix} = $prefix;
314 $prefix = lc $prefix;
316 $self->{uri} = "/$prefix";
318 my $dir = $self->{test_dir};
319 my $type = lc $self->{type};
321 return file( $dir, "$type\_$tname" );
324 # Do not touch this method, *EVER*, it is needed for back compat.
325 ## addendum: we had to split this method so we could have backwards
326 ## compatability. otherwise, we'd have no way to pass stuff from __DATA__
329 my ( $self, $file, $path, $vars ) = @_;
330 my $template = $self->get_file( ( caller(0) )[0], $file );
331 $self->render_file_contents($template, $path, $vars);
334 sub render_sharedir_file {
335 my ( $self, $file, $path, $vars ) = @_;
336 my $template = $self->get_sharedir_file( $file );
337 die("Cannot get template from $file for $self\n") unless $template;
338 $self->render_file_contents($template, $path, $vars);
341 sub render_file_contents {
342 my ( $self, $template, $path, $vars ) = @_;
344 my $t = Template->new;
345 return 0 unless $template;
347 $t->process( \$template, { %{$self}, %$vars }, \$output )
348 || Catalyst::Exception->throw(
349 message => qq/Couldn't process "$template", / . $t->error() );
350 $self->mk_file( $path, $output );
353 sub _mk_information {
355 print qq/Change to application directory and Run "perl Makefile.PL" to make sure your install is complete\n/;
360 my @dirs = $self->_build_dir_locations;
361 while (my ($name, $location) = (shift(@dirs), shift(@dirs))) {
362 $self->mk_dir( $location );
368 my $mod = $self->mod;
369 $self->render_sharedir_file( file('lib', 'MyApp.pm.tt'), "$mod.pm" );
374 $self->render_sharedir_file( file('lib', 'MyApp', 'Controller', 'Root.pm.tt'),
375 file( $self->c, "Root.pm" ) );
380 $self->{path} = dir( 'lib', split( '::', $self->{name} ) );
381 $self->{path} .= '.pm';
382 $self->render_sharedir_file( 'Makefile.PL.tt', file($self->dir, "Makefile.PL") );
384 if ( $self->{makefile} ) {
386 # deprecate the old Build.PL file when regenerating Makefile.PL
387 $self->_deprecate_file(
388 file( $self->dir, 'Build.PL' ) );
394 $self->render_sharedir_file( 'myapp.conf.tt',
395 file( $self->dir, $self->appprefix . ".conf" ) );
400 $self->render_sharedir_file( 'README.tt', file($self->dir, "README") );
405 my $time = strftime('%Y-%m-%d %H:%M:%S', localtime time);
406 $self->render_sharedir_file( 'Changes.tt', file($self->dir, "Changes"), { time => $time } );
412 $self->render_sharedir_file( file('t', '01app.t.tt'), file($t, "01app.t") );
413 $self->render_sharedir_file( file('t', '02pod.t.tt'), file($t, "02pod.t") );
414 $self->render_sharedir_file( file('t', '03podcoverage.t.tt'), file($t, "03podcoverage.t") );
419 my $fn = file($self->script, $self->appprefix . "_cgi.pl");
420 $self->render_sharedir_file( file('script', 'myapp_cgi.pl.tt'), $fn );
426 my $fn = file($self->script, $self->appprefix . "_fastcgi.pl");
427 $self->render_sharedir_file( file('script', 'myapp_fastcgi.pl.tt'), $fn );
433 my $fn = file($self->script, $self->appprefix . "_server.pl");
434 $self->render_sharedir_file( file('script', 'myapp_server.pl.tt'), $fn );
440 my $fn = file($self->script, $self->appprefix . "_test.pl");
441 $self->render_sharedir_file( file('script', 'myapp_test.pl.tt'), $fn );
447 my $fn = file($self->script, $self->appprefix . "_create.pl");
448 $self->render_sharedir_file( file('script', 'myapp_create.pl.tt'), $fn );
454 my $file = $self->{file};
455 return $self->render_sharedir_file( file('lib', 'Helper', 'compclass.pm.tt'), $file );
460 my $test = $self->{test};
461 $self->render_sharedir_file( file('t', 'comptest.tt'), $test ); ## wtf do i rename this to?
466 my $images = $self->{images};
468 qw/catalyst_logo btn_120x50_built btn_120x50_built_shadow
469 btn_120x50_powered btn_120x50_powered_shadow btn_88x31_built
470 btn_88x31_built_shadow btn_88x31_powered btn_88x31_powered_shadow/;
471 for my $name (@images) {
472 my $image = $self->get_sharedir_file("root", "static", "images", "$name.png.bin");
473 $self->mk_file( file( $images, "$name.png" ), $image );
479 my $root = $self->{root};
480 my $favicon = $self->get_sharedir_file( 'root', 'favicon.ico.bin' );
481 my $dest = dir( $root, "favicon.ico" );
482 $self->mk_file( $dest, $favicon );
486 sub _deprecate_file {
487 my ( $self, $file ) = @_;
489 my ($f, $oldcontent);
490 if ( $f = IO::File->new("< $file") ) {
491 $oldcontent = join( '', (<$f>) );
493 my $newfile = $file . '.deprecated';
494 if ( $f = IO::File->new("> $newfile") ) {
496 print $f $oldcontent;
497 print qq/created "$newfile"\n/;
499 print qq/removed "$file"\n/;
502 Catalyst::Exception->throw(
503 message => qq/Couldn't create "$file", "$!"/ );
509 This module is used by B<catalyst.pl> to create a set of scripts for a
510 new catalyst application. The scripts each contain documentation and
511 will output help on how to use them if called incorrectly or in some
512 cases, with no arguments.
514 It also provides some useful methods for a Helper module to call when
515 creating a component. See L</METHODS>.
521 Used to create new components for a catalyst application at the
526 The catalyst test server, starts an HTTPD which outputs debugging to
531 A script for running tests from the command-line.
535 Run your application as a CGI.
539 Run the application as a fastcgi app. Either by hand, or call this
540 from FastCgiServer in your http server config.
544 The L</_create.pl> script creates application components using Helper
545 modules. The Catalyst team provides a good number of Helper modules
546 for you to use. You can also add your own.
548 Helpers are classes that provide two methods.
550 * mk_compclass - creates the Component class
551 * mk_comptest - creates the Component test
553 So when you call C<scripts/myapp_create.pl view MyView TT>, create
554 will try to execute Catalyst::Helper::View::TT->mk_compclass and
555 Catalyst::Helper::View::TT->mk_comptest.
557 See L<Catalyst::Helper::View::TT> and
558 L<Catalyst::Helper::Model::DBIC::Schema> for examples.
560 All helper classes should be under one of the following namespaces.
562 Catalyst::Helper::Model::
563 Catalyst::Helper::View::
564 Catalyst::Helper::Controller::
566 =head2 COMMON HELPERS
572 L<Catalyst::Helper::Model::DBIC::Schema> - DBIx::Class models
576 L<Catalyst::Helper::View::TT> - Template Toolkit view
580 L<Catalyst::Helper::Model::LDAP>
584 L<Catalyst::Helper::Model::Adaptor> - wrap any class into a Catalyst model
590 The helpers will read author name from /etc/passwd by default.
591 To override, please export the AUTHOR variable.
597 This method in your Helper module is called with C<$helper>
598 which is a L<Catalyst::Helper> object, and whichever other arguments
599 the user added to the command-line. You can use the $helper to call methods
602 If the Helper module does not contain a C<mk_compclass> method, it
603 will fall back to calling L</render_file>, with an argument of
608 This method in your Helper module is called with C<$helper>
609 which is a L<Catalyst::Helper> object, and whichever other arguments
610 the user added to the command-line. You can use the $helper to call methods
613 If the Helper module does not contain a C<mk_compclass> method, it
614 will fall back to calling L</render_file>, with an argument of
619 This method is called if the user does not supply any of the usual
620 component types C<view>, C<controller>, C<model>. It is passed the
621 C<$helper> object (an instance of L<Catalyst::Helper>), and any other
622 arguments the user typed.
624 There is no fallback for this method.
626 =head1 INTERNAL METHODS
628 These are the methods that the Helper classes can call on the
629 <$helper> object passed to them.
631 =head2 render_file ($file, $path, $vars)
633 Render and create a file from a template in DATA using Template
634 Toolkit. $file is the relevent chunk of the __DATA__ section, $path is
635 the path to the file and $vars is the hashref as expected by
636 L<Template Toolkit|Template>.
638 =head2 get_file ($class, $file)
640 Fetch file contents from the DATA section. This is used internally by
641 L</render_file>. $class is the name of the class to get the DATA
642 section from. __PACKAGE__ or ( caller(0) )[0] might be sensible
647 Create the main application skeleton. This is called by L<catalyst.pl>.
649 =head2 mk_component ($app)
651 This method is called by L<create.pl> to make new components
652 for your application.
654 =head2 mk_dir ($path)
656 Surprisingly, this function makes a directory.
658 =head2 mk_file ($file, $content)
660 Writes content to a file. Called by L</render_file>.
662 =head2 next_test ($test_name)
664 Calculates the name of the next numbered test file and returns it.
665 Don't give the number or the .t suffix for the test name.
669 =head2 get_sharedir_file
671 Method for getting a file out of share/
675 =head2 render_file_contents
677 Process a L<Template::Toolkit> template.
681 =head2 render_sharedir_file
683 Render a template/image file from our share directory
689 The helpers will read author name from /etc/passwd by default.
690 To override, please export the AUTHOR variable.
694 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
695 L<Catalyst::Response>, L<Catalyst>
699 Catalyst Contributors, see Catalyst.pm
703 This library is free software. You can redistribute it and/or modify
704 it under the same terms as Perl itself.