add files
|
1 |
package Mojolicious::Command::generate::app; |
2 |
use Mojo::Base 'Mojolicious::Command'; |
|
3 | ||
4 |
use Mojo::Util qw(class_to_file class_to_path); |
|
5 | ||
6 |
has description => "Generate Mojolicious application directory structure.\n"; |
|
7 |
has usage => "usage: $0 generate app [NAME]\n"; |
|
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_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 |
# Controller |
|
29 |
my $controller = "${class}::Example"; |
|
30 |
my $path = class_to_path $controller; |
|
31 |
$self->render_to_rel_file('controller', "$name/lib/$path", $controller); |
|
32 | ||
33 |
# Test |
|
34 |
$self->render_to_rel_file('test', "$name/t/basic.t", $class); |
|
35 | ||
36 |
# Log directory |
|
37 |
$self->create_rel_dir("$name/log"); |
|
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 |
__DATA__ |
|
51 | ||
52 |
@@ mojo |
|
53 |
% my $class = shift; |
|
54 |
#!/usr/bin/env perl |
|
55 | ||
56 |
use strict; |
|
57 |
use warnings; |
|
58 | ||
59 |
use FindBin; |
|
60 |
BEGIN { unshift @INC, "$FindBin::Bin/../lib" } |
|
61 | ||
62 |
# Start command line interface for application |
|
63 |
require Mojolicious::Commands; |
|
64 |
Mojolicious::Commands->start_app('<%= $class %>'); |
|
65 | ||
66 |
@@ appclass |
|
67 |
% my $class = shift; |
|
68 |
package <%= $class %>; |
|
69 |
use Mojo::Base 'Mojolicious'; |
|
70 | ||
71 |
# This method will run once at server start |
|
72 |
sub startup { |
|
73 |
my $self = shift; |
|
74 | ||
75 |
# Documentation browser under "/perldoc" |
|
76 |
$self->plugin('PODRenderer'); |
|
77 | ||
78 |
# Router |
|
79 |
my $r = $self->routes; |
|
80 | ||
81 |
# Normal route to controller |
|
82 |
$r->get('/')->to('example#welcome'); |
|
83 |
} |
|
84 | ||
85 |
1; |
|
86 | ||
87 |
@@ controller |
|
88 |
% my $class = shift; |
|
89 |
package <%= $class %>; |
|
90 |
use Mojo::Base 'Mojolicious::Controller'; |
|
91 | ||
92 |
# This action will render a template |
|
93 |
sub welcome { |
|
94 |
my $self = shift; |
|
95 | ||
96 |
# Render template "example/welcome.html.ep" with message |
|
97 |
$self->render(msg => 'Welcome to the Mojolicious real-time web framework!'); |
|
98 |
} |
|
99 | ||
100 |
1; |
|
101 | ||
102 |
@@ static |
|
103 |
<!DOCTYPE html> |
|
104 |
<html> |
|
105 |
<head> |
|
106 |
<title>Welcome to the Mojolicious real-time web framework!</title> |
|
107 |
</head> |
|
108 |
<body> |
|
109 |
<h2>Welcome to the Mojolicious real-time web framework!</h2> |
|
110 |
This is the static document "public/index.html", |
|
111 |
<a href="/">click here</a> to get back to the start. |
|
112 |
</body> |
|
113 |
</html> |
|
114 | ||
115 |
@@ test |
|
116 |
% my $class = shift; |
|
117 |
use Mojo::Base -strict; |
|
118 | ||
119 |
use Test::More; |
|
120 |
use Test::Mojo; |
|
121 | ||
122 |
my $t = Test::Mojo->new('<%= $class %>'); |
|
123 |
$t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i); |
|
124 | ||
125 |
done_testing(); |
|
126 | ||
127 |
@@ layout |
|
128 |
<!DOCTYPE html> |
|
129 |
<html> |
|
130 |
<head><title><%%= title %></title></head> |
|
131 |
<body><%%= content %></body> |
|
132 |
</html> |
|
133 | ||
134 |
@@ welcome |
|
135 |
%% layout 'default'; |
|
136 |
%% title 'Welcome'; |
|
137 |
<h2><%%= $msg %></h2> |
|
138 |
This page was generated from the template "templates/example/welcome.html.ep" |
|
139 |
and the layout "templates/layouts/default.html.ep", |
|
140 |
<a href="<%%== url_for %>">click here</a> to reload the page or |
|
141 |
<a href="/index.html">here</a> to move forward to a static page. |
|
142 | ||
143 |
__END__ |
|
144 | ||
145 |
=encoding utf8 |
|
146 | ||
147 |
=head1 NAME |
|
148 | ||
149 |
Mojolicious::Command::generate::app - App generator command |
|
150 | ||
151 |
=head1 SYNOPSIS |
|
152 | ||
153 |
use Mojolicious::Command::generate::app; |
|
154 | ||
155 |
my $app = Mojolicious::Command::generate::app->new; |
|
156 |
$app->run(@ARGV); |
|
157 | ||
158 |
=head1 DESCRIPTION |
|
159 | ||
160 |
L<Mojolicious::Command::generate::app> generates application directory |
|
161 |
structures for fully functional L<Mojolicious> applications. |
|
162 | ||
163 |
This is a core command, that means it is always enabled and its code a good |
|
164 |
example for learning to build new commands, you're welcome to fork it. |
|
165 | ||
166 |
=head1 ATTRIBUTES |
|
167 | ||
168 |
L<Mojolicious::Command::generate::app> inherits all attributes from |
|
169 |
L<Mojolicious::Command> and implements the following new ones. |
|
170 | ||
171 |
=head2 description |
|
172 | ||
173 |
my $description = $app->description; |
|
174 |
$app = $app->description('Foo!'); |
|
175 | ||
176 |
Short description of this command, used for the command list. |
|
177 | ||
178 |
=head2 usage |
|
179 | ||
180 |
my $usage = $app->usage; |
|
181 |
$app = $app->usage('Foo!'); |
|
182 | ||
183 |
Usage information for this command, used for the help screen. |
|
184 | ||
185 |
=head1 METHODS |
|
186 | ||
187 |
L<Mojolicious::Command::generate::app> inherits all methods from |
|
188 |
L<Mojolicious::Command> and implements the following new ones. |
|
189 | ||
190 |
=head2 run |
|
191 | ||
192 |
$app->run(@ARGV); |
|
193 | ||
194 |
Run this command. |
|
195 | ||
196 |
=head1 SEE ALSO |
|
197 | ||
198 |
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>. |
|
199 | ||
200 |
=cut |