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