experimental move to non-blocking reads in ReadChannel; fix log bugs; annotate fixes...
[scpubgit/Object-Remote.git] / lib / Object / Remote / FatNode.pm
1 package Object::Remote::FatNode;
2
3 #TODO If a file does not end in a new line by itself
4 #then fat node fails
5
6 use strictures 1;
7 use Config;
8 use B qw(perlstring);
9
10 my @exclude_mods = qw(XSLoader.pm DynaLoader.pm);
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 @file_names = keys %mods;
62 my @before_inc = grep { filter_not_core() } @file_names;
63 my @after_inc;
64
65 my $env_pass = '';
66 if (defined($ENV{OBJECT_REMOTE_LOG_LEVEL})) {
67     my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
68     return unless $level =~ /^\w+$/;
69     $env_pass = '$ENV{OBJECT_REMOTE_LOG_LEVEL} = "' . $level . "\";\n";
70 }
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   Object::Remote::Node->run;
110 END_END
111
112 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
113               @before_inc, @after_inc;
114
115 sub generate_fatpack_hash {
116   my ($hash_name, $orig) = @_;
117   (my $stub = $orig) =~ s/\.pm$//;
118   my $name = uc join '_', split '/', $stub;
119   my $data = $files{$orig} or die $orig; $data =~ s/^/  /mg;
120   return '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
121   .qq!${data}${name}\n!;
122 }
123
124 my @segments = (
125   map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @before_inc),
126   map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @after_inc),
127 );
128
129 our $DATA = join "\n", $start, $env_pass, @segments, $end;
130
131 1;