| 1 | package Padre::Plugin::Mojolicious::NewApp; | ||
| 2 | |||
| 3 | # ABSTRACT: A New Mojolicious Application for Padre | ||
| 4 | |||
| 5 | use 5.008; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use Cwd (); | ||
| 9 | use File::Spec (); | ||
| 10 | use Padre::Wx (); | ||
| 11 | use Padre::Plugin::Mojolicious::FBP::NewApp (); | ||
| 12 | use Padre::DB (); | ||
| 13 | |||
| 14 | our $VERSION = '0.06'; | ||
| 15 | |||
| 16 | our @ISA = qw{ | ||
| 17 | Padre::Plugin::Mojolicious::FBP::NewApp | ||
| 18 | }; | ||
| 19 | |||
| 20 | sub run { | ||
| 21 | my $self = shift; | ||
| 22 | |||
| 23 | $self->{ok_button}->SetDefault; | ||
| 24 | $self->{app_name}->SetFocus; | ||
| 25 | $self->Show(1); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub on_cancel_clicked { | ||
| 29 | my $self = shift; | ||
| 30 | $self->Destroy; | ||
| 31 | return; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub on_ok_clicked { | ||
| 35 | my ( $self, $event ) = @_; | ||
| 36 | $self->Destroy; | ||
| 37 | |||
| 38 | my $main = $self->main; | ||
| 39 | my $app_name = $self->{app_name}->GetValue; | ||
| 40 | my $directory = $self->{directory}->GetPath; | ||
| 41 | |||
| 42 | # TODO improve input validation ! | ||
| 43 | if ( $app_name =~ m{^\s*$|[^\w\:]}o ) { | ||
| 44 | Wx::MessageBox( Wx::gettext('Invalid Application name'), Wx::gettext('missing field'), Wx::wxOK, $main ); | ||
| 45 | return; | ||
| 46 | } elsif ( not $directory ) { | ||
| 47 | Wx::MessageBox( | ||
| 48 | Wx::gettext('You need to select a base directory'), Wx::gettext('missing field'), Wx::wxOK, | ||
| 49 | $main | ||
| 50 | ); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | # Prepare the output window for the output | ||
| 55 | $main->show_output(1); | ||
| 56 | $main->output->Remove( 0, $main->output->GetLastPosition ); | ||
| 57 | |||
| 58 | my @command = ( | ||
| 59 | 'mojo', | ||
| 60 | 'generate', | ||
| 61 | 'app', | ||
| 62 | $app_name, | ||
| 63 | ); | ||
| 64 | |||
| 65 | # go to the selected directory | ||
| 66 | my $pwd = Cwd::cwd(); | ||
| 67 | chdir $directory; | ||
| 68 | |||
| 69 | # run command, then immediately restore directory | ||
| 70 | my $output_text = qx(@command); | ||
| 71 | chdir $pwd; | ||
| 72 | |||
| 73 | $main->output->AppendText($output_text); | ||
| 74 | |||
| 75 | my $ret = Wx::MessageBox( | ||
| 76 | sprintf( Wx::gettext("%s apparently created. Do you want to open it now?"), $self->{_app_name_} ), | ||
| 77 | Wx::gettext('Done'), | ||
| 78 | Wx::wxYES_NO | Wx::wxCENTRE, | ||
| 79 | $main, | ||
| 80 | ); | ||
| 81 | if ( $ret == Wx::wxYES ) { | ||
| 82 | require Padre::Plugin::Mojolicious::Util; | ||
| 83 | my $file = Padre::Plugin::Mojolicious::Util::find_file_from_output( | ||
| 84 | $app_name, | ||
| 85 | $output_text | ||
| 86 | ); | ||
| 87 | $file = Cwd::realpath($file); # avoid relative paths | ||
| 88 | |||
| 89 | Padre::DB::History->create( | ||
| 90 | type => 'files', | ||
| 91 | name => $file, | ||
| 92 | ); | ||
| 93 | $main->setup_editor($file); | ||
| 94 | $main->refresh; | ||
| 95 | } | ||
| 96 | |||
| 97 | return; | ||
| 98 | } | ||
| 99 | |||
| 100 | 1; |