Updated par helper
[catagits/Catalyst-Runtime.git] / lib / Catalyst / PAR.pm
1 package Catalyst::PAR;
2
3 use strict;
4 use base 'Class::Accessor::Fast';
5 use FindBin;
6 use IO::File;
7 use File::Spec;
8 use File::Find;
9
10 =head1 NAME
11
12 Catalyst::PAR - Package Catalyst Applications
13
14 =head1 SYNOPSIS
15
16 See L<Catalyst>
17
18 =head1 DESCRIPTION
19
20 Package Catalyst Applications.
21
22 =head1 METHODS
23
24 =over 4
25
26 =item $self->package( $par, $engine )
27
28 =cut
29
30 sub package {
31     my ( $self, $options ) = @_;
32
33     my $par    = $options->{par}    || 'application.par';
34     my $engine = $options->{engine} || 'CGI';
35
36     # Check for PAR
37     eval "use PAR ()";
38     die "Please install PAR" if $@;
39     eval "use PAR::Packer ()";
40     die "Please install PAR::Packer" if $@;
41     eval "use App::Packer::PAR ()";
42     die "Please install App::Packer::PAR" if $@;
43     eval "use Module::ScanDeps ()";
44     die "Please install Module::ScanDeps" if $@;
45
46     chdir File::Spec->catdir( $FindBin::Bin, '..' );
47
48     # Find additional files
49     my @files;
50     finddepth(
51         sub {
52             my $name = $File::Find::name;
53             return if $name =~ /^\W*lib/;
54             return if $name =~ /\.par$/;
55             return if $name !~ /\w+/;
56             push @files, $name;
57         },
58         '.'
59     );
60
61     my $par_test = File::Spec->catfile( $FindBin::Bin, '..', 'par_test.pl' );
62     unlink $par_test;
63
64     my $class    = $options->{class};
65     my $tmp_file = IO::File->new("> $par_test");
66     print $tmp_file <<"EOF";
67 BEGIN { \$ENV{CATALYST_ENGINE} = '$engine' };
68 use FindBin;
69 use lib 'lib';
70 use $class;
71 EOF
72     $tmp_file->close;
73
74     # Create package
75     local $SIG{__WARN__} = sub { };
76     open my $olderr, '>&STDERR';
77     open STDERR, '>', File::Spec->devnull;
78     my %opt = ( 'x' => 1, 'n' => 0, 'o' => $par, 'a' => [@files] );
79     App::Packer::PAR->new(
80         frontend  => 'Module::ScanDeps',
81         backend   => 'PAR::Packer',
82         frontopts => \%opt,
83         backopts  => \%opt,
84         args      => ['par_test.pl'],
85     )->go;
86     open STDERR, '>&', $olderr;
87
88     unlink $par_test;
89 }
90
91 =back
92
93 =head1 AUTHOR
94
95 Sebastian Riedel, C<sri@oook.de>
96
97 =head1 LICENSE
98
99 This library is free software, you can redistribute it and/or modify it under
100 the same terms as Perl itself.
101
102 =cut
103
104 1;