[32205] | 1 | package Mojolicious::Command::generate::app;
|
---|
| 2 | use Mojo::Base 'Mojolicious::Command';
|
---|
| 3 |
|
---|
| 4 | use Mojo::Util qw(class_to_file class_to_path decamelize);
|
---|
| 5 |
|
---|
| 6 | has description => 'Generate Mojolicious application directory structure';
|
---|
| 7 | has usage => sub { shift->extract_usage };
|
---|
| 8 |
|
---|
| 9 | sub run {
|
---|
| 10 | my ($self, $class) = @_;
|
---|
| 11 | $class ||= 'MyApp';
|
---|
| 12 |
|
---|
| 13 | # Prevent bad applications
|
---|
| 14 | die <<EOF unless $class =~ /^[A-Z](?:\w|::)+$/;
|
---|
| 15 | Your application name has to be a well formed (CamelCase) Perl module name
|
---|
| 16 | like "MyApp".
|
---|
| 17 | EOF
|
---|
| 18 |
|
---|
| 19 | # Script
|
---|
| 20 | my $name = class_to_file $class;
|
---|
| 21 | $self->render_to_rel_file('mojo', "$name/script/$name", $class);
|
---|
| 22 | $self->chmod_rel_file("$name/script/$name", 0744);
|
---|
| 23 |
|
---|
| 24 | # Application class
|
---|
| 25 | my $app = class_to_path $class;
|
---|
| 26 | $self->render_to_rel_file('appclass', "$name/lib/$app", $class);
|
---|
| 27 |
|
---|
| 28 | # Config file (using the default moniker)
|
---|
| 29 | $self->render_to_rel_file('config', "$name/@{[decamelize $class]}.conf");
|
---|
| 30 |
|
---|
| 31 | # Controller
|
---|
| 32 | my $controller = "${class}::Controller::Example";
|
---|
| 33 | my $path = class_to_path $controller;
|
---|
| 34 | $self->render_to_rel_file('controller', "$name/lib/$path", $controller);
|
---|
| 35 |
|
---|
| 36 | # Test
|
---|
| 37 | $self->render_to_rel_file('test', "$name/t/basic.t", $class);
|
---|
| 38 |
|
---|
| 39 | # Static file
|
---|
| 40 | $self->render_to_rel_file('static', "$name/public/index.html");
|
---|
| 41 |
|
---|
| 42 | # Templates
|
---|
| 43 | $self->render_to_rel_file('layout',
|
---|
| 44 | "$name/templates/layouts/default.html.ep");
|
---|
| 45 | $self->render_to_rel_file('welcome',
|
---|
| 46 | "$name/templates/example/welcome.html.ep");
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | 1;
|
---|
| 50 |
|
---|
| 51 | =encoding utf8
|
---|
| 52 |
|
---|
| 53 | =head1 NAME
|
---|
| 54 |
|
---|
| 55 | Mojolicious::Command::generate::app - App generator command
|
---|
| 56 |
|
---|
| 57 | =head1 SYNOPSIS
|
---|
| 58 |
|
---|
| 59 | Usage: APPLICATION generate app [OPTIONS] [NAME]
|
---|
| 60 |
|
---|
| 61 | mojo generate app
|
---|
| 62 | mojo generate app TestApp
|
---|
| 63 |
|
---|
| 64 | Options:
|
---|
| 65 | -h, --help Show this summary of available options
|
---|
| 66 |
|
---|
| 67 | =head1 DESCRIPTION
|
---|
| 68 |
|
---|
| 69 | L<Mojolicious::Command::generate::app> generates application directory
|
---|
| 70 | structures for fully functional L<Mojolicious> applications.
|
---|
| 71 |
|
---|
| 72 | This is a core command, that means it is always enabled and its code a good
|
---|
| 73 | example for learning to build new commands, you're welcome to fork it.
|
---|
| 74 |
|
---|
| 75 | See L<Mojolicious::Commands/"COMMANDS"> for a list of commands that are
|
---|
| 76 | available by default.
|
---|
| 77 |
|
---|
| 78 | =head1 ATTRIBUTES
|
---|
| 79 |
|
---|
| 80 | L<Mojolicious::Command::generate::app> inherits all attributes from
|
---|
| 81 | L<Mojolicious::Command> and implements the following new ones.
|
---|
| 82 |
|
---|
| 83 | =head2 description
|
---|
| 84 |
|
---|
| 85 | my $description = $app->description;
|
---|
| 86 | $app = $app->description('Foo');
|
---|
| 87 |
|
---|
| 88 | Short description of this command, used for the command list.
|
---|
| 89 |
|
---|
| 90 | =head2 usage
|
---|
| 91 |
|
---|
| 92 | my $usage = $app->usage;
|
---|
| 93 | $app = $app->usage('Foo');
|
---|
| 94 |
|
---|
| 95 | Usage information for this command, used for the help screen.
|
---|
| 96 |
|
---|
| 97 | =head1 METHODS
|
---|
| 98 |
|
---|
| 99 | L<Mojolicious::Command::generate::app> inherits all methods from
|
---|
| 100 | L<Mojolicious::Command> and implements the following new ones.
|
---|
| 101 |
|
---|
| 102 | =head2 run
|
---|
| 103 |
|
---|
| 104 | $app->run(@ARGV);
|
---|
| 105 |
|
---|
| 106 | Run this command.
|
---|
| 107 |
|
---|
| 108 | =head1 SEE ALSO
|
---|
| 109 |
|
---|
| 110 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
| 111 |
|
---|
| 112 | =cut
|
---|
| 113 |
|
---|
| 114 | __DATA__
|
---|
| 115 |
|
---|
| 116 | @@ mojo
|
---|
| 117 | % my $class = shift;
|
---|
| 118 | #!/usr/bin/env perl
|
---|
| 119 |
|
---|
| 120 | use strict;
|
---|
| 121 | use warnings;
|
---|
| 122 |
|
---|
| 123 | use FindBin;
|
---|
| 124 | BEGIN { unshift @INC, "$FindBin::Bin/../lib" }
|
---|
| 125 | use Mojolicious::Commands;
|
---|
| 126 |
|
---|
| 127 | # Start command line interface for application
|
---|
| 128 | Mojolicious::Commands->start_app('<%= $class %>');
|
---|
| 129 |
|
---|
| 130 | @@ appclass
|
---|
| 131 | % my $class = shift;
|
---|
| 132 | package <%= $class %>;
|
---|
| 133 | use Mojo::Base 'Mojolicious';
|
---|
| 134 |
|
---|
| 135 | # This method will run once at server start
|
---|
| 136 | sub startup {
|
---|
| 137 | my $self = shift;
|
---|
| 138 |
|
---|
| 139 | # Load configuration from hash returned by "my_app.conf"
|
---|
| 140 | my $config = $self->plugin('Config');
|
---|
| 141 |
|
---|
| 142 | # Documentation browser under "/perldoc"
|
---|
| 143 | $self->plugin('PODRenderer') if $config->{perldoc};
|
---|
| 144 |
|
---|
| 145 | # Router
|
---|
| 146 | my $r = $self->routes;
|
---|
| 147 |
|
---|
| 148 | # Normal route to controller
|
---|
| 149 | $r->get('/')->to('example#welcome');
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | 1;
|
---|
| 153 |
|
---|
| 154 | @@ controller
|
---|
| 155 | % my $class = shift;
|
---|
| 156 | package <%= $class %>;
|
---|
| 157 | use Mojo::Base 'Mojolicious::Controller';
|
---|
| 158 |
|
---|
| 159 | # This action will render a template
|
---|
| 160 | sub welcome {
|
---|
| 161 | my $self = shift;
|
---|
| 162 |
|
---|
| 163 | # Render template "example/welcome.html.ep" with message
|
---|
| 164 | $self->render(msg => 'Welcome to the Mojolicious real-time web framework!');
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | 1;
|
---|
| 168 |
|
---|
| 169 | @@ static
|
---|
| 170 | <!DOCTYPE html>
|
---|
| 171 | <html>
|
---|
| 172 | <head>
|
---|
| 173 | <title>Welcome to the Mojolicious real-time web framework!</title>
|
---|
| 174 | </head>
|
---|
| 175 | <body>
|
---|
| 176 | <h2>Welcome to the Mojolicious real-time web framework!</h2>
|
---|
| 177 | This is the static document "public/index.html",
|
---|
| 178 | <a href="/">click here</a> to get back to the start.
|
---|
| 179 | </body>
|
---|
| 180 | </html>
|
---|
| 181 |
|
---|
| 182 | @@ test
|
---|
| 183 | % my $class = shift;
|
---|
| 184 | use Mojo::Base -strict;
|
---|
| 185 |
|
---|
| 186 | use Test::More;
|
---|
| 187 | use Test::Mojo;
|
---|
| 188 |
|
---|
| 189 | my $t = Test::Mojo->new('<%= $class %>');
|
---|
| 190 | $t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i);
|
---|
| 191 |
|
---|
| 192 | done_testing();
|
---|
| 193 |
|
---|
| 194 | @@ layout
|
---|
| 195 | <!DOCTYPE html>
|
---|
| 196 | <html>
|
---|
| 197 | <head><title><%%= title %></title></head>
|
---|
| 198 | <body><%%= content %></body>
|
---|
| 199 | </html>
|
---|
| 200 |
|
---|
| 201 | @@ welcome
|
---|
| 202 | %% layout 'default';
|
---|
| 203 | %% title 'Welcome';
|
---|
| 204 | <h2><%%= $msg %></h2>
|
---|
| 205 | <p>
|
---|
| 206 | This page was generated from the template "templates/example/welcome.html.ep"
|
---|
| 207 | and the layout "templates/layouts/default.html.ep",
|
---|
| 208 | <%%= link_to 'click here' => url_for %> to reload the page or
|
---|
| 209 | <%%= link_to 'here' => '/index.html' %> to move forward to a static page.
|
---|
| 210 | %% if (config 'perldoc') {
|
---|
| 211 | To learn more, you can also browse through the documentation
|
---|
| 212 | <%%= link_to 'here' => '/perldoc' %>.
|
---|
| 213 | %% }
|
---|
| 214 | </p>
|
---|
| 215 |
|
---|
| 216 | @@ config
|
---|
| 217 | % use Mojo::Util qw(sha1_sum steady_time);
|
---|
| 218 | {
|
---|
| 219 | perldoc => 1,
|
---|
| 220 | secrets => ['<%= sha1_sum $$ . steady_time . rand %>']
|
---|
| 221 | }
|
---|