| 1 | package Mojolicious::Command::Author::generate::automake_app; | ||
| 2 | use Mojo::Base 'Mojolicious::Command'; | ||
| 3 | use File::Basename qw(basename dirname); | ||
| 4 | use Mojo::Util qw(class_to_file class_to_path); | ||
| 5 | use Mojo::File; | ||
| 6 | |||
| 7 | use POSIX qw(strftime); | ||
| 8 | use Cwd 'getcwd'; | ||
| 9 | use File::Spec::Functions qw(catdir catfile); | ||
| 10 | our $VERSION = '0.7.4'; | ||
| 11 | has description => 'Generate Mojolicious web application with Automake'; | ||
| 12 | has usage => sub { shift->extract_usage }; | ||
| 13 | |||
| 14 | has cwd => sub { | ||
| 15 | getcwd(); | ||
| 16 | }; | ||
| 17 | |||
| 18 | has package => 'automake'; | ||
| 19 | |||
| 20 | has defaultName => 'MyMojoliciousApp'; | ||
| 21 | |||
| 22 | has class => sub { | ||
| 23 | die "class must be initialized in run." | ||
| 24 | }; | ||
| 25 | |||
| 26 | has class_file => sub { | ||
| 27 | class_to_file shift->class; | ||
| 28 | }; | ||
| 29 | |||
| 30 | has class_path => sub { | ||
| 31 | class_to_path shift->class; | ||
| 32 | }; | ||
| 33 | |||
| 34 | has filename => sub { | ||
| 35 | my $filename = shift->class_file; | ||
| 36 | $filename =~ s/_/-/g; | ||
| 37 | return $filename; | ||
| 38 | }; | ||
| 39 | |||
| 40 | sub rel_dir { | ||
| 41 | my $self=shift; | ||
| 42 | catdir $self->cwd, split('/', pop) | ||
| 43 | } | ||
| 44 | |||
| 45 | sub rel_file { | ||
| 46 | my $self =shift; | ||
| 47 | catfile $self->cwd, split('/', pop); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub file { | ||
| 51 | my $self = shift; | ||
| 52 | |||
| 53 | # Configure Main Dir | ||
| 54 | return { | ||
| 55 | 'configure.ac' => 'configure.ac', | ||
| 56 | 'bootstrap' => 'bootstrap', | ||
| 57 | 'cpanfile' => 'cpanfile', | ||
| 58 | 'VERSION' => 'VERSION', | ||
| 59 | 'README.md' => 'README.md', | ||
| 60 | 'AUTHORS' => 'AUTHORS', | ||
| 61 | '.gitignore' => '.gitignore', | ||
| 62 | '.perlcritic' => '.perlcritic', | ||
| 63 | '.github/workflows/unit-tests.yaml' => '.github/workflows/unit-tests.yaml', | ||
| 64 | 'LICENSE' => 'LICENSE', | ||
| 65 | 'COPYRIGHT' => 'COPYRIGHT', | ||
| 66 | 'CHANGES' => 'CHANGES', | ||
| 67 | 'Makefile.am' => 'Makefile.am', | ||
| 68 | 'bin/Makefile.am' => 'bin/Makefile.am', | ||
| 69 | 'thirdparty/Makefile.am' => 'thirdparty/Makefile.am', | ||
| 70 | 'etc/Makefile.am' => 'etc/Makefile.am', | ||
| 71 | 'etc/app.cfg' => 'etc/'.$self->filename.'.cfg', | ||
| 72 | 'bin/app.pl' => 'bin/'.$self->filename.'.pl', | ||
| 73 | 'lib/App.pm' => 'lib/'.$self->class_path, | ||
| 74 | 'lib/Makefile.am' => 'lib/Makefile.am', | ||
| 75 | 'lib/App/Controller/Example.pm' | ||
| 76 | => 'lib/'.$self->class.'/Controller/Example.pm', | ||
| 77 | 'public/index.html' | ||
| 78 | => 'public/index.html', | ||
| 79 | 'templates/layouts/default.html.ep' | ||
| 80 | => 'templates/layouts/default.html.ep', | ||
| 81 | 'templates/example/welcome.html.ep' | ||
| 82 | => 'templates/example/welcome.html.ep', | ||
| 83 | 't/basic.t' => 't/basic.t', | ||
| 84 | }; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub finalize { | ||
| 88 | my $self = shift; | ||
| 89 | my $name = $self->filename; | ||
| 90 | $self->chmod_rel_file("$name/bootstrap", 0755); | ||
| 91 | $self->chmod_rel_file("$name/bin/".$name.".pl", 0755); | ||
| 92 | $self->create_rel_dir("$name/public"); | ||
| 93 | $self->create_rel_dir("$name/templates"); | ||
| 94 | chdir $self->cwd.'/'.$name; | ||
| 95 | system "./bootstrap"; | ||
| 96 | |||
| 97 | } | ||
| 98 | sub run { | ||
| 99 | my ($self, $app) = @_; | ||
| 100 | $app ||= $self->defaultName; | ||
| 101 | my @dir = split /\//, $app; | ||
| 102 | $self->class(pop @dir); | ||
| 103 | |||
| 104 | die <<EOF unless $self->class =~ /^[A-Z](?:\w|::)+$/; | ||
| 105 | Your application name has to be a well formed (CamelCase) Perl module name | ||
| 106 | like "MyApp". | ||
| 107 | EOF | ||
| 108 | $self->cwd(join '/', @dir) if @dir; | ||
| 109 | |||
| 110 | my $file = $self->file; | ||
| 111 | |||
| 112 | my ($userName,$fullName) = (getpwuid $<)[0,6]; | ||
| 113 | $fullName =~ s/,.+//g; | ||
| 114 | chomp(my $domain = `hostname -d`); | ||
| 115 | my $email = $userName.'@'.$domain; | ||
| 116 | |||
| 117 | if ( -r $ENV{HOME} . '/.gitconfig' ){ | ||
| 118 | my $in = Mojo::File->new($ENV{HOME} . '/.gitconfig')->slurp; | ||
| 119 | $in =~ /name\s*=\s*(\S.+\S)/ and $fullName = $1; | ||
| 120 | $in =~ /email\s*=\s*(\S+)/ and $email = $1; | ||
| 121 | } | ||
| 122 | |||
| 123 | for my $key (keys %$file){ | ||
| 124 | $self->render_to_rel_file($key, $self->filename.'/'.$file->{$key}, { | ||
| 125 | class => $self->class, | ||
| 126 | 'package' => $self->package, | ||
| 127 | filename => $self->filename, | ||
| 128 | class_file => $self->class_file, | ||
| 129 | class_path => $self->class_path, | ||
| 130 | year => (localtime time)[5]+1900, | ||
| 131 | email => $email, | ||
| 132 | fullName => $fullName, | ||
| 133 | userName => $userName, | ||
| 134 | date => strftime('%Y-%m-%d',localtime(time)), | ||
| 135 | }); | ||
| 136 | } | ||
| 137 | |||
| 138 | $self->finalize; | ||
| 139 | |||
| 140 | say "** Generated App ".$self->class." in ".$self->cwd.'/'.$self->filename; | ||
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 | has search_path => sub { | ||
| 145 | my $self = shift; | ||
| 146 | my $src = $INC{class_to_path __PACKAGE__}; | ||
| 147 | return [ dirname($src).'/'.basename($src,'.pm').'/' ]; | ||
| 148 | }; | ||
| 149 | |||
| 150 | sub render_data { | ||
| 151 | my ($self, $name, $vars) = @_; | ||
| 152 | for my $path (@{$self->search_path}) { | ||
| 153 | next unless -f $path.$name; | ||
| 154 | my $data = Mojo::Template | ||
| 155 | ->new(vars => 1) | ||
| 156 | ->name("template $name") | ||
| 157 | ->render( | ||
| 158 | Mojo::File->new($path.$name)->slurp, $vars); | ||
| 159 | if (ref $data and $data->isa('Mojo::Exception')) { | ||
| 160 | warn " [ERROR] $path$name did not render properly!\n"; | ||
| 161 | } | ||
| 162 | return $data; | ||
| 163 | } | ||
| 164 | warn " [ERROR] $name not found\n"; | ||
| 165 | return ""; | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; | ||
| 169 | |||
| 170 | =encoding utf8 | ||
| 171 | |||
| 172 | =head1 NAME | ||
| 173 | |||
| 174 | Mojolicious::Command::Author::generate::automake_app - Mojolicious App generator command | ||
| 175 | |||
| 176 | =head1 SYNOPSIS | ||
| 177 | |||
| 178 | Usage: mojo generate automake_app [OPTIONS] [NAME] | ||
| 179 | |||
| 180 | mojo generate automake_app | ||
| 181 | mojo generate automake_app [/full/path/]TestApp | ||
| 182 | |||
| 183 | Options: | ||
| 184 | -h, --help Show this summary of available options | ||
| 185 | |||
| 186 | =head1 DESCRIPTION | ||
| 187 | |||
| 188 | L<Mojolicious::Command::Authos::generate::automake_app> generates application directory structures for fully functional L<Mojolicious> applications. | ||
| 189 | |||
| 190 | =head1 SEE ALSO | ||
| 191 | |||
| 192 | L<Mojolicious>, L<https://www.gnu.org/software/automake/>. | ||
| 193 | |||
| 194 | =cut |