reverted previous commit,
[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 my @exclude_mods = qw(XSLoader.pm DynaLoader.pm);
8 #used by t/watchdog_fatnode
9 our $INHIBIT_RUN_NODE = 0;
10
11 sub stripspace {
12   my ($text) = @_;
13   $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
14   $text;
15 }
16
17 my %maybe_libs = map +($_ => 1), grep defined, (values %Config, '.');
18
19 my @extra_libs = grep not(ref($_) or $maybe_libs{$_}), @INC;
20 my $extra_libs = join '', map {
21     my $lib = $_;
22     $lib =~ s{'}{'\\''}g;
23     "  -I'$lib'\n";
24 } @extra_libs;
25
26 my $command = qq(
27   $^X
28   $extra_libs
29   -mObject::Remote
30   -mObject::Remote::Connector::STDIO
31   -mFuture
32   -mMRO::Compat
33   -mClass::C3
34   -mClass::C3::next
35   -mAlgorithm::C3
36   -mObject::Remote::ModuleLoader
37   -mObject::Remote::Node
38   -mMethod::Generate::BuildAll
39   -mMethod::Generate::DemolishAll
40   -mJSON::PP
41   -e 'print join "\\n", \%INC'
42 );
43
44 $command =~ s/\n/ /g;
45
46 chomp(my @inc = qx($command));
47
48 my %exclude = map { $_ => 1 } @exclude_mods;
49 my %mods = reverse @inc;
50 my %file_names = @inc;
51
52 foreach(keys(%mods)) {
53   if ($exclude{ $mods{$_} }) {
54     delete($mods{$_});
55   }
56 }
57
58 my @non_core_non_arch = ( $file_names{'Devel/GlobalDestruction.pm'} );
59 push @non_core_non_arch, grep +(
60   not (
61     #some of the config variables can be empty which will eval as a matching regex
62     $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
63       or $Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
64       or $Config{vendorarchexp} ne '' && /^\Q$Config{vendorarchexp}/
65       or $Config{sitearchexp} ne '' && /^\Q$Config{sitearchexp}/
66   )
67 ), grep !/\Q$Config{archname}/, grep !/\Q$Config{myarchname}/, keys %mods;
68
69 my @core_non_arch = grep +(
70   $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
71   and not($Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
72     or /\Q$Config{archname}/ or /\Q$Config{myarchname}/)
73 ), keys %mods;
74
75 my $start = stripspace <<'END_START';
76   # This chunk of stuff was generated by Object::Remote::FatNode. To find
77   # the original file's code, look for the end of this BEGIN block or the
78   # string 'FATPACK'
79   BEGIN {
80   my (%fatpacked,%fatpacked_extra);
81 END_START
82
83 $start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
84
85 my $end = stripspace <<'END_END';
86   s/^  //mg for values %fatpacked, values %fatpacked_extra;
87
88   sub load_from_hash {
89     if (my $fat = $_[0]->{$_[1]}) {
90       if ($exclude{$_[1]}) {
91         warn "Will not pre-load '$_[1]'";
92         return undef;
93       }
94
95       #warn "Handling $_[1]";
96       open my $fh, '<', \$fat;
97       return $fh;
98     }
99
100     #Uncomment this to find brokenness
101     #warn "Missing $_[1]";
102     return;
103   }
104
105   unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
106   push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
107
108   } # END OF FATPACK CODE
109
110   use strictures 1;
111   use Object::Remote::Node;
112
113   unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
114     Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);
115   }
116
117 END_END
118
119 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
120               @non_core_non_arch, @core_non_arch;
121
122 sub generate_fatpack_hash {
123   my ($hash_name, $orig) = @_;
124   (my $stub = $orig) =~ s/\.pm$//;
125   my $name = uc join '_', split '/', $stub;
126   my $data = $files{$orig} or die $orig; $data =~ s/^/  /mg;
127   $data .= "\n" unless $data =~ m/\n$/;
128   my $ret = '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
129     .qq!${data}${name}\n!;
130 #  warn $ret;
131   return $ret;
132 }
133
134 my @segments = (
135   map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @non_core_non_arch),
136   map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @core_non_arch),
137 );
138
139 #print STDERR Dumper(\@segments);
140 our $DATA = join "\n", $start, @segments, $end;
141
142 1;