start adding logs and add support for routed logs and logging to stderr
[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 sub stripspace {
10   my ($text) = @_;
11   $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
12   $text;
13 }
14
15 my %maybe_libs = map +($_ => 1), grep defined, (values %Config, '.');
16
17 my @extra_libs = grep not(ref($_) or $maybe_libs{$_}), @INC;
18
19 my $extra_libs = join '', map "  -I$_\n", @extra_libs;
20
21 my $command = qq(
22   $^X
23   $extra_libs
24   -mObject::Remote
25   -mObject::Remote::Connector::STDIO
26   -mCPS::Future
27   -mMRO::Compat
28   -mClass::C3
29   -mClass::C3::next
30   -mAlgorithm::C3
31   -mObject::Remote::ModuleLoader
32   -mObject::Remote::Node
33   -mMethod::Generate::BuildAll
34   -mMethod::Generate::DemolishAll
35   -mJSON::PP
36   -e 'print join "\\n", \%INC'
37 );
38
39 $command =~ s/\n/ /g;
40
41 chomp(my @inc = qx($command));
42
43 my %exclude = map { $_ => 1 } @exclude_mods; 
44 my %mods = reverse @inc;
45
46 foreach(keys(%mods)) {
47   if ($exclude{ $mods{$_} }) {
48     delete($mods{$_});    
49   }
50 }
51
52 sub filter_not_core {
53   not (
54     /^\Q$Config{privlibexp}/ or /^\Q$Config{archlibexp}/
55   )        
56 }
57
58 my @file_names = keys %mods;
59 my @before_inc = grep { filter_not_core() } @file_names;
60 my @after_inc;
61
62 my $env_pass = '';
63 if (defined($ENV{OBJECT_REMOTE_LOG_LEVEL})) {
64     my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
65     return unless $level =~ /^\w+$/;
66     $env_pass = '$ENV{OBJECT_REMOTE_LOG_LEVEL} = "' . $level . "\";\n";
67 }
68
69 my $start = stripspace <<'END_START';
70   # This chunk of stuff was generated by Object::Remote::FatNode. To find
71   # the original file's code, look for the end of this BEGIN block or the
72   # string 'FATPACK'
73   BEGIN {
74   my (%fatpacked,%fatpacked_extra);
75 END_START
76
77 $start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
78
79 my $end = stripspace <<'END_END';
80   s/^  //mg for values %fatpacked, values %fatpacked_extra;
81
82   sub load_from_hash {
83     if (my $fat = $_[0]->{$_[1]}) {
84       if ($exclude{$_[1]}) {
85         warn "Will not pre-load '$_[1]'";
86         return undef; 
87       }
88  
89       #warn "Handling $_[1]";
90       open my $fh, '<', \$fat;
91       return $fh;
92     }
93     
94     #Uncomment this to find brokenness
95     #warn "Missing $_[1]";
96     return;
97   }
98
99   unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
100   push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
101
102   } # END OF FATPACK CODE
103
104   use strictures 1;
105   use Object::Remote::Node;
106   Object::Remote::Node->run;
107 END_END
108
109 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
110               @before_inc, @after_inc;
111
112 sub generate_fatpack_hash {
113   my ($hash_name, $orig) = @_;
114   (my $stub = $orig) =~ s/\.pm$//;
115   my $name = uc join '_', split '/', $stub;
116   my $data = $files{$orig} or die $orig; $data =~ s/^/  /mg;
117   return '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
118   .qq!${data}${name}\n!;
119 }
120
121 my @segments = (
122   map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @before_inc),
123   map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @after_inc),
124 );
125
126 our $DATA = join "\n", $start, $env_pass, @segments, $end;
127
128 1;