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