Catalyst/Helper.pm untested

filter: Statementstmt 0.0% 0 / 193 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 264 CC 12 SCAR 30.4
CC
12
Cov
0%
create
72.0
find_favourites
30.0
BEGIN
2.0
CRAP 20.9
1 package Padre::Plugin::Catalyst::Helper;
2 BEGIN {
3 $Padre::Plugin::Catalyst::Helper::VERSION = '0.13';
4 }
5  
6 # ABSTRACT: The Catalyst plugin helper
7  
8 use 5.008;
9 use strict;
10 use warnings;
11  
12 use Cwd ();
13 use File::Spec ();
14 use Padre::Wx ();
15 use Padre::Wx::Dialog ();
16 use Padre::Perl ();
17  
18 # TODO: these should've been passed as a parameter
19 # but I'm too tired to figure how to do it
20 # under a Wx::Dialog
21 my $helpers_for = {
22 'view' => [],
23 'model' => [],
24 'controller' => [],
25 };
26  
27 sub dialog {
28 my $layout = shift;
29 my $ok_sub = shift;
30  
31 my $main = Padre->ide->wx->main;
32 my $config = Padre->ide->config;
33  
34 my $dialog = Padre::Wx::Dialog->new(
35 parent => $main,
36 title => Wx::gettext('Create New Component'),
37 layout => $layout,
38 width => [ 100, 200 ],
39 bottom => 20,
40 );
41  
42 $dialog->{_widgets_}->{_ok_}->SetDefault;
43 Wx::Event::EVT_BUTTON( $dialog, $dialog->{_widgets_}->{_ok_}, $ok_sub );
44 Wx::Event::EVT_BUTTON( $dialog, $dialog->{_widgets_}->{_cancel_}, \&cancel_clicked );
45  
46 $dialog->{_widgets_}->{_name_}->SetFocus;
47  
48 return $dialog;
49 }
50  
51 sub get_model_layout {
52 my $available_models = $helpers_for->{'model'}; #shift; TODO: ungloball this
53  
54 my @layout = (
55 [ [ 'Wx::StaticText', undef, Wx::gettext('Model Name:') ],
56 [ 'Wx::TextCtrl', '_name_', 'DB' ],
57 ],
58 [ [ 'Wx::StaticText', undef, Wx::gettext('Type') ],
59 [ 'Wx::Choice', '_type_', $available_models ],
60 ],
61 [ [ 'Wx::StaticText', undef, Wx::gettext('Additional Parameters:') ],
62 [ 'Wx::TextCtrl', '_extra_params_', '' ],
63 ],
64 [ [ 'Wx::CheckBox', '_force_', 'force', 0 ], #TODO add -mechanize parameter too
65 ],
66 [ [ 'Wx::Button', '_ok_', Wx::wxID_OK ],
67 [ 'Wx::Button', '_cancel_', Wx::wxID_CANCEL ],
68 ],
69 );
70 return \@layout;
71 }
72  
73 sub get_view_layout {
74 my $available_views = $helpers_for->{'view'}; #shift; TODO: ungloball this
75  
76 my @layout = (
77 [ [ 'Wx::StaticText', undef, Wx::gettext('View Name:') ],
78 [ 'Wx::TextCtrl', '_name_', 'TT' ],
79 ],
80 [ [ 'Wx::StaticText', undef, Wx::gettext('Type') ],
81 [ 'Wx::Choice', '_type_', $available_views ],
82 ],
83 [ [ 'Wx::CheckBox', '_force_', Wx::gettext('force'), 0 ], #TODO add -mechanize parameter too
84 ],
85 [ [ 'Wx::Button', '_ok_', Wx::wxID_OK ],
86 [ 'Wx::Button', '_cancel_', Wx::wxID_CANCEL ],
87 ],
88 );
89 return \@layout;
90 }
91  
92 sub get_controller_layout {
93 my $available_controllers = $helpers_for->{'controller'}; #shift; TODO: ungloball this
94  
95 my @layout = (
96 [ [ 'Wx::StaticText', undef, Wx::gettext('Controller Name:') ],
97 [ 'Wx::TextCtrl', '_name_', '' ],
98 ],
99 [ [ 'Wx::StaticText', undef, Wx::gettext('Type') ],
100 [ 'Wx::Choice', '_type_', $available_controllers ],
101 ],
102 [ [ 'Wx::StaticText', undef, Wx::gettext('Additional Parameters:') ],
103 [ 'Wx::TextCtrl', '_extra_params_', '' ],
104 ],
105 [ [ 'Wx::CheckBox', '_force_', 'force', 0 ], #TODO add -mechanize parameter too
106 ],
107 [ [ 'Wx::Button', '_ok_', Wx::wxID_OK ],
108 [ 'Wx::Button', '_cancel_', Wx::wxID_CANCEL ],
109 ],
110 );
111 return \@layout;
112 }
113  
114  
115 sub find_helpers_for {
116 my $type = shift;
117 my $none_str = Wx::gettext('[none]');
118  
119 require Module::Pluggable::Object;
120 my @available_helpers = map {
121 s{Catalyst::Helper::$type\:\:}{}; $_
122 } Module::Pluggable::Object->new(
123 'search_path' => "Catalyst::Helper::$type",
124 )->plugins();
125 push @available_helpers, $none_str;
126  
127 ## Put preferred types first on the list. For example,
128 # as a view, TT is preferred.
129 # TODO: make this configurable under a Plugin Preferences window
130 my $favourite = find_favourites( $type, \@available_helpers );
131 $favourite ||= $none_str;
132  
133 # puts favourite option always on top of the list
134 @available_helpers = (
135 $favourite,
136 grep { $_ ne $favourite } sort @available_helpers
137 );
138 return \@available_helpers;
139 }
140  
141 sub find_favourites {
142 my $type = shift;
143 my $helpers = shift;
144 my $none_str = Wx::gettext('[none]');
145 if ( $type eq 'View' ) {
146 foreach ( @{$helpers} ) {
147 return $_ if ( $_ eq 'TT' );
148 }
149 } elsif ( $type eq 'Model' ) {
150 foreach ( @{$helpers} ) {
151 return $_ if ( $_ eq 'DBIC::Schema' );
152 }
153 }
154  
155 # no favorites found.
156 return;
157 }
158  
159  
160 sub on_create_view {
161 $helpers_for->{'view'} = find_helpers_for('View'); # TODO: unglobal this
162 my $layout = get_view_layout();
163 my $dialog = dialog( $layout, \&create_view );
164 $dialog->Show(1);
165 return;
166 }
167  
168  
169 sub on_create_controller {
170 $helpers_for->{'controller'} = find_helpers_for('Controller'); # TODO: unglobal this
171 my $layout = get_controller_layout();
172 my $dialog = dialog( $layout, \&create_controller );
173 $dialog->Show(1);
174 return;
175 }
176  
177  
178 sub on_create_model {
179 $helpers_for->{'model'} = find_helpers_for('Model'); # TODO: unglobal this
180 my $layout = get_model_layout();
181 my $dialog = dialog( $layout, \&create_model );
182 $dialog->Show(1);
183 return;
184 }
185  
186  
187 sub cancel_clicked {
188 my $dialog = shift;
189 $dialog->Destroy;
190 return;
191 }
192  
193  
194 sub create_view {
195 my $dialog = shift;
196 my $data = $dialog->get_data;
197 $dialog->Destroy;
198 create( 'View', $data );
199 }
200  
201 sub create_model {
202 my $dialog = shift;
203 my $data = $dialog->get_data;
204 $dialog->Destroy;
205 create( 'Model', $data );
206 }
207  
208 sub create_controller {
209 my $dialog = shift;
210 my $data = $dialog->get_data;
211 $dialog->Destroy;
212 create( 'Controller', $data );
213 }
214  
215  
216 sub create {
217 my $type = lc(shift);
218 my $data = shift;
219 my $main = Padre->ide->wx->main;
220  
221 unless ( $data->{'_name_'} ) {
222 Wx::MessageBox(
223 sprintf( Wx::gettext("You must provide a name for your %s module"), $type ),
224 Wx::gettext('Module name required'), Wx::wxOK, $main
225 );
226 return;
227 }
228  
229 require Padre::Plugin::Catalyst::Util;
230 my $project_dir = Padre::Plugin::Catalyst::Util::get_document_base_dir() || return;
231  
232 my $helper_filename = Padre::Plugin::Catalyst::Util::get_catalyst_project_name($project_dir);
233 $helper_filename .= '_create.pl';
234  
235 my $helper_full_path = File::Spec->catfile( $project_dir, 'script', $helper_filename );
236 if ( !-e $helper_full_path ) {
237 Wx::MessageBox(
238 sprintf(
239 Wx::gettext(
240 "Catalyst helper script not found at\n%s\n\nPlease make sure the active document is from your Catalyst project."
241 ),
242 $helper_full_path
243 ),
244 Wx::gettext('Helper not found'),
245 Wx::wxOK, $main
246 );
247 return;
248 }
249  
250 # Prepare the output window for the output
251 $main->show_output(1);
252 $main->output->Remove( 0, $main->output->GetLastPosition );
253  
254 my $perl = Padre::Perl->perl;
255 push my @cmd, $perl, File::Spec->catfile( 'script', $helper_filename ), $type,;
256  
257 my $helper = $helpers_for->{$type}; #TODO: unglobal this
258 push @cmd, $data->{'_name_'},
259 (
260 ${$helper}[ $data->{'_type_'} ] eq '[none]' ? ''
261 : ${$helper}[ $data->{'_type_'} ]
262 ),
263 (
264 defined $data->{'_extra_params_'} ? $data->{'_extra_params_'}
265 : ''
266 ),
267 ;
268  
269 if ( $data->{'_force_'} ) {
270 push @cmd, '-force';
271 }
272  
273 $main->output->AppendText( Wx::gettext('running:') . "@cmd\n" );
274  
275 # go to the selected directory
276 my $pwd = Cwd::cwd();
277 chdir $project_dir;
278  
279 # FIXME: STDERR output is going to the console
280 my $output_text = qx{@cmd};
281 $main->output->AppendText($output_text);
282  
283 chdir $pwd; # restore directory
284  
285 $main->output->AppendText( "\n" . Wx::gettext("Catalyst helper script ended.") . "\n" );
286  
287 require Padre::Plugin::Catalyst::Util;
288 my $file = Padre::Plugin::Catalyst::Util::find_file_from_output(
289 $data->{'_name_'},
290 $output_text
291 );
292 if ($file) {
293 $file = Cwd::realpath($file); # avoid relative paths
294 my $ret = Wx::MessageBox(
295 sprintf( Wx::gettext("%s apparently created. Do you want to open it now?"), $type ),
296 Wx::gettext('Done'),
297 Wx::wxYES_NO | Wx::wxCENTRE,
298 $main,
299 );
300 if ( $ret == Wx::wxYES ) {
301 Padre::DB::History->create(
302 type => 'files',
303 name => $file,
304 );
305 $main->setup_editor($file);
306 $main->refresh;
307 }
308 } else {
309 Wx::MessageBox(
310 sprintf( Wx::gettext("Error creating %s. Please check the error output and try again"), $type ),
311 Wx::gettext('Error'),
312 Wx::wxOK | Wx::wxCENTRE,
313 $main,
314 );
315 }
316 return;
317 }
318  
319 1;
320  
321 __END__