fix fatnode not parsing if included package did not end in new line
[scpubgit/Object-Remote.git] / lib / Object / Remote / FatNode.pm
CommitLineData
eee9a548 1package Object::Remote::FatNode;
2
3use strictures 1;
4use Config;
5use B qw(perlstring);
6
d0a91ad2 7my @exclude_mods = qw(XSLoader.pm DynaLoader.pm);
8
c003f065 9#used by t/watchdog_fatnode
7985ed9e 10our $INHIBIT_RUN_NODE = 0;
e5b3f03f 11
eee9a548 12sub stripspace {
13 my ($text) = @_;
14 $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
15 $text;
16}
17
18my %maybe_libs = map +($_ => 1), grep defined, (values %Config, '.');
19
20my @extra_libs = grep not(ref($_) or $maybe_libs{$_}), @INC;
21
22my $extra_libs = join '', map " -I$_\n", @extra_libs;
23
24my $command = qq(
25 $^X
26 $extra_libs
27 -mObject::Remote
28 -mObject::Remote::Connector::STDIO
29 -mCPS::Future
8c81a0e5 30 -mMRO::Compat
eee9a548 31 -mClass::C3
8c81a0e5 32 -mClass::C3::next
33 -mAlgorithm::C3
eee9a548 34 -mObject::Remote::ModuleLoader
35 -mObject::Remote::Node
4c8c83d7 36 -mMethod::Generate::BuildAll
37 -mMethod::Generate::DemolishAll
eee9a548 38 -mJSON::PP
df8e0ca6 39 -e 'print join "\\n", \%INC'
eee9a548 40);
41
42$command =~ s/\n/ /g;
43
c003f065 44#warn $command;
df8e0ca6 45chomp(my @inc = qx($command));
46
c003f065 47my %exclude = map { $_ => 1 } @exclude_mods;
48my %mod_files = @inc;
df8e0ca6 49my %mods = reverse @inc;
eee9a548 50
d0a91ad2 51foreach(keys(%mods)) {
52 if ($exclude{ $mods{$_} }) {
53 delete($mods{$_});
54 }
55}
56
c003f065 57#TODO quick and dirty mod for testing - vendorarchexp from a perlbrew build
58#was set to '' which evaluates as a true regex
59
60#use Data::Dumper;
61#print STDERR Dumper([keys %mod_files]);
62#print STDERR Dumper([keys %mods]);
eee9a548 63
c003f065 64my @non_core_non_arch = ( $mod_files{'Devel/GlobalDestruction.pm'} );
65push @non_core_non_arch, grep +(
66 not (
67 $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
68 or $Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
69 or $Config{vendorarchexp} ne '' && /^\Q$Config{vendorarchexp}/
70 or $Config{sitearchexp} ne '' && /^\Q$Config{sitearchexp}/
71 )
72), grep !/\Q$Config{archname}/, grep !/\Q$Config{myarchname}/, keys %mods;
73
74my @core_non_arch = grep +(
75 $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
76 and not($Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
77 or /\Q$Config{archname}/ or /\Q$Config{myarchname}/)
78), keys %mods;
79
80#print STDERR "non-core non-arch ", Dumper(\@non_core_non_arch);
81#print STDERR "core non-arch ", Dumper(\@core_non_arch);
1ef3d225 82
42ff5efb 83#TODO this is the wrong path to go down - fork() will bring
84#the env vars with it and the ssh connector can handle
85#forwarding the env vars
23591f5f 86my $env_pass = '';
87if (defined($ENV{OBJECT_REMOTE_LOG_LEVEL})) {
1a7f821f 88 my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
c0d4da69 89 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_LEVEL} = "' . $level . "\";\n";
90}
91if (defined($ENV{OBJECT_REMOTE_LOG_FORMAT})) {
92 my $format = $ENV{OBJECT_REMOTE_LOG_FORMAT};
93 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORMAT} = "' . $format . "\";\n";
23591f5f 94}
a0771eda 95if (defined($ENV{OBJECT_REMOTE_LOG_SELECTIONS})) {
96 my $selections = $ENV{OBJECT_REMOTE_LOG_SELECTIONS};
97 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_SELECTIONS} = "' . $selections . "\";\n";
98}
aa4a1d83 99if (defined($ENV{OBJECT_REMOTE_LOG_FORWARDING})) {
100 my $forwarding = $ENV{OBJECT_REMOTE_LOG_FORWARDING};
101 $env_pass .= '$ENV{OBJECT_REMOTE_LOG_FORWARDING} = "' . $forwarding . "\";\n";
102}
52a81cde 103if (defined($ENV{OBJECT_REMOTE_PERL_BIN})) {
104 my $perl_bin = $ENV{OBJECT_REMOTE_PERL_BIN};
105 $env_pass .= '$ENV{OBJECT_REMOTE_PERL_BIN} = "' . $perl_bin . "\";\n";
106}
aa4a1d83 107
eee9a548 108my $start = stripspace <<'END_START';
109 # This chunk of stuff was generated by Object::Remote::FatNode. To find
110 # the original file's code, look for the end of this BEGIN block or the
111 # string 'FATPACK'
112 BEGIN {
1ef3d225 113 my (%fatpacked,%fatpacked_extra);
eee9a548 114END_START
d0a91ad2 115
116$start .= 'my %exclude = map { $_ => 1 } qw(' . join(' ', @exclude_mods) . ");\n";
117
eee9a548 118my $end = stripspace <<'END_END';
1ef3d225 119 s/^ //mg for values %fatpacked, values %fatpacked_extra;
eee9a548 120
d0a91ad2 121sub load_from_hash {
1ef3d225 122 if (my $fat = $_[0]->{$_[1]}) {
d0a91ad2 123 if ($exclude{$_[1]}) {
124 warn "Will not pre-load '$_[1]'";
125 return undef;
126 }
127
128 #warn "handling $_[1]";
eee9a548 129 open my $fh, '<', \$fat;
130 return $fh;
131 }
d0a91ad2 132
8c81a0e5 133 #Uncomment this to find brokenness
134 #warn "Missing $_[1]";
d0a91ad2 135 return;
1ef3d225 136 }
137
138 unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
139 push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
eee9a548 140
141 } # END OF FATPACK CODE
142
143 use strictures 1;
144 use Object::Remote::Node;
e5b3f03f 145
146 unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
147 Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);
148 }
149
eee9a548 150END_END
151
152my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
c003f065 153 @non_core_non_arch, @core_non_arch;
eee9a548 154
c003f065 155my %did_pack;
1ef3d225 156sub generate_fatpack_hash {
157 my ($hash_name, $orig) = @_;
158 (my $stub = $orig) =~ s/\.pm$//;
eee9a548 159 my $name = uc join '_', split '/', $stub;
1ef3d225 160 my $data = $files{$orig} or die $orig; $data =~ s/^/ /mg;
c003f065 161 return () if $did_pack{$hash_name}{$orig};
162 $did_pack{$hash_name}{$orig} = 1;
fb2b75a2 163 $data .= "\n" unless $data =~ m/\n$/;
164 my $ret = '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
165 .qq!${data}${name}\n!;
166# warn $ret;
167 return $ret;
1ef3d225 168}
169
170my @segments = (
c003f065 171 map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @non_core_non_arch),
172 map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @core_non_arch),
1ef3d225 173);
eee9a548 174
c003f065 175#print STDERR Dumper(\@segments);
23591f5f 176our $DATA = join "\n", $start, $env_pass, @segments, $end;
eee9a548 177
1781;