committing progress
[catagits/Catalyst-Devel.git] / lib / Catalyst / Helper / ComponentGen.pm
1 package Catalyst::Helper::ComponentGen;
2 use Moose;
3 use namespace::autoclean;
4 extends { 'Catalyst::Helper' };
5
6 # Test
7 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
8 $self->{test}     = $self->next_test;
9
10 # Helper
11 if ($helper) {
12     my $comp  = $self->{long_type};
13     my $class = "Catalyst::Helper::$comp\::$helper";
14     eval "require $class";
15
16     if ($@) {
17         Catalyst::Exception->throw(
18             message => qq/Couldn't load helper "$class", "$@"/ );
19     }
20
21     ## NO TOUCHY
22     if ( $class->can('mk_compclass') ) {
23         return 1 unless $class->mk_compclass( $self, @args );
24     }
25     else { return 1 unless $self->_mk_compclass }
26
27     if ( $class->can('mk_comptest') ) {
28         $class->mk_comptest( $self, @args );
29     }
30     else { $self->_mk_comptest }
31 }
32
33 sub mk_component {
34     my $self = shift;
35     my $app  = shift;
36     $self->{app} = $app;
37     $self->{author} = $self->{author} = $ENV{'AUTHOR'}
38       || eval { @{ [ getpwuid($<) ] }[6] }
39       || 'A clever guy';
40     $self->{base} ||= File::Spec->catdir( $FindBin::Bin, '..' );
41     unless ( $_[0] =~ /^(?:model|view|controller)$/i ) {
42         my $helper = shift;
43         my @args   = @_;
44         my $class  = "Catalyst::Helper::$helper";
45         eval "require $class";
46
47         if ($@) {
48             Catalyst::Exception->throw(
49                 message => qq/Couldn't load helper "$class", "$@"/ );
50         }
51
52         if ( $class->can('mk_stuff') ) {
53             return 1 unless $class->mk_stuff( $self, @args );
54         }
55     }
56     else {
57         my $type   = shift;
58         my $name   = shift || "Missing name for model/view/controller";
59         my $helper = shift;
60         my @args   = @_;
61        return 0 if $name =~ /[^\w\:]/;
62         $type              = lc $type;
63         $self->{long_type} = ucfirst $type;
64         $type              = 'M' if $type =~ /model/i;
65         $type              = 'V' if $type =~ /view/i;
66         $type              = 'C' if $type =~ /controller/i;
67         my $appdir = File::Spec->catdir( split /\:\:/, $app );
68         my $test_path =
69           File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, 'C' );
70         $type = $self->{long_type} unless -d $test_path;
71         $self->{type}  = $type;
72         $self->{name}  = $name;
73         $self->{class} = "$app\::$type\::$name";
74
75         # Class
76         my $path =
77           File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
78         my $file = $name;
79         if ( $name =~ /\:/ ) {
80             my @path = split /\:\:/, $name;
81             $file = pop @path;
82             $path = File::Spec->catdir( $path, @path );
83         }
84         $self->mk_dir($path);
85         $file = File::Spec->catfile( $path, "$file.pm" );
86         $self->{file} = $file;
87
88         # Fallback
89         else {
90             return 1 unless $self->_mk_compclass;
91             $self->_mk_comptest;
92         }
93     }
94     return 1;
95 }
96
97 1;