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