| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | package Jifty::Script::Action; | ||
| 5 | use base qw/Jifty::Script/; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Jifty::Script::Action - Add an action class to your Jifty application | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | jifty action --name NewAction | ||
| 14 | jifty action --help | ||
| 15 | jifty action --man | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | Creates a skeleton action file. | ||
| 20 | |||
| 21 | =head2 options | ||
| 22 | |||
| 23 | There are only two possible options to this script: | ||
| 24 | |||
| 25 | =over 8 | ||
| 26 | |||
| 27 | =item --name NAME (required) | ||
| 28 | |||
| 29 | Name of the action class. | ||
| 30 | |||
| 31 | =item --force | ||
| 32 | |||
| 33 | By default, this will stop and warn you if any of the files it is | ||
| 34 | going to write already exist. Passing the --force flag will make it | ||
| 35 | overwrite the files. | ||
| 36 | |||
| 37 | =item B<--help> | ||
| 38 | |||
| 39 | Print a brief help message and exits. | ||
| 40 | |||
| 41 | =item B<--man> | ||
| 42 | |||
| 43 | Prints the manual page and exits. | ||
| 44 | |||
| 45 | =back | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | sub options { | ||
| 50 | my $self = shift; | ||
| 51 | return ( | ||
| 52 | $self->SUPER::options, | ||
| 53 | 'n|name=s' => 'name', | ||
| 54 | 'force' => 'force', | ||
| 55 | ); | ||
| 56 | } | ||
| 57 | |||
| 58 | =head1 DESCRIPTION | ||
| 59 | |||
| 60 | This creates a skeleton of a new action class for your jifty | ||
| 61 | application, complete with a skeleton of a test suite for it, | ||
| 62 | as well. | ||
| 63 | |||
| 64 | =head1 METHODS | ||
| 65 | |||
| 66 | =head2 run | ||
| 67 | |||
| 68 | Creates a skeleton file under C<lib/I<ApplicationClass>/Action/I<Action>>, as | ||
| 69 | well as a skeleton tests file. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub run { | ||
| 74 | my $self = shift; | ||
| 75 | |||
| 76 | $self->print_help; | ||
| 77 | |||
| 78 | my $action = $self->{name} || ''; | ||
| 79 | $self->print_help("You need to give your new action a --name") | ||
| 80 | unless $action =~ /\w+/; | ||
| 81 | |||
| 82 | Jifty->new( no_handle => 1 ); | ||
| 83 | my $root = Jifty::Util->app_root; | ||
| 84 | my $appclass = Jifty->config->framework("ApplicationClass"); | ||
| 85 | my $appclass_path = File::Spec->catfile(split(/::/,$appclass)); | ||
| 86 | |||
| 87 | # Detect if they're creating an App::Action::UpdateWidget, for example | ||
| 88 | my $subclass = "Jifty::Action"; | ||
| 89 | if ($action =~ /^(Create|Search|Execute|Update|Delete)(.+)$/) { | ||
| 90 | my($type, $model) = ($1, $2); | ||
| 91 | $model = Jifty->app_class( Model => $model ); | ||
| 92 | $subclass = Jifty->app_class( Action => Record => $type ) | ||
| 93 | if grep {$_ eq $model} Jifty->class_loader->models; | ||
| 94 | } | ||
| 95 | |||
| 96 | my $actionFile = <<"EOT"; | ||
| 97 | use strict; | ||
| 98 | use warnings; | ||
| 99 | |||
| 100 | =head1 NAME | ||
| 101 | |||
| 102 | @{[$appclass]}::Action::@{[$action]} | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | package @{[$appclass]}::Action::@{[$action]}; | ||
| 107 | use base qw/@{[$appclass]}::Action @{[$subclass]}/; | ||
| 108 | |||
| 109 | use Jifty::Param::Schema; | ||
| 110 | use Jifty::Action schema { | ||
| 111 | |||
| 112 | }; | ||
| 113 | |||
| 114 | =head2 take_action | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | sub take_action { | ||
| 119 | my \$self = shift; | ||
| 120 | |||
| 121 | # Custom action code | ||
| 122 | |||
| 123 | \$self->report_success if not \$self->result->failure; | ||
| 124 | |||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | =head2 report_success | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub report_success { | ||
| 133 | my \$self = shift; | ||
| 134 | # Your success message here | ||
| 135 | \$self->result->message('Success'); | ||
| 136 | } | ||
| 137 | |||
| 138 | 1; | ||
| 139 | |||
| 140 | EOT | ||
| 141 | |||
| 142 | |||
| 143 | my $testFile = <<"EOT"; | ||
| 144 | #!/usr/bin/env perl | ||
| 145 | use warnings; | ||
| 146 | use strict; | ||
| 147 | |||
| 148 | =head1 DESCRIPTION | ||
| 149 | |||
| 150 | A (very) basic test harness for the $action action. | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | use Jifty::Test tests => 1; | ||
| 155 | |||
| 156 | # Make sure we can load the action | ||
| 157 | use_ok('@{[$appclass]}::Action::@{[$action]}'); | ||
| 158 | |||
| 159 | EOT | ||
| 160 | |||
| 161 | $self->_write("$root/lib/$appclass_path/Action/$action.pm" => $actionFile, | ||
| 162 | "$root/t/00-action-$action.t" => $testFile, | ||
| 163 | ); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub _write { | ||
| 167 | my $self = shift; | ||
| 168 | my %files = (@_); | ||
| 169 | my $halt; | ||
| 170 | for my $path (keys %files) { | ||
| 171 | my ($volume, $dir, $file) = File::Spec->splitpath($path); | ||
| 172 | |||
| 173 | # Make sure the directories we need are there | ||
| 174 | Jifty::Util->make_path($dir); | ||
| 175 | |||
| 176 | # If it already exists, bail | ||
| 177 | if (-e $path and not $self->{force}) { | ||
| 178 | print "File $path exists already; Use --force to overwrite\n"; | ||
| 179 | $halt = 11; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | exit if $halt; | ||
| 183 | |||
| 184 | # Now that we've san-checked everything, we can write the files | ||
| 185 | for my $path (keys %files) { | ||
| 186 | print "Writing file $path\n"; | ||
| 187 | # Actually write the file out | ||
| 188 | open(FILE, ">$path") | ||
| 189 | or die "Can't write to $path: $!"; | ||
| 190 | print FILE $files{$path}; | ||
| 191 | close FILE; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | 1; |