5.8 also requires extra C3 bits
[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   -mMRO::Compat
26   -mClass::C3
27   -mClass::C3::next
28   -mAlgorithm::C3
29   -mObject::Remote::ModuleLoader
30   -mObject::Remote::Node
31   -mMethod::Generate::BuildAll
32   -mMethod::Generate::DemolishAll
33   -mJSON::PP
34   -e 'print join "\\n", reverse \%INC'
35 );
36
37 $command =~ s/\n/ /g;
38
39 chomp(my %mods = qx($command));
40
41 my @non_core_non_arch = grep +(
42   not (/^\Q$Config{privlibexp}/ or /^\Q$Config{archlibexp}/)
43 ), grep !/\Q$Config{archname}/, grep !/\W$Config{myarchname}/, keys %mods;
44
45 my $start = stripspace <<'END_START';
46   # This chunk of stuff was generated by Object::Remote::FatNode. To find
47   # the original file's code, look for the end of this BEGIN block or the
48   # string 'FATPACK'
49   BEGIN {
50   my %fatpacked;
51 END_START
52 my $end = stripspace <<'END_END';
53   s/^  //mg for values %fatpacked;
54
55   unshift @INC, sub {
56     if (my $fat = $fatpacked{$_[1]}) {
57       open my $fh, '<', \$fat;
58       return $fh;
59     }
60     #Uncomment this to find brokenness
61     #warn "Missing $_[1]";
62     return
63   };
64
65   } # END OF FATPACK CODE
66
67   use strictures 1;
68   use Object::Remote::Node;
69   Object::Remote::Node->run;
70 END_END
71
72 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
73               @non_core_non_arch;
74
75 my @segments = map {
76   (my $stub = $_) =~ s/\.pm$//;
77   my $name = uc join '_', split '/', $stub;
78   my $data = $files{$_}; $data =~ s/^/  /mg;
79   '$fatpacked{'.perlstring($_).qq!} = <<'${name}';\n!
80   .qq!${data}${name}\n!;
81 } sort keys %files;
82
83 our $DATA = join "\n", $start, @segments, $end;
84
85 1;