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