make notes of things to fix before release
[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 my @non_core_non_arch = ( $mods{'Devel/GlobalDestruction.pm'} );
55 push @non_core_non_arch, grep +(
56   not (
57     #some of the config variables can be empty which will eval as a matching regex
58     $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/ 
59       or $Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
60       or $Config{vendorarchexp} ne '' && /^\Q$Config{vendorarchexp}/
61       or $Config{sitearchexp} ne '' && /^\Q$Config{sitearchexp}/
62   )
63 ), grep !/\Q$Config{archname}/, grep !/\Q$Config{myarchname}/, keys %mods;
64
65 my @core_non_arch = grep +(
66   $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
67   and not($Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
68     or /\Q$Config{archname}/ or /\Q$Config{myarchname}/)
69 ), keys %mods;
70
71 #FIXME - encode the env vars in YAML or something similiar and make the
72 #env vars that will be forwarded driven by a list
73 my $env_pass = '';
74 if (defined($ENV{OBJECT_REMOTE_LOG_LEVEL})) {
75   my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
76   $env_pass .= '$ENV{OBJECT_REMOTE_LOG_LEVEL} = "' . $level . "\";\n";
77 }
78 if (defined($ENV{OBJECT_REMOTE_LOG_FORMAT})) {
79   my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT};
80   $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORMAT} = "' . $format . "\";\n";
81 }
82 if (defined($ENV{OBJECT_REMOTE_LOG_SELECTIONS})) {
83   my $selections = $ENV{OBJECT_REMOTE_LOG_SELECTIONS};
84   $env_pass .= '$ENV{OBJECT_REMOTE_LOG_SELECTIONS} = "' . $selections . "\";\n";
85 }
86 if (defined($ENV{OBJECT_REMOTE_LOG_FORWARDING})) {
87   my $forwarding = $ENV{OBJECT_REMOTE_LOG_FORWARDING};
88   $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORWARDING} = "' . $forwarding . "\";\n";
89 }
90 if (defined($ENV{OBJECT_REMOTE_PERL_BIN})) {
91   my $perl_bin = $ENV{OBJECT_REMOTE_PERL_BIN};
92   $env_pass .= '$ENV{OBJECT_REMOTE_PERL_BIN} = "' . $perl_bin . "\";\n";
93 }
94
95
96
97 my $start = stripspace <<'END_START';
98   # This chunk of stuff was generated by Object::Remote::FatNode. To find
99   # the original file's code, look for the end of this BEGIN block or the
100   # string 'FATPACK'
101   BEGIN {
102   my (%fatpacked,%fatpacked_extra);
103 END_START
104
105 $start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
106
107 my $end = stripspace <<'END_END';
108   s/^  //mg for values %fatpacked, values %fatpacked_extra;
109
110   sub load_from_hash {
111     if (my $fat = $_[0]->{$_[1]}) {
112       if ($exclude{$_[1]}) {
113         warn "Will not pre-load '$_[1]'";
114         return undef; 
115       }
116  
117       #warn "Handling $_[1]";
118       open my $fh, '<', \$fat;
119       return $fh;
120     }
121     
122     #Uncomment this to find brokenness
123     #warn "Missing $_[1]";
124     return;
125   }
126
127   unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
128   push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
129
130   } # END OF FATPACK CODE
131
132   use strictures 1;
133   use Object::Remote::Node;
134   
135   unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
136     Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);    
137   }
138   
139 END_END
140
141 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
142               @non_core_non_arch, @core_non_arch;
143
144 sub generate_fatpack_hash {
145   my ($hash_name, $orig) = @_;
146   (my $stub = $orig) =~ s/\.pm$//;
147   my $name = uc join '_', split '/', $stub;
148   my $data = $files{$orig} or die $orig; $data =~ s/^/  /mg;
149   $data .= "\n" unless $data =~ m/\n$/;
150   my $ret = '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
151     .qq!${data}${name}\n!;
152 #  warn $ret;
153   return $ret;
154 }
155
156 my @segments = (
157   map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @non_core_non_arch),
158   map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @core_non_arch),
159 );
160
161 our $DATA = join "\n", $start, $env_pass, @segments, $end;
162
163 1;