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