Renamed tests
[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 require Catalyst::Helper;
79 require Catalyst::PAR;
80 require Catalyst::Build;
81 require Catalyst::Test;
82 require Catalyst::Engine::HTTP;
83 require Catalyst::Engine::CGI;
84 require Catalyst::Controller;
85 require Catalyst::Model;
86 require Catalyst::View;
87 $classes
88 EOF
89     $tmp_file->close;
90
91     # Create package
92     local $SIG{__WARN__} = sub { };
93     open my $olderr, '>&STDERR';
94     open STDERR, '>', File::Spec->devnull;
95     my %opt = (
96         'x' => 1,
97         'n' => 0,
98         'o' => $par,
99         'a' => [@files],
100         'p' => 1,
101         'B' => $options->{core},
102         'm' => $options->{multiarch}
103     );
104     App::Packer::PAR->new(
105         frontend  => 'Module::ScanDeps',
106         backend   => 'PAR::Packer',
107         frontopts => \%opt,
108         backopts  => \%opt,
109         args      => ['par_test.pl'],
110     )->go;
111     open STDERR, '>&', $olderr;
112
113     unlink $par_test;
114 }
115
116 =head1 AUTHOR
117
118 Sebastian Riedel, C<sri@oook.de>
119
120 =head1 LICENSE
121
122 This library is free software, you can redistribute it and/or modify it under
123 the same terms as Perl itself.
124
125 =cut
126
127 1;