testing non-core filter heuristic and XS loader fall-back exclusion
[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", \%INC'
35 );
36
37 $command =~ s/\n/ /g;
38
39 chomp(my @inc = qx($command));
40
41 my %mods = reverse @inc;
42
43 my @non_core = grep +(
44   not (
45     /^\Q$Config{privlibexp}/ or /^\Q$Config{archlibexp}/
46   )
47 ), keys %mods;
48
49 #my @core_non_arch = grep +(
50 #  /^\Q$Config{privlibexp}/
51 #), grep !/\Q$Config{archname}/, grep !/\Q$Config{myarchname}/, keys %mods;
52 my @core_non_arch; 
53
54 my $start = stripspace <<'END_START';
55   # This chunk of stuff was generated by Object::Remote::FatNode. To find
56   # the original file's code, look for the end of this BEGIN block or the
57   # string 'FATPACK'
58   BEGIN {
59   my (%fatpacked,%fatpacked_extra);
60 END_START
61 my $end = stripspace <<'END_END';
62   s/^  //mg for values %fatpacked, values %fatpacked_extra;
63
64   sub load_from_hash {
65     if ($_[1] eq 'XSLoader.pm' || $_[1] eq 'DynaLoader.pm') {
66       warn "Will not pre-load '$_[1]'";
67       return undef; 
68     }
69
70     if (my $fat = $_[0]->{$_[1]}) {
71       open my $fh, '<', \$fat;
72       return $fh;
73     }
74     
75     #Uncomment this to find brokenness
76     #warn "Missing $_[1]";
77     return
78   }
79
80   unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
81   push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
82
83   } # END OF FATPACK CODE
84
85   use strictures 1;
86   use Object::Remote::Node;
87   Object::Remote::Node->run;
88 END_END
89
90 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
91               @non_core, @core_non_arch;
92
93 sub generate_fatpack_hash {
94   my ($hash_name, $orig) = @_;
95   (my $stub = $orig) =~ s/\.pm$//;
96   my $name = uc join '_', split '/', $stub;
97   my $data = $files{$orig} or die $orig; $data =~ s/^/  /mg;
98   return '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
99   .qq!${data}${name}\n!;
100 }
101
102 use Data::Dumper;
103 warn "Dumping list of shipped modules";
104 print STDERR "Core non-arch: ", Dumper(\@core_non_arch);
105 print STDERR "Non-core: ", Dumper(\@non_core); 
106
107 my @segments = (
108   map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @non_core),
109   map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @core_non_arch),
110 );
111
112 our $DATA = join "\n", $start, @segments, $end;
113
114 1;