File Coverage

blib/lib/Ado/Command/generate/apache2htaccess.pm
Criterion Covered Total %
statement 46 51 90.2
branch 13 22 59.0
condition 5 14 35.7
subroutine 5 5 100.0
pod 1 1 100.0
total 70 93 75.2


line stmt bran cond sub pod time code
1             package Ado::Command::generate::apache2htaccess;
2 2     2   1770 use Mojo::Base 'Ado::Command::generate';
  2         6  
  2         13  
3 2     2   219 use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
  2         9  
  2         11  
4 2     2   311 use Mojo::File 'path';
  2         5  
  2         1402  
5              
6             has description => "Generates Apache2 .htaccess file.\n";
7             has usage => sub { shift->extract_usage };
8              
9             # The next two are more or less stollen from File::Which.
10             my $IS_DOS = ($^O eq 'MSWin32' or $^O eq 'dos' or $^O eq 'os2');
11              
12             sub _which {
13 2     2   31075 my ($self, $basename) = @_;
14 2 100       9 return $self->{$basename} if $self->{$basename};
15 1         3 my @pathext = ('');
16 1 50 33     4 push @pathext, map {lc} split /;/, $ENV{PATHEXT} if $ENV{PATHEXT} && $IS_DOS;
  0         0  
17 1         21 foreach my $path (File::Spec->path($ENV{PATH})) {
18 4         8 foreach my $ext (@pathext) {
19 4 50       28 my $exe = File::Spec->catfile($path, ($ext ? "$basename.$ext" : $basename));
20 4         10 $exe =~ s|\\|/|g;
21 4 100       49 return $self->{$basename} = $exe if -e $exe;
22             }
23             }
24 0         0 return '';
25             }
26              
27             sub run {
28 1     1 1 50 my ($self, @args) = @_;
29 1         6 state $app = $self->app;
30 1         8 state $home = $app->home;
31 1         7 state $ado_home = $app->ado_home;
32 1         7 my $args = $self->args;
33             GetOptionsFromArray \@args,
34             'v|verbose' => \$args->{verbose},
35             'c|config_file=s' => \$args->{config_file},
36 1   50     8 'M|modules=s@' => \($args->{modules} //= []);
37              
38 1         331 @{$args->{modules}} = split(/\,/, join(',', @{$args->{modules}}));
  1         3  
  1         4  
39              
40 1 50       1 Carp::croak $self->usage unless scalar @{$args->{modules}};
  1         3  
41 1         2 $args->{DocumentRoot} = $home;
42 1         2 $args->{perl} = $^X;
43              
44 1 50       3 if ($IS_DOS) {
45 0         0 $args->{DocumentRoot} =~ s|\\|/|g;
46 0         0 $args->{perl} =~ s|\\|/|g;
47             }
48             $args->{plackup} = $self->_which('plackup')
49 1         645 if ( eval { require Plack }
50 1         368 && eval { require FCGI }
51 1         1049 && eval { require FCGI::ProcManager }
52 1 50 33     1 && eval { require Apache::LogFormat::Compiler });
  1   33     2540  
      33        
53              
54 1 50       4 say STDERR 'Using arguments:' . $app->dumper($args) if $args->{verbose};
55 1         3 state $rel_file = 'templates/partials/apache2htaccess.ep';
56 1 50       5 state $template_file = (
57             -s $home->rel_file($rel_file)
58             ? $home->rel_file($rel_file)
59             : $ado_home->rel_file($rel_file)
60             );
61 1         62 $args->{moniker} = $app->moniker;
62 1         13 my $config = Mojo::Template->new->render_file($template_file, $args);
63 1 50       28 if ($args->{config_file}) {
64 1 50       3 say STDERR 'Writing ' . $args->{config_file} if $args->{verbose};
65 1         4 path($args->{config_file})->spurt($config);
66             }
67             else {
68 0         0 say $config;
69             }
70 1         207 return $self;
71             }
72              
73             1;
74              
75              
76             =pod
77              
78             =encoding utf8
79              
80             =head1 NAME
81              
82             Ado::Command::generate::apache2htaccess - Generates Apache2 .htaccess file
83              
84             =head1 SYNOPSIS
85              
86             Usage:
87             #on the command-line
88              
89             $ bin/ado generate apache2htaccess --modules cgi,fcgid > .htaccess
90              
91             #programatically
92             use Ado::Command::generate::apache2htaccess;
93             my $v = Ado::Command::generate::apache2htaccess->new;
94             $v->run('--modules' => 'cgi,fcgid');
95              
96             =head1 DESCRIPTION
97              
98             L
99             generates an Apache2 C<.htaccess> configuration file for your L application.
100             You can use this command for a shared hosting account.
101              
102             This is a core command, that means it is always enabled and its code a good
103             example for learning to build new commands, you're welcome to fork it.
104              
105             =head1 OPTIONS
106              
107             Below are the options this command accepts, described in L notation.
108              
109             =head2 c|config_file=s
110              
111             Full path to the file in which the configuration will be written.
112             If not provided the configuration is printed to the screen.
113              
114             =head3 v|verbose
115              
116             Verbose output.
117              
118             =head2 M|modules=s@
119              
120             Apache modules to use for running C. Currently supported modules are
121             C and C. You can mention them both to add the corresponding
122             sections and Apache will use C if loaded or C
123             (almost always enabled).
124             The generated configuration for mod_fcgid will use L (C) or
125             L. So make sure you have at least one of them installed.
126             L is recommended.
127             To use L with C you will need to install
128             L, L and L.
129              
130             =head1 ATTRIBUTES
131              
132             L inherits all attributes from
133             L and implements the following new ones.
134              
135             =head2 description
136              
137             my $description = $htaccess->description;
138             $v = $htaccess->description('Foo!');
139              
140             Short description of this command, used for the command list.
141              
142             =head2 usage
143              
144             my $usage = $htaccess->usage;
145             $v = $htaccess->usage('Foo!');
146              
147             Usage information for this command, used for the help screen.
148              
149             =head1 METHODS
150              
151              
152             L inherits all methods from
153             L and implements the following new ones.
154              
155             =head2 run
156              
157             my $htaccess = Ado::Command::generate::apache2htaccess->run(@ARGV);
158             my $htaccess = $app->commands->run("generate", "apache2htaccess",
159             '-m' => 'cgi,fcgid', '-c' => $config_file);
160             Run this command. Returns C<$self>.
161              
162             =head1 SEE ALSO
163              
164             L,
165             L,
166             L,
167             L,
168             L, L,
169             L L,
170             L, L,
171             L.
172              
173             =cut