e0a0351c12ae208367158639d6d82d00f3e9e935
[scpubgit/Object-Remote.git] / lib / Object / Remote / FatNode.pm
1 package Object::Remote::FatNode;
2
3 use strictures 1;
4 use Config;
5 use B qw(perlstring);
6
7 sub stripspace {
8   my ($text) = @_;
9   $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
10   $text;
11 }
12
13 my %maybe_libs = map +($_ => 1), grep defined, (values %Config, '.');
14
15 my @extra_libs = grep not(ref($_) or $maybe_libs{$_}), @INC;
16
17 my $extra_libs = join '', map "  -I$_\n", @extra_libs;
18
19 my $command = qq(
20   $^X
21   $extra_libs
22   -mObject::Remote
23   -mObject::Remote::Connector::STDIO
24   -mCPS::Future
25   -mClass::C3
26   -mObject::Remote::ModuleLoader
27   -mObject::Remote::Node
28   -mJSON::PP
29   -e 'print join "\\n", reverse \%INC'
30 );
31
32 $command =~ s/\n/ /g;
33
34 chomp(my %mods = qx($command));
35
36 my @non_core_non_arch = grep +(
37   not (/^\Q$Config{privlibexp}/ or /^\Q$Config{archlibexp}/)
38 ), grep !/\Q$Config{archname}/, grep !/\W$Config{myarchname}/, keys %mods;
39
40 my $start = stripspace <<'END_START';
41   # This chunk of stuff was generated by Object::Remote::FatNode. To find
42   # the original file's code, look for the end of this BEGIN block or the
43   # string 'FATPACK'
44   BEGIN {
45   my %fatpacked;
46 END_START
47 my $end = stripspace <<'END_END';
48   s/^  //mg for values %fatpacked;
49
50   unshift @INC, sub {
51     if (my $fat = $fatpacked{$_[1]}) {
52       open my $fh, '<', \$fat;
53       return $fh;
54     }
55     return
56   };
57
58   } # END OF FATPACK CODE
59
60   use strictures 1;
61   use Object::Remote::Node;
62   Object::Remote::Node->run;
63 END_END
64
65 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
66               @non_core_non_arch;
67
68 my @segments = map {
69   (my $stub = $_) =~ s/\.pm$//;
70   my $name = uc join '_', split '/', $stub;
71   my $data = $files{$_}; $data =~ s/^/  /mg;
72   '$fatpacked{'.perlstring($_).qq!} = <<'${name}';\n!
73   .qq!${data}${name}\n!;
74 } sort keys %files;
75
76 our $DATA = join "\n", $start, @segments, $end;
77
78 1;