cleaned up sub runner
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Script / Server.pm
index 708b8a1..d087eb6 100644 (file)
@@ -10,18 +10,31 @@ use FindBin qw/$Bin/;
 use lib "$Bin/../lib";
 use Pod::Usage;
 use Moose;
+use Catalyst::Restarter;
 #use Catalyst::Engine::HTTP;
 use namespace::autoclean;
 
 with 'MooseX::Getopt';
 
+has debug => (
+    traits => [qw(Getopt)],
+    cmd_aliases => 'd',
+    isa => 'Bool', 
+    is => 'ro',
+    documentation => qq{
+    -d --debug force debug mode    
+    }
+
+);
+
 has help => ( 
     traits => [qw(Getopt)],
     cmd_aliases => 'h',
     isa => 'Bool',   
     is => 'ro', 
-    , 
-    default => 0,  
+    documentation => qq{
+    -h --help display this help and exits    
+    },  
 );
 
 has host => ( 
@@ -62,7 +75,7 @@ has keepalive => (
     isa => 'Bool',   
     is => 'ro', 
     , 
-    default => 0 
+     
 );
 
 has background => ( 
@@ -73,7 +86,6 @@ has background => (
 );
 
 
-## broken now, WHY?!
 has _app => ( 
     reader   => 'app', 
     init_arg => 'app',
@@ -88,6 +100,13 @@ has restart => (
     isa => 'Bool',   
     is => 'ro', 
      
+); 
+
+has restart_directory => (
+    traits => [qw(Getopt)],
+    cmd_aliases => 'rdir',
+    isa => 'Str',
+    is  => 'ro',
 );
 
 has restart_delay => ( 
@@ -114,15 +133,74 @@ has follow_symlinks => (
      
 );
 
+sub usage {
+    my ($self) = shift;
+    
+    return pod2usage();
+
+}
+
 my @argv = @ARGV;
 
 sub run {
     my $self = shift;
     
-    pod2usage() if $self->help;
+    $self->usage if $self->help;
+    
+    if ( $self->debug ) {
+        $ENV{CATALYST_DEBUG} = 1;
+    }
+
+    if ( $self->restart ) {
+        die "Cannot run in the background and also watch for changed files.\n"
+            if $self->background;
+
+        require Catalyst::Restarter;
+
+        my $subclass = Catalyst::Restarter->pick_subclass;
+
+        my %args;
+        $args{follow_symlinks} = $self->follow_symlinks;
+        $args{directories}     = $self->restart_directory;
+        $args{sleep_interval}  = $self->restart_delay;
+        $args{filter} = qr/$self->restart_regex/;
+
+        my $restarter = $subclass->new(
+            %args,
+            start_sub => $self->runner,
+            argv      => $self->ARGV,
+        );
+
+        $restarter->run_and_watch;
+    }
+    else {
+        $self->runner;
+    }
+
+
+}    
+sub runner {
+    my ($self) = shift;
+    
+    # If we load this here, then in the case of a restarter, it does not
+    # need to be reloaded for each restart.
+    require Catalyst;
+
+    # If this isn't done, then the Catalyst::Devel tests for the restarter
+    # fail.
+    $| = 1 if $ENV{HARNESS_ACTIVE};
+    
+    
+    $self->usage if $self->help;
     my $app = $self->app;
-    warn "App is $app";
     Class::MOP::load_class($app);
+
+    if ( $self->debug ) {
+        $ENV{CATALYST_DEBUG} = 1;
+    }
+
+    
     $app->run(
         $self->listen, $self->host,
         {  
@@ -131,17 +209,15 @@ sub run {
            background        => $self->background,
            pidfile           => $self->pidfile,
            keepalive         => $self->keepalive,
-           restart           => $self->restart,
-           restart_delay     => $self->restart_delay,
-           restart_regex     => qr/$self->restart_regex/,
-# FIXME    restart_directory => $self->restart_directory,
            follow_symlinks   => $self->follow_symlinks,
         }  
     );
-
 }
 
 
+no Moose;
+__PACKAGE__->meta->make_immutable;
+
 1;
 
 =head1 NAME