Mojolicious-Command-Author-generate-automake_app

 view release on metacpan or  search on metacpan

lib/Mojolicious/Command/Author/generate/automake_app.pm  view on Meta::CPAN

   
    # Configure Main Dir
    return {
        'configure.ac' => 'configure.ac',
        'bootstrap' => 'bootstrap',
        'cpanfile' => 'cpanfile',
        'VERSION' => 'VERSION',
        'README.md' => 'README.md',
        'AUTHORS' => 'AUTHORS',
        '.gitignore' => '.gitignore',
        '.perlcritic' => '.perlcritic',
        '.github/workflows/unit-tests.yaml' => '.github/workflows/unit-tests.yaml',
        'LICENSE' => 'LICENSE',
        'COPYRIGHT' => 'COPYRIGHT',
        'CHANGES' => 'CHANGES',
        'Makefile.am' => 'Makefile.am',
        'bin/Makefile.am' => 'bin/Makefile.am',
        'thirdparty/Makefile.am' => 'thirdparty/Makefile.am',
        'etc/Makefile.am' => 'etc/Makefile.am',
        'etc/app.cfg' => 'etc/'.$self->filename.'.cfg',
        'bin/app.pl' => 'bin/'.$self->filename.'.pl',
        'lib/App.pm' => 'lib/'.$self->class_path,
        'lib/Makefile.am' => 'lib/Makefile.am',
        'lib/App/Controller/Example.pm' 
            => 'lib/'.$self->class.'/Controller/Example.pm',
        'public/index.html' 
            => 'public/index.html',
        'templates/layouts/default.html.ep' 
            => 'templates/layouts/default.html.ep',
        'templates/example/welcome.html.ep'
            => 'templates/example/welcome.html.ep',
        't/basic.t' => 't/basic.t',
    };
}

sub finalize {
    my $self = shift;
    my $name = $self->filename;
    $self->chmod_rel_file("$name/bootstrap", 0755);
    $self->chmod_rel_file("$name/bin/".$name.".pl", 0755);
    $self->create_rel_dir("$name/public");
    $self->create_rel_dir("$name/templates");
    chdir $self->cwd.'/'.$name;
    system "./bootstrap";

}
sub run {
    my ($self, $app) = @_;
    $app ||= $self->defaultName;
    my @dir = split /\//, $app;
    $self->class(pop @dir);

    die <<EOF unless $self->class =~ /^[A-Z](?:\w|::)+$/;
Your application name has to be a well formed (CamelCase) Perl module name
like "MyApp".
EOF
    $self->cwd(join '/', @dir) if @dir;

    my $file = $self->file;

    my ($userName,$fullName) = (getpwuid $<)[0,6];
    $fullName =~ s/,.+//g;
    chomp(my $domain = `hostname -d`);
    my $email = $userName.'@'.$domain;

    if ( -r $ENV{HOME} . '/.gitconfig' ){
        my $in = Mojo::File->new($ENV{HOME} . '/.gitconfig')->slurp;
        $in =~ /name\s*=\s*(\S.+\S)/ and $fullName = $1;
        $in =~ /email\s*=\s*(\S+)/ and $email = $1;
    }

    for my $key (keys %$file){
        $self->render_to_rel_file($key, $self->filename.'/'.$file->{$key}, {
            class => $self->class,
            'package' => $self->package,
            filename => $self->filename,
            class_file => $self->class_file,
            class_path => $self->class_path,
            year => (localtime time)[5]+1900,
            email => $email,
            fullName => $fullName,
            userName => $userName,
            date => strftime('%Y-%m-%d',localtime(time)),
        });
    }

    $self->finalize;

    say "** Generated App ".$self->class." in ".$self->cwd.'/'.$self->filename;

}

has search_path => sub {
    my $self = shift;
    my $src = $INC{class_to_path __PACKAGE__};
    return [ dirname($src).'/'.basename($src,'.pm').'/' ];
};

sub render_data {
    my ($self, $name, $vars) = @_;
    for my $path (@{$self->search_path}) {
        next unless -f $path.$name;
        my $data = Mojo::Template
        ->new(vars => 1)
        ->name("template $name")
        ->render(
            Mojo::File->new($path.$name)->slurp, $vars);
        if (ref $data and $data->isa('Mojo::Exception')) {
            warn "  [ERROR] $path$name did not render properly!\n";
        }
        return $data;
    }
    warn " [ERROR] $name not found\n";
    return "";
}

1;

=encoding utf8

=head1 NAME



( run in 2.055 seconds using v1.01-cache-2.11-cpan-5c0b1e786e0 )