create instance, what-the-hey
[gitmo/MooseX-Runnable.git] / lib / MooseX / Runnable / Invocation / Plugin / PAR.pm
CommitLineData
b005539a 1package MooseX::Runnable::Invocation::Plugin::PAR;
2use Moose::Role;
3
4use Module::ScanDeps ();
5use App::Packer::PAR ();
6use MooseX::Runnable::Run;
7
dcfde64f 8use Data::Dump::Streamer;
9
b005539a 10use File::Temp qw(tempfile);
11
12my $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
26around 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);
d8080387 36 $self->create_instance($class, @args);
b005539a 37 };
38 }
39
40 my $inc = join " ",
41 map { "require '$_';\n" }
42 keys %INC;
dcfde64f 43 my %plugins = %{ $self->plugins };
44 delete $plugins{PAR};
45 my $plugins = Dump(\%plugins)->Out;
46
b005539a 47 my $app = $self->class;
48 my $script = <<"END";
49use MooseX::Runnable::Run;
50use MooseX::Runnable::Invocation;
51require Params::Validate; # XXX!
52$inc
dcfde64f 53$plugins
b005539a 54exit MooseX::Runnable::Invocation->new(
55 class => '$app',
dcfde64f 56 plugins => \$HASH1,
7b33437c 57)->run(\@ARGV);
b005539a 58END
59
dcfde64f 60 print "script: \n$script";
61
b005539a 62 $app =~ s/::/_/g;
63 $app = lc $app;
64
65 my $opt = { e => $script, o => $app, vvv => 1 };
66
67 App::Packer::PAR->new(
68 frontend => 'Module::ScanDeps',
69 backend => 'PAR::Packer',
70 frontopts => $opt,
71 backopts => $opt,
72 args => [],
73 )->go;
74
75 return 0;
76};
77
781;