f2d6c8d0602b0fb5dd22a98c94c994d1d3f6a87d
[gitmo/MooseX-Runnable.git] / lib / MooseX / Runnable / Invocation / Plugin / PAR.pm
1 package MooseX::Runnable::Invocation::Plugin::PAR;
2 use Moose::Role;
3
4 use Module::ScanDeps ();
5 use App::Packer::PAR ();
6 use MooseX::Runnable::Run;
7
8 use Data::Dump::Streamer;
9
10 use File::Temp qw(tempfile);
11
12 my $mk_scanner = sub {
13     my $class = Moose::Meta::Class->create_anon_class( superclasses => ['Moose::Object'] );
14
15     for my $m (qw/set_file set_options calculate_info
16                   go scan_deps add_deps _find_in_inc/){
17         $class->add_method( $m => sub { warn "$m @_" } );
18     }
19     $class->add_method( get_files => sub { warn 'get_files'; [ keys %INC ] } );
20     my $name = $class->name;
21     $name =~ s{::}{/}g;
22     $INC{ "$name.pm" } = 1;
23     return $class;
24 };
25
26 around run => sub {
27     my ($next, $self, @args) = @_;
28     print "Creating a PAR instead of runing the app.\n";
29
30     { # pre-load as much as possible
31         my $class = $self->load_class;
32         $self->apply_scheme($class);
33         eval {
34             # this is probably not possible, but we might as well try
35             $self->validate_class($class);
36         };
37     }
38
39     my $inc = join " ",
40       map { "require '$_';\n" }
41         keys %INC;
42     my %plugins = %{ $self->plugins };
43     delete $plugins{PAR};
44     my $plugins = Dump(\%plugins)->Out;
45
46     my $app = $self->class;
47     my $script = <<"END";
48 use MooseX::Runnable::Run;
49 use MooseX::Runnable::Invocation;
50 require Params::Validate; # XXX!
51 $inc
52 $plugins
53 exit MooseX::Runnable::Invocation->new(
54     class   => '$app',
55     plugins => \$HASH1,
56 )->run(\@ARGV);
57 END
58
59     print "script: \n$script";
60
61     $app =~ s/::/_/g;
62     $app = lc $app;
63
64     my $opt = { e => $script, o => $app, vvv => 1 };
65
66     App::Packer::PAR->new(
67         frontend  => 'Module::ScanDeps',
68         backend   => 'PAR::Packer',
69         frontopts => $opt,
70         backopts  => $opt,
71         args      => [],
72     )->go;
73
74     return 0;
75 };
76
77 1;