Newer Older
200 lines | 4.593kb
copy gitweblite soruce code
root authored on 2012-11-23
1
package Mojolicious::Command::generate::app;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
2
use Mojo::Base 'Mojolicious::Command';
3

            
4
use Mojo::Util qw(class_to_file class_to_path);
copy gitweblite soruce code
root authored on 2012-11-23
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) = @_;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
11
  $class ||= 'MyApp';
copy gitweblite soruce code
root authored on 2012-11-23
12

            
13
  # Prevent bad applications
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
14
  die <<EOF unless $class =~ /^[A-Z](?:\w|::)+$/;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
15
Your application name has to be a well formed (CamelCase) Perl module name
copy gitweblite soruce code
root authored on 2012-11-23
16
like "MyApp".
17
EOF
18

            
19
  # Script
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
20
  my $name = class_to_file $class;
copy gitweblite soruce code
root authored on 2012-11-23
21
  $self->render_to_rel_file('mojo', "$name/script/$name", $class);
22
  $self->chmod_file("$name/script/$name", 0744);
23

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
24
  # Application class
25
  my $app = class_to_path $class;
copy gitweblite soruce code
root authored on 2012-11-23
26
  $self->render_to_rel_file('appclass', "$name/lib/$app", $class);
27

            
28
  # Controller
29
  my $controller = "${class}::Example";
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
30
  my $path       = class_to_path $controller;
copy gitweblite soruce code
root authored on 2012-11-23
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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
36
  # Log directory
copy gitweblite soruce code
root authored on 2012-11-23
37
  $self->create_rel_dir("$name/log");
38

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
39
  # Static file
copy gitweblite soruce code
root authored on 2012-11-23
40
  $self->render_to_rel_file('static', "$name/public/index.html");
41

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
42
  # Templates
copy gitweblite soruce code
root authored on 2012-11-23
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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
56
use strict;
57
use warnings;
copy gitweblite soruce code
root authored on 2012-11-23
58

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
59
use FindBin;
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
60
BEGIN { unshift @INC, "$FindBin::Bin/../lib" }
copy gitweblite soruce code
root authored on 2012-11-23
61

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
62
# Start command line interface for application
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
63
require Mojolicious::Commands;
copy gitweblite soruce code
root authored on 2012-11-23
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
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
97
  $self->render(msg => 'Welcome to the Mojolicious real-time web framework!');
copy gitweblite soruce code
root authored on 2012-11-23
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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
119
use Test::More;
copy gitweblite soruce code
root authored on 2012-11-23
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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
125
done_testing();
126

            
copy gitweblite soruce code
root authored on 2012-11-23
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';
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
137
<h2><%%= $msg %></h2>
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
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.
copy gitweblite soruce code
root authored on 2012-11-23
142

            
143
__END__
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
144

            
145
=encoding utf8
146

            
copy gitweblite soruce code
root authored on 2012-11-23
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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
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

            
copy gitweblite soruce code
root authored on 2012-11-23
166
=head1 ATTRIBUTES
167

            
168
L<Mojolicious::Command::generate::app> inherits all attributes from
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
169
L<Mojolicious::Command> and implements the following new ones.
copy gitweblite soruce code
root authored on 2012-11-23
170

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
171
=head2 description
copy gitweblite soruce code
root authored on 2012-11-23
172

            
173
  my $description = $app->description;
174
  $app            = $app->description('Foo!');
175

            
176
Short description of this command, used for the command list.
177

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
178
=head2 usage
copy gitweblite soruce code
root authored on 2012-11-23
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
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
188
L<Mojolicious::Command> and implements the following new ones.
copy gitweblite soruce code
root authored on 2012-11-23
189

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
190
=head2 run
copy gitweblite soruce code
root authored on 2012-11-23
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