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