Ado

 view release on metacpan or  search on metacpan

lib/Ado/Command/generate/apache2htaccess.pm  view on Meta::CPAN

package Ado::Command::generate::apache2htaccess;
use Mojo::Base 'Ado::Command::generate';
use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
use Mojo::File 'path';

has description => "Generates Apache2 .htaccess file.\n";
has usage => sub { shift->extract_usage };

# The next two are more or less stollen from File::Which.
my $IS_DOS = ($^O eq 'MSWin32' or $^O eq 'dos' or $^O eq 'os2');

sub _which {
    my ($self, $basename) = @_;
    return $self->{$basename} if $self->{$basename};
    my @pathext = ('');
    push @pathext, map {lc} split /;/, $ENV{PATHEXT} if $ENV{PATHEXT} && $IS_DOS;
    foreach my $path (File::Spec->path($ENV{PATH})) {
        foreach my $ext (@pathext) {
            my $exe = File::Spec->catfile($path, ($ext ? "$basename.$ext" : $basename));
            $exe =~ s|\\|/|g;
            return $self->{$basename} = $exe if -e $exe;
        }
    }
    return '';
}

sub run {
    my ($self, @args) = @_;
    state $app      = $self->app;
    state $home     = $app->home;
    state $ado_home = $app->ado_home;
    my $args = $self->args;
    GetOptionsFromArray \@args,
      'v|verbose'       => \$args->{verbose},
      'c|config_file=s' => \$args->{config_file},
      'M|modules=s@'    => \($args->{modules} //= []);

    @{$args->{modules}} = split(/\,/, join(',', @{$args->{modules}}));

    Carp::croak $self->usage unless scalar @{$args->{modules}};
    $args->{DocumentRoot} = $home;
    $args->{perl}         = $^X;

    if ($IS_DOS) {
        $args->{DocumentRoot} =~ s|\\|/|g;
        $args->{perl} =~ s|\\|/|g;
    }
    $args->{plackup} = $self->_which('plackup')
      if ( eval { require Plack }
        && eval { require FCGI }
        && eval { require FCGI::ProcManager }
        && eval { require Apache::LogFormat::Compiler });

    say STDERR 'Using arguments:' . $app->dumper($args) if $args->{verbose};
    state $rel_file      = 'templates/partials/apache2htaccess.ep';
    state $template_file = (
        -s $home->rel_file($rel_file)
        ? $home->rel_file($rel_file)
        : $ado_home->rel_file($rel_file)
    );
    $args->{moniker} = $app->moniker;
    my $config = Mojo::Template->new->render_file($template_file, $args);
    if ($args->{config_file}) {
        say STDERR 'Writing ' . $args->{config_file} if $args->{verbose};
        path($args->{config_file})->spurt($config);
    }
    else {
        say $config;
    }
    return $self;
}

1;


=pod



( run in 0.972 second using v1.01-cache-2.11-cpan-39bf76dae61 )