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