Newer Older
171 lines | 3.489kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Mojolicious::Command::generate::plugin;
2
use Mojo::Base 'Mojolicious::Command';
3

            
4
use Mojo::Util qw(camelize class_to_path);
5
use Mojolicious;
6

            
7
has description => "Generate Mojolicious plugin directory structure.\n";
8
has usage       => "usage: $0 generate plugin [NAME]\n";
9

            
10
sub run {
11
  my ($self, $name) = @_;
12
  $name ||= 'MyPlugin';
13

            
14
  # Class
15
  my $class = $name =~ /^[a-z]/ ? camelize($name) : $name;
16
  $class = "Mojolicious::Plugin::$class";
17
  my $app = class_to_path $class;
18
  $self->render_to_rel_file('class', "$name/lib/$app", $class, $name);
19

            
20
  # Test
21
  $self->render_to_rel_file('test', "$name/t/basic.t", $name);
22

            
23
  # Makefile
24
  $self->render_to_rel_file('makefile', "$name/Makefile.PL", $class, $app);
25
}
26

            
27
1;
28
__DATA__
29

            
30
@@ class
31
% my ($class, $name) = @_;
32
package <%= $class %>;
33
use Mojo::Base 'Mojolicious::Plugin';
34

            
35
our $VERSION = '0.01';
36

            
37
sub register {
38
  my ($self, $app) = @_;
39
}
40

            
41
1;
42
<% %>__END__
43

            
44
<% %>=encoding utf8
45

            
46
<% %>=head1 NAME
47

            
48
<%= $class %> - Mojolicious Plugin
49

            
50
<% %>=head1 SYNOPSIS
51

            
52
  # Mojolicious
53
  $self->plugin('<%= $name %>');
54

            
55
  # Mojolicious::Lite
56
  plugin '<%= $name %>';
57

            
58
<% %>=head1 DESCRIPTION
59

            
60
L<<%= $class %>> is a L<Mojolicious> plugin.
61

            
62
<% %>=head1 METHODS
63

            
64
L<<%= $class %>> inherits all methods from
65
L<Mojolicious::Plugin> and implements the following new ones.
66

            
67
<% %>=head2 register
68

            
69
  $plugin->register(Mojolicious->new);
70

            
71
Register plugin in L<Mojolicious> application.
72

            
73
<% %>=head1 SEE ALSO
74

            
75
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
76

            
77
<% %>=cut
78

            
79
@@ test
80
% my $name = shift;
81
use Mojo::Base -strict;
82

            
83
use Test::More;
84
use Mojolicious::Lite;
85
use Test::Mojo;
86

            
87
plugin '<%= $name %>';
88

            
89
get '/' => sub {
90
  my $self = shift;
91
  $self->render(text => 'Hello Mojo!');
92
};
93

            
94
my $t = Test::Mojo->new;
95
$t->get_ok('/')->status_is(200)->content_is('Hello Mojo!');
96

            
97
done_testing();
98

            
99
@@ makefile
100
% my ($class, $path) = @_;
101
use strict;
102
use warnings;
103

            
104
use ExtUtils::MakeMaker;
105

            
106
WriteMakefile(
107
  NAME         => '<%= $class %>',
108
  VERSION_FROM => 'lib/<%= $path %>',
109
  AUTHOR       => 'A Good Programmer <nospam@cpan.org>',
110
  PREREQ_PM    => {'Mojolicious' => '<%= $Mojolicious::VERSION %>'},
111
  test         => {TESTS => 't/*.t'}
112
);
113

            
114
__END__
115

            
116
=encoding utf8
117

            
118
=head1 NAME
119

            
120
Mojolicious::Command::generate::plugin - Plugin generator command
121

            
122
=head1 SYNOPSIS
123

            
124
  use Mojolicious::Command::generate::plugin;
125

            
126
  my $plugin = Mojolicious::Command::generate::plugin->new;
127
  $plugin->run(@ARGV);
128

            
129
=head1 DESCRIPTION
130

            
131
L<Mojolicious::Command::generate::plugin> generates directory structures for
132
fully functional L<Mojolicious> plugins.
133

            
134
This is a core command, that means it is always enabled and its code a good
135
example for learning to build new commands, you're welcome to fork it.
136

            
137
=head1 ATTRIBUTES
138

            
139
L<Mojolicious::Command::generate::plugin> inherits all attributes from
140
L<Mojolicious::Command> and implements the following new ones.
141

            
142
=head2 description
143

            
144
  my $description = $plugin->description;
145
  $plugin         = $plugin->description('Foo!');
146

            
147
Short description of this command, used for the command list.
148

            
149
=head2 usage
150

            
151
  my $usage = $plugin->usage;
152
  $plugin   = $plugin->usage('Foo!');
153

            
154
Usage information for this command, used for the help screen.
155

            
156
=head1 METHODS
157

            
158
L<Mojolicious::Command::generate::plugin> inherits all methods from
159
L<Mojolicious::Command> and implements the following new ones.
160

            
161
=head2 run
162

            
163
  $plugin->run(@ARGV);
164

            
165
Run this command.
166

            
167
=head1 SEE ALSO
168

            
169
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
170

            
171
=cut