Initial on the fly fatnode
Matt S Trout [Mon, 18 Jun 2012 04:20:02 +0000 (05:20 +0100)]
bin/object-remote-node
lib/Object/Remote/FatNode.pm [new file with mode: 0644]
lib/Object/Remote/Node.pm [new file with mode: 0644]
maint/mk-fat [deleted file]

index 16defe6..652f078 100755 (executable)
@@ -1,25 +1,6 @@
 #!/usr/bin/env perl
 
 use strictures 1;
-use Object::Remote::Connector::STDIO;
-use Object::Remote;
-use CPS::Future;
+use Object::Remote::Node;
 
-my $c = Object::Remote::Connector::STDIO->new->connect;
-
-$c->register_class_call_handler;
-
-$c->ready_future->done;
-
-my $loop = Object::Remote->current_loop;
-
-my $f = CPS::Future->new;
-
-$f->on_ready(sub { $loop->want_stop });
-
-$c->on_close($f);
-
-print { $c->send_to_fh } "Shere\n";
-
-$loop->want_run;
-$loop->run_while_wanted;
+Object::Remote::Node->run;
diff --git a/lib/Object/Remote/FatNode.pm b/lib/Object/Remote/FatNode.pm
new file mode 100644 (file)
index 0000000..e0a0351
--- /dev/null
@@ -0,0 +1,78 @@
+package Object::Remote::FatNode;
+
+use strictures 1;
+use Config;
+use B qw(perlstring);
+
+sub stripspace {
+  my ($text) = @_;
+  $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
+  $text;
+}
+
+my %maybe_libs = map +($_ => 1), grep defined, (values %Config, '.');
+
+my @extra_libs = grep not(ref($_) or $maybe_libs{$_}), @INC;
+
+my $extra_libs = join '', map "  -I$_\n", @extra_libs;
+
+my $command = qq(
+  $^X
+  $extra_libs
+  -mObject::Remote
+  -mObject::Remote::Connector::STDIO
+  -mCPS::Future
+  -mClass::C3
+  -mObject::Remote::ModuleLoader
+  -mObject::Remote::Node
+  -mJSON::PP
+  -e 'print join "\\n", reverse \%INC'
+);
+
+$command =~ s/\n/ /g;
+
+chomp(my %mods = qx($command));
+
+my @non_core_non_arch = grep +(
+  not (/^\Q$Config{privlibexp}/ or /^\Q$Config{archlibexp}/)
+), grep !/\Q$Config{archname}/, grep !/\W$Config{myarchname}/, keys %mods;
+
+my $start = stripspace <<'END_START';
+  # This chunk of stuff was generated by Object::Remote::FatNode. To find
+  # the original file's code, look for the end of this BEGIN block or the
+  # string 'FATPACK'
+  BEGIN {
+  my %fatpacked;
+END_START
+my $end = stripspace <<'END_END';
+  s/^  //mg for values %fatpacked;
+
+  unshift @INC, sub {
+    if (my $fat = $fatpacked{$_[1]}) {
+      open my $fh, '<', \$fat;
+      return $fh;
+    }
+    return
+  };
+
+  } # END OF FATPACK CODE
+
+  use strictures 1;
+  use Object::Remote::Node;
+  Object::Remote::Node->run;
+END_END
+
+my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
+              @non_core_non_arch;
+
+my @segments = map {
+  (my $stub = $_) =~ s/\.pm$//;
+  my $name = uc join '_', split '/', $stub;
+  my $data = $files{$_}; $data =~ s/^/  /mg;
+  '$fatpacked{'.perlstring($_).qq!} = <<'${name}';\n!
+  .qq!${data}${name}\n!;
+} sort keys %files;
+
+our $DATA = join "\n", $start, @segments, $end;
+
+1;
diff --git a/lib/Object/Remote/Node.pm b/lib/Object/Remote/Node.pm
new file mode 100644 (file)
index 0000000..7190359
--- /dev/null
@@ -0,0 +1,30 @@
+package Object::Remote::Node;
+
+use strictures 1;
+use Object::Remote::Connector::STDIO;
+use Object::Remote;
+use CPS::Future;
+
+sub run {
+
+  my $c = Object::Remote::Connector::STDIO->new->connect;
+
+  $c->register_class_call_handler;
+
+  $c->ready_future->done;
+
+  my $loop = Object::Remote->current_loop;
+
+  my $f = CPS::Future->new;
+
+  $f->on_ready(sub { $loop->want_stop });
+
+  $c->on_close($f);
+
+  print { $c->send_to_fh } "Shere\n";
+
+  $loop->want_run;
+  $loop->run_while_wanted;
+}
+
+1;
diff --git a/maint/mk-fat b/maint/mk-fat
deleted file mode 100755 (executable)
index e95eb1f..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-#!/bin/sh
-
-if [ -e fatlib ]; then rm -r fatlib; fi
-fatpack tree $(fatpack packlists-for strictures.pm Module/Runtime.pm Moo.pm JSON/PP.pm MRO/Compat.pm Class/C3.pm Algorithm/C3.pm Class/Method/Modifiers.pm Role/Tiny.pm CPS/Future.pm)
-rm lib/Object/Remote/FatNode.pm
-(
-  echo 'package Object::Remote::FatNode;
-our $DATA = do { local $/; <DATA> };
-1;
-__DATA__
-';
-  fatpack file;
-  cat bin/object-remote-node
-)>FatNode.pm.new
-mv FatNode.pm.new lib/Object/Remote/FatNode.pm
-rm -r fatlib