INET connector
[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   -mMoo::HandleMoose::_TypeMap
41   -mJSON::PP
42   -e 'print join "\\n", \%INC'
43 );
44
45 $command =~ s/\n/ /g;
46
47 chomp(my @inc = qx($command));
48
49 my %exclude = map { $_ => 1 } @exclude_mods;
50
51 my %file_names = @inc;
52
53 # only include mods that match the filename,
54 # ie ones that will succeed with a require $module
55 # https://rt.cpan.org/Ticket/Display.html?id=100478
56 my %mods =
57   map { $file_names{$_} => $_ }
58   grep { $file_names{$_} =~ /\Q$_\E$/ } keys %file_names;
59
60 foreach(keys(%mods)) {
61   if ($exclude{ $mods{$_} }) {
62     delete($mods{$_});
63   }
64 }
65
66 my @non_core_non_arch = ( $file_names{'Devel/GlobalDestruction.pm'} );
67 push @non_core_non_arch, grep +(
68   not (
69     #some of the config variables can be empty which will eval as a matching regex
70     $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
71       or $Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
72       or $Config{vendorarchexp} ne '' && /^\Q$Config{vendorarchexp}/
73       or $Config{sitearchexp} ne '' && /^\Q$Config{sitearchexp}/
74   )
75 ), grep !/\Q$Config{archname}/, grep !/\Q$Config{myarchname}/, keys %mods;
76
77 my @core_non_arch = grep +(
78   $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
79   and not($Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
80     or /\Q$Config{archname}/ or /\Q$Config{myarchname}/)
81 ), keys %mods;
82
83 my $start = stripspace <<'END_START';
84   # This chunk of stuff was generated by Object::Remote::FatNode. To find
85   # the original file's code, look for the end of this BEGIN block or the
86   # string 'FATPACK'
87   BEGIN {
88   my (%fatpacked,%fatpacked_extra);
89 END_START
90
91 $start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
92
93 my $end = stripspace <<'END_END';
94   s/^  //mg for values %fatpacked, values %fatpacked_extra;
95
96   sub load_from_hash {
97     if (my $fat = $_[0]->{$_[1]}) {
98       if ($exclude{$_[1]}) {
99         warn "Will not pre-load '$_[1]'";
100         return undef;
101       }
102
103       #warn "Handling $_[1]";
104       open my $fh, '<', \$fat;
105       return $fh;
106     }
107
108     #Uncomment this to find brokenness
109     #warn "Missing $_[1]";
110     return;
111   }
112
113   unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
114   push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
115
116   } # END OF FATPACK CODE
117
118   use strictures 1;
119   use Object::Remote::Node;
120
121   unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
122     Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);
123   }
124
125 END_END
126
127 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
128               @non_core_non_arch, @core_non_arch;
129
130 sub generate_fatpack_hash {
131   my ($hash_name, $orig) = @_;
132   (my $stub = $orig) =~ s/\.pm$//;
133   my $name = uc join '_', split '/', $stub;
134   my $data = $files{$orig} or die $orig; $data =~ s/^/  /mg;
135   $data .= "\n" unless $data =~ m/\n$/;
136   my $ret = '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
137     .qq!${data}${name}\n!;
138 #  warn $ret;
139   return $ret;
140 }
141
142 my @segments = (
143   map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @non_core_non_arch),
144   map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @core_non_arch),
145 );
146
147 #print STDERR Dumper(\@segments);
148 our $DATA = join "\n", $start, @segments, $end;
149
150 1;