| 1 | package Padre::Plugin::Catalyst::NewApp; | ||
| 2 | BEGIN { | ||
| 3 | $Padre::Plugin::Catalyst::NewApp::VERSION = '0.13'; | ||
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: A Catalyst New Application | ||
| 7 | |||
| 8 | use 5.008; | ||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use Cwd (); | ||
| 12 | use File::Spec (); | ||
| 13 | use Padre::Wx (); | ||
| 14 | use Padre::Wx::Dialog (); | ||
| 15 | |||
| 16 | sub on_newapp { | ||
| 17 | my $main = Padre->ide->wx->main; | ||
| 18 | |||
| 19 | my $has_catalyst_devel = eval 'use Catalyst::Devel; 1;'; ## no critic (ProhibitStringyEval) | ||
| 20 | unless ($has_catalyst_devel) { | ||
| 21 | my $error_str = Wx::gettext(<<ERR); | ||
| 22 | To use the Catalyst development tools including catalyst.pl and the | ||
| 23 | generated script/myapp_create.pl you need Catalyst::Helper, which is | ||
| 24 | part of the Catalyst-Devel distribution. Please install this via a | ||
| 25 | vendor package or by running one of - | ||
| 26 | |||
| 27 | perl -MCPAN -e 'install Catalyst::Devel' | ||
| 28 | perl -MCPANPLUS -e 'install Catalyst::Devel' | ||
| 29 | ERR | ||
| 30 | return $main->error($error_str); | ||
| 31 | |||
| 32 | } | ||
| 33 | |||
| 34 | my $dialog = dialog($main); | ||
| 35 | $dialog->Show(1); | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub get_layout { | ||
| 40 | |||
| 41 | my @layout = ( | ||
| 42 | [ [ 'Wx::StaticText', undef, Wx::gettext('Application Name:') ], | ||
| 43 | [ 'Wx::TextCtrl', '_app_name_', '' ], | ||
| 44 | ], | ||
| 45 | [ [ 'Wx::StaticText', undef, Wx::gettext('Parent Directory:') ], | ||
| 46 | [ 'Wx::DirPickerCtrl', '_directory_', '', Wx::gettext('Pick parent directory') ], | ||
| 47 | ], | ||
| 48 | ); | ||
| 49 | require Catalyst; | ||
| 50 | if ( $Catalyst::VERSION < 5.80013 ) { | ||
| 51 | push @layout, | ||
| 52 | [ | ||
| 53 | [ 'Wx::CheckBox', '_short_', Wx::gettext('short names'), 0 ], | ||
| 54 | ]; | ||
| 55 | } | ||
| 56 | push @layout, | ||
| 57 | [ | ||
| 58 | [ 'Wx::Button', '_ok_', Wx::wxID_OK ], | ||
| 59 | [ 'Wx::Button', '_cancel_', Wx::wxID_CANCEL ], | ||
| 60 | ]; | ||
| 61 | |||
| 62 | return \@layout; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub dialog { | ||
| 66 | my $parent = shift; | ||
| 67 | my $config = Padre->ide->config; | ||
| 68 | |||
| 69 | my $layout = get_layout(); | ||
| 70 | my $dialog = Padre::Wx::Dialog->new( | ||
| 71 | parent => $parent, | ||
| 72 | title => Wx::gettext('New Catalyst Application'), | ||
| 73 | layout => $layout, | ||
| 74 | width => [ 100, 200 ], | ||
| 75 | bottom => 20, | ||
| 76 | ); | ||
| 77 | |||
| 78 | $dialog->{_widgets_}->{_directory_}->SetPath( $config->module_start_directory ); | ||
| 79 | |||
| 80 | $dialog->{_widgets_}->{_ok_}->SetDefault; | ||
| 81 | Wx::Event::EVT_BUTTON( $dialog, $dialog->{_widgets_}->{_ok_}, \&ok_clicked ); | ||
| 82 | Wx::Event::EVT_BUTTON( $dialog, $dialog->{_widgets_}->{_cancel_}, \&cancel_clicked ); | ||
| 83 | |||
| 84 | $dialog->{_widgets_}->{_app_name_}->SetFocus; | ||
| 85 | |||
| 86 | return $dialog; | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | sub cancel_clicked { | ||
| 91 | my ( $dialog, $event ) = @_; | ||
| 92 | |||
| 93 | $dialog->Destroy; | ||
| 94 | |||
| 95 | return; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub ok_clicked { | ||
| 99 | my ( $dialog, $event ) = @_; | ||
| 100 | |||
| 101 | my $data = $dialog->get_data; | ||
| 102 | $dialog->Destroy; | ||
| 103 | |||
| 104 | my $main = Padre->ide->wx->main; | ||
| 105 | |||
| 106 | # TODO improve input validation ! | ||
| 107 | if ( $data->{'_app_name_'} =~ m{^\s*$|[^\w\:]}o ) { | ||
| 108 | Wx::MessageBox( Wx::gettext('Invalid Application name'), Wx::gettext('missing field'), Wx::wxOK, $main ); | ||
| 109 | return; | ||
| 110 | } elsif ( not $data->{'_directory_'} ) { | ||
| 111 | Wx::MessageBox( Wx::gettext('You need to select a base directory'), Wx::gettext('missing field'), Wx::wxOK, $main ); | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | # We should probably call Catalyst::Helper directly | ||
| 116 | # (new() and mk_app()) here, as long as we can redirect | ||
| 117 | # print statements to $main->output->AppendText(). | ||
| 118 | # | ||
| 119 | # Perhaps if run_command() were to block before continuing, | ||
| 120 | # we could use something like: | ||
| 121 | #$main->run_command('catalyst.pl ' . $data->{'_app_name_'}); | ||
| 122 | |||
| 123 | # Prepare the output window for the output | ||
| 124 | $main->show_output(1); | ||
| 125 | $main->output->Remove( 0, $main->output->GetLastPosition ); | ||
| 126 | |||
| 127 | my @command = ( | ||
| 128 | |||
| 129 | # use catalyst.bat on Windows | ||
| 130 | 'catalyst' . ( $^O =~ /Win/o ? '' : '.pl' ), | ||
| 131 | ( $data->{'_short_'} | ||
| 132 | ? '-short' | ||
| 133 | : '' | ||
| 134 | ), | ||
| 135 | $data->{'_app_name_'}, | ||
| 136 | ); | ||
| 137 | |||
| 138 | # go to the selected directory | ||
| 139 | my $pwd = Cwd::cwd(); | ||
| 140 | chdir $data->{'_directory_'}; | ||
| 141 | |||
| 142 | # run command, then immediately restore directory | ||
| 143 | my $output_text = qx(@command); | ||
| 144 | chdir $pwd; | ||
| 145 | |||
| 146 | $main->output->AppendText($output_text); | ||
| 147 | |||
| 148 | my $ret = Wx::MessageBox( | ||
| 149 | sprintf( Wx::gettext("%s apparently created. Do you want to open it now?"), $data->{_app_name_} ), | ||
| 150 | 'Done', | ||
| 151 | Wx::wxYES_NO | Wx::wxCENTRE, | ||
| 152 | $main, | ||
| 153 | ); | ||
| 154 | if ( $ret == Wx::wxYES ) { | ||
| 155 | require Padre::Plugin::Catalyst::Util; | ||
| 156 | my $file = Padre::Plugin::Catalyst::Util::find_file_from_output( | ||
| 157 | 'Root', | ||
| 158 | $output_text | ||
| 159 | ); | ||
| 160 | $file = File::Spec->catfile( $data->{'_directory_'}, $file ); | ||
| 161 | $file = Cwd::realpath($file); # avoid relative paths | ||
| 162 | |||
| 163 | Padre::DB::History->create( | ||
| 164 | type => 'files', | ||
| 165 | name => $file, | ||
| 166 | ); | ||
| 167 | $main->setup_editor($file); | ||
| 168 | $main->refresh; | ||
| 169 | } | ||
| 170 | |||
| 171 | return; | ||
| 172 | } | ||
| 173 | |||
| 174 | 1; | ||
| 175 | |||
| 176 | __END__ |