77e692837bc34a89e8f62e2cdeeaf1c152a85084
[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 #TODO obviously this should be made into a method or configurable some how
65 my $env_pass = '';
66 if (defined($ENV{OBJECT_REMOTE_LOG_LEVEL})) {
67   my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
68   $env_pass .= '$ENV{OBJECT_REMOTE_LOG_LEVEL} = "' . $level . "\";\n";
69 }
70 if (defined($ENV{OBJECT_REMOTE_LOG_FORMAT})) {
71   my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT};
72   $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORMAT} = "' . $format . "\";\n";
73 }
74 if (defined($ENV{OBJECT_REMOTE_LOG_SELECTIONS})) {
75   my $selections = $ENV{OBJECT_REMOTE_LOG_SELECTIONS};
76   $env_pass .= '$ENV{OBJECT_REMOTE_LOG_SELECTIONS} = "' . $selections . "\";\n";
77 }
78
79
80 my $start = stripspace <<'END_START';
81   # This chunk of stuff was generated by Object::Remote::FatNode. To find
82   # the original file's code, look for the end of this BEGIN block or the
83   # string 'FATPACK'
84   BEGIN {
85   my (%fatpacked,%fatpacked_extra);
86 END_START
87
88 $start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
89
90 my $end = stripspace <<'END_END';
91   s/^  //mg for values %fatpacked, values %fatpacked_extra;
92
93   sub load_from_hash {
94     if (my $fat = $_[0]->{$_[1]}) {
95       if ($exclude{$_[1]}) {
96         warn "Will not pre-load '$_[1]'";
97         return undef; 
98       }
99  
100       #warn "Handling $_[1]";
101       open my $fh, '<', \$fat;
102       return $fh;
103     }
104     
105     #Uncomment this to find brokenness
106     #warn "Missing $_[1]";
107     return;
108   }
109
110   unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
111   push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
112
113   } # END OF FATPACK CODE
114
115   use strictures 1;
116   use Object::Remote::Node;
117   
118   unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
119     Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);    
120   }
121   
122 END_END
123
124 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
125               @before_inc, @after_inc;
126
127 sub generate_fatpack_hash {
128   my ($hash_name, $orig) = @_;
129   (my $stub = $orig) =~ s/\.pm$//;
130   my $name = uc join '_', split '/', $stub;
131   my $data = $files{$orig} or die $orig; $data =~ s/^/  /mg;
132   return '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
133   .qq!${data}${name}\n!;
134 }
135
136 my @segments = (
137   map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @before_inc),
138   map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @after_inc),
139 );
140
141 our $DATA = join "\n", $start, $env_pass, @segments, $end;
142
143 1;