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