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