a09680fb5b56f52c081471569bfba5b4f552a902
[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
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   -mCPS::Future
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
48 foreach(keys(%mods)) {
49   if ($exclude{ $mods{$_} }) {
50     delete($mods{$_});    
51   }
52 }
53
54 sub filter_not_core {
55   not (
56     /^\Q$Config{privlibexp}/ or /^\Q$Config{archlibexp}/
57   )        
58 }
59
60 my @file_names = keys %mods;
61 my @before_inc = grep { filter_not_core() } @file_names;
62 my @after_inc;
63
64 my $env_pass = '';
65 if (defined($ENV{OBJECT_REMOTE_LOG_LEVEL})) {
66   my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
67   return unless $level =~ /^\w+$/;
68   $env_pass = '$ENV{OBJECT_REMOTE_LOG_LEVEL} = "' . $level . "\";\n";
69 }
70
71 my $start = stripspace <<'END_START';
72   # This chunk of stuff was generated by Object::Remote::FatNode. To find
73   # the original file's code, look for the end of this BEGIN block or the
74   # string 'FATPACK'
75   BEGIN {
76   my (%fatpacked,%fatpacked_extra);
77 END_START
78
79 $start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
80
81 my $end = stripspace <<'END_END';
82   s/^  //mg for values %fatpacked, values %fatpacked_extra;
83
84   sub load_from_hash {
85     if (my $fat = $_[0]->{$_[1]}) {
86       if ($exclude{$_[1]}) {
87         warn "Will not pre-load '$_[1]'";
88         return undef; 
89       }
90  
91       #warn "Handling $_[1]";
92       open my $fh, '<', \$fat;
93       return $fh;
94     }
95     
96     #Uncomment this to find brokenness
97     #warn "Missing $_[1]";
98     return;
99   }
100
101   unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
102   push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
103
104   } # END OF FATPACK CODE
105
106   use strictures 1;
107   use Object::Remote::Node;
108   
109   unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
110     Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);    
111   }
112   
113 END_END
114
115 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
116               @before_inc, @after_inc;
117
118 sub generate_fatpack_hash {
119   my ($hash_name, $orig) = @_;
120   (my $stub = $orig) =~ s/\.pm$//;
121   my $name = uc join '_', split '/', $stub;
122   my $data = $files{$orig} or die $orig; $data =~ s/^/  /mg;
123   return '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
124   .qq!${data}${name}\n!;
125 }
126
127 my @segments = (
128   map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @before_inc),
129   map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @after_inc),
130 );
131
132 our $DATA = join "\n", $start, $env_pass, @segments, $end;
133
134 1;