419598f15f5d757f04e86b576b4d64f51a84800f
[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 #TODO oi this isn't right yet
44 my @non_core_non_arch = grep +(
45   not (
46     /^\Q$Config{privlibexp}/ or /^\Q$Config{archlibexp}/
47     #or /^\Q$Config{vendorarchexp}/ or /^\Q$Config{sitearchexp}/
48   )
49 ), keys %mods;
50
51 my @core_non_arch = grep +(
52   /^\Q$Config{privlibexp}/
53   and not(/^\Q$Config{archlibexp}/ or /\Q$Config{archname}/ or /\Q$Config{myarchname}/)
54 ), keys %mods;
55
56 my $env_pass = '';
57 if (defined($ENV{OBJECT_REMOTE_LOG_LEVEL})) {
58     my $level = $ENV{OBJECT_REMOTE_LOG_LEVEL};
59     return unless $level =~ /^\w+$/;
60     $env_pass = '$ENV{OBJECT_REMOTE_LOG_LEVEL} = "' . $level . "\";\n";
61 }
62
63 my $start = stripspace <<'END_START';
64   # This chunk of stuff was generated by Object::Remote::FatNode. To find
65   # the original file's code, look for the end of this BEGIN block or the
66   # string 'FATPACK'
67   BEGIN {
68   my (%fatpacked,%fatpacked_extra);
69 END_START
70 my $end = stripspace <<'END_END';
71   s/^  //mg for values %fatpacked, values %fatpacked_extra;
72
73   sub load_from_hash {
74     if (my $fat = $_[0]->{$_[1]}) {
75       open my $fh, '<', \$fat;
76       return $fh;
77     }
78     #Uncomment this to find brokenness
79     #warn "Missing $_[1]";
80     return
81   }
82
83   unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
84   push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
85
86   } # END OF FATPACK CODE
87
88   use strictures 1;
89   use Object::Remote::Node;
90   Object::Remote::Node->run;
91 END_END
92
93 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
94               @non_core_non_arch, @core_non_arch;
95
96 sub generate_fatpack_hash {
97   my ($hash_name, $orig) = @_;
98   (my $stub = $orig) =~ s/\.pm$//;
99   my $name = uc join '_', split '/', $stub;
100   my $data = $files{$orig} or die $orig; $data =~ s/^/  /mg;
101   return '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
102   .qq!${data}${name}\n!;
103 }
104
105 my @segments = (
106     map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @non_core_non_arch),
107     map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @core_non_arch),
108 );
109
110 our $DATA = join "\n", $start, $env_pass, @segments, $end;
111
112 1;