Crude Win32 development restarter

Corion edited this page Jan 31, 2011 · 2 revisions
  #!perl -w
  use strict;
  use Win32::ChangeNotify;
  use Getopt::Long;

  GetOptions(
      'dir|d:s' => \my @directories,
  );

  my $child;

  sub launch_child {
      warn "Launching [@_]";
      my $pid = system(1, @_);
      warn "Server PID: $pid";
      $pid;
  };

  my $path = $directories[0];

  while (1) {
      my $child = launch_child(@ARGV);

      my $notify = Win32::ChangeNotify->new($path,1,'DIR_NAME FILE_NAME LAST_WRITE SIZE');
      $notify->wait or warn "Something failed: $!\n";
      # There has been a change.
      warn "Killing $child";
      kill 9 => $child;
      undef $child;
  };

  END {
      kill $child if $child;
  };
  __END__
  =head1 SYNOPSIS

    server-restarter.pl -d lib -- perl -w \strawberry\perl\bin\plackup --server Twiggy bin\server.psg

  =head1 USAGE

    server-restarter.pl -d DIRECTORY -- command to launch

  =cut