added in file, line, and method info for log invocation
[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
9 #used by t/watchdog_fatnode 
10 our $INHIBIT_RUN_NODE = 0; 
11
12 sub stripspace {
13   my ($text) = @_;
14   $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
15   $text;
16 }
17
18 my %maybe_libs = map +($_ => 1), grep defined, (values %Config, '.');
19
20 my @extra_libs = grep not(ref($_) or $maybe_libs{$_}), @INC;
21
22 my $extra_libs = join '', map "  -I$_\n", @extra_libs;
23
24 my $command = qq(
25   $^X
26   $extra_libs
27   -mObject::Remote
28   -mObject::Remote::Connector::STDIO
29   -mCPS::Future
30   -mMRO::Compat
31   -mClass::C3
32   -mClass::C3::next
33   -mAlgorithm::C3
34   -mObject::Remote::ModuleLoader
35   -mObject::Remote::Node
36   -mMethod::Generate::BuildAll
37   -mMethod::Generate::DemolishAll
38   -mJSON::PP
39   -e 'print join "\\n", \%INC'
40 );
41
42 $command =~ s/\n/ /g;
43
44 chomp(my @inc = qx($command));
45
46 my %exclude = map { $_ => 1 } @exclude_mods; 
47 my %mods = reverse @inc;
48
49 foreach(keys(%mods)) {
50   if ($exclude{ $mods{$_} }) {
51     delete($mods{$_});    
52   }
53 }
54
55 sub filter_not_core {
56   not (
57     /^\Q$Config{privlibexp}/ or /^\Q$Config{archlibexp}/
58   )        
59 }
60
61 my @before_inc = grep { filter_not_core() } keys %mods;
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 } qw(' . 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;