| 1 | package Padre::Plugin::Plack; | ||
| 2 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | $Padre::Plugin::Plack::VERSION = '1.200'; | |
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: PSGI/Plack plugin for Padre | ||
| 7 | |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use base 'Padre::Plugin'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 0 | ▶ | use Padre::Util ('_T'); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use Padre::Logger; | ||
| 15 | |||
| 16 | |||
| 17 | sub padre_interfaces { | ||
| 18 | 'Padre::Plugin' => 0.43, | ||
| 19 | 'Padre::Document' => 0.57; | ||
| 20 | } | ||
| 21 | |||
| 22 | |||
| 23 | sub registered_documents { | ||
| 24 | 'application/x-psgi' => 'Padre::Document::PSGI'; | ||
| 25 | } | ||
| 26 | |||
| 27 | # Static cache for the dot-psgi examples (read off disk) | ||
| 28 | my %PSGI_EXAMPLES; | ||
| 29 | |||
| 30 | |||
| 31 | sub menu_plugins { | ||
| 32 | my $self = shift; | ||
| 33 | my $main = shift; | ||
| 34 | |||
| 35 | my $menu = Wx::Menu->new; | ||
| 36 | |||
| 37 | my $app_menu = Wx::Menu->new; | ||
| 38 | $menu->Append( -1, _T('New PSGI App'), $app_menu ); | ||
| 39 | |||
| 40 | for my $basename ( sort keys %PSGI_EXAMPLES ) { | ||
| 41 | Wx::Event::EVT_MENU( | ||
| 42 | $main, | ||
| 43 | $app_menu->Append( -1, $basename ), | ||
| 44 | sub { | ||
| 45 | $self->on_app_load( $PSGI_EXAMPLES{$basename} ); | ||
| 46 | return; | ||
| 47 | }, | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | my $docs_menu = Wx::Menu->new; | ||
| 52 | $menu->Append( -1, _T('Online References'), $docs_menu ); | ||
| 53 | |||
| 54 | Wx::Event::EVT_MENU( | ||
| 55 | $main, | ||
| 56 | $docs_menu->Append( -1, 'plackperl.org' ), | ||
| 57 | sub { | ||
| 58 | Padre::Wx::launch_browser('http://plackperl.org'); | ||
| 59 | } | ||
| 60 | ); | ||
| 61 | |||
| 62 | Wx::Event::EVT_MENU( | ||
| 63 | $main, | ||
| 64 | $docs_menu->Append( -1, _T('Plack Advent Calendar') ), | ||
| 65 | sub { | ||
| 66 | Padre::Wx::launch_browser('http://advent.plackperl.org'); | ||
| 67 | }, | ||
| 68 | ); | ||
| 69 | |||
| 70 | Wx::Event::EVT_MENU( $main, $menu->Append( -1, _T('About') ), sub { $self->on_about_load }, ); | ||
| 71 | |||
| 72 | # Return it and the label for our plug-in | ||
| 73 | return ( $self->plugin_name => $menu ); | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | sub on_app_load { | ||
| 78 | my $self = shift; | ||
| 79 | my $file = shift; | ||
| 80 | |||
| 81 | my $main = $self->main; | ||
| 82 | |||
| 83 | # Slurp in the new app content from the template file | ||
| 84 | my $template = Padre::Util::slurp($file); | ||
| 85 | unless ($template) { | ||
| 86 | |||
| 87 | # Rare failure, no need to translate | ||
| 88 | $self->main->error( sprintf( _T('Failed to open template file %s'), $file ) ); | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | |||
| 92 | # Create new document editor tab | ||
| 93 | $main->new_document_from_string( $$template, 'application/x-psgi' ); | ||
| 94 | my $editor = $main->current->editor; | ||
| 95 | my $doc = $editor->{Document}; | ||
| 96 | |||
| 97 | # N.B. It used to be necessary to deliberately use application/x-perl mime type and then rebless as | ||
| 98 | # a hack to make syntax highlighting work off the bat, but it seems to work now | ||
| 99 | # $doc->set_mimetype('application/x-psgi'); | ||
| 100 | # $doc->rebless; | ||
| 101 | $self->on_doc_load($doc); | ||
| 102 | |||
| 103 | # The tab exists, so trigger set_tab_icon | ||
| 104 | $doc->set_tab_icon; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | sub is_psgi_doc { | ||
| 109 | my $self = shift; | ||
| 110 | my $doc = shift; | ||
| 111 | |||
| 112 | return $doc->isa('Padre::Document::PSGI') && $doc->can('mimetype') && $doc->mimetype eq 'application/x-psgi'; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | sub editor_enable { | ||
| 117 | my $self = shift; | ||
| 118 | my $editor = shift; | ||
| 119 | my $doc = shift; | ||
| 120 | |||
| 121 | # Only respond to event on psgi docs | ||
| 122 | return unless $self->is_psgi_doc($doc); | ||
| 123 | |||
| 124 | TRACE('->editor_enable') if DEBUG; | ||
| 125 | |||
| 126 | $self->on_doc_load($doc); | ||
| 127 | |||
| 128 | # Deliberately don't trigger Padre::Document::PSGI::set_tab_icon here because the tab doesn't exist yet | ||
| 129 | # (it gets triggered by our tomfoolery in Padre::Document::PSGI::restore_cursor_position) | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | sub editor_changed { | ||
| 134 | my $self = shift; | ||
| 135 | |||
| 136 | my $main = $self->main or return; | ||
| 137 | my $editor = $main->current->editor or return; | ||
| 138 | my $doc = $editor->{Document} or return; | ||
| 139 | |||
| 140 | # Only respond to event on psgi docs | ||
| 141 | return unless $self->is_psgi_doc($doc); | ||
| 142 | |||
| 143 | TRACE('->editor_changed') if DEBUG; | ||
| 144 | |||
| 145 | ## TODO: add check that doc is now selected (for safety).. | ||
| 146 | $self->on_panel_load($doc); | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | sub on_panel_load { | ||
| 151 | my $self = shift; | ||
| 152 | my $doc = shift; | ||
| 153 | |||
| 154 | if ( !$doc->panel ) { | ||
| 155 | TRACE('->on_panel_load creating panel') if DEBUG; | ||
| 156 | require Padre::Plugin::Plack::Panel; | ||
| 157 | $doc->panel( Padre::Plugin::Plack::Panel->new($doc) ); | ||
| 158 | } | ||
| 159 | |||
| 160 | # Show the panel, and pass an onclose callback | ||
| 161 | Padre::Current->main->bottom->show( | ||
| 162 | $doc->panel, | ||
| 163 | sub { | ||
| 164 | |||
| 165 | # Closing the panel causes bad things to happen | ||
| 166 | $self->main->error( | ||
| 167 | _T(q{'Sorry Dave, I can't do that - you need to close the corresponding file tab to close this panel}) | ||
| 168 | ); | ||
| 169 | |||
| 170 | # We can't actually cancel the close, so re-create it | ||
| 171 | $self->plackdown($doc); | ||
| 172 | $doc->panel(undef); | ||
| 173 | $self->on_panel_load($doc); | ||
| 174 | } | ||
| 175 | ); | ||
| 176 | Padre::Current->main->refresh; | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | sub on_panel_close { | ||
| 181 | my $self = shift; | ||
| 182 | my $doc = shift; | ||
| 183 | |||
| 184 | return unless $doc && $doc->panel; | ||
| 185 | |||
| 186 | if ( my $panel = $doc->panel ) { | ||
| 187 | $self->plackdown($doc); | ||
| 188 | $self->main->bottom->hide($panel); | ||
| 189 | $doc->panel(undef); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | sub on_doc_load { | ||
| 195 | my $self = shift; | ||
| 196 | my $doc = shift; | ||
| 197 | |||
| 198 | TRACE('->on_doc_load') if DEBUG; | ||
| 199 | |||
| 200 | if ( !$doc->isa('Padre::Document::PSGI') ) { | ||
| 201 | $self->error( sprintf( _T('Expected a PSGI document, but instead got: %s'), ref $doc ) ); | ||
| 202 | return; | ||
| 203 | } | ||
| 204 | |||
| 205 | # Set the icon path, but don't actually trigger set_icon_tab() just yet | ||
| 206 | $doc->icon_path( $self->plugin_directory_share . "/icons/16x16/logo.png" ); | ||
| 207 | $doc->plugin($self); | ||
| 208 | |||
| 209 | # Trigger the Document's general setup event | ||
| 210 | $doc->on_load; | ||
| 211 | |||
| 212 | # Show the panel | ||
| 213 | $self->on_panel_load($doc); | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | sub on_doc_close { | ||
| 218 | my $self = shift; | ||
| 219 | my $doc = shift; | ||
| 220 | |||
| 221 | TRACE('->on_doc_close') if DEBUG; | ||
| 222 | |||
| 223 | if ( !$doc->isa('Padre::Document::PSGI') ) { | ||
| 224 | $self->error( sprintf( _T('Expected a PSGI document, but instead got: %s'), ref $doc ) ); | ||
| 225 | return; | ||
| 226 | } | ||
| 227 | |||
| 228 | $self->on_panel_close($doc); | ||
| 229 | } | ||
| 230 | |||
| 231 | |||
| 232 | sub on_about_load { | ||
| 233 | require Plack; | ||
| 234 | require Class::Unload; | ||
| 235 | my $about = Wx::AboutDialogInfo->new; | ||
| 236 | $about->SetName("Padre::Plugin::Plack"); | ||
| 237 | $about->SetDescription( _T('PSGI/Plack support for Padre') . "\n" | ||
| 238 | . _T('by') . "\n" | ||
| 239 | . 'Patrick Donelan (pat@patspam.com)' . "\n\n" | ||
| 240 | . _T('This system is running Plack version') | ||
| 241 | . " $Plack::VERSION\n" | ||
| 242 | . 'http://plackperl.org' ); | ||
| 243 | $about->SetVersion($Padre::Plugin::Plack::VERSION); | ||
| 244 | Class::Unload->unload('Plack'); | ||
| 245 | |||
| 246 | Wx::AboutBox($about); | ||
| 247 | return; | ||
| 248 | } | ||
| 249 | |||
| 250 | |||
| 251 | sub load_dot_psgi_examples { | ||
| 252 | my $self = shift; | ||
| 253 | |||
| 254 | require File::Find::Rule; | ||
| 255 | %PSGI_EXAMPLES = | ||
| 256 | map { File::Basename::basename($_) => $_ } | ||
| 257 | File::Find::Rule->file()->name('*.psgi')->in( $self->plugin_directory_share . '/dot-psgi' ); | ||
| 258 | } | ||
| 259 | |||
| 260 | |||
| 261 | |||
| 262 | |||
| 263 | sub plugin_enable { | ||
| 264 | my $self = shift; | ||
| 265 | |||
| 266 | $self->load_dot_psgi_examples; | ||
| 267 | } | ||
| 268 | |||
| 269 | |||
| 270 | sub plugin_disable { | ||
| 271 | my $self = shift; | ||
| 272 | |||
| 273 | # TODO: Loop over all docs and turn off their psgi goodies: panel, stop server, etc.. | ||
| 274 | |||
| 275 | # cleanup loaded classes | ||
| 276 | require Class::Unload; | ||
| 277 | Class::Unload->unload('Padre::Document::PSGI'); | ||
| 278 | Class::Unload->unload('Padre::Plugin::Plack::Panel'); | ||
| 279 | } | ||
| 280 | |||
| 281 | |||
| 282 | sub plackup { | ||
| 283 | my $self = shift; | ||
| 284 | my $doc = shift; | ||
| 285 | |||
| 286 | return unless $doc; | ||
| 287 | TRACE('->plackup') if DEBUG; | ||
| 288 | |||
| 289 | my $main = $self->main; | ||
| 290 | my $filename = $doc->filename; | ||
| 291 | |||
| 292 | if ( !$filename ) { | ||
| 293 | $main->on_save; | ||
| 294 | $filename = $doc->filename; | ||
| 295 | return unless $filename; | ||
| 296 | } | ||
| 297 | |||
| 298 | my $pwd = Cwd::cwd(); | ||
| 299 | chdir $doc->dirname; | ||
| 300 | |||
| 301 | # Server ("Let plackup guess" means leave as unspecified) | ||
| 302 | my $server = $doc->panel->{server}->GetValue; | ||
| 303 | $server = $server eq _T('Let plackup guess') ? '' : "-s $server"; | ||
| 304 | |||
| 305 | # Port (required for browser url) | ||
| 306 | my $port = $doc->panel->{port}->GetValue || 5000; | ||
| 307 | $port = "-p $port"; | ||
| 308 | |||
| 309 | my $restart = $doc->panel->{restart}->GetValue ? '-r' : ''; | ||
| 310 | my $plackup_options = $doc->panel->{plackup_options}->GetValue; | ||
| 311 | |||
| 312 | require File::Which; | ||
| 313 | my $plackup = File::Which::which('plackup'); | ||
| 314 | if ( !$plackup ) { | ||
| 315 | $main->error( _T('plackup command not found, please check your Plack installation and $PATH') ); | ||
| 316 | return; | ||
| 317 | } | ||
| 318 | |||
| 319 | my $cmd = qq{$plackup $port $restart $server $plackup_options "$filename"}; | ||
| 320 | TRACE("->plackup $cmd") if DEBUG; | ||
| 321 | $self->run_command( $doc, $cmd ); | ||
| 322 | |||
| 323 | # restore previous dir | ||
| 324 | chdir $pwd; | ||
| 325 | } | ||
| 326 | |||
| 327 | |||
| 328 | sub plackdown { | ||
| 329 | my $self = shift; | ||
| 330 | my $doc = shift; | ||
| 331 | |||
| 332 | return unless $doc; | ||
| 333 | |||
| 334 | TRACE('->plackdown') if DEBUG; | ||
| 335 | |||
| 336 | my $process = $doc->process; | ||
| 337 | return unless $process; | ||
| 338 | |||
| 339 | # sanity check | ||
| 340 | if ( !$process->IsAlive ) { | ||
| 341 | TRACE('->plackdown process was dead but not undef, strange') if DEBUG; | ||
| 342 | $doc->process(undef); | ||
| 343 | } | ||
| 344 | |||
| 345 | my $processid = $process->GetProcessId(); | ||
| 346 | my $panel = $doc->panel; | ||
| 347 | |||
| 348 | require Proc::Killfam; | ||
| 349 | my @signals = qw(INT TERM QUIT KILL STOP); | ||
| 350 | for my $sig (@signals) { | ||
| 351 | TRACE("Sending $sig to PID: $processid") if DEBUG; | ||
| 352 | my $signalled = Proc::Killfam::killfam( $sig, $processid ); | ||
| 353 | |||
| 354 | if ( $panel->{restart}->GetValue ) { | ||
| 355 | |||
| 356 | # with auto-restart, we expect 3 processes | ||
| 357 | return if $signalled > 1; | ||
| 358 | } | ||
| 359 | else { | ||
| 360 | |||
| 361 | # otherwise, just one | ||
| 362 | return if $signalled > 0; | ||
| 363 | } | ||
| 364 | } | ||
| 365 | |||
| 366 | $panel->output->AppendText( "\n" . "Process PID $processid did not respond, you may need to kill it manually\n" ); | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | sub run_command { | ||
| 371 | my ( $self, $doc, $command ) = (@_); | ||
| 372 | |||
| 373 | my $panel = $doc->panel; | ||
| 374 | |||
| 375 | # clear the panel | ||
| 376 | $panel->output->Remove( 0, $panel->output->GetLastPosition ); | ||
| 377 | |||
| 378 | # If this is the first time a command has been run, set up the ProcessStream bindings. | ||
| 379 | unless ( $panel->{bound} ) { | ||
| 380 | TRACE(' setting up ProcessStream bindings') if DEBUG; | ||
| 381 | |||
| 382 | require Wx::Perl::ProcessStream; | ||
| 383 | if ( $Wx::Perl::ProcessStream::VERSION < .20 ) { | ||
| 384 | $self->main->error( | ||
| 385 | sprintf( | ||
| 386 | _T( | ||
| 387 | 'Wx::Perl::ProcessStream is version %s' | ||
| 388 | . ' which is known to cause problems. Get at least 0.20 by typing' | ||
| 389 | . "\ncpan Wx::Perl::ProcessStream" | ||
| 390 | ), | ||
| 391 | $Wx::Perl::ProcessStream::VERSION | ||
| 392 | ) | ||
| 393 | ); | ||
| 394 | return 1; | ||
| 395 | } | ||
| 396 | |||
| 397 | Wx::Perl::ProcessStream::EVT_WXP_PROCESS_STREAM_STDOUT( | ||
| 398 | $panel->output, | ||
| 399 | sub { | ||
| 400 | $_[1]->Skip(1); | ||
| 401 | my $outpanel = $_[0]; | ||
| 402 | $outpanel->style_good; | ||
| 403 | $outpanel->AppendText( $_[1]->GetLine . "\n" ); | ||
| 404 | return; | ||
| 405 | }, | ||
| 406 | ); | ||
| 407 | Wx::Perl::ProcessStream::EVT_WXP_PROCESS_STREAM_STDERR( | ||
| 408 | $panel->output, | ||
| 409 | sub { | ||
| 410 | $_[1]->Skip(1); | ||
| 411 | my $outpanel = $_[0]; | ||
| 412 | $outpanel->style_neutral; | ||
| 413 | $outpanel->AppendText( $_[1]->GetLine . "\n" ); | ||
| 414 | |||
| 415 | return; | ||
| 416 | }, | ||
| 417 | ); | ||
| 418 | Wx::Perl::ProcessStream::EVT_WXP_PROCESS_STREAM_EXIT( | ||
| 419 | $panel->output, | ||
| 420 | sub { | ||
| 421 | $_[1]->Skip(1); | ||
| 422 | $_[1]->GetProcess->Destroy; | ||
| 423 | |||
| 424 | TRACE(' PROCESS_STREAM_EXIT') if DEBUG; | ||
| 425 | |||
| 426 | my $outpanel = $_[0]; | ||
| 427 | $outpanel->style_neutral; | ||
| 428 | $outpanel->AppendText("\nProcess terminated\n"); | ||
| 429 | $panel->set_as_stopped; | ||
| 430 | |||
| 431 | $doc->process(undef); | ||
| 432 | }, | ||
| 433 | ); | ||
| 434 | $panel->{bound} = 1; | ||
| 435 | } | ||
| 436 | |||
| 437 | # Start the command | ||
| 438 | my $process = Wx::Perl::ProcessStream::Process->new( $command, "Run $command", $panel->output ); | ||
| 439 | $doc->process( $process->Run ); | ||
| 440 | |||
| 441 | # Check if we started the process or not | ||
| 442 | if ( $doc->process ) { | ||
| 443 | $panel->set_as_started; | ||
| 444 | |||
| 445 | } | ||
| 446 | else { | ||
| 447 | |||
| 448 | # Failed to start the command. Clean up. | ||
| 449 | $panel->set_as_stopped; # should already be stopped, but just in case | ||
| 450 | Wx::MessageBox( sprintf( _T("Failed to start server via '%s'"), $command ), _T("Error"), Wx::wxOK, $self ); | ||
| 451 | } | ||
| 452 | |||
| 453 | return; | ||
| 454 | } | ||
| 455 | |||
| 456 | |||
| 457 | sub build_panel { | ||
| 458 | my $self = shift; | ||
| 459 | my $doc = shift; | ||
| 460 | my $panel = shift; | ||
| 461 | |||
| 462 | require Scalar::Util; | ||
| 463 | $panel->{doc} = $doc; | ||
| 464 | Scalar::Util::weaken( $panel->{doc} ); | ||
| 465 | |||
| 466 | # main container | ||
| 467 | my $box = Wx::BoxSizer->new(Wx::wxVERTICAL); | ||
| 468 | |||
| 469 | # top box, holding buttons, icons and checkboxes | ||
| 470 | my $top_box = Wx::BoxSizer->new(Wx::wxHORIZONTAL); | ||
| 471 | |||
| 472 | # LED showing process status | ||
| 473 | $panel->{led} = Wx::StaticBitmap->new( $panel, -1, Wx::wxNullBitmap ); | ||
| 474 | $top_box->Add( $panel->{led}, 0, Wx::wxALIGN_CENTER_VERTICAL ); | ||
| 475 | |||
| 476 | # Servers | ||
| 477 | my @servers = sort qw( | ||
| 478 | Standalone | ||
| 479 | Apache1 | ||
| 480 | Apache2 | ||
| 481 | Apache2::RegistryAnyEvent | ||
| 482 | AnyEvent::HTTPD | ||
| 483 | AnyEvent::ReverseHTTP | ||
| 484 | AnyEvent::SCGI | ||
| 485 | AnyEvent::Server::Starter | ||
| 486 | CGI | ||
| 487 | Corona | ||
| 488 | FCGI | ||
| 489 | FCGI::Engine | ||
| 490 | HTTP::Server::PSGI | ||
| 491 | HTTP::Server::Simple | ||
| 492 | Server::Simple | ||
| 493 | SCGI | ||
| 494 | Starman | ||
| 495 | Starlet | ||
| 496 | Twiggy | ||
| 497 | POE | ||
| 498 | ReverseHTTP | ||
| 499 | ); | ||
| 500 | unshift @servers, _T('Let plackup guess'); | ||
| 501 | $top_box->AddSpacer(5); | ||
| 502 | $top_box->Add( Wx::StaticText->new( $panel, -1, _T('Server') . ':' ), 0, Wx::wxALIGN_CENTER_VERTICAL ); | ||
| 503 | $panel->{server} = | ||
| 504 | Wx::ComboBox->new( $panel, -1, 'Standalone', Wx::wxDefaultPosition, Wx::wxDefaultSize, [@servers], | ||
| 505 | Wx::wxCB_DROPDOWN ); | ||
| 506 | $top_box->Add( $panel->{server}, 0, Wx::wxALIGN_CENTER_VERTICAL ); | ||
| 507 | |||
| 508 | # Port | ||
| 509 | $top_box->AddSpacer(5); | ||
| 510 | $top_box->Add( Wx::StaticText->new( $panel, -1, _T('Port') . ':' ), 0, Wx::wxALIGN_CENTER_VERTICAL ); | ||
| 511 | $panel->{port} = Wx::TextCtrl->new( $panel, -1, '5000' ); | ||
| 512 | $top_box->Add( $panel->{port}, 0, Wx::wxALIGN_CENTER_VERTICAL ); | ||
| 513 | |||
| 514 | # Plackup Options | ||
| 515 | $top_box->AddSpacer(5); | ||
| 516 | $top_box->Add( Wx::StaticText->new( $panel, -1, _T('Options') . ':' ), 0, Wx::wxALIGN_CENTER_VERTICAL ); | ||
| 517 | $panel->{plackup_options} = Wx::TextCtrl->new( $panel, -1, '' ); | ||
| 518 | $top_box->Add( $panel->{plackup_options}, 0, Wx::wxALIGN_CENTER_VERTICAL ); | ||
| 519 | |||
| 520 | # Restart | ||
| 521 | $top_box->AddSpacer(5); | ||
| 522 | $panel->{restart} = Wx::CheckBox->new( $panel, -1, _T('Auto-Restart') ); | ||
| 523 | $panel->{restart}->SetValue(1); | ||
| 524 | $top_box->Add( $panel->{restart}, 0, Wx::wxALIGN_CENTER_VERTICAL ); | ||
| 525 | |||
| 526 | # Start/stop button | ||
| 527 | $top_box->AddSpacer(5); | ||
| 528 | $panel->{start_stop} = Wx::Button->new( $panel, -1, '' ); | ||
| 529 | Wx::Event::EVT_BUTTON( | ||
| 530 | $panel, | ||
| 531 | $panel->{start_stop}, | ||
| 532 | sub { | ||
| 533 | my $panel = shift; | ||
| 534 | |||
| 535 | # Trigger plackup/down | ||
| 536 | if ( $panel->{start_stop}->GetLabel eq _T('Start') ) { | ||
| 537 | $doc->plugin->plackup($doc); | ||
| 538 | } | ||
| 539 | else { | ||
| 540 | $doc->plugin->plackdown($doc); | ||
| 541 | } | ||
| 542 | }, | ||
| 543 | ); | ||
| 544 | $top_box->Add( $panel->{start_stop}, 0, Wx::wxALIGN_CENTER_VERTICAL ); | ||
| 545 | |||
| 546 | # Browser | ||
| 547 | $top_box->AddSpacer(5); | ||
| 548 | $panel->{browse} = Wx::Button->new( $panel, -1, _T('View in Browser') ); | ||
| 549 | Wx::Event::EVT_BUTTON( | ||
| 550 | $panel, | ||
| 551 | $panel->{browse}, | ||
| 552 | sub { | ||
| 553 | my $panel = shift; | ||
| 554 | my $port = $panel->{port}->GetValue || 5000; | ||
| 555 | Padre::Wx::launch_browser("http://0:$port"); | ||
| 556 | }, | ||
| 557 | ); | ||
| 558 | $top_box->Add( $panel->{browse}, 0, Wx::wxALIGN_CENTER_VERTICAL ); | ||
| 559 | |||
| 560 | # finishing up the top_box | ||
| 561 | $box->Add( $top_box, 0, Wx::wxALIGN_LEFT | Wx::wxALIGN_CENTER_VERTICAL ); | ||
| 562 | |||
| 563 | # output panel for server | ||
| 564 | require Padre::Wx::Output; | ||
| 565 | my $output = Padre::Wx::Output->new( $self->main, $panel ); | ||
| 566 | |||
| 567 | $box->Add( $output, 1, Wx::wxGROW ); | ||
| 568 | |||
| 569 | # wrapping it up | ||
| 570 | $panel->SetSizer($box); | ||
| 571 | |||
| 572 | # holding on to some objects we'll need to manipulate later on | ||
| 573 | $panel->{output} = $output; | ||
| 574 | |||
| 575 | $panel->set_as_stopped; | ||
| 576 | } | ||
| 577 | |||
| 578 | |||
| 579 | 1; | ||
| 580 | |||
| 581 | |||
| 582 | =pod | ||
| 583 | |||
| 584 | =head1 NAME | ||
| 585 | |||
| 586 | Padre::Plugin::Plack - PSGI/Plack plugin for Padre | ||
| 587 | |||
| 588 | =head1 VERSION | ||
| 589 | |||
| 590 | version 1.200 | ||
| 591 | |||
| 592 | =head1 SYNOPSIS | ||
| 593 | |||
| 594 | # cpan install Padre::Plugin::Plack; | ||
| 595 | # Then enable it via L<Padre>, The Perl IDE: | ||
| 596 | # Padre > Plugins > Plugin Manager > Plack > enable | ||
| 597 | |||
| 598 | =head1 DESCRIPTION | ||
| 599 | |||
| 600 | As the name suggests, Padre::Plugin::Plack adds L<Plack> awareness to L<Padre>. | ||
| 601 | |||
| 602 | With the plugin installed, opening *.psgi files causes some special things to | ||
| 603 | happen. | ||
| 604 | |||
| 605 | PSGI files are really just ordinary Perl files, so Padre does its normal Perl | ||
| 606 | lexing/syntax highlighting magic on them, but the real fun starts with the | ||
| 607 | Plack-specific features that appear in the per-file graphical L<plackup> control | ||
| 608 | panel that shows up. | ||
| 609 | |||
| 610 | The panel lets you run your web app in a Plack server at the click of a button, | ||
| 611 | view server output, configure plackup options and launch a web browser on the | ||
| 612 | appropriate port. | ||
| 613 | |||
| 614 | The great thing about Plack/PSGI is that unlike my previous plugin | ||
| 615 | (L<Padre::Plugin::WebGUI>) which was specific to a single web app (albeit a big one), | ||
| 616 | this plugin can be used for any web app built in a web framework that supports | ||
| 617 | Plack (L<Catalyst>, L<CGI::Application>, L<HTTP::Engine>, etc..). This is the same | ||
| 618 | motivating factor that excites L<Plack::Middleware> authors. | ||
| 619 | |||
| 620 | The plugin turns on plackup’s C<--reload> option by default, which conveniently | ||
| 621 | causes the plack server to reload every time you modify your source files in Padre. | ||
| 622 | This makes for quite a nice, if somewhat minimal "Plack IDE" experience | ||
| 623 | (this is version 0.01 after all). | ||
| 624 | |||
| 625 | The plugin integrates all of the L<Plack> example "dot-psgi”"files as templates | ||
| 626 | that can be used to create different types of Plack apps straight from the GUI menu. | ||
| 627 | |||
| 628 | The pre-populated list of Plack servers and the simple start/stop button makes for | ||
| 629 | a nice way of exploring the Plack server ecosystem. You can use the other panel | ||
| 630 | options to enter a specific port to run on, toggle auto-start mode and pass additional | ||
| 631 | options to plackup (options that start with C<--> are passed through to the backend | ||
| 632 | server). | ||
| 633 | |||
| 634 | The output panel is similar to the output panel that Padre normally displays when you | ||
| 635 | execute Perl files, except that you get one panel per .psgi file meaning that you can | ||
| 636 | run multiple plack servers simultaneously and independently view their output. | ||
| 637 | The appropriate panel is automatically selected when you click on the corresponding | ||
| 638 | file tab, and running processes are stopped when you close the tab. | ||
| 639 | |||
| 640 | It should be really easy to turn Padre::Plugin::Plack into new plugins that involve the | ||
| 641 | same basic ingredients, namely a file extension and an external command for running those | ||
| 642 | files, with a per-file panel for command options and output. So I encourage anyone who has | ||
| 643 | a similar plugin in mind to steal liberally from Padre::Plugin::Plack (as I did from | ||
| 644 | L<Padre::Plugin::Catalyst> - thanks garu++). | ||
| 645 | Ruby Rack support comes to mind as a trivial example. | ||
| 646 | |||
| 647 | Make Padre your domain-specific IDE today :) | ||
| 648 | |||
| 649 | Blog post with screenshots: L<http://blog.patspam.com/2009/padrepluginplack> | ||
| 650 | |||
| 651 | =head1 METHODS | ||
| 652 | |||
| 653 | =head2 padre_interfaces | ||
| 654 | |||
| 655 | Declare the Padre interfaces this plugin uses | ||
| 656 | |||
| 657 | =head2 registered_documents | ||
| 658 | |||
| 659 | Declare ourselves as the handler for .psgi files | ||
| 660 | |||
| 661 | =head2 menu_plugins | ||
| 662 | |||
| 663 | Create the plugin menu | ||
| 664 | |||
| 665 | =head2 on_app_load | ||
| 666 | |||
| 667 | Called when Padre loads | ||
| 668 | |||
| 669 | =head2 is_psgi_doc | ||
| 670 | |||
| 671 | =head2 editor_enable | ||
| 672 | |||
| 673 | =head2 editor_changed | ||
| 674 | |||
| 675 | =head2 on_panel_load | ||
| 676 | |||
| 677 | =head2 on_panel_close | ||
| 678 | |||
| 679 | =head2 on_doc_load | ||
| 680 | |||
| 681 | Note that the new tab may or may not exist at this point | ||
| 682 | When triggered by user opening a new file (e.g. from L<on_app_load>), tab does not exist yet | ||
| 683 | Whereas, when triggered by user creating new app from template, tab exists | ||
| 684 | |||
| 685 | =head2 on_doc_close | ||
| 686 | |||
| 687 | =head2 on_about_load | ||
| 688 | |||
| 689 | =head2 load_dot_psgi_examples | ||
| 690 | |||
| 691 | =head2 plugin_enable | ||
| 692 | |||
| 693 | =head2 plugin_enable | ||
| 694 | |||
| 695 | =head2 plackup | ||
| 696 | |||
| 697 | =head2 plackdown | ||
| 698 | |||
| 699 | =head2 run_command | ||
| 700 | |||
| 701 | =head2 build_panel | ||
| 702 | |||
| 703 | This method belonds in Padre::Plugin::Plack::Panel but we keep it here | ||
| 704 | to speed up the dev edit-reload cycle | ||
| 705 | |||
| 706 | =head2 TRACE | ||
| 707 | |||
| 708 | =head1 CONTRIBUTORS | ||
| 709 | |||
| 710 | =encoding utf8 | ||
| 711 | |||
| 712 | =over 4 | ||
| 713 | |||
| 714 | =item * | ||
| 715 | |||
| 716 | Gábor Szabó - גאבור סבו (SZABGAB) E<lt>szabgab@gmail.comE<gt> | ||
| 717 | |||
| 718 | =back | ||
| 719 | |||
| 720 | =head1 TRANSLATORS | ||
| 721 | |||
| 722 | Big thanks to all the wonderful translators! | ||
| 723 | |||
| 724 | =over 4 | ||
| 725 | |||
| 726 | =item * | ||
| 727 | |||
| 728 | French - Jerome Quelin (jquelin) E<lt>jquelin@cpan.orgE<gt> | ||
| 729 | |||
| 730 | =item * | ||
| 731 | |||
| 732 | Dutch - Dirk De Nijs (ddn123456) E<lt>DIRKDN@cpan.orgE<gt> | ||
| 733 | |||
| 734 | =item * | ||
| 735 | |||
| 736 | Brazilian Portuguese - Breno G. de Oliveira (GARU) E<lt>garu@cpan.orgE<gt> | ||
| 737 | |||
| 738 | =item * | ||
| 739 | |||
| 740 | Arabic - أحمد محمد زواوي Ahmad M. Zawawi (azawawi) E<lt>ahmad.zawawi@gmail.comE<gt> | ||
| 741 | |||
| 742 | =item * | ||
| 743 | |||
| 744 | Turkish - Burak Gürsoy (burak) E<lt>burak@cpan.orgE<gt> | ||
| 745 | |||
| 746 | =item * | ||
| 747 | |||
| 748 | Italian - Simone Blandino (sblandin) | ||
| 749 | |||
| 750 | =back | ||
| 751 | |||
| 752 | =head1 SEE ALSO | ||
| 753 | |||
| 754 | L<Plack>, L<Padre> | ||
| 755 | |||
| 756 | =head1 AUTHOR | ||
| 757 | |||
| 758 | Patrick Donelan <pdonelan@cpan.org> | ||
| 759 | |||
| 760 | =head1 COPYRIGHT AND LICENSE | ||
| 761 | |||
| 762 | This software is copyright (c) 2010 by Patrick Donelan. | ||
| 763 | |||
| 764 | This is free software; you can redistribute it and/or modify it under | ||
| 765 | the same terms as the Perl 5 programming language system itself. | ||
| 766 | |||
| 767 | =cut | ||
| 768 | |||
| 769 | |||
| 770 | __END__ | ||