experimental move to non-blocking reads in ReadChannel; fix log bugs; annotate fixes...
[scpubgit/Object-Remote.git] / lib / Object / Remote / FatNode.pm
CommitLineData
eee9a548 1package Object::Remote::FatNode;
2
90a3a7f2 3#TODO If a file does not end in a new line by itself
4#then fat node fails
5
eee9a548 6use strictures 1;
7use Config;
8use B qw(perlstring);
9
10sub stripspace {
11 my ($text) = @_;
12 $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
13 $text;
14}
15
16my %maybe_libs = map +($_ => 1), grep defined, (values %Config, '.');
17
18my @extra_libs = grep not(ref($_) or $maybe_libs{$_}), @INC;
19
20my $extra_libs = join '', map " -I$_\n", @extra_libs;
21
22my $command = qq(
23 $^X
24 $extra_libs
25 -mObject::Remote
26 -mObject::Remote::Connector::STDIO
27 -mCPS::Future
8c81a0e5 28 -mMRO::Compat
eee9a548 29 -mClass::C3
8c81a0e5 30 -mClass::C3::next
31 -mAlgorithm::C3
eee9a548 32 -mObject::Remote::ModuleLoader
33 -mObject::Remote::Node
4c8c83d7 34 -mMethod::Generate::BuildAll
35 -mMethod::Generate::DemolishAll
eee9a548 36 -mJSON::PP
df8e0ca6 37 -e 'print join "\\n", \%INC'
eee9a548 38);
39
40$command =~ s/\n/ /g;
41
df8e0ca6 42chomp(my @inc = qx($command));
43
44my %mods = reverse @inc;
eee9a548 45
3a966220 46#TODO oi this isn't right yet
eee9a548 47my @non_core_non_arch = grep +(
df8e0ca6 48 not (
49 /^\Q$Config{privlibexp}/ or /^\Q$Config{archlibexp}/
3a966220 50 #or /^\Q$Config{vendorarchexp}/ or /^\Q$Config{sitearchexp}/
df8e0ca6 51 )
f225a199 52), keys %mods;
eee9a548 53
1ef3d225 54my @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
23591f5f 59my $env_pass = '';
60if (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
eee9a548 66my $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 {
1ef3d225 71 my (%fatpacked,%fatpacked_extra);
eee9a548 72END_START
73my $end = stripspace <<'END_END';
1ef3d225 74 s/^ //mg for values %fatpacked, values %fatpacked_extra;
eee9a548 75
1ef3d225 76 sub load_from_hash {
77 if (my $fat = $_[0]->{$_[1]}) {
eee9a548 78 open my $fh, '<', \$fat;
79 return $fh;
80 }
8c81a0e5 81 #Uncomment this to find brokenness
82 #warn "Missing $_[1]";
eee9a548 83 return
1ef3d225 84 }
85
86 unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
87 push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
eee9a548 88
89 } # END OF FATPACK CODE
90
91 use strictures 1;
92 use Object::Remote::Node;
93 Object::Remote::Node->run;
94END_END
95
96my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
1ef3d225 97 @non_core_non_arch, @core_non_arch;
eee9a548 98
1ef3d225 99sub generate_fatpack_hash {
100 my ($hash_name, $orig) = @_;
101 (my $stub = $orig) =~ s/\.pm$//;
eee9a548 102 my $name = uc join '_', split '/', $stub;
1ef3d225 103 my $data = $files{$orig} or die $orig; $data =~ s/^/ /mg;
104 return '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
eee9a548 105 .qq!${data}${name}\n!;
1ef3d225 106}
107
108my @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);
eee9a548 112
23591f5f 113our $DATA = join "\n", $start, $env_pass, @segments, $end;
eee9a548 114
1151;