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';
|
---|
8 | has usage => sub { shift->extract_usage };
|
---|
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 | my $dir = join '-', split('::', $class);
|
---|
19 | $self->render_to_rel_file('class', "$dir/lib/$app", $class, $name);
|
---|
20 |
|
---|
21 | # Test
|
---|
22 | $self->render_to_rel_file('test', "$dir/t/basic.t", $name);
|
---|
23 |
|
---|
24 | # Makefile
|
---|
25 | $self->render_to_rel_file('makefile', "$dir/Makefile.PL", $class, $app);
|
---|
26 | }
|
---|
27 |
|
---|
28 | 1;
|
---|
29 |
|
---|
30 | =encoding utf8
|
---|
31 |
|
---|
32 | =head1 NAME
|
---|
33 |
|
---|
34 | Mojolicious::Command::generate::plugin - Plugin generator command
|
---|
35 |
|
---|
36 | =head1 SYNOPSIS
|
---|
37 |
|
---|
38 | Usage: APPLICATION generate plugin [OPTIONS] [NAME]
|
---|
39 |
|
---|
40 | mojo generate plugin
|
---|
41 | mojo generate plugin TestPlugin
|
---|
42 |
|
---|
43 | Options:
|
---|
44 | -h, --help Show this summary of available options
|
---|
45 |
|
---|
46 | =head1 DESCRIPTION
|
---|
47 |
|
---|
48 | L<Mojolicious::Command::generate::plugin> generates directory structures for
|
---|
49 | fully functional L<Mojolicious> plugins.
|
---|
50 |
|
---|
51 | This is a core command, that means it is always enabled and its code a good
|
---|
52 | example for learning to build new commands, you're welcome to fork it.
|
---|
53 |
|
---|
54 | See L<Mojolicious::Commands/"COMMANDS"> for a list of commands that are
|
---|
55 | available by default.
|
---|
56 |
|
---|
57 | =head1 ATTRIBUTES
|
---|
58 |
|
---|
59 | L<Mojolicious::Command::generate::plugin> inherits all attributes from
|
---|
60 | L<Mojolicious::Command> and implements the following new ones.
|
---|
61 |
|
---|
62 | =head2 description
|
---|
63 |
|
---|
64 | my $description = $plugin->description;
|
---|
65 | $plugin = $plugin->description('Foo');
|
---|
66 |
|
---|
67 | Short description of this command, used for the command list.
|
---|
68 |
|
---|
69 | =head2 usage
|
---|
70 |
|
---|
71 | my $usage = $plugin->usage;
|
---|
72 | $plugin = $plugin->usage('Foo');
|
---|
73 |
|
---|
74 | Usage information for this command, used for the help screen.
|
---|
75 |
|
---|
76 | =head1 METHODS
|
---|
77 |
|
---|
78 | L<Mojolicious::Command::generate::plugin> inherits all methods from
|
---|
79 | L<Mojolicious::Command> and implements the following new ones.
|
---|
80 |
|
---|
81 | =head2 run
|
---|
82 |
|
---|
83 | $plugin->run(@ARGV);
|
---|
84 |
|
---|
85 | Run this command.
|
---|
86 |
|
---|
87 | =head1 SEE ALSO
|
---|
88 |
|
---|
89 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
90 |
|
---|
91 | =cut
|
---|
92 |
|
---|
93 | __DATA__
|
---|
94 |
|
---|
95 | @@ class
|
---|
96 | % my ($class, $name) = @_;
|
---|
97 | package <%= $class %>;
|
---|
98 | use Mojo::Base 'Mojolicious::Plugin';
|
---|
99 |
|
---|
100 | our $VERSION = '0.01';
|
---|
101 |
|
---|
102 | sub register {
|
---|
103 | my ($self, $app) = @_;
|
---|
104 | }
|
---|
105 |
|
---|
106 | 1;
|
---|
107 | <% %>__END__
|
---|
108 |
|
---|
109 | <% %>=encoding utf8
|
---|
110 |
|
---|
111 | <% %>=head1 NAME
|
---|
112 |
|
---|
113 | <%= $class %> - Mojolicious Plugin
|
---|
114 |
|
---|
115 | <% %>=head1 SYNOPSIS
|
---|
116 |
|
---|
117 | # Mojolicious
|
---|
118 | $self->plugin('<%= $name %>');
|
---|
119 |
|
---|
120 | # Mojolicious::Lite
|
---|
121 | plugin '<%= $name %>';
|
---|
122 |
|
---|
123 | <% %>=head1 DESCRIPTION
|
---|
124 |
|
---|
125 | L<<%= $class %>> is a L<Mojolicious> plugin.
|
---|
126 |
|
---|
127 | <% %>=head1 METHODS
|
---|
128 |
|
---|
129 | L<<%= $class %>> inherits all methods from
|
---|
130 | L<Mojolicious::Plugin> and implements the following new ones.
|
---|
131 |
|
---|
132 | <% %>=head2 register
|
---|
133 |
|
---|
134 | $plugin->register(Mojolicious->new);
|
---|
135 |
|
---|
136 | Register plugin in L<Mojolicious> application.
|
---|
137 |
|
---|
138 | <% %>=head1 SEE ALSO
|
---|
139 |
|
---|
140 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
141 |
|
---|
142 | <% %>=cut
|
---|
143 |
|
---|
144 | @@ test
|
---|
145 | % my $name = shift;
|
---|
146 | use Mojo::Base -strict;
|
---|
147 |
|
---|
148 | use Test::More;
|
---|
149 | use Mojolicious::Lite;
|
---|
150 | use Test::Mojo;
|
---|
151 |
|
---|
152 | plugin '<%= $name %>';
|
---|
153 |
|
---|
154 | get '/' => sub {
|
---|
155 | my $c = shift;
|
---|
156 | $c->render(text => 'Hello Mojo!');
|
---|
157 | };
|
---|
158 |
|
---|
159 | my $t = Test::Mojo->new;
|
---|
160 | $t->get_ok('/')->status_is(200)->content_is('Hello Mojo!');
|
---|
161 |
|
---|
162 | done_testing();
|
---|
163 |
|
---|
164 | @@ makefile
|
---|
165 | % my ($class, $path) = @_;
|
---|
166 | use strict;
|
---|
167 | use warnings;
|
---|
168 |
|
---|
169 | use ExtUtils::MakeMaker;
|
---|
170 |
|
---|
171 | WriteMakefile(
|
---|
172 | NAME => '<%= $class %>',
|
---|
173 | VERSION_FROM => 'lib/<%= $path %>',
|
---|
174 | AUTHOR => 'A Good Programmer <[email protected]>',
|
---|
175 | PREREQ_PM => {'Mojolicious' => '<%= $Mojolicious::VERSION %>'},
|
---|
176 | test => {TESTS => 't/*.t'}
|
---|
177 | );
|
---|