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