[Bps-public-commit] SD branch, master, updated. 0.74-57-g4c8510a

Christine Spang spang at bestpractical.com
Sun Oct 24 01:19:41 EDT 2010


The branch, master has been updated
       via  4c8510a0b8f39bb34fa3a2abb19655837682c339 (commit)
      from  8a5a436d942ad1c2c4d14f22fd50bad25ee82d9e (commit)

Summary of changes:
 lib/App/SD/CLI/Command/Browser.pm |   54 ++---------------------------------
 lib/App/SD/Server.pm              |   57 +++++++++++++++++++++++++++++++++++-
 2 files changed, 58 insertions(+), 53 deletions(-)

- Log -----------------------------------------------------------------
commit 4c8510a0b8f39bb34fa3a2abb19655837682c339
Author: Christine Spang <christine at debian.org>
Date:   Sun Oct 24 01:14:15 2010 -0400

    open browser using HTTP::Server::Simple's after_setup_listener hook
    
    This is a lot nicer than using a fork(), and allows us to handle the case
    where the server fails to start up properly in the browser command. (By,
    like, not starting up a browser when the server hasn't started.)

diff --git a/lib/App/SD/CLI/Command/Browser.pm b/lib/App/SD/CLI/Command/Browser.pm
index ca4e5fe..a93f78d 100644
--- a/lib/App/SD/CLI/Command/Browser.pm
+++ b/lib/App/SD/CLI/Command/Browser.pm
@@ -6,62 +6,14 @@ extends 'App::SD::CLI::Command::Server';
 override run => sub {
     my $self = shift;
     $self->print_usage if $self->has_arg('h');
-    $self->server;  # make sure server is initialised to not delay later
+
+    $self->server->with_browser(1);
 
     Prophet::CLI->end_pager();
     print "Browser will be opened after server has been started.\n";
-    $self->open_browser(url => 'http://localhost:'. $self->server->port);
     $self->SUPER::run();
 };
 
-sub open_browser {
-    my $self = shift;
-    my %args = (@_);
-    my $opener = $self->open_url_cmd;
-
-    if (!$opener) {
-        warn "I'm unable to figure out what browser I should open for you.\n";
-        return;
-    }
-
-    if ($args{url}) {
-        defined (my $child_pid = fork) or die "Cannot fork: $!\n";
-        if ( $child_pid == 0 ) {
-            # child runs this block
-            sleep 2;
-            if ( $^O eq 'MSWin32' ) {
-                system($opener, $args{url}) && die "Couldn't run $opener: $!";
-            }
-            else {
-                exec($opener, $args{url}) or die "Couldn't run $opener: $!";
-            }
-            exit(0);
-        }
-        return;     # parent just returns to run the server
-    }
-}
-
-sub open_url_cmd {
-    my $self = shift;
-
-    if ( $^O eq 'darwin' ) {
-        return 'open';
-    }
-    elsif ( $^O eq 'MSWin32' ) {
-        return 'start';
-    }
-
-    for my $cmd (qw|x-www-browser htmlview
-                    gnome-open gnome-moz-remote
-                    firefox iceweasel opera www-browser w3m lynx|) {
-        my $cmd_path = `which $cmd`;
-        chomp($cmd_path);
-        if ( $cmd_path &&  -f $cmd_path && -x _ ) {
-            return $cmd_path;
-        }
-    }
-}
-
 __PACKAGE__->meta->make_immutable;
 no Any::Moose;
-
+1;
diff --git a/lib/App/SD/Server.pm b/lib/App/SD/Server.pm
index 86a17a6..05eaebb 100644
--- a/lib/App/SD/Server.pm
+++ b/lib/App/SD/Server.pm
@@ -1,7 +1,12 @@
 package App::SD::Server;
-use base 'Prophet::Server';
-
+use Any::Moose;
+extends 'Prophet::Server';
 
+has with_browser => (
+    isa => 'Bool',
+    is => 'rw',
+    default => 0,
+);
 
 =head2 database_bonjour_name
 
@@ -25,5 +30,53 @@ sub css {
 sub js {
     return shift->SUPER::js(@_);
 }
+
+# open up a browser after the server has been started (basically a
+# hook for the browser command)
+sub after_setup_listener {
+    my $self = shift;
+
+    local $SIG{CHLD}; # allow browser to be run with system()
+
+    if ( $self->with_browser ) {
+        $self->open_browser( url => 'http://localhost:' . $self->port );
+    }
+}
+
+sub open_browser {
+    my $self = shift;
+    my %args = (@_);
+    my $opener = $self->open_url_cmd;
+
+    if (!$opener) {
+        warn "I'm unable to figure out what browser I should open for you.\n";
+        return;
+    }
+
+    system($opener, $args{url}) && die "Couldn't run $opener: $!";
+}
+
+sub open_url_cmd {
+    my $self = shift;
+
+    if ( $^O eq 'darwin' ) {
+        return 'open';
+    }
+    elsif ( $^O eq 'MSWin32' ) {
+        return 'start';
+    }
+
+    for my $cmd (qw|x-www-browser htmlview
+                    gnome-open gnome-moz-remote
+                    firefox iceweasel opera www-browser w3m lynx|) {
+        my $cmd_path = `which $cmd`;
+        chomp($cmd_path);
+        if ( $cmd_path &&  -f $cmd_path && -x _ ) {
+            return $cmd_path;
+        }
+    }
+}
+
+no Any::Moose;
 1;
 

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list